Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

help with trail and take profit

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

    help with trail and take profit

    Hi i cant figure out why my take profit is also trailing and not just fixed. if i remove all enums bools my trail take profit works fine, but when i try to impelement take profits from enum, take profits are not fixed and always chaning. Its onBarupdate.
    do you have a suggestion?
    Thank you

    Code:
            
                    if (SetTrail )              
                    {                    
                        if((myFreeTrail == true)
                            &&(Position.MarketPosition == MarketPosition.Long || Position.MarketPosition == MarketPosition.Short))
                        {
                            if ((Position.MarketPosition == MarketPosition.Long )
                                 && (ParabolicSAR1[0] <= Low[0]))
    
                            {
                                    MySarStop = ParabolicSAR1[0];
                            }            
    
                            if (Position.MarketPosition == MarketPosition.Long )
                            {
                                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");
                                }
    
                            }    
    
    
                            if ((Position.MarketPosition == MarketPosition.Short )
                                 && (ParabolicSAR1[0] >= High[0]))
    
                            {
    
                                    MySarStop = ParabolicSAR1[0];
    
                            }
    
    
                            if (Position.MarketPosition == MarketPosition.Short )
                            {
    
                                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");    
                                }
    
                            }
                        }
                    }
    
                    #endregion​

    #2
    Hello tkaboris,

    Each time you call the order method again it will update the existing order. If you see your target is trailing and is not fixed that means you are calling that order again. You are intentionally making live until cancelled orders by setting them to true when you call the method, that means you do not need to call that method again after you initially submit the orders.

    To find out how your logic is working specifically you would need to use prints.
    Last edited by NinjaTrader_Jesse; 07-18-2023, 07:27 AM.

    Comment


      #3
      I put up prints for trr1to3Short and its trailing, even if I changed it to false to liveuntilcanceled
      if (onetothree){
      ExitShortLimit(1, false, Position.Quantity, Position.AveragePrice - (TickSize * trr1to3Short), "PTS", "TMS");
      }​

      Comment


        #4
        Hello tkaboris,

        Right, changing live until cancelled doesn't change the fact that you are updating the order. You just confirmed that your logic is the problem based on that print.

        Orders which are not live until cancelled need to continuously be called to remain active, if the price you use for the order changes at all that would create a trailing effect, it would otherwise be stationary as long as the price is not changing.

        Orders which are live until cancelled need to be called one time only and then not again, its live until its cancelled or filled.

        Comment


          #5
          I understand. If i set myFreeTradeShortTrail = false; at the end it fixes take profit but it also fixes trail stop which i dont want. I need to seperate these two logics and i have a hard time. I need both trailstop to trai and take profit to set only once


          EnterShort(1,PositionSize, "TMS");
          MySarStop = (Position.AveragePrice + (StopTicksShort * TickSize)) ;
          myCurrentTrail = true;
          myFreeTradeShortTrail = true;


          if ((Position.MarketPosition == MarketPosition.Short && myFreeTradeShortTrail == true )
          && (ParabolicSAR1[0] >= High[0]))

          {

          MySarStop = ParabolicSAR1[0];

          }

          if (Position.MarketPosition == MarketPosition.Short && switchtrail == false )
          {
          ExitShortStopMarket(1, true, Position.Quantity, MySarStop, "SLS", "TMS");

          }

          if (Position.MarketPosition == MarketPosition.Short && myFreeTradeShortTrail == 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 * trr1to3Short), "PTS", "TMS");
          }
          else
          ExitShortLimit(1, true, Position.Quantity, Position.AveragePrice - (TickSize * TrailTakeProfit), "PTS", "TMS");
          }


          myFreeTradeShortTrail = false;

          }​​

          Comment


            #6
            Hello tkaboris,

            You can make another condition using the same or seperate variable below that. You would have to seperate or change the logic however you need for it to work as you are expecting. If both targets are in the same overall condition they are going to be controlled in the same way.

            Comment


              #7
              I dont know how to, if i place that myFreeTradeShortTrail = false; it sets it to false and it never comes back to true only after new order.
              it seems i need to keep myFreeTradeShortTrail = true, not false for trail to work

              Comment


                #8
                Hello tkaboris,

                I wouldn't be able to comment on what specific changes you need to make, this is a script you made so only you know how it fully works.

                You already have a few if conditions that you are showing so you do know how to make conditions. If you need to separate logic you can continue to make new conditions similar to what you have shown using the same or new variables depending on your needs. If you need to separate one of the targets and control it on its own you can create a new variable and then use that in a new condition.

                Using prints here would be a necessary part of the development process. You can use prints to see how each of your conditions are being evaluated and also output any variables values you want to see. Based on those observations you can change or add new logic to meet your overall needs for each target.

                Comment


                  #9
                  I got it to work . thank you

                  Comment


                    #10
                    I have a question, where does trailing stop behave more consistently and without trhowing any errors when backtesting, is it in onBarUpdate or in OnMarketData
                    Thank you

                    Comment


                      #11
                      Hello tkaboris,

                      If your code is inside OnBarUpdate then the backtest uses OnBarUpdate.

                      I wouldn't be able to say what is different in your logic to cause that, this is a good use case for prints and test it in backtest and realtime to observe differences in the prints.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by NullPointStrategies, Yesterday, 05:17 AM
                      0 responses
                      65 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