NexusFi: Find Your Edge


Home Menu

 



Categories Help    






 
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
Comments/ratings
5 * Silver Dragon September 19th, 2011 10:11 PM
Excellent code! Thanks for posting!

 
Sort by

 




© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
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.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top