Categories Show Search Help

Set overall max profit/loss for a strategy
Here's some code I came up with from various samples to give me some peace of mind if you were to run your strategies unattended.

Nothing fancy, but it can be easily personalized with your own messages, input variables, etc.

Please test before using with real money!!!

Place in the Variables section...

Code
 ` private double priorCumProfit = 0;`
Place at the beginning of the OnBarUpdate() section...

Code
 ```// 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; }```

Category NinjaTrader 6.5 Miscellaneous

 Details: Set overall max profit/loss for a strategy Category: NinjaTrader 6.5 Miscellaneous  July 1st, 2009 Size: 1.59 KB Downloaded: 78 times