Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

EnterLong, ExitLongStopMarket and ExitLongLimit NOT working together on live data ...

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

    EnterLong, ExitLongStopMarket and ExitLongLimit NOT working together on live data ...

    Dear Experts - I have a strange observation. I wrote a test script with the intent of doing my own stop-loss and target management and I noticed that in Playback the part of my code snippet below called in between comment lines seems to work fine (irrespective of strategy calculation configured) but when I run this script against real-time data in a SIM account the brackets are only entered as per strategy calculation approach (OnBarClose or OnPriceChange). Currently a Boolean flag based approach is my workaround but that is constraining.

    Any guidance and advise would be helpful as I have not found this problem posted earlier on this forum - and I sincerely hope its not something on my NT8 instance alone!

    protected override void OnBarUpdate()
    {
    ...
    ...
    ...

    region LONG POSITION MANAGEMENT
    // ENTER LONG
    if ( << ENTRY CONDITIONS MET >>)
    {
    EnterLong(Convert.ToInt32(Order_Qty), "");
    TradeCheck = true; // Flag that a position has just been taken. This enables execution of one-time logic till position is exited.

    //============= NOT WORKING IN REALTIME. BRACKETS ARE STILL ENTERED ON STRAEGY CLCULATION INTERVAL (BAR CLOSE OR PRICE CHANGE)
    ATRPrice = (ATR1[0] * ATRMultiplier);
    if ((ATRPrice > PreferredPositionLoss) && (boolOverrideATRMultiplier == true)) // if ATR based stop greater than user provided stop loss amount & elected to override ATRMultiplier
    {
    ATRPrice = PreferredPositionLoss;
    }
    ATRStop = (Position.AveragePrice - ATRPrice);
    ExitLongStopMarket(Convert.ToInt32(Order_Qty), ATRStop);
    ExitLongLimit(Position.AveragePrice + PreferredPositionTarget);
    //=====================================
    }
    // Trailing Stop & Target Profit for LONG POSITION
    if ((Position.MarketPosition == MarketPosition.Long)
    && (TradeCheck == true))
    {
    ...
    ...
    ExitLongStopMarket(Convert.ToInt32(Order_Qty), ATRStop); // Enter a stop-loss order (stop market)
    ExitLongLimit(Position.AveragePrice + PreferredPositionTarget); // Enter a target order (limit order)
    TradeCheck = false; // Disable the flag so this piece of logic is not executed till position is exited
    }

    ...
    ...
    ...
    ...

    if ((Position.MarketPosition == MarketPosition.Long)
    && (TradeCheck == false))
    {
    ExitLongStopMarket(Convert.ToInt32(Order_Qty), ATRStop); // Enter a stop-loss order (stop market)
    ExitLongLimit(Position.AveragePrice + PreferredPositionTarget); // Enter a target order (limit order)
    ...
    ...
    ...
    }

    #endregion

    #2
    Hello WattMan,

    Thanks for your post.

    In the code you shared I see you are calling your Entry order method and Exit order methods within the same condition in OnBarUpdate().

    Your Entry order method and Exit order methods should not be called within the same condition in OnBarUpdate(). This means that you will need to separate your Exit order methods from your Entry order method so that it is not called within the same condition.

    Something you could consider is creating a condition that checks if you are in a Long market position and then call your exit order methods within that condition. Position.MarketPosition would be used to check if you are in a Long market position.

    For example, the code might look something like this:

    Code:
    if ( << ENTRY CONDITIONS MET >>)
    {
        EnterLong(Convert.ToInt32(Order_Qty), "");
        TradeCheck = true; // Flag that a position has just been taken. This enables execution of one-time logic till position is exited.
        //============= NOT WORKING IN REALTIME. BRACKETS ARE STILL ENTERED ON STRAEGY CLCULATION INTERVAL (BAR CLOSE OR PRICE CHANGE)
        ATRPrice = (ATR1[0] * ATRMultiplier);
        if ((ATRPrice > PreferredPositionLoss) && (boolOverrideATRMultiplier == true)) // if ATR based stop greater than user provided stop loss amount & elected to override ATRMultiplier
        {
            ATRPrice = PreferredPositionLoss;
        }
    
        ATRStop = (Position.AveragePrice - ATRPrice);​
    }
    
    if (Position.MarketPosition == MarketPosition.Long)
    {
        ExitLongStopMarket(Convert.ToInt32(Order_Qty), ATRStop);
        ExitLongLimit(Position.AveragePrice + PreferredPositionTarget);
        //=====================================
    }


    See this help guide page for more information about Position.MarketPosition: https://ninjatrader.com/support/help...etposition.htm
    Last edited by NinjaTrader_BrandonH; 08-27-2023, 05:01 PM.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Originally posted by NinjaTrader_BrandonH View Post
      Hello WattMan,
      Your Entry order method and Exit order methods should not be called within the same condition in OnBarUpdate(). This means that you will need to separate your Exit order methods from your Entry order method so that it is not called within the same condition.
      I already do this by the use of a flag (TradeCheck in my case) in conjunction with the Position.MarketPosition check before entering the stop loss and target orders. Is tthere any other condiction that I can check this in? I set my strategy to re-calculate OnBarClose.

      Originally posted by NinjaTrader_BrandonH View Post
      Something you could consider is creating a condition that checks if you are in a Long market position and then call your exit order methods within that condition. Position.MarketPosition would be used to check if you are in a Long market position.
      My issue is 2-fold (1) that in Playback this gives the impression that it will work as I coded, but the real time data does not honor this - pretty sure seems like a bug to me ... (2) if I have a long timeframe (e.g., 1000 ticks) then my stop-loss and target orders are not placed till the next bar (if called from OnBarUpdate) or I get stopped-out too early if I set the strategy calculation to OnPriceChange.

      Comment


        #4
        Hello WattMan,

        Thanks for your notes.

        You would still need to call the ExitLongStopMarket(Convert.ToInt32(Order_Qty), ATRStop); and the ExitLongLimit(Position.AveragePrice + PreferredPositionTarget); methods in a separate condition outside of the condition to place your Entry order.

        In the code you shared, those two methods are being called within the same condition as the Entry order method.

        if ( << ENTRY CONDITIONS MET >>)
        {
        EnterLong(Convert.ToInt32(Order_Qty), "");
        TradeCheck = true; // Flag that a position has just been taken. This enables execution of one-time logic till position is exited.
        //============= NOT WORKING IN REALTIME. BRACKETS ARE STILL ENTERED ON STRAEGY CLCULATION INTERVAL (BAR CLOSE OR PRICE CHANGE)
        ATRPrice = (ATR1[0] * ATRMultiplier);
        if ((ATRPrice > PreferredPositionLoss) && (boolOverrideATRMultiplier == true)) // if ATR based stop greater than user provided stop loss amount & elected to override ATRMultiplier
        {
        ATRPrice = PreferredPositionLoss;
        }
        ATRStop = (Position.AveragePrice - ATRPrice);
        ExitLongStopMarket(Convert.ToInt32(Order_Qty), ATRStop); //THIS SHOULD BE CALLED IN A SEPARATE CONDITION OUTSIDE THE CONDITION TO PLACE THE ENTRY
        ExitLongLimit(Position.AveragePrice + PreferredPositionTarget); //THIS SHOULD BE CALLED IN A SEPARATE CONDITION OUTSIDE THE CONDITION TO PLACE THE ENTRY

        }

        That said, this is the expected behavior when comparing Playback to running the strategy on realtime data.

        When running a strategy that places Exit order methods and is set to Calculate.OnBarClose, it is expected that the strategy will place the Exit order on the next bar close if the condition to do so becomes true.

        When using a strategy on Playback with Market Replay data and the strategy places Exit methods for stop/target orders and is set to Calculate.OnBarClose, Exit order methods are placed on the same bar the Entry order is placed.

        If you would like I could submit a feature request to the Development team for you for the ability to have strategies place Exit orders on the next bar when using Playback so it more closely resembles the strategy running on realtime data.

        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Originally posted by NinjaTrader_BrandonH View Post
          If you would like I could submit a feature request to the Development team for you for the ability to have strategies place Exit orders on the next bar when using Playback so it more closely resembles the strategy running on realtime data.
          Yes that would be great - please do - thanks. Its a needed feature as in my opinion playback connection (or simulated feed) based validation of strategy scenarios should be identical to how they would perform in Realtime esp. for NT Brokerage (it may vary for other brokers IMO)

          Comment


            #6
            Hello WattMan,

            Thanks for your notes.

            I have submitted your feature request to the Development team. This request is being tracked under the number SFT-6050.

            As with all feature requests, interest is tracked before implementation is considered, so we cannot offer an ETA or promise of fulfillment. If implemented, it will be noted on the Release Notes page of the Help Guide.

            Release Notes — https://ninjatrader.com/support/help...ease_notes.htm
            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Yesterday, 05:17 AM
            0 responses
            71 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            143 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            76 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            47 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            51 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X