Code as follows:
#region Using declarations
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
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
public class AutomatedHMAUnlocked : Strategy
{
private HMAFilterexample HMAFilterexample1;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "AutomatedHMAUnlocked";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.High;
OrderFillResolutionType = BarsPeriodType.Minute;
OrderFillResolutionValue = 1;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
int SlopeBars = 3;
int Quantity = 1;
int HMAPeriod = 50;
double HMAthreshold = 0.300;
bool PrintSlope = false;
IsInstantiatedOnEachOptimizationIteration = false;
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
HMAFilterexample = HMAFilterexample(Close, HMAPeriod, SlopeBars, HMAthreshold, false);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < Math.Max(HMAPeriod, SlopeBars))
{
return;
}
double HMAslope = Slope(HMAFilterexample1, SlopeBars, 0); // Calculate slope of HMA line
if (PrintSlope)
{
Print ("Time: "+Time[0].TimeOfDay+" Slope: "+HMAslope);
}
if (HMAslope > HMAthreshold)
{
PlotBrushes[0][0] = Brushes.Green;
EnterLong(Convert.ToInt32(Quantity), "");
}
else if (HMAslope < -HMAthreshold)
{
PlotBrushes[0][0] = Brushes.Red;
EnterShort(Convert.ToInt32(Quantity), "");
}
else
{
PlotBrushes[0][0] = Brushes.Gold;
}
}
}
}
Comment