I have been working on this strategy more and have run into some more roadblocks. The idea of this strategy is that whenever a order is filled and travels back through the stoploss, the long/short position will be closed due to the stop loss, but another limit order will fill its place going the other direction (long or short). Here is the code I have been trying. I have tried changing all the defaults to see if I can get anything to work. Attached also is a screenshot of the chart when I run the script. At the stop loss at 4079.00 I would expect to also see a 'Short' at the exact same price. At the stoploss at 4081.00 I would also expect to see a 'long' at the exact same price. Hopefully this is making sense. I just dont understand why the stoploss/takeprofit cant execute at the exact time the new 'long' or 'short' order executes.
Thank you so much!
namespace NinjaTrader.NinjaScript.Strategies
{
public class TEST10 : Strategy
{
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "TEST10";
Calculate = Calculate.OnEachTick;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = false;
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;
KPLa = 1;
KPLb = 1;
KPLc = 1;
Buf = 1;
}
else if (State == State.Configure)
{
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 1)
return;
// Set 1
if (CrossAbove(Close, KPLb, 1))
{
EnterLongStopMarket(0, true, 1, KPLb + Buf, "Long");
SetStopLoss("Long", CalculationMode.Price, KPLb - Buf, true);
SetProfitTarget("Long", CalculationMode.Price, KPLc - Buf, true);
}
else if (CrossBelow(Close, KPLb, 1))
{
EnterShortStopMarket(0, true, 1, KPLb - Buf, "Short");
SetStopLoss("Short", CalculationMode.Price, KPLb + Buf, true);
SetProfitTarget("Short", CalculationMode.Price, KPLa + Buf, true);
}
}
#region Properties
[NinjaScriptProperty]
[Range(1, double.MaxValue)]
[Display(Name="KPLa", Order=1, GroupName="Parameters")]
public double KPLa
{ get; set; }
[NinjaScriptProperty]
[Range(1, double.MaxValue)]
[Display(Name="KPLb", Order=2, GroupName="Parameters")]
public double KPLb
{ get; set; }
[NinjaScriptProperty]
[Range(1, double.MaxValue)]
[Display(Name="KPLc", Order=3, GroupName="Parameters")]
public double KPLc
{ get; set; }
[NinjaScriptProperty]
[Range(1, double.MaxValue)]
[Display(Name="Buf", Order=4, GroupName="Parameters")]
public double Buf
{ get; set; }
#endregion
}
}
Comment