NinjaTrader 6.5 Miscellaneous - Set overall max profit/loss for a strategy
futures io futures trading


Categories Help    

NinjaTrader 6.5 Miscellaneous
Downloads in this section are for NinjaTrader 6.5 and include files that don't fit into any other category (ie: Market Replay Data, Custom Bar Types)


Entries
Set overall max profit/loss for a strategy 5 *
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 
 
Suggest other entries I might like
Details: Set overall max profit/loss for a strategy
Category: NinjaTrader 6.5 Miscellaneous 


July 1st, 2009
Size: 1.59 KB
Downloaded: 78 times
 


 
Category
 



Copyright © 2021 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
All information is for educational use only and is not investment advice.
There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.