SetStopLoss/SetProfitTarget fail in Strategy Analyzer at my end. They trigger only on bar close at a close price. But they work (seems on each tick which is expected behavior) on Market Replay. In Market replay I have Calculate = OnBarClose.
I didn't find any description on this in Help.
Sample strategy is here.
//
// Copyright (C) 2018, NinjaTrader LLC <www.ninjatrader.com>.
// NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
//
#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.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 SampleMACrossOver2 : Strategy
{
private SMA smaFast;
private SMA smaSlow;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = NinjaTrader.Custom.Resource.NinjaScriptStrategyDes criptionSampleMACrossOver;
Name = "SampleMACrossOver2";
Fast = 10;
Slow = 25;
// This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = false;
}
else if (State == State.DataLoaded)
{
smaFast = SMA(Fast);
smaSlow = SMA(Slow);
smaFast.Plots[0].Brush = Brushes.Goldenrod;
smaSlow.Plots[0].Brush = Brushes.SeaGreen;
AddChartIndicator(smaFast);
AddChartIndicator(smaSlow);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < BarsRequiredToTrade)
return;
SetProfitTarget(CalculationMode.Ticks, 20);
SetStopLoss(CalculationMode.Ticks, 6);
if (CrossAbove(smaFast, smaSlow, 1))
EnterLong();
else if (CrossBelow(smaFast, smaSlow, 1))
EnterShort();
}
#region Properties
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(ResourceType = typeof(Custom.Resource), Name = "Fast", GroupName = "NinjaScriptStrategyParameters", Order = 0)]
public int Fast
{ get; set; }
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(ResourceType = typeof(Custom.Resource), Name = "Slow", GroupName = "NinjaScriptStrategyParameters", Order = 1)]
public int Slow
{ get; set; }
#endregion
}
}
Comment