I want the strategy to stop trading after $200 profit has been reached in one trading day (from hours 9:30 EST to 16:00 EST).
I have downloaded the Sample PnL code from this thread: http://ninjatrader.com/support/forum...ead.php?t=4084 and I have tried merging the two together but it doesn't seem to work.
It seems really easy to do but for some reason I cannot figure it out.
This is my simple code:
namespace NinjaTrader.Strategy
{
/// <summary>
/// Enter the description of your strategy here
/// </summary>
[Description("Enter the description of your strategy here")]
public class Original : Strategy
{
#region Variables
// Wizard generated variables
private int myInput0 = 1; // Default setting for MyInput0
// User defined variables (add any user defined variables below)
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Condition set 1
if (CrossAbove(EMA(9), EMA(21), 1)
&& ToTime(Time[0]) >= ToTime(9, 30, 0)
&& ToTime(Time[0]) <= ToTime(16, 0, 0))
{
EnterLong(DefaultQuantity, "");
}
// Condition set 2
if (CrossBelow(EMA(9), EMA(21), 1)
&& ToTime(Time[0]) >= ToTime(9, 30, 0)
&& ToTime(Time[0]) <= ToTime(16, 0, 0))
{
EnterShort(DefaultQuantity, "");
}
}
#region Properties
[Description("")]
[GridCategory("Parameters")]
public int MyInput0
{
get { return myInput0; }
set { myInput0 = Math.Max(1, value); }
}
#endregion
}
}
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// At the start of a new session
if (Bars.FirstBarOfSession)
{
// Store the strategy's prior cumulated realized profit and number of trades
priorTradesCount = Performance.AllTrades.Count;
priorTradesCumProfit = Performance.AllTrades.TradesPerformance.Currency.C umProfit;
/* NOTE: Using .AllTrades will include both historical virtual trades as well as real-time trades.
If you want to only count profits from real-time trades please use .RealtimeTrades. */
}
/* Prevents further trading if the current session's realized profit exceeds $1000 or if realized losses exceed $400.
Also prevent trading if 10 trades have already been made in this session. */
if (Performance.AllTrades.TradesPerformance.Currency. CumProfit - priorTradesCumProfit >= 1000
|| Performance.AllTrades.TradesPerformance.Currency.C umProfit - priorTradesCumProfit <= -400
|| Performance.AllTrades.Count - priorTradesCount > 10)
{
/* TIP FOR EXPERIENCED CODERS: This only prevents trade logic in the context of the OnBarUpdate() method. If you are utilizing
other methods like OnOrderUpdate() or OnMarketData() you will need to insert this code segment there as well. */
// Returns out of the OnBarUpdate() method. This prevents any further evaluation of trade logic in the OnBarUpdate() method.
return;
}
Comment