I am trying to code a strategy after doing the most with the Builder.
I want to set a Stop Loss on the Highest High of 4 bars before the entry.
The entry is good, but on the Stop calculation, I calculate:
1. The Bar Position with the Highest High in the last 4 bars <HighestBarPosition = HighestBar(High, 4>
2. The High Price of that bar <MyStopPrice = High[HighestBarPosition>
3. I put that Price in the Stop Set <SetStopLoss(@"", CalculationMode.Price, MyStopPrice, false)>.
It looks like the Stop is Located the number of ticks away (In the direction of the trade, not even in the direction against the trade) from the entry place that is Calculated by the HighestBarPosition but not in the pricing calculated.
Any idea?
Thanks in advance.
namespace NinjaTrader.NinjaScript.Strategies
{
public class Keltner : Strategy
{
private double MyExitPrice;
private double MyStopPrice;
private int HighestBarPosition;
private KeltnerChannel KeltnerChannel1;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "Keltner";
Calculate = Calculate.OnBarClose;
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;
KeltnerRatio = 2.5;
KeltnerPeriod = 20;
MyExitPrice = 1;
MyStopPrice = 1;
HighestBarPosition = 1;
}
else if (State == State.Configure)
{
SetStopLoss(@"", CalculationMode.Price, MyStopPrice, false);
SetProfitTarget("", CalculationMode.Ticks, 20);
}
else if (State == State.DataLoaded)
{
KeltnerChannel1 = KeltnerChannel(Close, KeltnerRatio, Convert.ToInt32(KeltnerPeriod));
KeltnerChannel1.Plots[0].Brush = Brushes.DarkGray;
KeltnerChannel1.Plots[1].Brush = Brushes.Red;
KeltnerChannel1.Plots[2].Brush = Brushes.ForestGreen;
AddChartIndicator(KeltnerChannel1);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 3)
return;
// Set 1
if ((Close[0] < (Close[1] + (-3 * TickSize)) )
&& (Close[1] > Close[2])
&& (Close[2] > Close[3])
&& (Close[3] > KeltnerChannel1.Upper[3]))
{
EnterShortLimit(Convert.ToInt32(DefaultQuantity), (Close[0] + (-1 * TickSize)) , "");
HighestBarPosition = HighestBar(High, 4);
MyStopPrice = High[HighestBarPosition];
Comment