This is an opening range strategy, so there's a condition met nearly every day. I'm sure I did something wrong, but I can't tell what. I need a fresh set of eyes.
The code is supposed to look at the first 30 minutes of market open, and establish a range. After the first 30min, if the range is broken it takes a trade with a target of 1/2 the range and a stop loss of 1/2 the range. It's designed to take either one long or one short trade each day, and it resets at 2PM.
Here's the code, can anyone help me spot what I've missed?
region Using declarations
using System.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
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;
using NinjaTrader.NinjaScript.Strategies;
#endregion
class CrudeOilOpeningRangeBreakoutStrategy : Strategy
{
private DateTime lastTradeTime = DateTime.MinValue;
private double stopLoss;
private double profitTarget;
private int Quantity = 1;
private bool longPositionOpened = false;
private bool shortPositionOpened = false;
private double openingRangeHigh;
private double openingRangeLow;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = "Crude Oil Opening Range Breakout Strategy";
Name = "CrudeOilOpeningRangeBreakoutStrategy";
Calculate = Calculate.OnEachTick;
}
else if (State == State.Realtime)
{
}
base.OnStateChange();
}
protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
{
// Check if it's outside the allowed trading time
if (Time[0].Hour < 8 || Time[0].Hour >= 14) return;
if (Time[0].Hour == 8 && Time[0].Minute < 30) return;
if (Time[0].Hour == 14 && Time[0].Minute >= 0) return;
// Set opening range high and low
if (Time[0].Hour == 8 && Time[0].Minute == 30)
{
double highValue = High[1];
double lowValue = Low[1];
openingRangeHigh = Math.Max(highValue, openingRangeHigh);
openingRangeLow = Math.Min(lowValue, openingRangeLow);
stopLoss = openingRangeLow - (openingRangeHigh - openingRangeLow) * 0.5;
profitTarget = openingRangeHigh + (openingRangeHigh - openingRangeLow) * 0.5;
}
// Reset trade flags and range at 2PM
if (Time[0].Hour == 14)
{
longPositionOpened = false;
shortPositionOpened = false;
openingRangeHigh = 0;
openingRangeLow = 0;
stopLoss = 0;
profitTarget = 0;
}
// Place a long trade if price rises above the opening range high and no long trade has been placed
if (Close[0] > openingRangeHigh && !longPositionOpened)
{
longPositionOpened = true;
EnterLong(Quantity, "Long");
lastTradeTime = Time[0];
}
// Place a short trade if price falls below the opening range low and no short trade has been placed
if (Close[0] < openingRangeLow && !shortPositionOpened)
{
shortPositionOpened = true;
EnterShort(Quantity, "Short");
lastTradeTime = Time[0];
}
}}
Comment