NexusFi: Find Your Edge


Home Menu

 





Strategy stop trading at realized PnL


Discussion in NinjaTrader

Updated
    1. trending_up 8,182 views
    2. thumb_up 4 thanks given
    3. group 3 followers
    1. forum 4 posts
    2. attach_file 0 attachments




 
Search this Thread

Strategy stop trading at realized PnL

  #1 (permalink)
 zwentz 
Austin, TX
 
Experience: Advanced
Platform: NinjaTrader
Broker: Optimus Futures/Rithmic
Trading: Futures [ZB]
Posts: 36 since Sep 2009
Thanks Given: 12
Thanks Received: 22

I'm trying to code a strategy that stops trading that day if it loses too much that is backtester compatible. I found this thread over at [AUTOLINK]NinjaTrader[/AUTOLINK]'s support forum, but it's still wrong. I think I saw Big Mike in that thread over there as well, so I figured this would be the best place to ask.

Here's my code, but it doesn't seem to be working. Can anyone help?

Note: I'm speaking specifically about the backtester, the problem is that it acts as if the strategy ran straight through the entire backtesting period. So your cumulated profits are always rising, hopefully, and you can't find out what it is for the day.

 
Code
protected override void OnBarUpdate()
        { 
            double currentProfits = Position.GetProfitLoss(Close[0], PerformanceUnit.Currency);
            double cumulatedProfits = Performance.AllTrades.TradesPerformance.Currency.CumProfit;
            int indicator = MyIndicator(1)[0];

	    if (ToTime(Time[0]) < 143000 && ((cumulatedProfits - realized) + currentProfits > PosiProfs || (cumulatedProfits - realized) + currentProfits < -NegiProfs))
            {
                canEnterTrade = false;
            }
	    else
            {
		canEnterTrade = true;
            }


            if (ToTime(Time[0]) >= 63300 && canEnterTrade)
            {
                    if (indicator == 1)
                    {
                        EnterLong(DefaultQuantity, "");
                    }

                    if (indicator == -1)
                    {
                         EnterShort(DefaultQuantity, "");
                    }
            }

            //if realized profits have been reached or we're at the close of the day, flatten everything
            if ((!canEnterTrade && Position.MarketPosition != MarketPosition.Flat) || (ToTime(Time[0]) >= 142700 && Position.MarketPosition != MarketPosition.Flat))
            {
                ExitLong("Close Out", "");
                ExitShort("Close Out", "");
                realized = Performance.AllTrades.TradesPerformance.Currency.CumProfit;
            }
        }

Started this thread Reply With Quote
Thanked by:

Can you help answer these questions
from other members on NexusFi?
Better Renko Gaps
The Elite Circle
Futures True Range Report
The Elite Circle
NexusFi Journal Challenge - April 2024
Feedback and Announcements
Exit Strategy
NinjaTrader
ZombieSqueeze
Platforms and Indicators
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
61 thanks
Funded Trader platforms
38 thanks
NexusFi site changelog and issues/problem reporting
27 thanks
GFIs1 1 DAX trade per day journal
19 thanks
The Program
18 thanks
  #2 (permalink)
 
nanobiotech's Avatar
 nanobiotech 
Brisbane, Australia
 
Experience: Intermediate
Platform: NinjaTrader
Broker: Mirus/Zen-Fire
Trading: CL
Posts: 670 since Jun 2009
Thanks Given: 134
Thanks Received: 430

I know it is possible to stop a strategy when a predefined profit/loss is hit, but is it possible to have NT flatten everything when a total profit or loss target is hit when running a strategy on two or more instruments? For example, when running two strategies and net profit for the two strategies hits $1000 or loss hits $400, all strategies are stopped.

Nano

Reply With Quote
  #3 (permalink)
 zwentz 
Austin, TX
 
Experience: Advanced
Platform: NinjaTrader
Broker: Optimus Futures/Rithmic
Trading: Futures [ZB]
Posts: 36 since Sep 2009
Thanks Given: 12
Thanks Received: 22


nano,

You have to first tackle the task of getting the strategies to speak to one another. There are a lot of solutions to this, but the most promising looks to be this. Once you get that, you'd be looking to do the same thing I am asking. As far as my question goes, I'm mainly looking at it for backtesting purposes, I can make it work realtime quite easily, but in the backtester it totals them all over all the days, so it becomes slightly more complex.

Also, anyone know when NT7 is coming out? Is it ever?

Started this thread Reply With Quote
  #4 (permalink)
 
nanobiotech's Avatar
 nanobiotech 
Brisbane, Australia
 
Experience: Intermediate
Platform: NinjaTrader
Broker: Mirus/Zen-Fire
Trading: CL
Posts: 670 since Jun 2009
Thanks Given: 134
Thanks Received: 430

Thanks zwentz.

Given that NT6.5 does keep totals of net trades across multiple instruments, hopefully NT7 may have the feature to close positions for individual instruments, groups of instruments and all instruments when specific targets for profit/loss are hit.

Anyone know if NT7 will have this facility?

Nano

Reply With Quote
  #5 (permalink)
 
jackyd's Avatar
 jackyd 
Calgary Canada
 
Experience: Intermediate
Platform: Ninja
Broker: IB/Kinetic
Trading: 6E, CL
Posts: 47 since Jun 2009
Thanks Given: 44
Thanks Received: 20

Hi zwentz,

Here is a code snippet I posted back in July that should have what you're looking for...


specifically this part...
 
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;}
Basically, you track the cumulative profit at the start of each session so that you can later eliminate it when you calculate the daily max profit/loss amounts.


zwentz View Post
I'm trying to code a strategy that stops trading that day if it loses too much that is backtester compatible. I found this thread over at [AUTOLINK]NinjaTrader[/AUTOLINK]'s support forum, but it's still wrong. I think I saw Big Mike in that thread over there as well, so I figured this would be the best place to ask.

Here's my code, but it doesn't seem to be working. Can anyone help?

Note: I'm speaking specifically about the backtester, the problem is that it acts as if the strategy ran straight through the entire backtesting period. So your cumulated profits are always rising, hopefully, and you can't find out what it is for the day.

 
Code
protected override void OnBarUpdate()
        { 
            double currentProfits = Position.GetProfitLoss(Close[0], PerformanceUnit.Currency);
            double cumulatedProfits = Performance.AllTrades.TradesPerformance.Currency.CumProfit;
            int indicator = MyIndicator(1)[0];

	    if (ToTime(Time[0]) < 143000 && ((cumulatedProfits - realized) + currentProfits > PosiProfs || (cumulatedProfits - realized) + currentProfits < -NegiProfs))
            {
                canEnterTrade = false;
            }
	    else
            {
		canEnterTrade = true;
            }


            if (ToTime(Time[0]) >= 63300 && canEnterTrade)
            {
                    if (indicator == 1)
                    {
                        EnterLong(DefaultQuantity, "");
                    }

                    if (indicator == -1)
                    {
                         EnterShort(DefaultQuantity, "");
                    }
            }

            //if realized profits have been reached or we're at the close of the day, flatten everything
            if ((!canEnterTrade && Position.MarketPosition != MarketPosition.Flat) || (ToTime(Time[0]) >= 142700 && Position.MarketPosition != MarketPosition.Flat))
            {
                ExitLong("Close Out", "");
                ExitShort("Close Out", "");
                realized = Performance.AllTrades.TradesPerformance.Currency.CumProfit;
            }
        }


Reply With Quote
Thanked by:




Last Updated on December 1, 2009


© 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