• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by traderpards, 12-12-2018, 01:15 PM
    5 responses
    152 views
    0 likes
    Last Post BrianL
    by BrianL
     
    Started by Cyanez, Today, 08:41 AM
    1 response
    7 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by TonyParisi, Today, 07:38 AM
    1 response
    8 views
    0 likes
    Last Post NinjaTrader_Kate  
    Started by tgn55, 09-24-2019, 06:34 AM
    13 responses
    49 views
    0 likes
    Last Post NinjaTrader_Jim  
    Started by jasphere, Today, 07:37 AM
    4 responses
    20 views
    0 likes
    Last Post jasphere  
    Working...
    X