2024-05-11 13:03:52 +00:00
|
|
|
#region Using declarations
|
|
|
|
using NinjaTrader.Cbi;
|
|
|
|
using NinjaTrader.Data;
|
|
|
|
using NinjaTrader.NinjaScript.Indicators;
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
//This namespace holds Strategies in this folder and is required. Do not change it.
|
|
|
|
namespace NinjaTrader.NinjaScript.Strategies
|
|
|
|
{
|
|
|
|
public class OpeningRangeBot : Strategy
|
|
|
|
{
|
|
|
|
private const int PrimaryBars = 0;
|
|
|
|
|
|
|
|
private OpeningRange OR;
|
|
|
|
|
|
|
|
protected override void OnStateChange()
|
|
|
|
{
|
|
|
|
if (State == State.SetDefaults)
|
|
|
|
{
|
|
|
|
Description = @"Strategy based on opening range breakouts";
|
|
|
|
Name = "Opening Range Bot";
|
|
|
|
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;
|
|
|
|
IsInstantiatedOnEachOptimizationIteration = true;
|
|
|
|
}
|
|
|
|
else if (State == State.Configure)
|
|
|
|
{
|
2024-05-13 14:36:58 +00:00
|
|
|
// Data series required by the opening range indicator, must be loaded here.
|
2024-05-11 13:03:52 +00:00
|
|
|
AddDataSeries(BarsPeriodType.Minute, 1);
|
2024-05-13 14:36:58 +00:00
|
|
|
AddDataSeries(Instrument.FullName, BarsPeriod, "US Equities RTH");
|
2024-05-11 13:03:52 +00:00
|
|
|
}
|
|
|
|
else if (State == State.DataLoaded)
|
|
|
|
{
|
|
|
|
OR = OpeningRange(30, OpeningRangeBarType.Minutes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnBarUpdate()
|
|
|
|
{
|
2024-05-14 12:44:35 +00:00
|
|
|
if (PrimaryBars != BarsInProgress)
|
|
|
|
return;
|
|
|
|
|
2024-05-11 13:03:52 +00:00
|
|
|
// TODO: Make this timezone-agnostic.
|
|
|
|
if (ToTime(Time[0]) <= 70000)
|
|
|
|
return;
|
|
|
|
|
2024-05-14 12:44:35 +00:00
|
|
|
if (Position.MarketPosition != MarketPosition.Flat)
|
2024-05-11 13:03:52 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (Open[0] < OR.ORH[0] && Close[0] > OR.ORH[0])
|
2024-05-14 12:44:35 +00:00
|
|
|
{
|
2024-05-11 13:03:52 +00:00
|
|
|
SetStopLoss(CalculationMode.Price, Low[0]);
|
|
|
|
EnterLong();
|
|
|
|
}
|
|
|
|
else if (Open[0] > OR.ORL[0] && Close[0] < OR.ORL[0])
|
|
|
|
{
|
|
|
|
SetStopLoss(CalculationMode.Price, High[0]);
|
|
|
|
EnterShort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|