I couldn't find any threads on my following Problem in NT8:
I am back testing a strategy on the S&P500 with daily bars. The timeframe is 1995-2016.
If I don't add a stop loss in the code everything is fine (see screenshot 1). However, as soon as I add a stop loss, the strategy ends during 1999, so it doesn't execute any further trades.
The code is set out below. To add the stop loss just uncomment (delete "//") the stop loss code right before the "Region Properties" pretty much in the end of the code.
Would very much appreciate your help.
Regards
Kirk
#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 Test : Strategy
{
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Test";
Name = "Test";
BarsRequiredToTrade = 20;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
Capital = 100000;
}
else if (State == State.Configure)
{
}
}
protected override void OnBarUpdate()
{
if (this.CurrentBar < this.BarsRequiredToTrade)
return;
double entryPrice = Close[0];
int size = (int) (this.Capital / entryPrice);
if (Close[0] < WMA(14)[0])
{
EnterLong(size, "Buy");
}
if (BarsSinceEntryExecution() > 14)
ExitLong();
// SetStopLoss(CalculationMode.Currency, 3000);
}
#region Properties
[NinjaScriptProperty]
[Range(0, int.MaxValue)]
[Display(ResourceType = typeof(Custom.Resource), Name="Capital", Order=1, GroupName="NinjaScriptStrategyParameters")]
public int Capital
{ get; set; }
#endregion
}
}
Comment