ninjatrader/strategies/daily-reversal/DailyReversalBot.cs

71 lines
2.6 KiB
C#

#region Using declarations
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using System;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class DailyReversalBot : Strategy
{
private const int PrimaryBars = 0;
private const int MinuteBars = 1;
private DateTime sessionClose;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Based on an idea shared by u/Russ_CW on r/algotrading";
Name = "Daily Reversal 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 = 2;
IsInstantiatedOnEachOptimizationIteration = true;
}
else if (State == State.Configure)
{
AddDataSeries(BarsPeriodType.Minute, 1);
}
else if (State == State.DataLoaded)
{
SessionIterator chartSession = new SessionIterator(BarsArray[PrimaryBars]);
sessionClose = chartSession.GetTradingDayEndLocal(chartSession.ActualTradingDayExchange);
}
}
protected override void OnBarUpdate()
{
if (CurrentBars[PrimaryBars] < BarsRequiredToTrade || BarsInProgress != MinuteBars)
return;
// Using fixed position sizing to account for lower index values in the past.
int quantity = (int)Math.Floor(10000 / Close[0]);
if (Lows[PrimaryBars][0] < Lows[PrimaryBars][1] && Highs[PrimaryBars][0] < Highs[PrimaryBars][1])
{
if (Bars.IsFirstBarOfSession && Open[0] > Highs[PrimaryBars][0])
EnterLong(quantity);
else
EnterLongStopLimit(quantity, Highs[PrimaryBars][0], Highs[PrimaryBars][0]);
}
if (ToTime(Time[0]) == ToTime(sessionClose.AddMinutes(-1)))
ExitLong();
}
}
}