I have the next code in NT8 that trade well in backtesting, but not in Real time or marker repaly.
can someone help me?
(i attached the script below)
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
namespace NinjaTrader.NinjaScript.Strategies
{
public class TPD : Strategy
{
private int profitTargetTicks = 15;
private int stopLossTicks = 10;
private int trades_till_today = 0;
private SMA SMA1;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"trade per day Limit (one session per day)";
Name = "TPD";
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;
IsInstantiatedOnEachOptimizationIteration = true;
_cntrcts = 1;
_MaxEntriesDay = 2;
}
else if (State == State.Configure)
{
SetStopLoss(CalculationMode.Ticks, stopLossTicks);
SetProfitTarget(CalculationMode.Ticks, profitTargetTicks);
}
else if (State == State.DataLoaded)
{
SMA1 = SMA(10);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < BarsRequiredToTrade) return;
if (CurrentBars[0] < 1)
return;
if ( Bars.IsFirstBarOfSession )
{
trades_till_today = SystemPerformance.AllTrades.Count;
}
int trades_today = SystemPerformance.AllTrades.Count - trades_till_today + (Position.MarketPosition != MarketPosition.Flat ? 1 : 0);
if (trades_today < _MaxEntriesDay)
{
// Set 1
if (Close[0] > (SMA1[0] ))
{
EnterLong(Convert.ToInt32(_cntrcts), "");
}
// Set 2
if (Close[0] < (SMA1[0]))
{
EnterShort(Convert.ToInt32(_cntrcts), "");
}
}
}
#region Properties
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(ResourceType = typeof(Custom.Resource), Name="_cntrcts", Description="cntrcts", Order=1, GroupName="NinjaScriptStrategyParameters")]
public int _cntrcts
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(ResourceType = typeof(Custom.Resource), Name="_MaxEntriesDay", Description="_MaxEntriesDay ", Order=2, GroupName="NinjaScriptStrategyParameters")]
public int _MaxEntriesDay
{ get; set; }
[Range(0, int.MaxValue)]
[NinjaScriptProperty]
[Display(Name="Profit Target Ticks", Description="Number of ticks away from entry price for the Profit Target order", Order=3, GroupName="Parameters")]
public int ProfitTargetTicks
{
get { return profitTargetTicks; }
set { profitTargetTicks = value; }
}
[Range(0, int.MaxValue)]
[NinjaScriptProperty]
[Display(Name="Stop Loss Ticks", Description="Numbers of ticks away from entry price for the Stop Loss order", Order=4, GroupName="Parameters")]
public int StopLossTicks
{
get { return stopLossTicks; }
set { stopLossTicks = value; }
}
#endregion
}
}
Comment