I'm trying to make a gap strategy, where I wish to open a position if todays opening price is higher than yesterdays closing price. The problem is that I want to take a position as soon at the market opens if it opens higher. But I can only get it to take a position on the open of the second bar of the session. This also means that I can't test it on a daily timeframe, since there is no second bar in the session (I have the strategy set to exit on close of session).
This is my code, can you please tell what I'm doing wrong?
#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 Gaps : Strategy
{
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"How to trade gaps";
Name = "Gaps";
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;
MinimumGapUp = 40;
MinimumGapDown = 1;
MinimumGapUpPercent = 0;
MinimumGapDownPercent = 0;
MAshorttrend = 7;
MAmediumtrend = 1;
MAlongtrend = 1;
SLpoint = 1;
}
else if (State == State.Configure)
{
}
}
protected override void OnBarUpdate()
{
if(CurrentBar < MAshorttrend) return;
if(GapUp)
{
EnterShort();
SetProfitTarget(CalculationMode.Price, Close[1]);
SetStopLoss(CalculationMode.Price, (CurrentDayOHL().CurrentOpen[0]-PriorDayOHLC().PriorClose[0])/3+CurrentDayOHL().CurrentOpen[0]);
Draw.Dot(this, "short"+CurrentBar, true, 0, Low[0], Brushes.Yellow);
}
}
#region Helpers
bool GapUp { get {return (CurrentDayOHL().CurrentOpen[0] - PriorDayOHLC().PriorClose[0]) > MinimumGapUp;}}
#endregion
#region Properties
[NinjaScriptProperty]
[Range(0, double.MaxValue)]
[Display(ResourceType = typeof(Custom.Resource), Name="MinimumGapUp", Description="The minimum size of a gap up", Order=1, GroupName="NinjaScriptStrategyParameters")]
public double MinimumGapUp
{ get; set; }
[NinjaScriptProperty]
[Range(0, double.MaxValue)]
[Display(ResourceType = typeof(Custom.Resource), Name="MinimumGapDown", Description="The minimum size of a gap down", Order=2, GroupName="NinjaScriptStrategyParameters")]
public double MinimumGapDown
{ get; set; }
[NinjaScriptProperty]
[Range(0, double.MaxValue)]
[Display(ResourceType = typeof(Custom.Resource), Name="MinimumGapUpPercent", Description="The minimum gap up as a percent of price", Order=3, GroupName="NinjaScriptStrategyParameters")]
public double MinimumGapUpPercent
{ get; set; }
[NinjaScriptProperty]
[Range(0, double.MaxValue)]
[Display(ResourceType = typeof(Custom.Resource), Name="MinimumGapDownPercent", Description="The minimum gap up as a percent of price", Order=4, GroupName="NinjaScriptStrategyParameters")]
public double MinimumGapDownPercent
{ get; set; }
[NinjaScriptProperty]
[Range(0, int.MaxValue)]
[Display(ResourceType = typeof(Custom.Resource), Name="MAshorttrend", Description="The best MA to describe the short trend", Order=5, GroupName="NinjaScriptStrategyParameters")]
public int MAshorttrend
{ get; set; }
[NinjaScriptProperty]
[Range(0, int.MaxValue)]
[Display(ResourceType = typeof(Custom.Resource), Name="MAmediumtrend", Description="The best MA to describe the medium trend", Order=6, GroupName="NinjaScriptStrategyParameters")]
public int MAmediumtrend
{ get; set; }
[NinjaScriptProperty]
[Range(0, int.MaxValue)]
[Display(ResourceType = typeof(Custom.Resource), Name="MAlongtrend", Description="The best MA to describe the long trend", Order=7, GroupName="NinjaScriptStrategyParameters")]
public int MAlongtrend
{ get; set; }
[NinjaScriptProperty]
[Range(0, int.MaxValue)]
[Display(ResourceType = typeof(Custom.Resource), Name="SLpoint", Description="Number of points to StopLoss", Order=8, GroupName="NinjaScriptStrategyParameters")]
public int SLpoint
{ get; set; }
#endregion
}
}
Comment