Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.
    Paul H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by chartchart, 05-19-2021, 04:14 PM
    3 responses
    577 views
    1 like
    Last Post NinjaTrader_Gaby  
    Started by bsbisme, Yesterday, 02:08 PM
    1 response
    15 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by prdecast, Today, 06:07 AM
    0 responses
    3 views
    0 likes
    Last Post prdecast  
    Started by i019945nj, 12-14-2023, 06:41 AM
    3 responses
    60 views
    0 likes
    Last Post i019945nj  
    Started by TraderBCL, Today, 04:38 AM
    2 responses
    18 views
    0 likes
    Last Post TraderBCL  
    Working...
    X