Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Setting Stop Loss To initial value

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

    Setting Stop Loss To initial value

    GM, In my strategy i wanted to enter a trade and have SL imideatly set to parabolic stop. Currently when i enter strategy waits for like a bar before it sets stop to trail stop.
    I wanted first Stop to be initial stop set to StopTicksShort but its not doing it..

    Code:
    StopTicksLong                    = -20;
                    StopTicksShort                    = 20;​
    
        EnterShort(1,PositionSize, "TMS");    
                        myFreeTradeShort = true;
                        myCurrentTrail = true;                  
                        MySarStop = (Position.AveragePrice - (StopTicksShort * TickSize)) ;
                        myFreeTradeShortTrail = true;
                        switchtrail = true;​
    
    #region Trail Stop Long and Short
    
                    if (SetTrail )              
                    {                    
                        if((myFreeTrail == true )
                            &&(Position.MarketPosition == MarketPosition.Long || Position.MarketPosition == MarketPosition.Short))
                        {
                            if ((Position.MarketPosition == MarketPosition.Long && myFreeTradeLongTrail == true)
                                 && (ParabolicSAR1[0] < Low[0]) )
    
                            {
                                    MySarStop = ParabolicSAR1[0];
                            }    
    
                            if (Position.MarketPosition == MarketPosition.Long && myFreeTradeLongTrail == true  )
                            {
                                 if(MySarStop <= bid ){
                                ExitLongStopMarket(1, true, Position.Quantity, MySarStop, "SLL", "TML");
                                 }
                            }
    
                            if (Position.MarketPosition == MarketPosition.Long && myFreeTradeLongTrail == true && switchtrail == true)
                            {
    //                            ExitLongStopMarket(1, true, Position.Quantity, MySarStop, "SLL", "TML");
    //                            ExitLongLimit(1, true, Position.Quantity, Position.AveragePrice + (TickSize * TrailTakeProfit), "PTL", "TML");
                                if(UseRiskReward)
                                {
                                if(onetoone){
                                    ExitLongLimit(1, true, Position.Quantity, Position.AveragePrice + (TickSize * rr1to1Long), "PTL", "TML");
                                }                                                                                
                                if(onetoonehalf){                                                                
                                    ExitLongLimit(1, true, Position.Quantity, Position.AveragePrice + (TickSize * rr1to15Long), "PTL", "TML");
                                }                                                                                
                                if (onetotwo){                                                                    
                                    ExitLongLimit(1, true, Position.Quantity, Position.AveragePrice + (TickSize * rr1to2Long), "PTL", "TML");
                                }                                                                                
                                if (onetotwohalf){                                                                
                                    ExitLongLimit(1, true, Position.Quantity, Position.AveragePrice + (TickSize * rr1to25Long), "PTL", "TML");
                                }                                                                                
                                if (onetothree){                                                                  
                                    ExitLongLimit(1, true, Position.Quantity, Position.AveragePrice + (TickSize * rr1to3Long), "PTL", "TML");
                                }
                                else
                                    ExitLongLimit(1, true, Position.Quantity, Position.AveragePrice + (TickSize * TrailTakeProfit), "PTL", "TML");
                                }
                                switchtrail =false;
    
                            }    
    
    
    
    
                            if ((Position.MarketPosition == MarketPosition.Short && myFreeTradeShortTrail == true )
                                 && (ParabolicSAR1[0] > High[0]))
    
                            {
                                    MySarStop = ParabolicSAR1[0];
                            }
    
                            if (Position.MarketPosition == MarketPosition.Short && myFreeTradeShortTrail == true  )
                            {
                                  if(MySarStop >= ask){
                                ExitShortStopMarket(1, true, Position.Quantity, MySarStop, "SLS", "TMS");
                                  }
                            }
    
                            if (Position.MarketPosition == MarketPosition.Short && myFreeTradeShortTrail == true && switchtrail == true )
                            {
    
    //                            ExitShortStopMarket(1, true, Position.Quantity, MySarStop, "SLS", "TMS");
    //                            ExitShortLimit(1, true, Position.Quantity, Position.AveragePrice - (TickSize * TrailTakeProfit), "PTS", "TMS");    
                                if(UseRiskReward )
                                {
                                if(onetoone){
                                    ExitShortLimit(1, true, Position.Quantity, Position.AveragePrice - (TickSize * rr1to1Short), "PTS", "TMS");
                                }                                                                                  
                                if(onetoonehalf){                                                                  
                                    ExitShortLimit(1, true, Position.Quantity, Position.AveragePrice - (TickSize * rr1to15Short), "PTS", "TMS");
                                }                                                                                  
                                if (onetotwo){                                                                    
                                    ExitShortLimit(1, true, Position.Quantity, Position.AveragePrice - (TickSize * rr1to2Short), "PTS", "TMS");
                                }                                                                                  
                                if (onetotwohalf){                                                                
                                    ExitShortLimit(1, true, Position.Quantity, Position.AveragePrice - (TickSize * rr1to25Short), "PTS", "TMS");
                                }                                                                                  
                                if (onetothree){                                                                  
                                    ExitShortLimit(1, true, Position.Quantity, Position.AveragePrice - (TickSize * rr1to3Short), "PTS", "TMS");
                                }
                                else
                                    ExitShortLimit(1, true, Position.Quantity, Position.AveragePrice - (TickSize * TrailTakeProfit), "PTS", "TMS");    
                                }
    
    
                             switchtrail =false;
    
                            }
                        }
                    }
    
                    #endregion​

    #2
    Hello tkaboris,

    To have the order placed immediately after the entry fills would require using OnExecutionUpdate. Your logic in OnBarUpdate will work in accordance with the frequency that OnBarUpdate is called. You would have to wait for OnBarUpdate to be called once you are in a position for that type of logic to work.

    There are some samples of using OnExecutionUpdate to drive logic here: https://ninjatrader.com/support/help...ub=onexecution

    Comment


      #3
      So i keep onBarupdate logic and create a new one using OnExecutionUpdate?

      Comment


        #4
        Hello tkaboris,

        Yes if that is what you want to happen, if the OnBarUpdate logic is still relevant you would keep that. OnExecutionUpdate observes executions and can be used to observe the entry fill and drive logic like submitting targets.

        Comment


          #5
          thank you
          i was wondering can this logic be achieved in OnMarketData and not in OnExecutionUpdate

          Comment


            #6
            Hello tkaboris,

            OnMarketData is not driven by your execution fills that is just the streaming market data override, that would be similar to using OnBarUpdate to drive logic. You would be checking the position and then submitting the order once your position changes. If you want to submit an order based on the actual entry fills that logic would go inside OnExecutionUpdate

            Comment


              #7
              Thank you. I am looking at that help guide for execution update and its bit confusing there are series, for loops in that example and i am just not sure if i really need that just to place initial SL before SAR trail kicks in.
              Do you maybe have other example script you can share? I searched forum but didnt find a good simple example.. ​

              Comment


                #8
                Hello tkaboris,

                You can see the following example which submits targets from OnExecutionUpdate: https://ninjatrader.com/support/help...ub=onexecution

                Comment


                  #9
                  Ok i will study it, does onOrderUpdate required then also?

                  Comment


                    #10
                    HI
                    I am was able to modify my logic and make it work.
                    However i still have issue with price cant be changed above market. I used prints and noticed there are slight differences in MySarStop and what on the chart. I attached screenshots
                    prints printed mysarstop14990.5 and error is at 14990.25. I was wondering if you can help me to avoid this issue..
                    I appriciate it, i already applied last condition4 but it still gives out this error

                    Code:
                    private double MySarStop;
                    in dataloaded i have
                    ParabolicSAR1                = ParabolicSAR(Close,Acc, AccStep, AccMax);
                    
                    EnterLong(1,PositionSize, "TML");    
                                      myFreeTradeLong = true;
                                      myCurrentTrail = true;
                                      MySarStop = (Position.AveragePrice - (StopTicksLong * TickSize)) ;    ​
                    ===========================
                    if (SetTrail )              
                                    {                    
                                        if((myFreeTrail == true )
                    //                        &&(Position.MarketPosition == MarketPosition.Long || Position.MarketPosition == MarketPosition.Short))
                                             && (Position.MarketPosition == MarketPosition.Short)
                                            || (Position.MarketPosition == MarketPosition.Long)
                                            )
                                        {
                                            initialTrailStopShort = Swing(2).SwingHigh[1];
                                            initialTrailStopLong = Position.AveragePrice + (TickSize * StopTicksLong);
                    //                        trailAreaShort = High[1];
                                        }
                    
                                        if ((Position.MarketPosition == MarketPosition.Long && myFreeTradeLongTrail == true )
                                                 && (ParabolicSAR1[0] > High[0]))
                    
                    
                                            {
                                                    Print("condition1");
                                                    ExitLongStopMarket(1, true, Position.Quantity, MySarStop, "SLL", "TML");
                                            }
                    
                                        {
                    
                                            if ((Position.MarketPosition == MarketPosition.Long && myFreeTradeLongTrail == true)
                                                 && (ParabolicSAR1[0] <= Low[0]) )
                    
                                            {
                                                    Print("condition2");
                                                Print("MySarStop" + MySarStop);
                                                    MySarStop = ParabolicSAR1[0];
                                            }    
                    
                                            if (Position.MarketPosition == MarketPosition.Long && myFreeTradeLongTrail == true  )
                                            {
                                                 if(MySarStop <= bid ){
                                                    Print("condition3");
                                                     Print("MySarStop" + MySarStop);
                                                     ExitLongStopMarket(1, true, Position.Quantity, MySarStop, "SLL", "TML");
                                                 }
                                            }
                                            if (Position.MarketPosition == MarketPosition.Long && myFreeTradeLongTrail == true  )
                                            {
                                                 if(MySarStop == bid ){
                                                    Print("condition4");
                                                     Print("MySarStop" + MySarStop);
                                                     ExitLong();
                    //                                 ExitLongStopMarket(1, true, Position.Quantity, MySarStop, "SLL", "TML");
                                                 }
                                            }
                    
                                            if (Position.MarketPosition == MarketPosition.Long && myFreeTradeLongTrail == true && switchtrail == true)
                                            {
                    //                            ExitLongStopMarket(1, true, Position.Quantity, MySarStop, "SLL", "TML");
                    //                            ExitLongLimit(1, true, Position.Quantity, Position.AveragePrice + (TickSize * TrailTakeProfit), "PTL", "TML");
                                                if(UseRiskReward)
                                                {
                                                if(onetoone){
                                                    ExitLongLimit(1, true, Position.Quantity, Position.AveragePrice + (TickSize * trr1to1Long), "PTL", "TML");
                                                }                                                                                
                                                if(onetoonehalf){                                                                
                                                    ExitLongLimit(1, true, Position.Quantity, Position.AveragePrice + (TickSize * trr1to15Long), "PTL", "TML");
                                                }                                                                                
                                                if (onetotwo){                                                                    
                                                    ExitLongLimit(1, true, Position.Quantity, Position.AveragePrice + (TickSize * trr1to2Long), "PTL", "TML");
                                                }                                                                                
                                                if (onetotwohalf){                                                                
                                                    ExitLongLimit(1, true, Position.Quantity, Position.AveragePrice + (TickSize * trr1to25Long), "PTL", "TML");
                                                }                                                                                
                                                if (onetothree){                                                                  
                                                    ExitLongLimit(1, true, Position.Quantity, Position.AveragePrice + (TickSize * trr1to3Long), "PTL", "TML");
                                                }
                                                else
                                                    ExitLongLimit(1, true, Position.Quantity, Position.AveragePrice + (TickSize * TrailTakeProfit), "PTL", "TML");
                                                }
                                                switchtrail =false;
                    
                                            }    ​
                    }


                    Comment


                      #11
                      Hello tkaboris,

                      If you are placing trades on the wrong side of the market you would get this type of error. You need to make sure the price you are using for the target is valid at the time you submit the order. The code you have has no error checking for prices being used to confirm they are on the correct side of the market. The Position.AveragePrice are not guaranteed to be on the correct side of the market because the market can move up or down. You need to add logic to check that yourself and make sure a valid price is being used at that time.

                      As a side note we have already covered this error in great detail the last time you had asked about it, the solution is still going to be the same. You need to modify your logic to check if the price is valid before submitting that change to the order. https://forum.ninjatrader.com/forum/...cant-be-placed



                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by NullPointStrategies, Yesterday, 05:17 AM
                      0 responses
                      63 views
                      0 likes
                      Last Post NullPointStrategies  
                      Started by argusthome, 03-08-2026, 10:06 AM
                      0 responses
                      139 views
                      0 likes
                      Last Post argusthome  
                      Started by NabilKhattabi, 03-06-2026, 11:18 AM
                      0 responses
                      75 views
                      0 likes
                      Last Post NabilKhattabi  
                      Started by Deep42, 03-06-2026, 12:28 AM
                      0 responses
                      45 views
                      0 likes
                      Last Post Deep42
                      by Deep42
                       
                      Started by TheRealMorford, 03-05-2026, 06:15 PM
                      0 responses
                      50 views
                      0 likes
                      Last Post TheRealMorford  
                      Working...
                      X