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 Trader.Jon with 1 posts (0 thanks)
    1. trending_up 9,739 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

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

I am wondering if it is possible to stop a strategy after a certain amount of $ loss to your Cumulative profit.
I have read and used the settings for stopping if the CumProfit is > $ amount.
ie Performance.RealtimeTrades.TradesPerformance.Currency.CumProfit - priorTradesCumProfit >= 1000
But can't get my head around how to achieve what I would like
ie. Can I store the highest daily profit achieved and then stop the strategy once a predetermined $ amount is lost from this value.
Thanks

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Trade idea based off three indicators.
Traders Hideout
PowerLanguage & EasyLanguage. How to get the platfor …
EasyLanguage Programming
ZombieSqueeze
Platforms and Indicators
REcommedations for programming help
Sierra Chart
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Spoo-nalysis ES e-mini futures S&P 500
29 thanks
Just another trading journal: PA, Wyckoff & Trends
25 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
21 thanks
GFIs1 1 DAX trade per day journal
17 thanks
  #2 (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,442 since Jun 2009
Thanks Given: 33,215
Thanks Received: 101,603

From:


jackyd View Post
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. (in case I ever have a strategy that makes money!!!)

This is probably very simplistic and old hat for many, but hopefully it will help someone. It can be easily personalized with your own messages, input variables, etc.

 
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;
    } 
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:
  #3 (permalink)
 
vast's Avatar
 vast 
Australia
 
Experience: Intermediate
Platform: Ninja
Posts: 167 since Jun 2009
Thanks Given: 154
Thanks Received: 62


Thank you Mike. Didn't even think to look there.
I am sure that there is info in here that I could use.
Sorry for not looking

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

Is there a way to calculate maximum cumulative profit?
I realise that there is gross profit, but this is different to max cumulative profit.

Started this thread Reply With Quote
  #5 (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,442 since Jun 2009
Thanks Given: 33,215
Thanks Received: 101,603


vast View Post
Is there a way to calculate maximum cumulative profit?
I realise that there is gross profit, but this is different to max cumulative profit.

Hmm. Maybe create your own DataSeries.

 
Code
                            
#variables
private DataSeries mycumprofit;

#init
mycumprofit = new DataSeries(this);

#onbarupdate

if (Bars.FirstBarOfSession)
mycumprofit = new DataSeries(this);  // re-init each day

mycumprofit.Set(Performance.AllTrades.TradesPerformance.Currency.CumProfit);

// then to get the highest value:
double _highestcumprofit MAX(mycumprofitBars.BarsSinceSession 1)[0];

// and to find out how many bars ago that was
int _highestcumprofit_bar HighestBar(mycumprofitBars.BarsSinceSession 1)[0];

// and to find out the time stamp of that bar
Print(Time[_highestcumprofit_bar] + ": this was the time I made " _highestcumprofit.ToString()); 
Just a thought, untested code.

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:
  #6 (permalink)
 
vast's Avatar
 vast 
Australia
 
Experience: Intermediate
Platform: Ninja
Posts: 167 since Jun 2009
Thanks Given: 154
Thanks Received: 62

Thank you once again. Will have a look.
I am wanting to stop the risk of losing all the profits of the day . So I was thinking of this method. I think that it makes good money management. ie. If I get up to $500 cumulative profit and then get on a string of losers or low trades, I don't want to whittle it down to zero. I am willing to keep trading until I lose a set amount form this highest total. So if I am willing to risk a loss of $300 from my highest point then the strat stops at that point.

Started this thread Reply With Quote
  #7 (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,442 since Jun 2009
Thanks Given: 33,215
Thanks Received: 101,603


vast View Post
Thank you once again. Will have a look.
I am wanting to stop the risk of losing all the profits of the day . So I was thinking of this method. I think that it makes good money management. ie. If I get up to $500 cumulative profit and then get on a string of losers or low trades, I don't want to whittle it down to zero. I am willing to keep trading until I lose a set amount form this highest total. So if I am willing to risk a loss of $300 from my highest point then the strat stops at that point.

Yup good job.

Gary and I have similar thoughts and MM plan, think of it like a trailing stop for your daily profit target/loss.

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
  #8 (permalink)
 
vast's Avatar
 vast 
Australia
 
Experience: Intermediate
Platform: Ninja
Posts: 167 since Jun 2009
Thanks Given: 154
Thanks Received: 62

Hi Mike
I tried the code and had a few errors. come up.
It didn't like the [0] is this:
int _highestcumprofit_bar = HighestBar(mycumprofit, Bars.BarsSinceSession - 1)[0];

Also had to add (double) after the = signs.
double _highestcumprofit = (double) MAX(mycumprofit, Bars.BarsSinceSession - 1)[0];
But now when I apply the strat it just freezes up.
Thanks forth ehelp so far though.
Regards

Started this thread Reply With Quote
  #9 (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,442 since Jun 2009
Thanks Given: 33,215
Thanks Received: 101,603

Sorry can't test right now. You may need to do a check first:

if (Performance.AllTrades.Count > 1)

If it's zero it may screw up the rest. Check the log for any obvious errors.

Sorry about the type-o on HighestBar, I don't use it very often and forgot you can't index it.

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
  #10 (permalink)
BigDog
Chicago
 
Posts: 60 since Jun 2009
Thanks Given: 0
Thanks Received: 17


Here's some code ive used in a production robot.... works well and does pretty much what i think you're trying to do

 
Code
////////////////////////
/// TRACK LAST TRADE //
/// //////////////////

if (Performance.RealtimeTrades.Count > 0) 
{
    lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 1]; 
}

////////////////////
// STRATEGY HALT //
//////////////////
/// 
// Strategy is halted if Net P&L is <= -$450.00
if (strategyhalt)
    return;

if (Performance.RealtimeTrades.TradesPerformance.Currency.CumProfit <= -450)
{
    strategyhalt = true;
    Print ("Strategy Halted" + "    " + "P&L is:" + " " + Performance.RealtimeTrades.TradesPerformance.Currency.CumProfit + "    " + "Number of trades is:" + " " + Performance.RealtimeTrades.Count);
}



////////////////////////
/// PROFIT PROTECTION //
/// ///////////////////
 
if (Performance.RealtimeTrades.TradesPerformance.Currency.CumProfit >= 500)
{
    ceiling = true;
}

if (ceiling == true
    && lastTrade !=null
    && lastTrade.ProfitCurrency < 0)
{
    strategyhalt = true;
    Print ("Strategy Halted" + "    " + "P&L is:" + " " + Performance.RealtimeTrades.TradesPerformance.Currency.CumProfit + "    " + "Number of trades is:" + " " + Performance.RealtimeTrades.Count);
}

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