NexusFi: Find Your Edge


Home Menu

 





Stop strategy after a set $ amount lost from CumProfit


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one vast with 11 posts (2 thanks)
    2. looks_two Big Mike with 8 posts (7 thanks)
    3. looks_3 MXASJ with 1 posts (1 thanks)
    4. looks_4 BigDog with 1 posts (0 thanks)
    1. trending_up 9,732 views
    2. thumb_up 10 thanks given
    3. group 4 followers
    1. forum 21 posts
    2. attach_file 0 attachments




 
Search this Thread

Stop strategy after a set $ amount lost from CumProfit

  #11 (permalink)
 
vast's Avatar
 vast 
Australia
 
Experience: Intermediate
Platform: Ninja
Posts: 167 since Jun 2009
Thanks Given: 154
Thanks Received: 62

Thank you all for your ideas and guides.
I felt a variable would be better as it would use less resources.
So I have come up with the following.

protected override void OnBarUpdate()

{

////// *** Calculate the toal profit (cumulative profit minus prior profit plus the current position profit
////
double CumProfit = (double) Math.Round(Performance.RealtimeTrades.TradesPerformance.Currency.CumProfit);
Print("CumProfit is " + CumProfit);
double CurPosition = (double) Math.Round(Position.GetProfitLoss(Close[0], PerformanceUnit.Currency));
// double totCumProfit = (double) Math.Round(cumProfit + curPosition) ;
double MaxConsecLoser = (double) Performance.RealtimeTrades.TradesPerformance.MaxConsecLoser;
Print("ConsecLoser is " + MaxConsecLoser);

if (Performance.RealtimeTrades.Count > 0)

// { // Accesses the first/last trade in the strategy (the first trade is at index 0)
Trade firstTrade = Performance.RealtimeTrades[0];

Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 1];
// Calculate the PnL for the last completed real-time trade
double lastProfit = Math.Round(lastTrade.ProfitCurrency);
// Print the PnL to the Output window
Print("The last trade's profit is " + lastProfit);

double PrevCumProfit = Math.Round(Performance.RealtimeTrades.TradesPerformance.Currency.CumProfit) - lastProfit;
Print("PrevCumProfit is " + PrevCumProfit);

if
(CumProfit > PrevCumProfit)
{double MaxCumProfit = CumProfit;
Print("Max Cum Profit is " + MaxCumProfit);
}
// }



////// *** STOP the strategy! if a total profit or loss exceeds the max
if (CumProfit <= myMaxLoss || CumProfit >= myMaxProfit || MaxConsecLoser >= myMaxConsecLoss ||CumProfit <= MaxCumProfit - maxRiskProfit )
{
if (Position.MarketPosition == MarketPosition.Long) {ExitLong("Exit Long - max Profit/Loss exceeded", "");}
if (Position.MarketPosition == MarketPosition.Short) {ExitShort("Exit Short - max Profit/Loss exceeded", "");}
Print(Time[0] + ": EXIT STRATEGY - Max Profit/Loss exceeded: $" + myMaxProfit + "/$" + myMaxLoss + ", Current: $" + CumProfit);
return;
}
All works well and I get the MaxCumProfit to work well. But... Always a but. As soon as I put in the ||CumProfit <= MaxCumProfit - maxRiskProfit I get an error saying that MaxCumProfit does not exist.
I guess that it is as a result of being inside the {} of the if (Performance.RealtimeTrades.Count > 0) statement. But I can't work out how to get it out of there as I need the CumProfit - Last Trade Profit.
And the MaxCumProfit is based on comparing the two.
Any ideas would be greatly appreciated.

Started this thread Reply With Quote
Thanked by:

Can you help answer these questions
from other members on NexusFi?
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Trade idea based off three indicators.
Traders Hideout
Increase in trading performance by 75%
The Elite Circle
ZombieSqueeze
Platforms and Indicators
REcommedations for programming help
Sierra Chart
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
36 thanks
Spoo-nalysis ES e-mini futures S&P 500
24 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
20 thanks
GFIs1 1 DAX trade per day journal
16 thanks
  #12 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,440 since Jun 2009
Thanks Given: 33,215
Thanks Received: 101,599

vast,

First you should post code using the PHP Code tag (is on editor toolbar) so it doesn't word-wrap.

The issue with your code is in this area:

 
Code
                            
if
            (
CumProfit PrevCumProfit)
            {
double MaxCumProfit CumProfit;
            Print(
"Max Cum Profit is " MaxCumProfit);
            } 
Change it to:
 
Code
                            
double MaxCumProfit 0;
if (
CumProfit PrevCumProfit)
 {
   
MaxCumProfit CumProfit;
   Print(
"Max Cum Profit is " MaxCumProfit);
 } 
I think that should do it.

Also I should mention that if you don't want this value overwritten OnBarUpdate, then you need to define MaxCumProfit in #variables and not directly above if the if statement.

Mike

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #13 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,440 since Jun 2009
Thanks Given: 33,215
Thanks Received: 101,599



vast View Post
////// *** STOP the strategy! if a total profit or loss exceeds the max
if (CumProfit <= myMaxLoss || CumProfit >= myMaxProfit || MaxConsecLoser >= myMaxConsecLoss ||CumProfit <= MaxCumProfit - maxRiskProfit )

Also my suggestion is to simplify things a bit. You can write C# code in a way where it flows like English and in a logical fashion. It may not be the most optimized code but don't worry about that part right now, you can improve your methods later.

 
Code
                            
string exitreason null;

if (
CumProfit <= MyMaxLoss)
  
exitreason "MyMaxLoss";

if (
CumProfit >= MyMaxProfit)
  
