Stop strategy after a set $ amount lost from CumProfit - NinjaTrader Programming | futures io social day trading
futures io futures trading


Stop strategy after a set $ amount lost from CumProfit
Updated: Views / Replies:5,447 / 21
Created: by vast Attachments:0

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors Ė all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you donít need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

Reply
 
Thread Tools Search this Thread
 

Stop strategy after a set $ amount lost from CumProfit

  #11 (permalink)
Elite Member
Australia
 
Futures Experience: Intermediate
Platform: Ninja
 
vast's Avatar
 
Posts: 167 since Jun 2009
Thanks: 154 given, 59 received

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.

Reply With Quote
The following user says Thank You to vast for this post:
 
  #12 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,238 since Jun 2009
Thanks: 29,350 given, 83,214 received

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

Due to time constraints, please do not PM me if your question can be resolved or answered on the forum.

Need help?
1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first.
2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses.
3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make.
4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance.
5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers.
6)
Help using the forum? Watch this video to learn general tips on using the site.

If you want
to support our community, become an Elite Member.

Reply With Quote
 
  #13 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,238 since Jun 2009
Thanks: 29,350 given, 83,214 received



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

Due to time constraints, please do not PM me if your question can be resolved or answered on the forum.

Need help?
1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first.
2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses.
3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make.
4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance.
5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers.
6)
Help using the forum? Watch this video to learn general tips on using the site.

If you want
to support our community, become an Elite Member.

Reply With Quote
 
  #14 (permalink)
Elite Member
Australia
 
Futures Experience: Intermediate
Platform: Ninja
 
vast's Avatar
 
Posts: 167 since Jun 2009
Thanks: 154 given, 59 received

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

Reply With Quote
 
  #15 (permalink)
Elite Member
Australia
 
Futures Experience: Intermediate
Platform: Ninja
 
vast's Avatar
 
Posts: 167 since Jun 2009
Thanks: 154 given, 59 received

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.
Please register on futures.io to view futures trading content such as post attachment(s), image(s), and screenshot(s).

Please register on futures.io to view futures trading content such as post attachment(s), image(s), and screenshot(s).
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

Reply With Quote
 
  #16 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,238 since Jun 2009
Thanks: 29,350 given, 83,214 received

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

Due to time constraints, please do not PM me if your question can be resolved or answered on the forum.

Need help?
1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first.
2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses.
3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make.
4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance.
5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers.
6)
Help using the forum? Watch this video to learn general tips on using the site.

If you want
to support our community, become an Elite Member.

Reply With Quote
 
  #17 (permalink)
Elite Member
Australia
 
Futures Experience: Intermediate
Platform: Ninja
 
vast's Avatar
 
Posts: 167 since Jun 2009
Thanks: 154 given, 59 received

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.

Reply With Quote
 
  #18 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,238 since Jun 2009
Thanks: 29,350 given, 83,214 received


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

Due to time constraints, please do not PM me if your question can be resolved or answered on the forum.

Need help?
1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first.
2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses.
3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make.
4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance.
5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers.
6)
Help using the forum? Watch this video to learn general tips on using the site.

If you want
to support our community, become an Elite Member.

Reply With Quote
The following 2 users say Thank You to Big Mike for this post:
 
  #19 (permalink)
Elite Member
Australia
 
Futures Experience: Intermediate
Platform: Ninja
 
vast's Avatar
 
Posts: 167 since Jun 2009
Thanks: 154 given, 59 received

Thanks Mike. Will try it today some time.

Reply With Quote
 
  #20 (permalink)
Elite Member
Near the BEuTiFULL Horse Shoe
 
Futures Experience: Beginner
Platform: NinjaTrader
Broker/Data: MBTrading Dukascopy ZenFire
Favorite Futures: $EURUSD when it is trending
 
Trader.Jon's Avatar
 
Posts: 500 since Jul 2009
Thanks: 401 given, 176 received

Good Ideas



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

Reply



futures io > > > > > Stop strategy after a set $ amount lost from CumProfit

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Linda Bradford Raschke: Reading The Tape

Elite only

Adam Grimes: TBA

Elite only

NinjaTrader: TBA

January

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using SuperTrend for trailing stop in a strategy - help needed DarrenH NinjaTrader Programming 16 September 29th, 2017 11:03 PM
Volume stop Strategy Possible ? isarhodes NinjaTrader Programming 5 July 12th, 2011 05:02 PM
How to set up a Stop (Limit) Loss order - Ninja Trader jdumanic NinjaTrader 2 June 7th, 2011 06:40 PM
MOVE STOP LOSS AFTER STRATEGY firecan NinjaTrader Programming 2 December 13th, 2010 06:54 AM
Drawdowns, when has a strategy lost its 'edge' fluxsmith Psychology and Money Management 6 October 16th, 2010 07:24 PM


All times are GMT -4. The time now is 07:40 AM.

Copyright © 2017 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.
no new posts
Page generated 2017-12-11 in 0.37 seconds with 19 queries on phoenix via your IP 54.226.132.197