I can't seem to get a strategy to work if it has both a SetStopLoss and ExitLong. If I run Optimization on a strategy with say 200 different parameter sets over 12 years, they nearly all at some point stop taking entry signals, some after a year, some after several years. By adding a SetProfitTarget this greatly improves how many of the parameter sets actually work and take all signals. Even if I set the profit target so it never gets hit, i.e. 90%.
If you run the strategy below (and attached) with the parameters all set to "1" (Line1a, Line2a & Line2b), on instrument ES from 2005 to 2010, you'll see the strategy abruptly stops entering after 27 July 2007.
How do I fix this? Thanks
#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.MyStrategies
{
public class StopExitStrategy : Strategy
{
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "StopExitStrategy";
Calculate = Calculate.OnBarClose;
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;
Line1b = 1;
Line2a = 1;
Line2b = 1;
}
else if (State == State.Configure)
{
}
}
protected override void OnBarUpdate()
{
if (CurrentBars[0] < BarsRequiredToTrade)
return;
if (Close[0] < Open[Line1b] &&
Close[Line2a] < Open[Line2b + Line2a] &&
//(Position.MarketPosition == MarketPosition.Flat) &&
//(BarsSinceEntryExecution() == -1 || BarsSinceEntryExecution(0, "", 0) >= 0) &&
(BarsSinceExitExecution() == -1 || BarsSinceExitExecution() >= 0))
{
Draw.ArrowUp(this, CurrentBar.ToString(), true, 0, Low[0] - TickSize * 100, Brushes.Blue);
SetProfitTarget(CalculationMode.Percent, 0.90);
SetStopLoss("",CalculationMode.Percent, 0.04, false);
EnterLong(1);
}
if (Close[0] > Open[0] &&
Position.MarketPosition == MarketPosition.Long)
{
ExitLong(1);
}
}
#region Properties
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(ResourceType = typeof(Custom.Resource), Name = "Line1b", Order = 1, GroupName = "NinjaScriptStrategyParameters")]
public int Line1b
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(ResourceType = typeof(Custom.Resource), Name = "Line2a", Order = 1, GroupName = "NinjaScriptStrategyParameters")]
public int Line2a
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(ResourceType = typeof(Custom.Resource), Name = "Line2b", Order = 1, GroupName = "NinjaScriptStrategyParameters")]
public int Line2b
{ get; set; }
#endregion
}
}
Comment