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 NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    56 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    133 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    73 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    45 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