NexusFi: Find Your Edge


Home Menu

 





Frustrated with Resetting return :(


Discussion in NinjaTrader

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




 
Search this Thread

Frustrated with Resetting return :(

  #1 (permalink)
jsk123
Hyderabad,India
 
Posts: 88 since Oct 2013
Thanks Given: 44
Thanks Received: 21

Hi.... I'm trying to learn how to stop trading for the day if my profit target / loss limit is reached for the day.

Please take a look at my strategy's code. When I tried to backtest for over 1 year data, only the data of first day is being backtested and not anymore after the Profit / Loss limits are reached.

I tried to reset the totPnl if the time is between 3:45 AM and 3:46 AM (my local time at which the electronic market starts again after the daily break). but it is clearly not working....

I don't know how to reset this totPnl so that it would go back to zero again for the next day to backtest the data... Please help.

 
Code
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
    /// <summary>
    /// Enter the description of your strategy here
    /// </summary>
    [Description("Enter the description of your strategy here")]
    public class profitandlosslimiting : Strategy
    {
        #region Variables
        private double totPnl =0 ; // This variable stores Total Pnl. By default it is 0
		private int i =0;
        // User defined variables (add any user defined variables below)
        #endregion

        /// <summary>
        /// This method is used to configure the strategy and is called once before any strategy method is called.
        /// </summary>
        protected override void Initialize()
        {
			SetProfitTarget(500);
			SetStopLoss(80);
			
            CalculateOnBarClose = true;
        }

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
       		 protected override void OnBarUpdate()
        	{
			totPnl=Performance.AllTrades.TradesPerformance.Currency.CumProfit;
			Print("value : " +totPnl);
			if(ToTime(Time[0]) >= 034500 && ToTime(Time[0]) <= 034600)
			{totPnl=0;}
			if (totPnl <= -320 || totPnl >=400)
			{						
			//	return; // this return is the reason for the strategy not being executed on other days except the first day of the selected period
						// you must find some way to reset is totalPnl at the start of the next day.
			i=0; // just a dummy variable;
			}	
			else {
				if (Close[0] > Close[1])
			{
				EnterLong();
			}
			
			if (Close[0] < Close[1])
			{
				EnterShort();
			}
			}			
			}

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
What broker to use for trading palladium futures
Commodities
How to apply profiles
Traders Hideout
MC PL editor upgrade
MultiCharts
 
  #3 (permalink)
jsk123
Hyderabad,India
 
Posts: 88 since Oct 2013
Thanks Given: 44
Thanks Received: 21


Just made some modifications to this code and here is the modified version


 
Code
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
    /// <summary>
    /// Enter the description of your strategy here
    /// </summary>
    [Description("Enter the description of your strategy here")]
    public class profitandlosslimiting : Strategy
    {
        #region Variables
        private double totPnl =0 ; // This variable stores Total Pnl. By default it is 0
		private int i =0;
        // User defined variables (add any user defined variables below)
        #endregion

        /// <summary>
        /// This method is used to configure the strategy and is called once before any strategy method is called.
        /// </summary>
        protected override void Initialize()
        {
			SetProfitTarget(500);
			SetStopLoss(80);
			
            CalculateOnBarClose = true;
        }

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
       		 protected override void OnBarUpdate()
        	{
			totPnl=Performance.AllTrades.TradesPerformance.Currency.CumProfit;
			Print("value : " +totPnl);
			
			if (totPnl <= -32000000 || totPnl >=40000000)
				{						
				return; // this return is the reason for the strategy not being executed on other days except the first day of the selected period
							// you must find some way to reset is totalPnl at the start of the next day.
				}	

			
				if (Close[0] > Close[1])
			{
				EnterLong();
			}
			
				if (Close[0] < Close[1])
			{
				EnterShort();
			}
			
			
			}
I have clearly understood that 'not being able to reset the 'return' is the only problem in the code... I deliberately increased the values of totPnl in this code and ran the strategy analyzer, it showed thousands of trades as the if () condition is satisfied. Can anyone enlighten me how to achieve this please....

Reply With Quote
  #4 (permalink)
jsk123
Hyderabad,India
 
Posts: 88 since Oct 2013
Thanks Given: 44
Thanks Received: 21

The logic i wrote may be enough for trading live in the sim as it would stop trading after one of those conditions in the if() are met. I would shutdown my computer and restart tomorrow and the program must work normal as it is a fresh start for it. But for the sake of backtesting, I need to reset the totPnl and then evaluate if the strategies I'm going to write are profitable or not...

Reply With Quote
  #5 (permalink)
 baruchs 
Israel
 
Experience: Intermediate
Platform: NinjaTrader
Broker: pfg
Trading: eminis
Posts: 323 since Jun 2009

You need to store the PnL of previos days in if(FirstBarOfSession) totPnL=...
Then on each bar you query todays PnL as totPnL - Performance.AllTrades.TradesPerformance.Currency.CumProfit;

Reply With Quote




Last Updated on March 16, 2015


© 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