exitreason "MyMaxProfit";

if (
MaxConsecLoser >= myMaxConsecLoss)
  
exitreason "MaxConsecLoser";

if (
CumProfit <= MaxCumProfit maxRiskProfit)
  
exitreason "MaxRiskProfit";

if (
exitreason != null)
 Print(
Time[0] + ": Exited strategy: " exitreason); 
Mike

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #14 (permalink)
 
vast's Avatar
 vast 
Australia
 
Experience: Intermediate
Platform: Ninja
Posts: 167 since Jun 2009
Thanks Given: 154
Thanks Received: 62

Mike, one day I dream of being able to code like you. I can understand after the fact, but before is like being in a jungle.
Thank you for your time and explanation

Started this thread Reply With Quote
  #15 (permalink)
 
vast's Avatar
 vast 
Australia
 
Experience: Intermediate
Platform: Ninja
Posts: 167 since Jun 2009
Thanks Given: 154
Thanks Received: 62

Mike
I am still having a problem with this idea.
if
(cumProfit > prevCumProfit)
{maxCumProfit = cumProfit;
// Print("Max Cum Profit is " + maxCumProfit);
}
This works well. But I am always comparing cumProfit with prevCumProfit. If I use this method to get the original MaxCumProfit, how do I compare any new cum profits with the MaxCumProfit and no longer with the prevCumProfit?
So that the only way maxCumProfit changes is that a new higher cumProfit comes along and exceeds it.

http://screencast.com/t/hRCL9f1dgddT
So trade 1 MaxCumProfit = $35.60
Trade 4 becomes new maxCumProfit of $ 82.40
Trade 7 becomes new maxCumProfit of $209.20

Started this thread Reply With Quote
  #16 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,440 since Jun 2009
Thanks Given: 33,215
Thanks Received: 101,599

vast,

Please explain in plain english what it is you are trying to accomplish with all of your code, list all the objectives please. I am sorry but I am not sure what you are trying to do exactly. Then we can go from there after I know what all the objectives are.

Mike

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #17 (permalink)
 
vast's Avatar
 vast 
Australia
 
Experience: Intermediate
Platform: Ninja
Posts: 167 since Jun 2009
Thanks Given: 154
Thanks Received: 62

Mike
I am wanting to have a variable that stores the highest +ve Cumulative profit total for that session.
I am then going to use it as a reference for what I am willing to risk against to stop trading.
ie. If it get up positive $500 for the day. I want to continue trading but if I reduce my cumulative total to $300 for the day( a total loss of $200 from highest point). I want to stop trading.
My daily profit total may be $1000. So I have not yet reached that criteria. So I continue to trade in the expectation that I will reach it.
But maybe I get on a series of losers. This way will stop me from frittering away any +ve position I have had.
I do not want to continue trading until my hard stop (-$300 for the day) is reached.
Your idea of using a data series made sense, but it uses more system resources. Good ol Ninja Josh suggested that it could be donoe using variable. So I tried that route as well. I am nearly there I think.
I hope that this makes sense.

Started this thread Reply With Quote
  #18 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,440 since Jun 2009
Thanks Given: 33,215
Thanks Received: 101,599


vast View Post
Mike
I am wanting to have a variable that stores the highest +ve Cumulative profit total for that session.

Good plan vast. Here are my thoughts, untested code below. There is probably a simpler way to do this, perhaps using MAX on the Performance.RealtimeTrades DataSeries, but I didn't have time to test code. Also this is going to assume you restart the strategy once per day. If the strategy is run continuously another couple lines of code will be needed.

 
Code
                            

#variables
private DataSeries myTradeProfit;

#initialize
myTradeProfit = new DataSeries(this);

#onbarupdate
if (Performance.RealtimeTrades.Count 0)
  
myTradeProfit.Set(Performance.RealtimeTrades.TradesPerformance.Currency.CumProfit); 
To find the maximum totalProfit value to have ever existed:

 
Code
                            

double myMaxProfit 
MAX(myTradeProfitBars.BarsSinceSession)[0]; 
Sorry I don't have time to test, but I think this works. It simply records the CumProfit on every bar. Then you can query the highest cum profit using MAX.

So if you want to compare the MAX to the current value it would be:

 
Code
                            

double profitDiff 
MAX(myTradeProfitBars.BarsSinceSession)[0] - myTradeProfit[0];

if (
profitDiff 200)
  Print(
Time[0] + ":  Hang on, we lost more than 200 from our highest value..."); 
Mike

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #19 (permalink)
 
vast's Avatar
 vast 
Australia
 
Experience: Intermediate
Platform: Ninja
Posts: 167 since Jun 2009
Thanks Given: 154
Thanks Received: 62

Thanks Mike. Will try it today some time.

Started this thread Reply With Quote
  #20 (permalink)
 
Trader.Jon's Avatar
 Trader.Jon 
Near the BEuTiFULL Horse Shoe
 
Experience: Beginner
Platform: NinjaTrader
Broker: MBTrading Dukascopy ZenFire
Trading: $EURUSD when it is trending
Posts: 473 since Jul 2009
Thanks Given: 401
Thanks Received: 184



vast View Post
Mike
I am wanting to have a variable that stores the highest +ve Cumulative profit total for that session.
I am then going to use it as a reference for what I am willing to risk against to stop trading.
ie. If it get up positive $500 for the day. I want to continue trading but if I reduce my cumulative total to $300 for the day( a total loss of $200 from highest point). I want to stop trading. ...I hope that this makes sense.

vast,

have you completed the code? This is something I would like to add to my strategy: are you able to repost the working code please?

Jon

Reply With Quote




Last Updated on November 15, 2010


© 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
no new posts