Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Dynamic Stops

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Dynamic Stops

    Based on what I have read in numerous threads I compiled this,

    Code:
    [INDENT] /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
                            
                SetStopLoss("", CalculationMode.Ticks, Stop, false);
                SetProfitTarget("",CalculationMode.Ticks, finalTarget);
                TraceOrders = true;
                CalculateOnBarClose = false;
                EntriesPerDirection = 1;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                   // Resets the stop loss to the original value when all positions are closed
                if (Position.MarketPosition == MarketPosition.Flat)
                {
                    SetStopLoss(CalculationMode.Ticks, stop);
                }
                
                    
                // If a long position is open, allow for stop loss modification
                else if (Position.MarketPosition == MarketPosition.Long)
                {
                    // BREAKEVEN Once the price is greater than entry pric + breakEvenStop set stop loss to breakeven
                    if (Close[0] >= Position.AvgPrice + breakEvenStop * TickSize)
                    {
                        SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                    }
                            
                    // 1stTarget Once the price is greater than entry price + firstTarget, set stop loss to 1 times above B E
                    if (Close[0] >= Position.AvgPrice + firstTarget * TickSize)
                    {
                        SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * TickSize));
                    }
                    
                    // 2ndTarget Once the price is greater than entry price + secondTarget, set stop loss to 2 times above B E
                    if (Close[0] >= Position.AvgPrice + secondTarget * TickSize)
                    {
                        SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 2 * TickSize));
                    }
                    
                    // 3rdTarget Once the price is greater than entry price + thirdTarget, set stop loss to 3 times above B E
                    if (Close[0] >= Position.AvgPrice + thirdTarget * TickSize)
                    {
                        SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 3 * TickSize));
                    }
                    
                }
                
                
                // If a short position is open, allow for stop loss modification
                else if (Position.MarketPosition == MarketPosition.Short)
                {
                    // BreakEven Once the price is less than entry price - breakEvenStop set stop loss to breakeven
                    if (Close[0] <= Position.AvgPrice - breakEvenStop * TickSize)
                    {
                        SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                    }
                            
                    // 1stTarget Once the price is less than entry price - firstTarget, set stop loss to 1 times above B E
                    if (Close[0] <= Position.AvgPrice - firstTarget * TickSize)
                    {
                        SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * TickSize));
                    }
                    
                    // 2ndTarget Once the price is less than entry price - secondTarget, set stop loss to 2 times above B E
                    if (Close[0] <= Position.AvgPrice - secondTarget * TickSize)
                    {
                        SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * 2 * TickSize));
                    }
                    
                    // 3rdTarget Once the price is less than entry price - thirdTarget, set stop loss to 3 times above B E
                    if (Close[0] <= Position.AvgPrice - thirdTarget * TickSize)
                    {
                        SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * 3 * TickSize));
                    }
                                
                }
    [/INDENT]
    Breakeven is always initiated and sometimes the higher targets are initiated, but it is inconsistent on what one and infrequent. I have tested certain parts by themselves and I still got some funky results. Before I spend days scratching my head am I pushing SetStopLoss to far and do I need to go the ExitShort script with Boolean and control stuff???

    EDIT: The latter was demonstrated back testing and market replay.
    Last edited by Hammerhorn; 05-07-2013, 02:00 AM.

    #2
    Hi Hammerhorn,

    Thank you for posting.

    From your code you could be running into the issue where your conditions are being met at the same time and therefore causing the higher targets to be used.

    This code snippet is a quick solution to what you are want to achieve. This will test the closes against the Avg.Price and the other targets so that they don't called at the same time. You will need to apply this snippet to both the long and short sections of your code.

    Code:
     if (Close[0] >= Position.AvgPrice + breakEvenStop * TickSize && Close[0] < Position.AvgPrice + firstTarget * TickSize )
                    {
                        SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                    }
                            
                    // 1stTarget Once the price is greater than entry price + firstTarget, set stop loss to 1 times above B E
                    if (Close[0] >= Position.AvgPrice + firstTarget * TickSize && Close[0] < Position.AvgPrice + secondTarget * TickSize )
                    {
                        SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * TickSize));
                    }

    Please let me know if I can be of further assistance.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by Hammerhorn View Post
      Based on what I have read in numerous threads I compiled this,

      Code:
      [INDENT]/// <summary>
              /// This method is used to configure the strategy and is called once before any strategy method is called.
              /// </summary>
              protected override void Initialize()
              {
       
                  SetStopLoss("", CalculationMode.Ticks, Stop, false);
                  SetProfitTarget("",CalculationMode.Ticks, finalTarget);
                  TraceOrders = true;
                  CalculateOnBarClose = false;
                  EntriesPerDirection = 1;
              }
       
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
                     // Resets the stop loss to the original value when all positions are closed
                  if (Position.MarketPosition == MarketPosition.Flat)
                  {
                      SetStopLoss(CalculationMode.Ticks, stop);
                  }
       
       
                  // If a long position is open, allow for stop loss modification
                  else if (Position.MarketPosition == MarketPosition.Long)
                  {
                      // BREAKEVEN Once the price is greater than entry pric + breakEvenStop set stop loss to breakeven
                      if (Close[0] >= Position.AvgPrice + breakEvenStop * TickSize)
                      {
                          SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                      }
       
                      // 1stTarget Once the price is greater than entry price + firstTarget, set stop loss to 1 times above B E
                      if (Close[0] >= Position.AvgPrice + firstTarget * TickSize)
                      {
                          SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * TickSize));
                      }
       
                      // 2ndTarget Once the price is greater than entry price + secondTarget, set stop loss to 2 times above B E
                      if (Close[0] >= Position.AvgPrice + secondTarget * TickSize)
                      {
                          SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 2 * TickSize));
                      }
       
                      // 3rdTarget Once the price is greater than entry price + thirdTarget, set stop loss to 3 times above B E
                      if (Close[0] >= Position.AvgPrice + thirdTarget * TickSize)
                      {
                          SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 3 * TickSize));
                      }
       
                  }
       
       
                  // If a short position is open, allow for stop loss modification
                  else if (Position.MarketPosition == MarketPosition.Short)
                  {
                      // BreakEven Once the price is less than entry price - breakEvenStop set stop loss to breakeven
                      if (Close[0] <= Position.AvgPrice - breakEvenStop * TickSize)
                      {
                          SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                      }
       
                      // 1stTarget Once the price is less than entry price - firstTarget, set stop loss to 1 times above B E
                      if (Close[0] <= Position.AvgPrice - firstTarget * TickSize)
                      {
                          SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * TickSize));
                      }
       
                      // 2ndTarget Once the price is less than entry price - secondTarget, set stop loss to 2 times above B E
                      if (Close[0] <= Position.AvgPrice - secondTarget * TickSize)
                      {
                          SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * 2 * TickSize));
                      }
       
                      // 3rdTarget Once the price is less than entry price - thirdTarget, set stop loss to 3 times above B E
                      if (Close[0] <= Position.AvgPrice - thirdTarget * TickSize)
                      {
                          SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * 3 * TickSize));
                      }
       
                  }
      [/INDENT]
      Breakeven is always initiated and sometimes the higher targets are initiated, but it is inconsistent on what one and infrequent. I have tested certain parts by themselves and I still got some funky results. Before I spend days scratching my head am I pushing SetStopLoss to far and do I need to go the ExitShort script with Boolean and control stuff???

      EDIT: The latter was demonstrated back testing and market replay.
      Looks correct. How is your code misbehaving?

      Comment


        #4
        Looks correct. How is your code misbehaving?
        Always reassuring to have be told that the code looks correct, thank you. Working fine now thanks to Cal.

        Cal, why does your solution work for all targets, not just the ones referenced? Can you put your snippet into newbie English?

        Comment


          #5
          Hi Hammerhorn,

          I sure can!

          The original code -
          Code:
          if (Close[0] <= Position.AvgPrice - breakEvenStop * TickSize)
                          {
                              SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                          }
                                  
                          // 1stTarget Once the price is less than entry price - firstTarget, set stop loss to 1 times above B E
                          if (Close[0] <= Position.AvgPrice - firstTarget * TickSize)
                          {
                              SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * TickSize));
                          }
          This will only test if the Close[0] is less than equal to the other side of the condition.
          However, in this case, if the second condition is true then so is the first condition, since the close is still less than the points you are testing against.

          The snippet I gave you is designed to test only between the targets and not just the general area.

          Example:
          Code:
           if (Close[0] >= Position.AvgPrice + firstTarget * TickSize && Close[0] < Position.AvgPrice + secondTarget * TickSize )
                          {
                              SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * TickSize));
                          }
          This condition tests if the Close[0] is greater than or equal to the AvgPrice - first target AND less than the AvgPrice + second target. Meaning only equal to the first target to the point less than the second target

          Please let me know if I can be of further assistance.
          Cal H.NinjaTrader Customer Service

          Comment


            #6
            I was going to bet that your original snippet was only going to work between each target, but in back testing it seems to work across the board. I was baffled so that is why I asked for the English. Will be testing market replay when I get a chance. Thank you again.

            Comment


              #7
              Not working and I don't know why, but I swore it worked when I first back tested it. Could not get it to work during market replay and can't get it to work during back test. Hanging on first one still. If I remove the "Breakeven" line, it will fire on the "firstTarget." See the attached back test image and my long code below.

              Code:
                          SetStopLoss("", CalculationMode.Ticks, Stop, false);
                          SetProfitTarget("",CalculationMode.Ticks, finalTarget);
                          TraceOrders = true;
                          CalculateOnBarClose = false;
                          EntriesPerDirection = 1;
                      }
              
                      /// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
                      protected override void OnBarUpdate()
                      {
                             // Resets the stop loss to the original value when all positions are closed
                          if (Position.MarketPosition == MarketPosition.Flat)
                          {
                              SetStopLoss(CalculationMode.Ticks, stop);
                          }
                          
                              
                          // If a long position is open, allow for stop loss modification
                          else if (Position.MarketPosition == MarketPosition.Long)
                          {                                
                              // BREAKEVEN Once the price is greater than entry pric + breakEvenStop set stop loss to breakeven
              //                if (Close[0] >= Position.AvgPrice + breakEvenStop * TickSize)
              //                {
              //                    SetStopLoss(CalculationMode.Price, Position.AvgPrice);
              //                
                              if (Close[0] >= Position.AvgPrice + breakEvenStop * TickSize && Close[0] < Position.AvgPrice + firstTarget * TickSize )
                              {
                                  SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                              }
                                      
                              // 1stTarget Once the price is greater than entry price + firstTarget, set stop loss to 1 times above B E
              //                if (Close[0] >= Position.AvgPrice + firstTarget * TickSize)
              //                {
              //                    SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * TickSize));
              //                }
                              
                              if (Close[0] >= Position.AvgPrice + firstTarget * TickSize && Close[0] < Position.AvgPrice + secondTarget * TickSize )
                              {
                                  SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * TickSize));
                              }
                              
                              // 2ndTarget Once the price is greater than entry price + secondTarget, set stop loss to 2 times above B E
              //                if (Close[0] >= Position.AvgPrice + secondTarget * TickSize)
              //                {
              //                    SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 2 * TickSize));
              //                }
                              
                              if (Close[0] >= Position.AvgPrice + secondTarget * TickSize && Close[0] < Position.AvgPrice + thirdTarget * TickSize )
                              {
                                  SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 2 * TickSize));
                              }
                                              
                              // 3rdTarget Once the price is greater than entry price + thirdTarget, set stop loss to 3 times above B E
              //                if (Close[0] >= Position.AvgPrice + thirdTarget * TickSize)
              //                {
              //                    SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 3 * TickSize));
              //                }
                              
                              if (Close[0] >= Position.AvgPrice + fourthTarget * TickSize && Close[0] < Position.AvgPrice + fourthTarget * TickSize )
                              {
                                  SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 3 * TickSize));
                              }
                              
              //                // 4thTarget Once the price is greater than entry price + fourthTarget, set stop loss to 4 times above B E
              //                if (Close[0] >= Position.AvgPrice + thirdTarget * TickSize)
              //                {
              //                    SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 4 * TickSize));
              //                }
              //                
              //                // 5thTarget Once the price is greater than entry price + fifthTarget, set stop loss to 5 times above B E
              //                if (Close[0] >= Position.AvgPrice + thirdTarget * TickSize)
              //                {
              //                    SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 5 * TickSize));
              //                }
                          }
              Attached Files

              Comment


                #8
                Originally posted by Hammerhorn View Post
                Not working and I don't know why, but I swore it worked when I first back tested it. Could not get it to work during market replay and can't get it to work during back test. Hanging on first one still. If I remove the "Breakeven" line, it will fire on the "firstTarget." See the attached back test image and my long code below.

                Code:
                 
                            SetStopLoss("", CalculationMode.Ticks, Stop, false);
                            SetProfitTarget("",CalculationMode.Ticks, finalTarget);
                            TraceOrders = true;
                            CalculateOnBarClose = false;
                            EntriesPerDirection = 1;
                        }
                 
                        /// <summary>
                        /// Called on each bar update event (incoming tick)
                        /// </summary>
                        protected override void OnBarUpdate()
                        {
                               // Resets the stop loss to the original value when all positions are closed
                            if (Position.MarketPosition == MarketPosition.Flat)
                            {
                                SetStopLoss(CalculationMode.Ticks, stop);
                            }
                 
                 
                            // If a long position is open, allow for stop loss modification
                            else if (Position.MarketPosition == MarketPosition.Long)
                            {                                
                                // BREAKEVEN Once the price is greater than entry pric + breakEvenStop set stop loss to breakeven
                //                if (Close[0] >= Position.AvgPrice + breakEvenStop * TickSize)
                //                {
                //                    SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                //                
                                if (Close[0] >= Position.AvgPrice + breakEvenStop * TickSize && Close[0] < Position.AvgPrice + firstTarget * TickSize )
                                {
                                    SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                                }
                 
                                // 1stTarget Once the price is greater than entry price + firstTarget, set stop loss to 1 times above B E
                //                if (Close[0] >= Position.AvgPrice + firstTarget * TickSize)
                //                {
                //                    SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * TickSize));
                //                }
                 
                                if (Close[0] >= Position.AvgPrice + firstTarget * TickSize && Close[0] < Position.AvgPrice + secondTarget * TickSize )
                                {
                                    SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * TickSize));
                                }
                 
                                // 2ndTarget Once the price is greater than entry price + secondTarget, set stop loss to 2 times above B E
                //                if (Close[0] >= Position.AvgPrice + secondTarget * TickSize)
                //                {
                //                    SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 2 * TickSize));
                //                }
                 
                                if (Close[0] >= Position.AvgPrice + secondTarget * TickSize && Close[0] < Position.AvgPrice + thirdTarget * TickSize )
                                {
                                    SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 2 * TickSize));
                                }
                 
                                // 3rdTarget Once the price is greater than entry price + thirdTarget, set stop loss to 3 times above B E
                //                if (Close[0] >= Position.AvgPrice + thirdTarget * TickSize)
                //                {
                //                    SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 3 * TickSize));
                //                }
                 
                                if (Close[0] >= Position.AvgPrice + fourthTarget * TickSize && Close[0] < Position.AvgPrice + fourthTarget * TickSize )
                                {
                                    SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 3 * TickSize));
                                }
                 
                //                // 4thTarget Once the price is greater than entry price + fourthTarget, set stop loss to 4 times above B E
                //                if (Close[0] >= Position.AvgPrice + thirdTarget * TickSize)
                //                {
                //                    SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 4 * TickSize));
                //                }
                //                
                //                // 5thTarget Once the price is greater than entry price + fifthTarget, set stop loss to 5 times above B E
                //                if (Close[0] >= Position.AvgPrice + thirdTarget * TickSize)
                //                {
                //                    SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 5 * TickSize));
                //                }
                            }
                Your results will seem to imply that the SetStopLoss() method is not reentrant, and will only be called once on each barUpdate. Using TraceOrders may be able to confirm that.

                Regardless, the code is rather inefficient, so it might be better to simply code it using the mathematical logic. Any Close that is greater than the 3rd target is also greater than the first 2, so try coding it from this pseudocode.
                Code:
                if (Close[0] >= Target3) SetStopLoss(...)
                else if (Close[0] >= Target2) SetStopLoss(...)
                else if (Close[0] >= Target1) SetStopLoss(...)
                so that by cascading down, only one branch is ever called, and it will always be the one where the price exceeds the nearest target.
                Last edited by koganam; 05-07-2013, 09:42 PM.

                Comment


                  #9
                  Trace orders do confirm it is only modify the stop order once, not at the other levels. I will attempt to use your code. Thanks.

                  Comment


                    #10
                    Koganam-

                    Got it working, but in market replay it is modifying the stop back down if the price drops, see below.

                    4/7/2013 11:16:35 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7681 Currency=0 Simulated=False
                    4/7/2013 11:16:35 PM Amended stop order: Order='66d75952254f416d93c2d7a958a97f77/Replay101' Name='Stop loss' State=Accepted Instrument='FDAX 06-13' Action=Sell Limit price=0 Stop price=7681 Quantity=1 Strategy='ScalperZLEMADPOCCIstops' Type=Stop Tif=Gtc Oco='22eb77f8efbe444d8cf409934ff600dc-1842' Filled=0 Fill price=0 Token='66d75952254f416d93c2d7a958a97f77' Gtd='12/1/2099 12:00:00 AM'
                    4/7/2013 11:16:35 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7681 Currency=0 Simulated=False
                    4/7/2013 11:16:35 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7681 Currency=0 Simulated=False
                    4/7/2013 11:16:35 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7681 Currency=0 Simulated=False
                    4/7/2013 11:16:35 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7681 Currency=0 Simulated=False
                    4/7/2013 11:16:35 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7681 Currency=0 Simulated=False
                    4/7/2013 11:16:35 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7682.5 Currency=0 Simulated=False
                    4/7/2013 11:16:35 PM Amended stop order: Order='66d75952254f416d93c2d7a958a97f77/Replay101' Name='Stop loss' State=Accepted Instrument='FDAX 06-13' Action=Sell Limit price=0 Stop price=7682.5 Quantity=1 Strategy='ScalperZLEMADPOCCIstops' Type=Stop Tif=Gtc Oco='22eb77f8efbe444d8cf409934ff600dc-1842' Filled=0 Fill price=0 Token='66d75952254f416d93c2d7a958a97f77' Gtd='12/1/2099 12:00:00 AM'
                    4/7/2013 11:16:38 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7681 Currency=0 Simulated=False
                    4/7/2013 11:16:38 PM Amended stop order: Order='66d75952254f416d93c2d7a958a97f77/Replay101' Name='Stop loss' State=Accepted Instrument='FDAX 06-13' Action=Sell Limit price=0 Stop price=7681 Quantity=1 Strategy='ScalperZLEMADPOCCIstops' Type=Stop Tif=Gtc Oco='22eb77f8efbe444d8cf409934ff600dc-1842' Filled=0 Fill price=0 Token='66d75952254f416d93c2d7a958a97f77' Gtd='12/1/2099 12:00:00 AM'
                    4/7/2013 11:16:38 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7681 Currency=0 Simulated=False
                    4/7/2013 11:16:38 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7681 Currency=0 Simulated=False
                    4/7/2013 11:16:38 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7681 Currency=0 Simulated=False
                    4/7/2013 11:16:38 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7682.5 Currency=0 Simulated=False
                    4/7/2013 11:16:38 PM Amended stop order: Order='66d75952254f416d93c2d7a958a97f77/Replay101' Name='Stop loss' State=Accepted Instrument='FDAX 06-13' Action=Sell Limit price=0 Stop price=7682.5 Quantity=1 Strategy='ScalperZLEMADPOCCIstops' Type=Stop Tif=Gtc Oco='22eb77f8efbe444d8cf409934ff600dc-1842' Filled=0 Fill price=0 Token='66d75952254f416d93c2d7a958a97f77' Gtd='12/1/2099 12:00:00 AM'
                    4/7/2013 11:16:41 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7681 Currency=0 Simulated=False
                    4/7/2013 11:16:41 PM Amended stop order: Order='66d75952254f416d93c2d7a958a97f77/Replay101' Name='Stop loss' State=Accepted Instrument='FDAX 06-13' Action=Sell Limit price=0 Stop price=7681 Quantity=1 Strategy='ScalperZLEMADPOCCIstops' Type=Stop Tif=Gtc Oco='22eb77f8efbe444d8cf409934ff600dc-1842' Filled=0 Fill price=0 Token='66d75952254f416d93c2d7a958a97f77' Gtd='12/1/2099 12:00:00 AM'
                    4/7/2013 11:16:41 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7681 Currency=0 Simulated=False
                    4/7/2013 11:16:42 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7681 Currency=0 Simulated=False
                    4/7/2013 11:16:42 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7681 Currency=0 Simulated=False
                    4/7/2013 11:16:46 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7681 Currency=0 Simulated=False
                    4/7/2013 11:16:46 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7681 Currency=0 Simulated=False
                    4/7/2013 11:16:46 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7681 Currency=0 Simulated=False
                    4/7/2013 11:16:46 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7682.5 Currency=0 Simulated=False
                    4/7/2013 11:16:46 PM Amended stop order: Order='66d75952254f416d93c2d7a958a97f77/Replay101' Name='Stop loss' State=Accepted Instrument='FDAX 06-13' Action=Sell Limit price=0 Stop price=7682.5 Quantity=1 Strategy='ScalperZLEMADPOCCIstops' Type=Stop Tif=Gtc Oco='22eb77f8efbe444d8cf409934ff600dc-1842' Filled=0 Fill price=0 Token='66d75952254f416d93c2d7a958a97f77' Gtd='12/1/2099 12:00:00 AM'
                    4/7/2013 11:16:46 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7682.5 Currency=0 Simulated=False
                    4/7/2013 11:16:47 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7681 Currency=0 Simulated=False
                    4/7/2013 11:16:47 PM Amended stop order: Order='66d75952254f416d93c2d7a958a97f77/Replay101' Name='Stop loss' State=Accepted Instrument='FDAX 06-13' Action=Sell Limit price=0 Stop price=7681 Quantity=1 Strategy='ScalperZLEMADPOCCIstops' Type=Stop Tif=Gtc Oco='22eb77f8efbe444d8cf409934ff600dc-1842' Filled=0 Fill price=0 Token='66d75952254f416d93c2d7a958a97f77' Gtd='12/1/2099 12:00:00 AM'
                    4/7/2013 11:16:47 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7682.5 Currency=0 Simulated=False
                    4/7/2013 11:16:47 PM Amended stop order: Order='66d75952254f416d93c2d7a958a97f77/Replay101' Name='Stop loss' State=Accepted Instrument='FDAX 06-13' Action=Sell Limit price=0 Stop price=7682.5 Quantity=1 Strategy='ScalperZLEMADPOCCIstops' Type=Stop Tif=Gtc Oco='22eb77f8efbe444d8cf409934ff600dc-1842' Filled=0 Fill price=0 Token='66d75952254f416d93c2d7a958a97f77' Gtd='12/1/2099 12:00:00 AM'
                    4/7/2013 11:16:47 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7682.5 Currency=0 Simulated=False
                    4/7/2013 11:16:55 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7681 Currency=0 Simulated=False
                    4/7/2013 11:16:55 PM Amended stop order: Order='66d75952254f416d93c2d7a958a97f77/Replay101' Name='Stop loss' State=Accepted Instrument='FDAX 06-13' Action=Sell Limit price=0 Stop price=7681 Quantity=1 Strategy='ScalperZLEMADPOCCIstops' Type=Stop Tif=Gtc Oco='22eb77f8efbe444d8cf409934ff600dc-1842' Filled=0 Fill price=0 Token='66d75952254f416d93c2d7a958a97f77' Gtd='12/1/2099 12:00:00 AM'
                    4/7/2013 11:16:56 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=7681 Currency=0 Simulated=False
                    Here is what did,

                    Code:
                    SetStopLoss("", CalculationMode.Ticks, Stop, false);
                                SetProfitTarget("",CalculationMode.Ticks, finalTarget);
                                TraceOrders = true;
                                CalculateOnBarClose = false;
                                EntriesPerDirection = 1;
                            }
                    
                            /// <summary>
                            /// Called on each bar update event (incoming tick)
                            /// </summary>
                            protected override void OnBarUpdate()
                            {
                                   // Resets the stop loss to the original value when all positions are closed
                                if (Position.MarketPosition == MarketPosition.Flat)
                                {
                                    SetStopLoss(CalculationMode.Ticks, stop);
                                }
                                
                                    
                                // If a long position is open, allow for stop loss modification
                                else if (Position.MarketPosition == MarketPosition.Long)
                                {                                
                                    
                                    if (Close[0] >= Position.AvgPrice + thirdTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 3 * TickSize));
                                        else if (Close[0] >= Position.AvgPrice + secondTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 2 * TickSize));
                                        else if (Close[0] >= Position.AvgPrice + firstTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 2 * TickSize));
                                        else if (Close[0] >= Position.AvgPrice + breakEvenStop * TickSize) SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                                            
                    
                                }
                                
                                
                                // If a short position is open, allow for stop loss modification
                                else if (Position.MarketPosition == MarketPosition.Short)
                                {    
                                    
                                    if (Close[0] <= Position.AvgPrice - thirdTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * 3 * TickSize));
                                        else if (Close[0] <= Position.AvgPrice - secondTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * 2 * TickSize));
                                        else if (Close[0] <= Position.AvgPrice - firstTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * 2 * TickSize));
                                        else if (Close[0] <= Position.AvgPrice - breakEvenStop * TickSize) SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                                            
                                }

                    Comment


                      #11
                      Originally posted by Hammerhorn View Post
                      Koganam-

                      Got it working, but in market replay it is modifying the stop back down if the price drops, see below.

                      Here is what did,

                      Code:
                      SetStopLoss("", CalculationMode.Ticks, Stop, false);
                                  SetProfitTarget("",CalculationMode.Ticks, finalTarget);
                                  TraceOrders = true;
                                  CalculateOnBarClose = false;
                                  EntriesPerDirection = 1;
                              }
                       
                              /// <summary>
                              /// Called on each bar update event (incoming tick)
                              /// </summary>
                              protected override void OnBarUpdate()
                              {
                                     // Resets the stop loss to the original value when all positions are closed
                                  if (Position.MarketPosition == MarketPosition.Flat)
                                  {
                                      SetStopLoss(CalculationMode.Ticks, stop);
                                  }
                       
                       
                                  // If a long position is open, allow for stop loss modification
                                  else if (Position.MarketPosition == MarketPosition.Long)
                                  {                                
                       
                                      if (Close[0] >= Position.AvgPrice + thirdTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 3 * TickSize));
                                          else if (Close[0] >= Position.AvgPrice + secondTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 2 * TickSize));
                                          else if (Close[0] >= Position.AvgPrice + firstTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 2 * TickSize));
                                          else if (Close[0] >= Position.AvgPrice + breakEvenStop * TickSize) SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                       
                       
                                  }
                       
                       
                                  // If a short position is open, allow for stop loss modification
                                  else if (Position.MarketPosition == MarketPosition.Short)
                                  {    
                       
                                      if (Close[0] <= Position.AvgPrice - thirdTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * 3 * TickSize));
                                          else if (Close[0] <= Position.AvgPrice - secondTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * 2 * TickSize));
                                          else if (Close[0] <= Position.AvgPrice - firstTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * 2 * TickSize));
                                          else if (Close[0] <= Position.AvgPrice - breakEvenStop * TickSize) SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                       
                                  }
                      That is because your code does not quite stop you from going backwards. Read the code and you will see why.

                      You are going to have to store the current Stop Price, and only update the Stop if movement is in the desired direction.

                      Comment


                        #12
                        I see why it is doing it, I just have no idea how to stop it and if I did figure it out, it would not be clean. Can you point me in the right direction?
                        Last edited by Hammerhorn; 05-07-2013, 07:16 PM.

                        Comment


                          #13
                          Originally posted by Hammerhorn View Post
                          I see why it is doing it, I just have no idea how to stop and if I did figure it out it would not be clean. Can you point me in the right direction?
                          Unfortunately, my coding style is very different from yours, so it is not very easy to show you what I mean, but here is one way to go about it. (Showing code as an example for Long side only)
                          Code:
                           
                          //Define your class variables to hold stops
                          private double LongStopPrice = 0.00;
                          private doulbe ShortStopPrice = 100000000000.00; //just a huge number, at which  no stock trades
                          .
                          Code:
                                         // Resets the stop loss to the original value when all positions are closed
                                      if (Position.MarketPosition == MarketPosition.Flat)
                                      {
                                          SetStopLoss(CalculationMode.Ticks, stop);
                                          LongStopPrice = 0; //initial long stop as small as possible.
                                          ShortStopPrice = Close[0] + 1000; //initial short step pretty large
                                      }
                           
                                      // If a long position is open, allow for stop loss modification
                                      else if (Position.MarketPosition == MarketPosition.Long && Close[0] >= Math.Max(LongStopPrice, Position.AvgPrice + breakEvenStop * TickSize))
                                      {                                
                           
                                          if (Close[0] >= Position.AvgPrice + thirdTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 3 * TickSize));
                                              else if (Close[0] >= Position.AvgPrice + secondTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 2 * TickSize));
                                              else if (Close[0] >= Position.AvgPrice + firstTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 2 * TickSize));
                                              else if (Close[0] >= Position.AvgPrice + breakEvenStop * TickSize) SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                           
                             LongStopPrice = Close[0] + TickSize; //update the StopPrice
                           
                           
                           
                                      }
                          Last edited by koganam; 05-07-2013, 08:04 PM.

                          Comment


                            #14
                            I don't understand this part do you mind bearing with me a little bit more and explaining it to me? Specifically the variables equaling something else.

                            Code:
                             // Resets the stop loss to the original value when all positions are closed             if (Position.MarketPosition == MarketPosition.Flat)             {                 SetStopLoss(CalculationMode.Ticks, stop);                 
                            LongStopPrice = 0; //initial long stop as small as possible.                
                             ShortStopPrice = Close[0] + 1000; //initial short step pretty large             }

                            Comment


                              #15
                              Long works fine. EDITED: Short not working, going back to breakeven, take a look, please.

                              Code:
                                      private double LongStopPrice = 0.00;
                                      private double ShortStopPrice = 100000000000.00; //just a huge number, at which  no stock trades
                                       #endregion
                              
                                      /// <summary>
                                      /// This method is used to configure the strategy and is called once before any strategy method is called.
                                      /// </summary>
                                      protected override void Initialize()
                                      {
                                                    
                                          SetStopLoss("", CalculationMode.Ticks, Stop, false);
                                          SetProfitTarget("",CalculationMode.Ticks, finalTarget);
                                          TraceOrders = true;
                                          CalculateOnBarClose = false;
                                          EntriesPerDirection = 1;
                                      }
                              
                                      /// <summary>
                                      /// Called on each bar update event (incoming tick)
                                      /// </summary>
                                      protected override void OnBarUpdate()
                                      {
                                             // Resets the stop loss to the original value when all positions are closed
                                          if (Position.MarketPosition == MarketPosition.Flat)
                                          {
                                              SetStopLoss(CalculationMode.Ticks, stop);
                                              LongStopPrice = 0; //initial long stop as small as possible.
                                              ShortStopPrice = Close[0] + 1000; //initial short step pretty large
                                          }
                                                          
                                          // If a long position is open, allow for stop loss modification
                                          else if (Position.MarketPosition == MarketPosition.Long && Close[0] >= Math.Max(LongStopPrice, Position.AvgPrice + breakEvenStop * TickSize))
                                          {                                
                                              if (Close[0] >= Position.AvgPrice + seventhTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 7 * TickSize));
                                                  else if (Close[0] >= Position.AvgPrice + sixthTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 6 * TickSize));
                                                  else if (Close[0] >= Position.AvgPrice + fifthTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 5 * TickSize));                    
                                                  else if (Close[0] >= Position.AvgPrice + fourthTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 4 * TickSize));
                                                  else if (Close[0] >= Position.AvgPrice + thirdTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 3 * TickSize));                
                                                  else if (Close[0] >= Position.AvgPrice + secondTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 2 * TickSize));
                                                  else if (Close[0] >= Position.AvgPrice + firstTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 1 * TickSize));
                                                  else if (Close[0] >= Position.AvgPrice + breakEvenStop * TickSize) SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                                                  
                                                  LongStopPrice = Close[0]; //update the StopPrice
                                          }
                                          
                                          // If a short position is open, allow for stop loss modification
                                          else if (Position.MarketPosition == MarketPosition.Short && Close[0] <= Math.Max(ShortStopPrice, Position.AvgPrice - breakEvenStop * TickSize))
                                          {    
                                              if (Close[0] <= Position.AvgPrice - seventhTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * 7 * TickSize));
                                                  else if (Close[0] <= Position.AvgPrice - sixthTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * 6 * TickSize));
                                                  else if (Close[0] <= Position.AvgPrice - fifthTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * 5 * TickSize));                    
                                                  else if (Close[0] <= Position.AvgPrice - fourthTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * 4 * TickSize));
                                                  else if (Close[0] <= Position.AvgPrice - thirdTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * 3 * TickSize));                
                                                  else if (Close[0] <= Position.AvgPrice - secondTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * 2 * TickSize));
                                                  else if (Close[0] <= Position.AvgPrice - firstTarget * TickSize) SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * 1 * TickSize));
                                                  else if (Close[0] <= Position.AvgPrice - breakEvenStop * TickSize) SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                                                  
                                                  ShortStopPrice = Close[0]; //update the StopPrice
                                          }
                              Last edited by Hammerhorn; 05-07-2013, 08:07 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Brevo, Today, 01:45 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post Brevo
                              by Brevo
                               
                              Started by aussugardefender, Today, 01:07 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post aussugardefender  
                              Started by pvincent, 06-23-2022, 12:53 PM
                              14 responses
                              239 views
                              0 likes
                              Last Post Nyman
                              by Nyman
                               
                              Started by TraderG23, 12-08-2023, 07:56 AM
                              9 responses
                              384 views
                              1 like
                              Last Post Gavini
                              by Gavini
                               
                              Started by oviejo, Today, 12:28 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post oviejo
                              by oviejo
                               
                              Working...
                              X