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

Scale Out of a Position

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

    #16
    Highlight the text and press CTRL-C to copy, then CTRL-V to paste wherever you wish.
    RayNinjaTrader Customer Service

    Comment


      #17
      Ray - thx. easy when you know how!
      Josh - have been thinking about this. As written I believe the strategy is doing the following:

      1/ price hits first target - stop loss moved to -5. No problem here.
      2/ price hits second target - stop loss doesn't know where to go since 2x conditions are true, i.e. Close[0] > Position.AvgPrice + 5 AND Close[0] > Position.AvgPrice + 10 ARE BOTH TRUE.
      3/ if price then starts to retrace to a point where Close[0] > Position.AvgPrice + 5 ONLY is true, then then stop loss moved to -5.
      4/ if price retraces further, final contract closed out at -5.

      The above sounds logical.........but I am no expert in logic.
      Have included the Output from the Output window below.
      Long trade entered at 12270. -5 stop loss = 12265.

      Hope it helps !
      thx
      David


      19/01/2008 21:12:37 Entered internal PlaceOrder() method at 19/01/2008 21:12:37: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Long 1a' FromEntrySignal=''
      19/01/2008 21:12:37 Entered internal PlaceOrder() method at 19/01/2008 21:12:37: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Long 1b' FromEntrySignal=''
      19/01/2008 21:12:37 Entered internal PlaceOrder() method at 19/01/2008 21:12:37: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Long 1c' FromEntrySignal=''
      19/01/2008 21:12:37 Entered internal PlaceOrder() method at 19/01/2008 21:12:37: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Long 1d' FromEntrySignal=''
      19/01/2008 21:13:07 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=12265.75 Currency=0 Simulated=False
      19/01/2008 21:13:07 Amended stop order: Order='c7b4004e9e5a43ec884fcf29c81df117/Sim101' Name='Stop loss' State=Accepted Instrument='YM ##-##' Action=Sell Limit price=0 Stop price=12266 Quantity=1 Strategy='JMACrossContinuationMod' Type=Stop Tif=Gtc Oco='45a4ebd375514276a031e250427b3e59' Filled=0 Fill price=0 Token='c7b4004e9e5a43ec884fcf29c81df117' Gtd='01/12/2099 00:00:00'
      19/01/2008 21:13:07 Raise: 12265.75
      19/01/2008 21:13:07 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=12270.75 Currency=0 Simulated=False
      19/01/2008 21:13:07 Amended stop order: Order='c7b4004e9e5a43ec884fcf29c81df117/Sim101' Name='Stop loss' State=PendingChange Instrument='YM ##-##' Action=Sell Limit price=0 Stop price=12271 Quantity=1 Strategy='JMACrossContinuationMod' Type=Stop Tif=Gtc Oco='45a4ebd375514276a031e250427b3e59' Filled=0 Fill price=0 Token='c7b4004e9e5a43ec884fcf29c81df117' Gtd='01/12/2099 00:00:00'
      19/01/2008 21:13:07 Breakeven: 12270.75
      19/01/2008 21:13:17 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=12265.75 Currency=0 Simulated=False
      19/01/2008 21:13:17 Raise: 12265.75
      19/01/2008 21:13:17 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=12270.75 Currency=0 Simulated=False
      19/01/2008 21:13:17 Amended stop order: Order='c7b4004e9e5a43ec884fcf29c81df117/Sim101' Name='Stop loss' State=Accepted Instrument='YM ##-##' Action=Sell Limit price=0 Stop price=12271 Quantity=1 Strategy='JMACrossContinuationMod' Type=Stop Tif=Gtc Oco='45a4ebd375514276a031e250427b3e59' Filled=0 Fill price=0 Token='c7b4004e9e5a43ec884fcf29c81df117' Gtd='01/12/2099 00:00:00'
      19/01/2008 21:13:17 Breakeven: 12270.75
      19/01/2008 21:13:27 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=12265.75 Currency=0 Simulated=False
      19/01/2008 21:13:27 Amended stop order: Order='c7b4004e9e5a43ec884fcf29c81df117/Sim101' Name='Stop loss' State=Accepted Instrument='YM ##-##' Action=Sell Limit price=0 Stop price=12266 Quantity=1 Strategy='JMACrossContinuationMod' Type=Stop Tif=Gtc Oco='45a4ebd375514276a031e250427b3e59' Filled=0 Fill price=0 Token='c7b4004e9e5a43ec884fcf29c81df117' Gtd='01/12/2099 00:00:00'
      19/01/2008 21:13:27 Raise: 12265.75
      19/01/2008 21:13:47 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Ticks Value=20 Currency=0 Simulated=False

      Comment


        #18
        Good assessment. When both conditions are true it will take the last modification to your stop loss. It will do the +5 mod and then come to the code where it will then do the +10 mod. What we want to do for both of our stop loss mod conditions is probably add a bool to them. This will allow us only to modify the stop loss once per condition evaluated to true. Does this make sense?

        If our Close[0] raised to above price by 10 ticks we want our stop loss to be raised to breakeven. Adding the bool will prevent the strategy from ever lowering the stop loss.
        Josh P.NinjaTrader Customer Service

        Comment


          #19
          the logic part was the easy bit, pls point me in the right direction regarding the boolean expression. Apologise, but I am no coder.....

          thx
          David

          Comment


            #20
            if (Close[0] > Position.AvgPrice + 10 * TickSize && breakevenSet == false)
            {
            SetStopLoss(CalculationMode.Price, Position.AvgPrice);
            breakevenSet = true;
            }

            if (breakevenSet && Position.MarketPosition == MarketPosition.Flat)
            {
            breakevenSet = false;
            }

            Create breakevenSet in the Variables section up top.
            private bool breakevenSet = false;
            Josh P.NinjaTrader Customer Service

            Comment


              #21
              Josh
              this works OK on the way up. i.e. when at price > + 10 from Position.AvgPrice StopLoss = Breakeven, but as soon as price drops below +5 from Position.AvgPrice, StopLoss = -5.

              any ideas how to stop this change on the way down?
              thx
              DG

              Comment


                #22
                forget my last post.
                inserting the code below, does not correct the issue even on the way up, i.e. at +10, stop loss reverts to -5 100% of the time one tick after stating that it was at breakeven.

                DG

                Comment


                  #23
                  Adding the text in bold fixes the issue.
                  Josh - help greatly appreciated. it is 11:30pm here in Europe.............now off to bed !!!!!!

                  thx
                  David



                  if (Close[0] > Position.AvgPrice + 5 * TickSize
                  && breakevenSet == false)
                  {
                  SetStopLoss(CalculationMode.Price, Position.AvgPrice - 5 * TickSize);
                  Print(Time[0] + " Raise: " + (Position.AvgPrice - 5 * TickSize));
                  }

                  if (Close[0] > Position.AvgPrice + 10 * TickSize
                  && breakevenSet == false)
                  {
                  SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                  Print(Time[0] + " Breakeven: " + Position.AvgPrice);
                  breakevenSet = true;

                  Comment


                    #24
                    Josh

                    One more comment

                    It may be worth updating your sample strategy code that you placed in the forum (that I used as the baseline for this code) with what I have learnt here based on scaling out of a position with more than two contracts. People like me rely a lot on thise sample codes you place in that part of the forum.

                    many thx
                    David

                    Comment


                      #25
                      I was originally suggesting using 2 bools (one for each condition). I only posted code for one of condition set. Sorry if that caused confusion. Your current implementation uses 1 bool for both conditions. That could work, but it would still allow for unnecessary resubmissions of the Position.AvgPrice + 5 *TickSize stop loss modification.

                      If you use two bools you will only allow for the strategy to modify the stop loss once for each condition set.

                      Code:
                      if[FONT=Courier New] (Close[[/FONT][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][FONT=Courier New]] > Position.AvgPrice + [/FONT][FONT=Courier New][COLOR=#800080]5[/COLOR][/FONT][FONT=Courier New] * TickSize [/FONT][FONT=Courier New]&& raiseSet == [/FONT][FONT=Courier New][COLOR=#0000ff]false[/COLOR][/FONT][FONT=Courier New])[/FONT][FONT=Courier New]
                      {[/FONT]
                            [FONT=Courier New]SetStopLoss(CalculationMode.Price, Position.AvgPrice - [/FONT][FONT=Courier New][COLOR=#800080]5[/COLOR][/FONT][FONT=Courier New] * TickSize);[/FONT][FONT=Courier New]
                            raiseSet = true;[/FONT][FONT=Courier New]
                      }[/FONT]
                       
                      [FONT=Courier New][COLOR=#0000ff]if[/COLOR][/FONT][FONT=Courier New] (Close[[/FONT][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][FONT=Courier New]] > Position.AvgPrice + [/FONT][FONT=Courier New][COLOR=#800080]10[/COLOR][/FONT][FONT=Courier New] * TickSize [/FONT][FONT=Courier New]&& breakevenSet == [/FONT][FONT=Courier New][COLOR=#0000ff]false[/COLOR][/FONT][FONT=Courier New])[/FONT]
                      [FONT=Courier New]{[/FONT]
                            [FONT=Courier New]SetStopLoss(CalculationMode.Price, Position.AvgPrice);[/FONT]
                            [FONT=Courier New]breakevenSet = [/FONT][FONT=Courier New][COLOR=#0000ff]true[/COLOR][/FONT][FONT=Courier New];[/FONT]
                      }
                      
                      if ((breakevenSet && Position.MarketPosition == MarketPosition.Flat) || (raiseSet && Position.MarketPosition == MarketPosition.Flat))
                      {
                           breakevenSet = false;
                           raiseSet = false;
                      }
                      (I have not tested this code, but you get the idea.)
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #26
                        thx Josh with your patience with me on this one. I have revised the strategy as per your comments below & validated today on the simulated data feed. Works as expected !!

                        thx
                        David

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by jclose, Today, 09:37 PM
                        0 responses
                        5 views
                        0 likes
                        Last Post jclose
                        by jclose
                         
                        Started by WeyldFalcon, 08-07-2020, 06:13 AM
                        10 responses
                        1,413 views
                        0 likes
                        Last Post Traderontheroad  
                        Started by firefoxforum12, Today, 08:53 PM
                        0 responses
                        11 views
                        0 likes
                        Last Post firefoxforum12  
                        Started by stafe, Today, 08:34 PM
                        0 responses
                        11 views
                        0 likes
                        Last Post stafe
                        by stafe
                         
                        Started by sastrades, 01-31-2024, 10:19 PM
                        11 responses
                        169 views
                        0 likes
                        Last Post NinjaTrader_Manfred  
                        Working...
                        X