Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Entries limitation OnPriceChahge

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

    Entries limitation OnPriceChahge

    Hello.
    Dear colleagues, when creating a strategy, an error occurred that I cannot solve on my own.
    I was looking for solutions and read many topics on this forum, but, unfortunately, I could not find an answer.

    I understand that during Onbarupdate() and calculating OnPriceChahge strategy is executed every time the prices change, but I cannot figure out how to correctly limit the execution to only one entry.

    If I run the strategy code without a stop-loss, it works fine, but in the ninjascript out-put appear multiple values ​​on certain bars. Why exactly on these bars and how to fix this problem, I cannot understand. Visually, the problem appears when you add a stop loss (see screenshots).

    I need to enter the entry in one direction only once (Long or Short).

    Please tell me how can I fix this error?

    Thanks in advance for your help.

    Strategy code:
    protected override void OnBarUpdate ()
    {
    if (((((indicator1 [0]> indicator1 [1]) && (indicator2 [0]> = 30)) == false)) //
    && (((indicator1 [1]> indicator1 [2]) && (indicator2 [1]> = 30)) == true)) //
    {EnterLong (1);
    SetStopLoss (CalculationMode.Currency, 50);
    Print("Time " + Time[0] + " LongEntered: " + Close[0]);
    }

    if (((((indicator1 [0] <= indicator1 [1]) && (indicator2 [0] <30)) == false))
    && (((indicator1 [1] <= indicator1 [2]) && (indicator2 [1] <30)) == true))
    {EnterShort (1);
    SetStopLoss (CalculationMode.Currency, 50);
    Print("Time " + Time[0] + " ShortEntered: " + Close[0]);
    }
    }
    Click image for larger version  Name:	Screenshot4.jpg Views:	0 Size:	99.8 KB ID:	1147546Click image for larger version  Name:	Screenshot3.jpg Views:	0 Size:	161.6 KB ID:	1147547
    Last edited by readerror; 03-20-2021, 07:05 AM.

    #2
    Hello readerror,

    Use a bool to know the order was submitted and prevent further submissions.

    Or assign the order to a variable in OnOrderUpdate and require the order variable to be null to allow a new order.


    As a heads up, Set methods like SetStopLoss, should be called before the entry is called, as these cannot be unset.
    "Should you call this method to dynamically change the stop loss price in the strategy OnBarUpdate() method, you should always reset the stop loss price / offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your stop loss order on your next open position"
    https://ninjatrader.com/support/help...etstoploss.htm
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Mindset, 04-21-2026, 06:46 AM
    0 responses
    88 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    135 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    68 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    119 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    69 views
    0 likes
    Last Post PaulMohn  
    Working...
    X