I have some issues with the strategy below. The current thing i am trying to work out is where it enters. The strategy is a 5 min ORB that tracks the orb then signals on a close outside the ORB using that candle with the first close outside candle as the range for the entries. Entering a long on the break of the high and a short with a break of the low. It should enter at the H/L of the signal candle but its seems to be entering at the 5 min close. I believe I have it configured to evaluate by the tick so im not sure why its only entering on the close.
#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
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;
#endregion
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
public class FiveBreak : Strategy
{
private DateTime lastExitSessionTime = Core.Globals.MinDate;
private TimeSpan tradingStartTime = new TimeSpan(7, 30, 0); // 9:30 AM
private TimeSpan tradingEndTime = new TimeSpan(15, 59, 0);
private TimeSpan tradingStartEndTime = new TimeSpan(8, 30, 0);
private TimeSpan endTradingTime = new TimeSpan(14, 0, 0); // 9:30 AM
private TimeSpan orbStartTime = new TimeSpan(7, 30, 0); // 9:30 AM
private TimeSpan orbEndTime = new TimeSpan(7, 35, 0);
private double orbRangeHigh;
private double orbRangeLow;
private double orbRangeMid;
private bool ordersPlaced = false;
private bool lastBarWasSignal = false;
private bool hasTraded = false;
private double longOrderPrice = 0;
private double shortOrderPrice = 0;
private double signalBarHigh = 0;
private double signalBarLow = 0;
private string longOrder = "LongBreakout";
private string shortOrder = "ShortBreakdown";
private int signalBarNum = 0;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "FiveBreak";
Calculate = Calculate.OnEachTick;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.UniqueEntries;
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;
IsTickReplay = true; // This must also be enabled on the chart or strategy analyzer
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < 25 )
{
return;
}
// Only act on the last bar of the session AND only once
if (Bars.IsLastBarOfSession && Times[0][0].Date > lastExitSessionTime.Date)
{
if (Position.MarketPosition != MarketPosition.Flat)
{
ExitLong(); // Will only affect open longs
ExitShort(); // Will only affect open shorts
}
// Mark this session as already processed
lastExitSessionTime = Times[0][0];
}
// Get ORB H/Ls
if ( Time[0].TimeOfDay >= orbStartTime && Time[0].TimeOfDay <= orbEndTime)
{
if (Time[0].TimeOfDay == orbStartTime)
{
orbRangeHigh = High[0];
orbRangeLow = Low[0];
//fiveHasClosedAbove = false;
//fiveHasClosedBelow = false;
ordersPlaced = false;
hasTraded = false;
}
if (High[0] > orbRangeHigh)
{
orbRangeHigh = High[0];
}
if (Low[0] < orbRangeLow)
{
orbRangeLow = Low[0];
}
}
// Print lines For ORB
if ( Time[0].TimeOfDay >= orbStartTime)
{
orbRangeMid = ((orbRangeHigh - orbRangeLow) / 2) + orbRangeLow;
Draw.Line(this, "ORBHigh" + CurrentBar, false, 1, orbRangeHigh, 0, orbRangeHigh, Brushes.Green, DashStyleHelper.Solid, 2);
Draw.Line(this, "ORBLow" + CurrentBar, false, 1, orbRangeLow, 0, orbRangeLow, Brushes.Red, DashStyleHelper.Solid, 2);
Draw.Line(this, "ORBMid" + CurrentBar, false, 1, orbRangeMid, 0, orbRangeMid, Brushes.Yellow, DashStyleHelper.Solid, 2);
}
// Look for close outside ORB
if (Time[0].TimeOfDay >= orbEndTime)
{
// Break High of range
if (IsFirstTickOfBar && Close[1] > orbRangeHigh && Close[2] < orbRangeHigh && !lastBarWasSignal)
{
Draw.ArrowUp(this, "UpperBreak " + CurrentBar + Time, false, 1, Low[1] - (TickSize * 10), Brushes.Green);
signalBarNum = CurrentBar;
signalBarHigh = High[1];
signalBarLow = Low[1];
lastBarWasSignal = true;
}
if (IsFirstTickOfBar && Close[1] < orbRangeLow && Close[2] > orbRangeLow && !lastBarWasSignal)
{
Draw.ArrowDown(this, "UpperBreak " + CurrentBar + Time, false, 1, High[1] + (TickSize * 10), Brushes.Green);
signalBarNum = CurrentBar;
signalBarHigh = High[1];
signalBarLow = Low[1];
lastBarWasSignal = true;
}
if (BarsInProgress != 0)
return;
//CurrentBar - signalBarNum > 0
if (Time[0].TimeOfDay >= orbEndTime && Position.MarketPosition == MarketPosition.Flat && lastBarWasSignal ) // && CurrentBar - signalBarNum > 0 && CurrentBar - signalBarNum < 2)
{
// Test for close outside ORB
if (Close[0] > signalBarHigh )
{
Draw.Diamond(this, "EnterLongHere" + CurrentBar + Time, false, 1, Low[0] - TickSize * 10, Brushes.Green);
SetUpLongOrders(1, 1, signalBarHigh, signalBarLow);
lastBarWasSignal = false;
}
if (Close[0] < signalBarLow )
{
Draw.Diamond(this, "EnterShortHere" + CurrentBar + Time, false, 0, High[0] + TickSize * 10, Brushes.Red);
SetUpShortOrders(1, 1, signalBarHigh, signalBarLow);
lastBarWasSignal = false;
}
}
}
}
private void SetUpLongOrders(int orderMultiplier, int orderTypes, double highOrderPrice, double lowOrderPrice)
{
double longSL = lowOrderPrice;
double slRange = highOrderPrice - lowOrderPrice;
double targetMultiplier = 1;
double longTarget = (highOrderPrice + (slRange * targetMultiplier));
EnterLong(1, longOrder);
SetStopLoss(longOrder, CalculationMode.Price, shortOrderPrice, false);
SetProfitTarget(longOrder, CalculationMode.Price, longTarget);
ordersPlaced = true;
lastBarWasSignal = false;
}
private void SetUpShortOrders(int orderMultiplier, int orderTypes, double highOrderPrice, double lowOrderPrice)
{
double slRange = highOrderPrice - lowOrderPrice;
double targetMultiplier = 1;
double shortTarget = (lowOrderPrice - (slRange * targetMultiplier));
EnterShort( 1, shortOrder);
SetStopLoss(shortOrder, CalculationMode.Price, longOrderPrice, false);
SetProfitTarget(shortOrder, CalculationMode.Price, shortTarget);
ordersPlaced = true;
lastBarWasSignal = false;
}
}
}