I am trying to test the: https://ninjatrader.com/support/foru...limit-examples
Daily Pnl. I am testing this under Sim101 account. I am getting all return value as 0. Here is my code:
namespace NinjaTrader.NinjaScript.Strategies
{
public class DailyLossLimitExample : Strategy
{
private double currentPnL;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Prevents new entries after PnL is less than the LossLimit";
Name = "DailyLossLimitExample";
Calculate = Calculate.OnBarClose;
BarsRequiredToTrade = 1;
LossLimit = 500;
}
else if (State == State.DataLoaded)
{
ClearOutputWindow();
}
}
protected override void OnBarUpdate()
{
// at the start of a new session, reset the currentPnL for a new day of trading
if (Bars.IsFirstBarOfSession)
currentPnL = 0;
// if flat and below the loss limit of the day enter long
if (Position.MarketPosition == MarketPosition.Flat && currentPnL > -LossLimit)
{
Print("daily limit hit" + Time[0].ToString());
Print("currentPnl" + currentPnL);
Print("Profit per month of all trades is: " + SystemPerformance.AllTrades.TradesPerformance.Curr ency.ProfitPerMonth);
}
if (Position.MarketPosition == MarketPosition.Flat && currentPnL < -LossLimit)
{
Print("daily limit NOT hit " + Time[0].ToString());
}
}
protected override void OnPositionUpdate(Position position, double averagePrice, int quantity, MarketPosition marketPosition)
{
if (Position.MarketPosition == MarketPosition.Flat && SystemPerformance.AllTrades.Count > 0)
{
// when a position is closed, add the last trade's Profit to the currentPnL
currentPnL += SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1].ProfitCurrency;
// print to output window if the daily limit is hit
if (currentPnL <= -LossLimit)
{
Print("daily limit hit, no new orders" + Time[0].ToString());
}
}
}
#region Properties
[NinjaScriptProperty]
[Range(0, double.MaxValue)]
[Display(ResourceType = typeof(Custom.Resource), Name="LossLimit", Description="Amount of dollars of acceptable loss", Order=1, GroupName="NinjaScriptStrategyParameters")]
public double LossLimit
{ get; set; }
#endregion
}
}
daily limit hit1/13/2021 12:30:00 PM currentPnl0 Profit per month of all trades is: 0 Disabling NinjaScript strategy 'DailyLossLimitExample/219006979'
Please advice the soultion.
Thanks

Comment