73 lines
2.5 KiB
C#
73 lines
2.5 KiB
C#
|
#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)
|
||
|
{
|
||
|
AddDataSeries(BarsPeriodType.Minute, 1);
|
||
|
AddDataSeries(Instrument.FullName, new BarsPeriod
|
||
|
{ BarsPeriodType = BarsPeriodType.Minute, Value = 5 }, "US Equities RTH");
|
||
|
}
|
||
|
else if (State == State.DataLoaded)
|
||
|
{
|
||
|
OR = OpeningRange(30, OpeningRangeBarType.Minutes);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected override void OnBarUpdate()
|
||
|
{
|
||
|
// TODO: Make this timezone-agnostic.
|
||
|
if (ToTime(Time[0]) <= 70000)
|
||
|
return;
|
||
|
|
||
|
if (PrimaryBars != BarsInProgress)
|
||
|
return;
|
||
|
|
||
|
if (Open[0] < OR.ORH[0] && Close[0] > OR.ORH[0])
|
||
|
{
|
||
|
SetStopLoss(CalculationMode.Price, Low[0]);
|
||
|
EnterLong();
|
||
|
}
|
||
|
else if (Open[0] > OR.ORL[0] && Close[0] < OR.ORL[0])
|
||
|
{
|
||
|
SetStopLoss(CalculationMode.Price, High[0]);
|
||
|
EnterShort();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|