Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
Stop strategy after a set $ amount lost from CumProfit
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);
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.
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:
Change it to:
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.
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.
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
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
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
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.
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.
To find the maximum totalProfit value to have ever existed:
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: