private double priorCumProfit = 0;
// At the start of a new session calculate the prior cum profit so it won't be included in the // calculation. Need this for historical testing. if (Bars.FirstBarOfSession) {priorCumProfit = Performance.AllTrades.TradesPerformance.Currency.CumProfit;} // *** Calculate the toal profit (cumulative profit minus prior profit plus the current position profit double myMaxProfit = (double) 1000; double myMaxLoss = (double) -1000; double cumProfit = (double) Performance.AllTrades.TradesPerformance.Currency.CumProfit; double curPosition = (double) Position.GetProfitLoss(Close[0], PerformanceUnit.Currency); double totCumProfit = (double) cumProfit - priorCumProfit + curPosition ; // *** STOP the strategy! if a total profit or loss exceeds the max if (totCumProfit <= myMaxLoss || totCumProfit >= myMaxProfit) { if (Position.MarketPosition == MarketPosition.Long) {ExitLong("DMA: Exit Long - max Profit/Loss exceeded", "");} if (Position.MarketPosition == MarketPosition.Short) {ExitShort("DMA: Exit Short - max Profit/Loss exceeded", "");} Print(Time[0] + ": EXIT STRATEGY - Max Profit/Loss exceeded: $" + myMaxProfit + "/$" + myMaxLoss + ", Current: $" + totCumProfit); return; }