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 NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    55 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    132 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