Error on calling 'OnBarUpdate' method for indicator 'Vswing' on bar 86: Object reference not set to an instance of an object.
Code as follow:
public class Vscraper : Strategy
{
#region Variables
private int target = 24;
private int stop = 12;
private bool be2 = false;
private bool be3 = false;
private int vswingStrength = 20;
#endregion
bool _initialized = false;
protected override void Initialize()
{
Add(Vscraper());
Add(Vswing(true, true, vswingStrength, Color.Navy, Color.Gold));
CalculateOnBarClose = true;
EntryHandling = EntryHandling.UniqueEntries;
EntriesPerDirection = 1;
TraceOrders = true;
}
private void GoLong()
{
SetStopLoss("target", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
SetProfitTarget("target", CalculationMode.Price, Close[0] + (Target*TickSize));
EnterLong("Target");
}
private void GoShort()
{
SetStopLoss("target", CalculationMode.Price, Close[0] + (Stop*TickSize), false);
SetProfitTarget("target", CalculationMode.Price, Close[0] - (Target*TickSize));
EnterShort("Target");
}
protected override void OnBarUpdate()
{
if(CurrentBar <= 30)
return;
if(!_initialized)
//Vswing vswing = Vswing(true, true, 20, Color.DarkCyan, Color.Magenta);
if (Position.MarketPosition != MarketPosition.Flat) return;
// Condition set 1
if (Vscraper().Signal[0] == 1
&& Vswing(true, true, vswingStrength, Color.Navy, Color.Gold).LowRay[0] - 4 * TickSize < Low[0]
&& Vswing(true, true, vswingStrength, Color.Navy, Color.Gold).LowRay[0] + 4 * TickSize > Low[0])
{
EnterLong();
}
// Condition set 2
if (Vscraper().Signal[0] == -1
&& Vswing(true, true, 20, Color.Navy, Color.Gold).HighRay[0] - 4 * TickSize < High[0]
&& Vswing(true, true, 20, Color.Navy, Color.Gold).HighRay[0] + 4 * TickSize > High[0])
{
EnterShort();
}
_initialized = true;
}

Comment