• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Time exits

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Time exits

    Code:
    		
    private int entrytime=0;
    private int currenttime=0;
    private int diff=0;
    Code:
    			currenttime=ToDay(Time[0]);
    
    			//reset times
    			if (Position.MarketPosition == MarketPosition.Flat)
    			{
    								entrytime=0;
    								currenttime=0;
    			}
    
    			//store entrytime
    			if (Position.MarketPosition == MarketPosition.Flat 
    				&& CurrentBars[0] > BarsRequiredToTrade
    				&&CrossAbove(SMA1, SMA2, 1))
    			{
    				EnterLong(Convert.ToInt32(DefaultQuantity), "");
    				entrytime=ToDay(Time[0]);
    			}
    			
    			//calculate difference between entry time and current time in a position
    			if (Position.MarketPosition == MarketPosition.Long);
    			{
    				diff=currenttime-entrytime;
    				Print(diff);
    			}
    			
    			//exit after 5 days and reset entrytime and currenttime
    			
    			if(diff>5)
    			{
    				ExitLong();
    				entrytime=0;
    				currenttime=0;
    			}
    Basically I am storing the date for my entry (similar to how it would appear in strategy analyzer trade report) and then exiting after 5 days have passed.

    Thoughts on this code? Looks like it works fine. Is this an efficient way to keep track of time? I could use barssinceexecution but I can't for my purposes. Asking b/c my strategy has thousands of lines of codes and trades up to 5 instruments at the same time so trying to reduce the code as much as possible.
    Last edited by staycool3_a; 12-20-2017, 08:56 PM.

    #2
    It is fine. I doubt it will run any faster if you alter it a little like this:


    Code:
    			currenttime=ToDay(Time[0]);
    
    			//reset times
    			if (Position.MarketPosition == MarketPosition.Flat)
    			{
    								entrytime=0;
    								currenttime=0;
    			}
    
    			//store entrytime
    			if (Position.MarketPosition == MarketPosition.Flat 
    				&& CurrentBars[0] > BarsRequiredToTrade
    				&&CrossAbove(SMA1, SMA2, 1))
    			{
    				EnterLong(Convert.ToInt32(DefaultQuantity), "");
    				entrytime=ToDay(Time[0]);
    			}
    			
    			//calculate difference between entry time and current time in a position
    			if (Position.MarketPosition == MarketPosition.Long);
    			{
    				diff=currenttime-entrytime;
    				Print(diff);
    
    			
    			     //exit after 5 days and reset entrytime and currenttime
    			
    			     if(diff>5)
    			     {
    				ExitLong();
    				entrytime=0;
    				currenttime=0;
    			    }
    
    			}

    Comment


      #3
      Originally posted by sledge View Post
      It is fine. I doubt it will run any faster if you alter it a little like this:


      Code:
      			currenttime=ToDay(Time[0]);
      
      			//reset times
      			if (Position.MarketPosition == MarketPosition.Flat)
      			{
      								entrytime=0;
      								currenttime=0;
      			}
      
      			//store entrytime
      			if (Position.MarketPosition == MarketPosition.Flat 
      				&& CurrentBars[0] > BarsRequiredToTrade
      				&&CrossAbove(SMA1, SMA2, 1))
      			{
      				EnterLong(Convert.ToInt32(DefaultQuantity), "");
      				entrytime=ToDay(Time[0]);
      			}
      			
      			//calculate difference between entry time and current time in a position
      			if (Position.MarketPosition == MarketPosition.Long);
      			{
      				diff=currenttime-entrytime;
      				Print(diff);
      
      			
      			     //exit after 5 days and reset entrytime and currenttime
      			
      			     if(diff>5)
      			     {
      				ExitLong();
      				entrytime=0;
      				currenttime=0;
      			    }
      
      			}
      Thanks! Btw I see you're in DC. I'm always there. We should link up!

      www.linkedin.com/in/umairchaudhry

      Comment


        #4
        Actually ended up using barssinceexecution

        Code:
        				AddDataSeries("NZDUSD", Data.BarsPeriodType.Minute, 90, Data.MarketDataType.Last);//11
        				AddDataSeries("AUDUSD", Data.BarsPeriodType.Minute, 90, Data.MarketDataType.Last);//12
        				AddDataSeries("AUDNZD", Data.BarsPeriodType.Minute, 90, Data.MarketDataType.Last);//13
        				AddDataSeries("EURUSD", Data.BarsPeriodType.Minute, 90, Data.MarketDataType.Last);//14
        				AddDataSeries("USDCAD", Data.BarsPeriodType.Minute, 90, Data.MarketDataType.Last);//15
        Code:
        			if(TimeStop == true)
        			{
        			if(BarsInProgress==11 && Positions[11].MarketPosition == MarketPosition.Long && BarsSinceEntryExecution(11,"nzdb1",0) >= 96)
        			{
        				ExitLong(Convert.ToInt32(Position.Quantity), "nzdtimestop", "nzdb1");
        				ExitLong(Convert.ToInt32(Position.Quantity), "nzdtimestop", "nzdb2");				
        			}			
        
        			if(BarsInProgress==12 && Positions[12].MarketPosition == MarketPosition.Long && BarsSinceEntryExecution(12,"audb1",0) >= 96)
        			{
        				ExitLong(Convert.ToInt32(Position.Quantity), "audtimestop", "audb1");
        				ExitLong(Convert.ToInt32(Position.Quantity), "audtimestop", "audb2");				
        			}
        			
        			if(BarsInProgress==13 && Positions[13].MarketPosition == MarketPosition.Long && BarsSinceEntryExecution(13,"audnzdb1",0) >= 96)
        			{
        				ExitLong(Convert.ToInt32(Position.Quantity), "audnzdtimestop", "audnzdb1");
        				ExitLong(Convert.ToInt32(Position.Quantity), "audnzdtimestop", "audnzdb2");				
        			}
        			
        			if(BarsInProgress==14 && Positions[14].MarketPosition == MarketPosition.Long && BarsSinceEntryExecution(14,"eurb1",0) >= 96)
        			{
        				ExitLong(Convert.ToInt32(Position.Quantity), "eurtimestop", "eurb1");
        				ExitLong(Convert.ToInt32(Position.Quantity), "eurtimestop", "eurb2");				
        			}
        			
        			if(BarsInProgress==15 && Positions[15].MarketPosition == MarketPosition.Long && BarsSinceEntryExecution(15,"cadb1",0) >= 96)
        			{
        				ExitLong(Convert.ToInt32(Position.Quantity), "cadtimestop", "cadb1");
        				ExitLong(Convert.ToInt32(Position.Quantity), "cadtimestop", "cadb2");				
        			}
        			}
        additional information: i have a bool for each instrument so i can "turn on/off" a particular instrument within the strategy. when i turn off the instrument, it does not generate any trades, when i turn on the instrument, it generates trades.

        my primary instrument is using 90 minute bars. and so is all additional instruments (see above).

        problem:

        if i only turn on 1 instrument, the strategy exits the trade as expected after 96 bars (see BarsSinceexeuction code above). however, if I turn on more than 1 instrument, for some strange reason, the TimeExit is being initiated after 3 hours. All of my trades end up exiting after 3 hours. Why is this happening?

        Does barSinceExecution work on multiple instruments? Are my indexing for barssinceexeuction correct?

        Comment


          #5
          Originally posted by calhawk01 View Post
          Actually ended up using barssinceexecution

          Code:
          				AddDataSeries("NZDUSD", Data.BarsPeriodType.Minute, 90, Data.MarketDataType.Last);//11
          				AddDataSeries("AUDUSD", Data.BarsPeriodType.Minute, 90, Data.MarketDataType.Last);//12
          				AddDataSeries("AUDNZD", Data.BarsPeriodType.Minute, 90, Data.MarketDataType.Last);//13
          				AddDataSeries("EURUSD", Data.BarsPeriodType.Minute, 90, Data.MarketDataType.Last);//14
          				AddDataSeries("USDCAD", Data.BarsPeriodType.Minute, 90, Data.MarketDataType.Last);//15
          Code:
          			if(TimeStop == true)
          			{
          			if(BarsInProgress==11 && Positions[11].MarketPosition == MarketPosition.Long && BarsSinceEntryExecution(11,"nzdb1",0) >= 96)
          			{
          				ExitLong(Convert.ToInt32(Position.Quantity), "nzdtimestop", "nzdb1");
          				ExitLong(Convert.ToInt32(Position.Quantity), "nzdtimestop", "nzdb2");				
          			}			
          
          			if(BarsInProgress==12 && Positions[12].MarketPosition == MarketPosition.Long && BarsSinceEntryExecution(12,"audb1",0) >= 96)
          			{
          				ExitLong(Convert.ToInt32(Position.Quantity), "audtimestop", "audb1");
          				ExitLong(Convert.ToInt32(Position.Quantity), "audtimestop", "audb2");				
          			}
          			
          			if(BarsInProgress==13 && Positions[13].MarketPosition == MarketPosition.Long && BarsSinceEntryExecution(13,"audnzdb1",0) >= 96)
          			{
          				ExitLong(Convert.ToInt32(Position.Quantity), "audnzdtimestop", "audnzdb1");
          				ExitLong(Convert.ToInt32(Position.Quantity), "audnzdtimestop", "audnzdb2");				
          			}
          			
          			if(BarsInProgress==14 && Positions[14].MarketPosition == MarketPosition.Long && BarsSinceEntryExecution(14,"eurb1",0) >= 96)
          			{
          				ExitLong(Convert.ToInt32(Position.Quantity), "eurtimestop", "eurb1");
          				ExitLong(Convert.ToInt32(Position.Quantity), "eurtimestop", "eurb2");				
          			}
          			
          			if(BarsInProgress==15 && Positions[15].MarketPosition == MarketPosition.Long && BarsSinceEntryExecution(15,"cadb1",0) >= 96)
          			{
          				ExitLong(Convert.ToInt32(Position.Quantity), "cadtimestop", "cadb1");
          				ExitLong(Convert.ToInt32(Position.Quantity), "cadtimestop", "cadb2");				
          			}
          			}
          additional information: i have a bool for each instrument so i can "turn on/off" a particular instrument within the strategy. when i turn off the instrument, it does not generate any trades, when i turn on the instrument, it generates trades.

          my primary instrument is using 90 minute bars. and so is all additional instruments (see above).

          problem:

          if i only turn on 1 instrument, the strategy exits the trade as expected after 96 bars (see BarsSinceexeuction code above). however, if I turn on more than 1 instrument, for some strange reason, the TimeExit is being initiated after 3 hours. All of my trades end up exiting after 3 hours. Why is this happening?

          Does barSinceExecution work on multiple instruments? Are my indexing for barssinceexeuction correct?

          found the solution:

          https://ninjatrader.com/support/help...s/?welcome.htm

          barsInProgressIndex- The index of the Bars object the entry order was submitted against.

          Needed to change the index to match appropriate dataseries (1:5) bc my entries are on tick data.

          Comment


            #6
            Hello calhawk01,

            Thank you for updating this thread with your solutions.
            Patrick H.NinjaTrader Customer Service

            Comment


              #7
              hi

              for some reason same code is generating different results in market replay vs strategy analyzer

              i have

              Code:
              		AddDataSeries(Data.BarsPeriodType.Minute, 14);//5
              then timed exit:

              Code:
              			if(Position.MarketPosition == MarketPosition.Long
              				&& BarsSinceEntryExecution(1,"Target1S",0) >=1)
              			{
              				ExitLong(1,Position.Quantity,  @"STP1",@"Target1S");	
              				ExitLong(1,Position.Quantity,  @"STP2",@"Target2S");	
              				ExitLong(1,Position.Quantity,  @"STP3",@"Target3S");	
              			}
              I am subimitting my orders at the above mentioned data series of 14 minutes. but I am generating my orders based on [0] data series, [0] data series = 1440 minutes.

              for some reason during my backtests on strategy analzyer; my exits are:

              Code:
              entry time:10/4/2017 18:38
              exit time: 10/8/2017 17:14
              notice the above are about 4 days apart.

              during market replay; my exits are following:

              Code:
              entry time: 10/4/2017 18:10
              exit time: 10/5/2017 17:00
              **note both above "entries" are same exact but one is from market replay and the other is from strategy analyzer.

              why is the same BarSinceEntry logic behaving differently during strategy analyzer and market replay? I have: "Calculate.OnEachTick;=true"

              thanks

              Comment


                #8
                Hello calhawk01,
                Thanks for your post.

                The Strategy Analyzer does the best it can to simulate the real market but it is not perfect and it works on bar closes instead of every tick. It would be expected for a strategy running real-time to produce different results than the performance results generated during a backtest. In this case, using Market Replay would be akin to real-time.

                Please review the following help guide documentation that discusses the differences between Real-Time strategy results vs Strategy Backtest results.

                Discrepancies: Real-Time vs Backtest
                https://ninjatrader.com/support/help...ime_vs_bac.htm

                Please let me know if you have any further questions.
                Josh G.NinjaTrader Customer Service

                Comment


                  #9
                  Did you seriously just response stating that there are discrepancies between backtest and replay, in response to my question? I'm much familiar with the discrepancies... is this another one that I have to add to the list?? Exits that are FOUR days apart due to barsinceexecution is hardly a discrepancy. That's a HUGE variance.

                  Can you make a suggestion on how to fix this? Is this behaving as expected??

                  Comment


                    #10
                    Hello calhawk01,
                    Thanks for your note.

                    Intrabar granularity and inaccurate fill concerns are present on all platforms in back testing/optimization. Most individuals use back testing/optimization along with walk-forward simulated testing to ensure their strategies are indeed profitable. The main issue is that there is no way to know how exactly an order would be filled by your broker using historical data, as well as computational complexity issues. For example, imagine running an optimization on an hourly chart of 10 variables over three months with thousands of ticks per hour. Most of the design of the strategy analyzer has this in mind, to be the most accurate it can be while not trading off efficiency, and was not unintended.

                    There are a few options for improving the accuracy to your strategies results:
                    1. Use a reference sample available in our forums on increasing intra-bar granularity
                    2. Change your backtest fill algorithm
                    3. Changing your CalculateOnEachTick setting
                    4. Do your backtesting using Market Replay.

                    Using Market Replay for back testing would replay historical market tick data, and its possible to have a strategy running and placing trades during the replay on the Replay101 account.

                    I am including the relevant help guide documentation and forum links from this post, for your convenience.

                    Market Replay : https://ninjatrader.com/support/help...connection.htm

                    Calculate
                    https://ninjatrader.com/support/help...?calculate.htm

                    Historical Order Backfill Logic
                    https://ninjatrader.com/support/help...fill_logic.htm

                    Understanding order fill resolution
                    https://ninjatrader.com/support/help...FillResolution

                    Here is a reference sample on back-testing with intra-bar granularity : https://ninjatrader.com/support/foru...ead.php?t=6652

                    Also, If you have a suggestion for a feature you want me to pass along to the development team I would be happy to do so.

                    Please let me know if you have any further questions.
                    Josh G.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by vpzdcv, Today, 02:31 AM
                    0 responses
                    3 views
                    0 likes
                    Last Post vpzdcv
                    by vpzdcv
                     
                    Started by YevhenShynkarenko, Today, 01:22 AM
                    0 responses
                    7 views
                    0 likes
                    Last Post YevhenShynkarenko  
                    Started by ttodua, Today, 12:52 AM
                    0 responses
                    4 views
                    0 likes
                    Last Post ttodua
                    by ttodua
                     
                    Started by ttodua, Today, 12:50 AM
                    0 responses
                    2 views
                    0 likes
                    Last Post ttodua
                    by ttodua
                     
                    Started by mulshah, Yesterday, 11:12 PM
                    0 responses
                    3 views
                    0 likes
                    Last Post mulshah
                    by mulshah
                     
                    Working...
                    X