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

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 judysamnt7, 03-13-2023, 09:11 AM
    4 responses
    59 views
    0 likes
    Last Post DynamicTest  
    Started by ScottWalsh, Today, 06:52 PM
    4 responses
    36 views
    0 likes
    Last Post ScottWalsh  
    Started by olisav57, Today, 07:39 PM
    0 responses
    7 views
    0 likes
    Last Post olisav57  
    Started by trilliantrader, Today, 03:01 PM
    2 responses
    21 views
    0 likes
    Last Post helpwanted  
    Started by cre8able, Today, 07:24 PM
    0 responses
    10 views
    0 likes
    Last Post cre8able  
    Working...
    X