Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adding Custom Trail Frequency to trail stop

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

    Adding Custom Trail Frequency to trail stop

    Currently i have trail set to every tick in sIL . How can i make it not every tick but custom, starting lets say from 10 Ticks? Its an unmanaged approach

    Code:
    (Condition)
    {
                            entryOrder = SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.Market, TradeSize, 0, 0, "", "34B");
                            IsInLongTrade = true;                        
                            LastClose = Close[0];
                            if (SystemPrint == true && Trade34B == true)
    
                            return;
                        }
                        if ((UseTrailingStop) && (IsInLongTrade) && (LastClose < Close[0]) && (stopOrder != null))
                        {
                            double slL = Instrument.MasterInstrument.RoundToTickSize(Close[0] - StopLoss * TickSize);    
                            if(entryOrder != null)ChangeOrder(stopOrder, Position.Quantity, 0, slL);                        
                            LastClose = Close[0];                        
    
                        }​

    #2
    Hello tkaboris,

    You would need to do something similar to this strategy builder trailing example, you would use logic and variables to control the frequency of trailing that you wanted.

    Comment


      #3
      Thank you
      I edited to this logic for Longs, but my trail is still based per tick. I want to have move every 5 ticks.

      TrailFrequency = 5;

      Code:
      if (longsOn)
                      {
                          if (
                                 (!IsInLongTrade)
      
                              && (!IsInShortTrade)
      
                              && (stopOrder == null)
      
                              && (Position.Quantity == 0)
      
                              && (EMAAngulationSelectorInputTurnedOn ? (emaAngulation > (EMAAngulation * TickSize))  :   Close[0] > 0)                        // EMA Angulation is greater then (x)
      
                              && (EMAAngulationSelectorInputTurnedOn ? (emaAngulationXBarsBefore > (EMAAngulationXBarsBefore * TickSize))  :   Close[0] > 0)    // EMA Angulation X bars before - angulation not on current bar but 5 bars before for example
      
                              && (OCSelectorInputTurnedOn ? Close[1 + 1] < Open[1 + 1]      :   Close[0] > 0)                                        // previous bar bearish
      
                              && (HHLLSelectorInputTurnedOn ? Low[0 + 1] < Low[1 + 1]          :   Close[0] > 0)                                        // and low of signal bar is lower then previous bar
      
                              && (SlowEMASelectorInputTurnedOn ? ((Low[0 + 1] < slowEMA[0 + 1]) && (Close[0 + 1] > (slowEMA[0 + 1] + (AboveEMATicks * TickSize)))) :   Close[0] > 0)
      
                              && (OCHLSelectorInputTurnedOn ? !(Open[1 + 1] < High[1 + 1] && Close[0 + 1] < High[1 + 1]) :   Close[0] > 0)
                           )
                          {    
                              entryOrder = SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.Market, TradeSize, 0, 0, "", "BT");
      
                              IsInLongTrade = true;
      
                              LastClose = Close[0];
      
      
                              if(PrintToOutputScreen)
                              {
                                  Print("");
                                  Print("Enter Long Trade");
                              }
      
      
                              return;
                          }
      
                          if ((UseTrailingStop) && (IsInLongTrade) && (LastClose < Close[0]) && (stopOrder != null))
                          {
                              double slL = Instrument.MasterInstrument.RoundToTickSize(Close[0] - (StopLoss + TrailFrequency) * TickSize);
      
                              if(entryOrder != null)ChangeOrder(stopOrder, Position.Quantity, 0, slL);
      
                              LastClose = Close[0];
      
                              if(PrintToOutputScreen)
                              Print("Long Trade Trailing Stop         slL = " + slL + "        Position.Quantity = " + Position.Quantity + "        CurrentBar = " + CurrentBar);
                          }
                      }​
      In your example there is trailstopdistance variable and i am not sure what it does...

      EnterLong(Convert.ToInt32(DefaultQuantity), @"longEntry");
      CurrentTriggerPrice = (Close[0] + (TrailFrequency * TickSize)) ;
      CurrentStopPrice = (Close[0] + (TrailStopDistance * TickSize)) ;​​
      Last edited by tkaboris; 03-10-2023, 10:01 AM.

      Comment


        #4
        Hello tkaboris,

        I wouldn't be able to tell you what's wrong with the code you made, you would have to use prints to identify how that logic is working.

        The strategy builder sample uses variables to determine a target price and then checks that price in a condition to know when the trail needs to be updated. That is done in set 3 of that sample. Your code would need to be similar to that sample where a new target price is made only when the condition becomes true to change the price. The variable CurrentTriggerPrice is used to set the trigger price which is the amount of offset for the frequency.



        Comment


          #5
          How can I adapt that set 3 portion to my long order script? i need to change these two variables
          CurrentTriggerPrice = (Close[0] + (TrailFrequency * TickSize)) ;
          CurrentStopPrice = (Close[0] + (TrailStopDistance * TickSize)) ;​

          Code:
                          if (longsOn)
                          {
                              if (
                                     (!IsInLongTrade)
          
                                  && (!IsInShortTrade)
          
                                  && (stopOrder == null)
          
                                  && (Position.Quantity == 0)
          
                                  && (EMAAngulationSelectorInputTurnedOn ? (emaAngulation > (EMAAngulation * TickSize))  :   Close[0] > 0)                        // EMA Angulation is greater then (x)
          
                                  && (EMAAngulationSelectorInputTurnedOn ? (emaAngulationXBarsBefore > (EMAAngulationXBarsBefore * TickSize))  :   Close[0] > 0)    // EMA Angulation X bars before - angulation not on current bar but 5 bars before for example
          
                                  && (OCSelectorInputTurnedOn ? Close[1 + 1] < Open[1 + 1]      :   Close[0] > 0)                                        // previous bar bearish
          
                                  && (HHLLSelectorInputTurnedOn ? Low[0 + 1] < Low[1 + 1]          :   Close[0] > 0)                                        // and low of signal bar is lower then previous bar
          
                                  && (SlowEMASelectorInputTurnedOn ? ((Low[0 + 1] < slowEMA[0 + 1]) && (Close[0 + 1] > (slowEMA[0 + 1] + (AboveEMATicks * TickSize)))) :   Close[0] > 0)
          
                                  && (OCHLSelectorInputTurnedOn ? !(Open[1 + 1] < High[1 + 1] && Close[0 + 1] < High[1 + 1]) :   Close[0] > 0)
                               )
                              {    
                                  entryOrder = SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.Market, TradeSize, 0, 0, "", "BT");
          
                                  IsInLongTrade = true;
          
                                  LastClose = Close[0];
          
          
                                  if(PrintToOutputScreen)
                                  {
                                      Print("");
                                      Print("Enter Long Trade");
                                  }
          
          
                                  return;
                              }
          
                              if ((UseTrailingStop) && (IsInLongTrade) && (LastClose < Close[0]) && (stopOrder != null))
                              {
                                  double slL = Instrument.MasterInstrument.RoundToTickSize(Close[0] - (StopLoss + TrailFrequency) * TickSize);
          
                                  if(entryOrder != null)ChangeOrder(stopOrder, Position.Quantity, 0, slL);
          
                                  LastClose = Close[0];
          
                                  if(PrintToOutputScreen)
                                  Print("Long Trade Trailing Stop         slL = " + slL + "        Position.Quantity = " + Position.Quantity + "        CurrentBar = " + CurrentBar);
                              }
          
                              if ((Position.MarketPosition == MarketPosition.Long)
                                   && (Close[0] > CurrentTriggerPrice))
                              {
                                  CurrentTriggerPrice = (Close[0] + (TrailFrequency * TickSize)) ;
                                  CurrentStopPrice = (Close[0] + (TrailStopDistance * TickSize)) ;
                              }
          
                               // Set 4
                              if (CurrentStopPrice != 0)
                              {
                                  ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), CurrentStopPrice, @"", "");
                              }
                          }​

          Comment


            #6
            Hello tkaboris,

            That wouldn't be something I could answer because you developed that code. You would need to figure out how to do similar logic in combination with what you already made. The concept of making the trail happen at a frequency would be to use a trigger price variable and compare the Close price against that variable, if the close is beyond the variable the trigger price and stop price variable gets reset so the target price is changed and a new trigger price is made for the next trail frequency.

            Comment


              #7
              i am a little confused because I also have set take profit and stop loss. And example you provided doesnt have TP or SL and how it integrates between them.

              Comment


                #8
                Hello tkaboris,

                The sample I had linked shows a single stoploss for simplicity, that is the last set in that example. You can use the concept from that samples set 3 which shows how to design a condition which sets a orders price to a variable and then sets a separate variables price for trailing at a frequency.

                Because you are using the unmanaged approach you would need to use ChangeOrder to update your existing order object if you wanted to change its price.

                Comment


                  #9
                  Thank you. I was able to implement similar logic

                  I also have one more question. In trailbuilder example we only have example for long position, code provided below. Do we need to change anything for Short Position?


                  Code:
                   // Set 2
                              if ((Position.MarketPosition == MarketPosition.Flat)
                                   && (State == State.Realtime))
                              {
                                  EnterLong(Convert.ToInt32(DefaultQuantity), @"longEntry");
                                  CurrentTriggerPrice = (Close[0] + (TrailFrequency * TickSize)) ;
                                  CurrentStopPrice = (Close[0] + (TrailStopDistance * TickSize)) ;
                              }
                  
                               // Set 3
                              if ((Position.MarketPosition == MarketPosition.Long)
                                   && (Close[0] > CurrentTriggerPrice))
                              {
                                  CurrentTriggerPrice = (Close[0] + (TrailFrequency * TickSize)) ;
                                  CurrentStopPrice = (Close[0] + (TrailStopDistance * TickSize)) ;
                              }​

                  Comment


                    #10
                    Hello tkaboris,

                    For a long position the stop is placed below the entry and then is moved up by calculating a higher price.

                    In the case of a short position, the stop is placed above the entry and then is moved down by calculating a lower price.

                    Meaning once the close is less than the trigger price, you would subtract ticks from the trigger price and stop price.
                    Chelsea B.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by NullPointStrategies, 03-13-2026, 05:17 AM
                    0 responses
                    86 views
                    0 likes
                    Last Post NullPointStrategies  
                    Started by argusthome, 03-08-2026, 10:06 AM
                    0 responses
                    151 views
                    0 likes
                    Last Post argusthome  
                    Started by NabilKhattabi, 03-06-2026, 11:18 AM
                    0 responses
                    79 views
                    0 likes
                    Last Post NabilKhattabi  
                    Started by Deep42, 03-06-2026, 12:28 AM
                    0 responses
                    53 views
                    0 likes
                    Last Post Deep42
                    by Deep42
                     
                    Started by TheRealMorford, 03-05-2026, 06:15 PM
                    0 responses
                    60 views
                    0 likes
                    Last Post TheRealMorford  
                    Working...
                    X