Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

trailing stop is wrong on tradovate accounts

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

    trailing stop is wrong on tradovate accounts

    I could really use your help. I have been trying to fix this for days.

    My NT8 strategy works great on ninjatrader sim account.
    • Behavior on sim (good): When trade is opened, it has a 20 tick stop loss. If price moves into some loss, then into profit enough to trigger my breakeven, the stop loss is moved to breakeven.
    • Behavior on tradovate demo (bad): When trade is opened, it has a 20 tick stop loss. If price moves into some loss, then back in the right direction, it seems like the stop is trailed right behind the price, and if it moves back in the wrong direction even 1 tick, the order is closed and I hear "stop filled" from ninjatrader even though the stop was several points away.
    Result:
    • On NT sim account, if a trade is opened and moves into loss, the trade is ONLY closed if it hits the stop loss 20 ticks away.
    • On tradovate accounts, if a trade is opened and moves into some loss, the trade is closed as soon as it starts moving back in the right direction and ninjatrader says "stop filled" even though the stop didn't appear to trail on the chart, and it was quite far away.

    I disabled trailing stop setting in my strategy, and this no longer happens. So something is wrong in my trailing stop management, which is causing the issue on Tradovate accounts only.

    Relevant code is below. Please let me know if I need to provide more information.

    Code:
    private void ManageTrailingStop(double currentPrice)
            {
                if (Position.MarketPosition == MarketPosition.Flat)
                {
                    Print($"[{Time[0]}] ManageTrailingStop: Position is flat. Not managing trailing stop.");
                    return;
                }
                double currentStopPrice = GetCurrentStopPrice();
                Print($"[{Time[0]}] ManageTrailingStop - Current price: {currentPrice}, Current stop: {currentStopPrice}, Position: {Position.MarketPosition}");
                if (Position.MarketPosition == MarketPosition.Long)
                {
                    double potentialNewStop = currentPrice - TrailingStopTicks * TickSize;
                    if (potentialNewStop > currentStopPrice)
                    {
                        UpdateStopLoss(potentialNewStop);
                        Print($"[{Time[0]}] Updated long trailing stop from {currentStopPrice} to {potentialNewStop}");
                    }
                    else
                    {
                        Print($"[{Time[0]}] Long trailing stop not updated. Potential: {potentialNewStop}, Current: {currentStopPrice}");
                    }
                }
                else // Short position
                {
                    double potentialNewStop = currentPrice + TrailingStopTicks * TickSize;
                    if (potentialNewStop < currentStopPrice)
                    {
                        UpdateStopLoss(potentialNewStop);
                        Print($"[{Time[0]}] Updated short trailing stop from {currentStopPrice} to {potentialNewStop}");
                    }
                    else
                    {
                        Print($"[{Time[0]}] Short trailing stop not updated. Potential: {potentialNewStop}, Current: {currentStopPrice}");
                    }
                }
            }​
    Last edited by iantriestrading; 08-20-2024, 11:40 AM.

    #2
    Hello iantriestrading,

    Thank you for your post.

    I recommend debugging the script using prints to follow your trailing stop logic.

    To understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the strategy add prints (outside of any conditions) that print the date time of the bar and all values compared in every condition that places an order. I recommend your prints also illustrate the price your trailing stop is being submitted to along with the current price, so we can see these prices in relation to eachother.

    The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.

    The debugging print output should clearly show what the condition is, what time the conditions are being compared, all values being compared, and how they are being compared.

    Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

    Further, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

    After enabling TraceOrders remove the instance of the strategy from the Configured list in the Strategies window and add a new instance of the strategy from the Available list.

    I am happy to assist you with analyzing the output from the output window.

    Run or backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.

    Below is a link to a support article that demonstrates using informative prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.

    https://support.ninjatrader.com/s/ar...nd-TraceOrders

    Comment


      #3
      Hey Gaby, thanks so much for the detailed reply. I'm sorry to say that I think I was wrong about the issue. I checked the orders report for the tradovate account and I can see that the stop loss order price in tradovate is points away from the stop loss price set by the strategy.

      For example, the strategy enters long NQ at 19807 with stop loss at 19802. I can see it shows this stop loss in the correct place on the chart in Ninjatrader. But if i check orders in tradovate, the stop loss (stop market) order in Tradovate is in status "working" with price 19805.75. So in NT8 the stop loss is 19802, but in tradovate the stop loss is at 19805.75

      Curious if this is something you may be familiar with. It's a stop market order. This happens with every trade. My logs show that when SetStopLoss is called, it's being set to the correct/desired price. But when I check tradovate orders report, the stop loss price is always multiple points closer to my entry than it's supposed to be.
      Last edited by iantriestrading; 08-20-2024, 01:09 PM.

      Comment


        #4
        Hello,

        To confirm, is this an actual Tradovate demo account or a NinjaTrader Brokerage demo?

        We'll need output to confirm the issue. ​Print the order object in OnOrderUpdate(), and enable TraceOrders.

        TraceOrders - https://ninjatrader.com/support/help...raceorders.htm
        OnOrderUpdate() - https://ninjatrader.com/support/help...rderupdate.htm

        Please provide the output for review.

        If possible, please also provide a screenshot of what the same order is reporting in Tradovate. I'm not sure exactly what screen you are looking in TDV so please make sure sensitive info like your account number is not included in the screenshot, we just want to see what is being reported in TDV to compare to what we are seeing happening in NinjaTrader.

        Comment


          #5
          I've attached the log including prints of the order object whenever OnOrderUpdate runs. This is on a tradovate demo account (from tradovate).

          I found something that looks weird.

          Starting on line 18 you can see the stop is set to 19829.75, and then it's amended on line 19 to 19827.25

          Here's the snippet:

          Code:
          [8/20/2024 3:36:00 PM] - In SetStopLossAndProfitTarget:: Adjusted stopLossPrice to 19829.75
          [8/20/2024 3:36:00 PM] - In CancelExistingStopLosses:: Cancelled existing stop loss order: Stop loss
          8/20/2024 3:36:01 PM Strategy 'OneMinuteScalpingStrategy/332112081': Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=19829.75 IsSimulatedStop=False IsMarketIfTouched=False
          8/20/2024 3:36:01 PM Strategy 'OneMinuteScalpingStrategy/332112081': Amended stop order orderId='09ae5d74455f47de85f22a2e49fea15e' account='DEMO2655675' name='Stop loss' orderState=Initialized instrument='NQ SEP24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=19827.25 quantity=5 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=40140 time='2024-08-20 15:36:01' gtd='2099-12-01' statementDate='2024-08-20' stopPriceChanged=19829.75
          [8/20/2024 3:36:00 PM] - In OnOrderUpdate:: Order Update - Name: Stop loss, State: ChangePending, Error: NoError, Native Error:
          [8/20/2024 3:36:00 PM] - In OnOrderUpdate:: Order object: orderId='09ae5d74455f47de85f22a2e49fea15e' account='DEMO2655675' name='Stop loss' orderState=ChangePending instrument='NQ SEP24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=19827.25 quantity=5 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=40140 time='2024-08-20 15:36:01' gtd='2099-12-01' statementDate='2024-08-20'
          [8/20/2024 3:36:00 PM] - In SetStopLossAndProfitTarget:: Set stop loss at 19829.75 for entire position of 5 contracts​
          Attached Files
          Last edited by iantriestrading; 08-20-2024, 02:05 PM.

          Comment


            #6
            Hello iantriestrading,

            If the stop is being amended, you will need to add further descriptive prints to the code to find out why the logic in your code is amending the stop loss.

            In the strategy add prints (outside of any conditions) that print the date time of the bar and all values compared in every condition that places an order.

            The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.

            The debugging print output should clearly show what the condition is, what time the conditions are being compared, all values being compared, and how they are being compared.

            If you need assistance creating a descriptive print, please let us know. ​

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Today, 05:17 AM
            0 responses
            50 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            126 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            69 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            42 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            46 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X