New to coding completely. I have written something that plots the full gap for me in CME futures RTH from the close of the last 30 minute candle to the open. I'm working to replicate a very basic strategy that takes entry at open(in the direction of the gap) when the gap is under 25pts and targets half of the said gap. This is all for practice, but I cannot get the correct definitions for my profit target to be and ABS value of the gap size. It just ignores my profit target at the moment and only exits on close or at a stop loss. I have tried multiple different ways to write this code and I've done a bunch of self exploration to no avail. Can someone please help me understand where my code is wrong? I have included the code below which shows the two ways I have tried to calculate this gap as my profit target.
Thanks.
namespace NinjaTrader.NinjaScript.Strategies
{
public class TryNumber2 : Strategy
{
private bool StopTrading = false;
private bool doitonce = true;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "TryNumber2";
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)
{
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
StopTrading = false;
// Make sure not in a position
if (Position.MarketPosition == MarketPosition.Flat && StopTrading == false)
{
// Check if gap up
if (PriorDayOHLC().PriorOpen[0] < Open[0])
{
EnterShort(2);
if (Position.MarketPosition == MarketPosition.Short && doitonce)
{
// Set Profit Target at half the gap size
SetProfitTarget(CalculationMode.Price, Math.Abs(PriorDayOHLC().PriorClose[0] - Open.GetValueAt(0) / 2));
// Set Trail stop at $250
SetStopLoss(CalculationMode.Ticks,40);
StopTrading = true;
}
}
// Check if gap down
if (PriorDayOHLC().PriorClose[0] > Open[0])
{
EnterLong(2);
// Set Profit Target at half the gap size
double gapSize = Open[0] - PriorDayOHLC().PriorClose[0];
double profitTarget = gapSize / 2;
SetProfitTarget(CalculationMode.Price, profitTarget);
// Set Trail stop at $250
SetStopLoss(CalculationMode.Ticks, 40);
StopTrading = true;
}
}
Draw.Line(this, "Gap"+CurrentBar, false, Bars.BarsSinceNewTradingDay, PriorDayOHLC().PriorClose[0] , 0, PriorDayOHLC().PriorClose[0], Brushes.Green, DashStyleHelper.Solid, 3);
}
}
}
Comment