Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trailing SetStopLoss() managed approach - not working

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

    Trailing SetStopLoss() managed approach - not working

    Hello,

    Recently I try to develop a strategy based on my own indicator.

    The indicator is working fine and it is related to imbalances. Its CalculationMode is OnBarClose because during the bar development the imbalance may change, so I need to wait until the end of the bar before analyse the volumes at each price levels and conclude if there is imbalances in some levels.

    On the other side, the strategy is using Calculate.OnEachTick, because I need a tick precision for the stopLoss and takeProfit positions handling. This way the strategy Calculate mode override the indicators's Calculate mode. That is fine for me and I think there is nothing to related to this problem because on the other time all the orders are executed as expected. The strategy is run with TickReplay on ES for 1 week test period. I am connected to live market data during the back-testing and also I have the MarketReplay historical data downloaded locally.

    However, during back-testing I got some strange behaviour on the strategy side. It seems like there are case where stopLosses are not executed properly and I do not know why.

    I wish to use Managed approach for OCO orders handling using SetStopLoss() and SetProfitTarget(). I also needed and impelemted a trailing stop loss using the SetStopLoss(Calculate.Price), but I do not think this is the problem.

    I attach the code, TraceOrders only for one of the screenshots, and some additional logging from the strategy output that is showing that the current UnrealizedProfitAndLoss is more than the stop loss.

    Any ideas why there is no stop losses executed?

    Best wishes
    Attached Files
    Last edited by nrgful; 05-06-2025, 11:15 AM.

    #2
    Hello nrgful,

    Any hosted symbiote script will use the same Calculate setting as the host strategy or indicator.
    This is necessary to prevent unexpected behavior.

    Set methods will only update when the bar updates. Any trailing behavior will occur when the bar updates.

    Below is a link to an example, ProfitChaseStopTrailSetMethodsExample, that adjusts the price of a SetStopLoss() order for custom trailing behavior.


    For order fills at an accurate price and time in historical, 1-tick intra-bar granularity is necessary. If you are using Calculate.OnEachTick or .OnPriceChange, then TickReplay is also necesssary.

    See the support article below.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you @NinjaTrader_ChelseaB​,

      In my first post I uploaded additional files with detailed data from the strategy execution, but now it seems that they are not available. I will do that again.

      I upload the actual strategy code - I already researched the ProfitChaseStopTrailSetMethodsExample that you provided me, but it seems to me that there is no differences and I use this approach as described into the example. I think that I did everything as described.

      I also upload some data from the output. TicksInProfit.txt file is showing the actual calculation of the profit and loss and it seems to me like the stopLoss order is actually not set, because I have some huge negative values for the UPnL. It seems like the stopLoss should be executed long time before this huge UPnL is reached.

      I also upload the TraceOrder = true; logs.
      All of these files are actually related to one of the screenshots that I uploaded earlier showing the gap in the chart where no orders are executed.

      I do not know what else I can do.
      Thank you for your advide to improve the accuracy by adding a 1-tick granularity, and I think it will valuable for me. However, I think that this is not the root cause for the mention problem above.

      Any ideas are welcome

      Best wishes
      Attached Files
      Last edited by nrgful; 05-08-2025, 04:49 AM.

      Comment


        #4
        Hello nrgful,

        "However, I think that this is not the root cause for the mention problem above."

        In historical (backtest) in the Strategy Analyzer which I am seeing in your screenshot, stop orders will fill with end of bar information. This means they may fill at a different price than the stop price.

        If the issue is with the stop order fills, then yes your script is affected by a lack of 1-tick intra-bar granularity.

        You will have orders that fill with the end of bar price points and not at the actual time or price of where the order should have filled.
        Using Calculate.OnEachTick or .OnPriceChange with TickReplay will mean the script is updating intra-bar, but the orders are filling at the end of the bar.
        This means the pnl of a trade will not be what you expect.

        Further, you would need to use Exit methods and not Set methods to implement 1-tick intra-bar granularity.

        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          NinjaTrader_ChelseaB Thanl you very much.

          But I am afraid that you did not went deep enough to understand my problem. If you look at the screenshots that I provided, I have upper and down borders of several consequent levels of selling imbalances - red lines. And also upper and down borders of several stacked buying imbalances - in green. The strategy is implemented to open a position in the direction of the selling or buying imbalances when they are stacked together.

          My problem is not the exact fulfilment of the order on expected price level, but not having at all any trade after a given time on. The problem is that the stop loss order is somehow not set. I have no message of the order is failed/cancelled/ignored. And the strategy is in open position generating massive losses.

          I did what is needed to implement trailing stop loss, but it seems that neither the stop loss is executed when it is expected to do that, nor I have an error message that the order is ignored/canceled/failed.
          Attached Files

          Comment


            #6
            Hello nrgful,

            In addition to TraceOrders, you will need to print the order.ToString() in OnOrderUpdate() as well as the High[0] and Low[0] and bar Time[0] in OnBarUpdate().

            Below is a link to a support article on adding debugging prints to understand behavior.

            From the output you have provided the last order was a short entry order, and this was likely closed by a profit target as the stop order was cancelled due to the OCO.

            4/22/2025 23:30:57 Strategy 'HXA Strategy Imbalance/-1': Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='ShortEntry' Mode=Price Value=5320.75 IsSimulatedStop=False IsMarketIfTouched=False
            4/22/2025 23:30:57 Strategy 'HXA Strategy Imbalance/-1': Entered internal SetStopTarget() method: Type=Target FromEntrySignal='ShortEntry' Mode=Price Value=5316.75 IsSimulatedStop=False IsMarketIfTouched=False

            4/22/2025 23:30:57 Strategy 'HXA Strategy Imbalance/-1': Entered internal SubmitOrderManaged() method at 4/22/2025 23:30:57: BarsInProgress=0 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='ShortEntry' FromEntrySignal=''

            4/22/2025 23:34:42 Strategy 'HXA Strategy Imbalance/-1': Cancelled OCO paired order: BarsInProgress=0, orderId='NT-00256-1318' account='Backtest' name='Stop loss' orderState=Cancelled instrument='ES 06-25' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=5320.75 quantity=1 tif=Gtc oco='NT-00170-1318' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2025-04-22 23:30:57' gtd='2099-12-01' statementDate='2025-05-06'​

            As the orders are filling with end of bar data, if both the limit and stop order are within the high and low of the bar, either order could fill. With 1-tick intra-bar granularity the orders would fill intra-bar at the price and time of the first tick that satisfies the order price, and would fill the correct order of the pair.
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Today, 05:17 AM
            0 responses
            52 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            130 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            70 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            44 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            49 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X