I would like to enter only once but as soon as I initialize it, the “OkToTrade” variable is already set to “false” when it should be equal to “true” before entering the position for the first time.
I don't understand why, but thanks in advance for your answers

namespace NinjaTrader.NinjaScript.Strategies
{
public class MyCustomStrategy : Strategy
{
private double buyPrice;
private double sellPrice;
private bool OkToTrade = true;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = "";
Name = "My Strategy";
TakeProfit = 40;
StopLoss = 20;
Distance = 30;
AddPlot(Brushes.Green, "BuyPlot");
AddPlot(Brushes.Red, "SellPlot");
}
else if (State == State.Configure)
{
SetProfitTarget(CalculationMode.Ticks, TakeProfit);
SetStopLoss(CalculationMode.Ticks, StopLoss);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < BarsRequiredToTrade)
return;
BuyPlot[0] = buyPrice;
SellPlot[0] = sellPrice;
double currentAsk = GetCurrentAsk();
// Update of entry prices
if (ToTime(Time[0]) <= 93000)
{
buyPrice = GetCurrentBid() + TickSize * Distance;
sellPrice = GetCurrentAsk() - TickSize * Distance;
}
// Entry
if (OkToTrade)
{
if (currentAsk >= buyPrice)
{
EnterLong(1);
OkToTrade = false;
}
else if (currentAsk <= sellPrice)
{
EnterShort(1);
OkToTrade = false;
}
}
}
#region Properties
[Browsable(false)]
[XmlIgnore]
public Series<double> BuyPlot
{
get { return Values[0]; }
}
[Browsable(false)]
[XmlIgnore]
public Series<double> SellPlot
{
get { return Values[1]; }
}
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(ResourceType = typeof(Custom.Resource), Name = "Take Profit", GroupName = "NinjaScriptStrategyParameters", Order = 0)]
public int TakeProfit
{ get; set; }
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(ResourceType = typeof(Custom.Resource), Name = "Stop Loss", GroupName = "NinjaScriptStrategyParameters", Order = 1)]
public int StopLoss
{ get; set; }
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(ResourceType = typeof(Custom.Resource), Name = "Distance", GroupName = "NinjaScriptStrategyParameters", Order = 2)]
public int Distance
{ get; set; }
#endregion
}
}

Comment