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]);
}
}
Comment