protected override void OnBarUpdate()
{
// Only run on real-time data
if (Historical)
return;
}
per this official guidance: https://ninjatrader.com/support/help...cal.htm
I get an error code of CS0103 "The name 'Historical' does not exist in the current context" when trying to compile the code.
My entire code is this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Gui.Tools;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.Indicators;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class SampleMACrossoverWithATRstopsV3 : Strategy
{
private SMA smaFast;
private SMA smaSlow;
private ATR atr;
private double atrStopLong;
private double atrStopShort;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = "SampleMACrossoverWithATRstopsV3";
Name = "SampleMACrossoverWithATRstopsV3";
Calculate = Calculate.OnPriceChange;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
IsInstantiatedOnEachOptimizationIteration = true;
Fast = 9;
Slow = 21;
AtrPeriod = 14;
AtrMultiplier = 2;
DrawIndicators = false;
}
else if (State == State.DataLoaded)
{
smaFast = SMA(Fast);
smaSlow = SMA(Slow);
atr = ATR(Close, Convert.ToInt32(AtrPeriod));
if (DrawIndicators)
{
smaFast.Plots[0].Brush = Brushes.Snow;
smaSlow.Plots[0].Brush = Brushes.Yellow;
atr.Plots[0].Brush = Brushes.DarkCyan;
AddChartIndicator(smaFast);
AddChartIndicator(smaSlow);
AddChartIndicator(atr);
}
}
}
protected override void OnBarUpdate()
{
// Only run on real-time data
if (Historical)
return;
if (BarsInProgress != 0 || CurrentBars[0] < Slow) return;
double atrValue = atr[0] * AtrMultiplier;
if (Position.MarketPosition == MarketPosition.Flat)
{
if (CrossAbove(smaFast, smaSlow, 1))
{
EnterLong(Convert.ToInt32(DefaultQuantity), "Long");
atrStopLong = Close[0] - atrValue;
}
else if (CrossBelow(smaFast, smaSlow, 1))
{
EnterShort(Convert.ToInt32(DefaultQuantity), "Short");
atrStopShort = Close[0] + atrValue;
}
}
if (Position.MarketPosition == MarketPosition.Long)
{
double newStop = Close[0] - atrValue;
if (newStop > atrStopLong)
{
atrStopLong = newStop;
ExitLongStopMarket(0, true, Position.Quantity, atrStopLong, "ExitLong", "Long");
}
}
if (Position.MarketPosition == MarketPosition.Short)
{
double newStop = Close[0] + atrValue;
if (newStop < atrStopShort)
{
atrStopShort = newStop;
ExitShortStopMarket(0, true, Position.Quantity, atrStopShort, "ExitShort", "Short");
}
}
}
region Properties
[Range(1, int.MaxValue)]
[NinjaScriptProperty]
public int Fast { get; set; }
[Range(1, int.MaxValue)]
[NinjaScriptProperty]
public int Slow { get; set; }
[Range(1, int.MaxValue)]
[NinjaScriptProperty]
public int AtrPeriod { get; set; }
[Range(0.1, double.MaxValue)]
[NinjaScriptProperty]
public double AtrMultiplier { get; set; }
[NinjaScriptProperty]
public bool DrawIndicators { get; set; }
#endregion
}
}
Comment