Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Does EnterLimitLong() need to be called on every bar?

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

    Does EnterLimitLong() need to be called on every bar?

    I am just figuring out how this Ninjatrader System works for scripts.

    I have a simple OnUpdate() function that does the following

    if (BarsInProgress == 0) {
    if (IsFirstTickOfBar && Open[0] < EMA1[0] && Close[0] > EMA1[0])
    {
    EnterLong(2)
    ExitLongStopMarket(Close[0] - 10);
    }
    }

    I also have the same function with one more condition.

    if (BarsInProgress == 0) {
    if (Position.MarketPosition == MarketPosition.Flat) {
    if (IsFirstTickOfBar && Open[0] < EMA1[0] && Close[0] > EMA1[0])
    {
    EnterLong(2)
    ExitLongStopMarket(Close[0] - 10);
    }
    }
    }

    If I run the first one, the Stop is recalculated on each bar so it basically stops out if the price goes below the previous bar's close - 10

    If I run the second one... my stop never hits. Is this because EnterLongStopMarket() needs to be set on each bar update? I thought if I call this function once, the stop order should exist forever until
    1. it is hit
    2. I close my long position
    3. I go short so the long stop should be canceled because I exited all longs.

    How do I call ExitLongStopMarket() just once and have the stop order exist for more than just one bar?
    Last edited by stokhastic; 11-15-2018, 08:24 PM.

    #2
    Hello stokhastic,

    Thanks for your post.

    As a general reference, when a strategy/indicator is not performing as expected, please check the "log" tab of the control center for any error messages related to the script.

    The differences between the two blocks of code are that the second would prevent any updates to the exit because you are checking for a "Flat" market position which would not be true when the market entry order is filled. You could re-write to update on each bar this like:

    if (Position.MarketPosition == MarketPosition.Long)
    {
    ExitLongStopMarket(Close[0] - 10);
    }


    By default, in the "Managed approach" an order that is not filled by the end of the bar is automatically canceled unless it is resubmitted (which is what your first code block would be more likely to do). Alternative you can set the exit once if you do not wish to change it and have it remain by using the advance order method overload: ExitLongStopMarket(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double stopPrice, string signalName, string fromEntrySignal) The key point is the bool isLiveUntilCancelled. Reference: https://ninjatrader.com/support/help...stopmarket.htm

    Please also review: https://ninjatrader.com/support/help...d_approach.htm for a comprehensive review.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    47 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    22 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    33 views
    1 like
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    50 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    42 views
    0 likes
    Last Post CarlTrading  
    Working...
    X