I am writing my strategy and have run into some struggles. I searched the forums and NT help a lot, but all I can find is code for Bar high/Low but I am trying to script Bar Open to Close (size of the Candle/bar excluding the high/low). I have the following script but it does not seem to work when I back test it.
I am trying to check that the current bar (last closed) is 4x bigger than the previous 2 bars and 2x bigger than the last 15 bars. The bar needs to open above/below the SMA's then it can go long/short. The SMA's should be 24. And 1 SMA on the 15 Minute chart, the other on the 5 Minute chart. The trade will be taken on the 2 Minute chart.
Any help would be appreciated. Thank you in advanced
#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 MyCustomStrategy : Strategy
{
private SMA SMA1;
private SMA SMA2;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "MyCustomStrategy";
Calculate = Calculate.OnBarClose;
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;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
}
else if (State == State.Configure)
{
AddDataSeries("MES 03-23", Data.BarsPeriodType.Minute, 15, Data.MarketDataType.Last);
AddDataSeries(Data.BarsPeriodType.Minute, 5);
AddDataSeries(Data.BarsPeriodType.Minute, 2);
}
else if (State == State.DataLoaded)
{
SMA1 = SMA(Closes[1], 24);
SMA2 = SMA(Closes[2], 24);
SetProfitTarget(Convert.ToString((Close[0] * 1.25) ), CalculationMode.Currency, 0);
SetStopLoss(Convert.ToString(Open[0]), CalculationMode.Currency, 0, false);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 1
|| CurrentBars[1] < 0
|| CurrentBars[2] < 0
|| CurrentBars[3] < 15)
return;
// Set 1
if ((Open[1] > SMA2[1])
&& (Open[1] > SMA1[0])
&& ((Open[1] - Close[1]) > ((Open[16] - Close[16]) * 2))
&& ((Open[1] - Close[1]) > ((Open[3] - Close[3]) * 4)))
{
EnterLong(1, @"long");
}
// Set 2
if ((Open[1] < SMA2[1])
&& (Open[1] < SMA1[0])
&& ((Open[1] - Close[1]) > ((Open[16] - Close[16]) * 2))
&& ((Open[1] - Close[1]) > ((Open[3] - Close[3]) * 4)))
{
EnterShort(1, @"short");
}
}
}
}

Comment