namespace NinjaTrader.NinjaScript.Strategies
{
public class OpeningRangeUnlocked : Strategy
{
private HighLowByTimeRange HighLowByTimeRange1;
private Stochastics Stochastics1;
private int priorTradesCount = 0;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"";
Name = "OpeningRangeUnlocked";
Calculate = Calculate.OnEachTick;
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)
{
SetProfitTarget(@"LongMarket", CalculationMode.Ticks, 8);
SetStopLoss(@"LongMarket", CalculationMode.Ticks, 8, false);
SetProfitTarget(@"ShortMarket", CalculationMode.Ticks, 8);
SetStopLoss(@"ShortMarket", CalculationMode.Ticks, 8, false);
HighLowByTimeRange1 = HighLowByTimeRange(8, 30, 8, 50);
HighLowByTimeRange1.Plots[0].Brush = new SolidColorBrush(Colors.Chartreuse);
HighLowByTimeRange1.Plots[1].Brush = new SolidColorBrush(Colors.Red);
AddChartIndicator(HighLowByTimeRange1);
Stochastics1 = Stochastics(7, 14, 3);
Stochastics1.Plots[0].Brush = new SolidColorBrush(Colors.IndianRed);
Stochastics1.Plots[1].Brush = new SolidColorBrush(Colors.LimeGreen);
AddChartIndicator(Stochastics1);
}
}
protected override void OnBarUpdate()
{
// Make sure there are enough bars.
if (CurrentBars[0] < 1) return;
if (CurrentBar < BarsRequiredToTrade) return;
// At the start of a new session
if (Bars.IsFirstBarOfSession)
{
// Store the strategy's prior cumulated realized profit and number of trades
priorTradesCount = SystemPerformance.AllTrades.Count;
/* NOTE: Using .AllTrades will include both historical virtual trades as well as real-time trades.
If you want to only count profits from real-time trades please use .RealtimeTrades. */
}
/* Prevents further trading if the current session's realized profit exceeds $1000 or if realized losses exceed $400.
Also prevent trading if 10 trades have already been made in this session. */
if (SystemPerformance.AllTrades.Count - priorTradesCount > 0)
{
/* TIP FOR EXPERIENCED CODERS: This only prevents trade logic in the context of the OnBarUpdate() method. If you are utilizing
other methods like OnOrderUpdate() or OnMarketData() you will need to insert this code segment there as well. */
// Returns out of the OnBarUpdate() method. This prevents any further evaluation of trade logic in the OnBarUpdate() method.
return;
}
// Set 1 - Long Entry
if ((Time[0].TimeOfDay > new TimeSpan(8, 50, 01))
&& (Time[0].TimeOfDay < new TimeSpan(10, 30, 00))
&& ((High[0] + (2 * TickSize)) >= HighLowByTimeRange1.HighestHigh[0])
&& (Stochastics1.K[0] > Stochastics1.D[0]))
{EnterLong(Convert.ToInt32(DefaultQuantity), @"LongMarket");}
// Set 2 - Short Entry
if ((Time[0].TimeOfDay > new TimeSpan(8, 50, 01))
&& (Time[0].TimeOfDay < new TimeSpan(10, 30, 00))
&& ((Low[0] + (2 * TickSize)) <= HighLowByTimeRange1.LowestLow[0])
&&(Stochastics1.K[0] < Stochastics1.D[0]))
{EnterShort(Convert.ToInt32(DefaultQuantity), @"ShortMarket");}
}
}
}
Comment