Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Entry and exit on same bar

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

    Entry and exit on same bar

    Can someone help me with my entry and exit position code? I cannot seem to figure out how to properly execute my positions and it seems like it should be so simple that it is embarrassing. Perhaps the condition or type of chart I am using makes the entry and exit code not possible, so any clarification on that would be greatly appreciated.

    The strategy is very basic:
    Follow the day to day trend by reading yesterday's Open compared to yesterday's Close, enter position (long or short) on CurrentBar Open and close out position on CurrentBar Close.

    The types of charts I've tried are Kinetick's end of day bars and I've also imported custom minute data spanning multiple days, viewing both as candlestick charts. Neither chart seem to work correctly. I will say that the code does actually enter and exit in some way but I cannot figure out the reasoning for their placements on the chart.

    I know that my conditional code works as the Print lines are printing out in my Output window exactly as they should. My print lines display the entry and exit on their respective Open and Close, I just cannot seem to get the position code to execute. I have the chart draw some arrows too under the conditions and that logic is perfectly what I want them to do! I really don't understand the fault.


    Ninjatrader 8
    Code:
    OnStateChange{
    ...
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    TimeInForce = TimeInForce.Day;
    BarsRequiredToTrade = 1;
    ...
    }

    Code:
    OnBarUpdate{
            // Must have at least 1 previous bar to start
    	if (CurrentBar < 1)
    		return;
    			
    	// Enter long position on up trend
    	if (Close[1] >= Open[1])
    	{
    		Draw.ArrowUp(this,CurrentBars[0].ToString(),false,0,(Low[0] + (-380 * TickSize)), Brushes.Lime);
    		Print(ToDay(Time[0])+" - 093000 - Entering long position at Open: "+Open[0]);
    		EnterLongLimit(1, Open[0]);
    				
    		Print(ToDay(Time[0])+" - 160000 - Leaving long position at Close: "+Close[0]);
    		ExitLongLimit(Close[0]);
    	}	
    			
    				
    	// Enter short position on down trend
    	if (Close[1] < Open[1])
    	{
    		Draw.ArrowDown(this,CurrentBars[0].ToString(),false,0,(High[0] + (380 * TickSize)), Brushes.Red);
    		Print(ToDay(Time[0])+" - 093000 - Entering short position at Open: "+Open[0]);
    		EnterShortLimit(1, Open[0]);
    				
    		Print(ToDay(Time[0])+" - 160000 - Leaving short position at Close: "+Close[0]);
    		ExitShortLimit(Close[0]);
    	}
    }
    I've included a picture of the strategy running on an IBM daily bar chart and my output window showing the Open and Close prices on each bar. You can see the arrows point to the position I need to take on each bar are correct, but the position entries and exits are erratic. The strategy should have an entry and exit on each bar.
    Attached Files
    Last edited by bradcd; 02-16-2018, 12:42 PM.

    #2
    Hello bradcd,

    Thanks for writing in.

    You are using the barsAgo reference appropriately to compare the open and close for the previous day, but there are some other items that should be noted to achieve your goal.

    Follow the day to day trend by reading yesterday's Open compared to yesterday's Close, enter position (long or short) on CurrentBar Open and close out position on CurrentBar Close.
    Since we are using a daily series and using end of day data, we cannot have intrabar actions to enter and exit within that same bar. To attain intrabar actions, we would need to have more data and would want to use Calculate.OnEachTick. IsFirstTickOfBar can tell you when the the bar closes. The tick that closes a bar would be the open of the open of the next bar.

    Code:
    if (IsFirstTickOfBar)
    	BarThatJustClosed = Close[1];
    CurrentTick = Close[0];
    Another approach would be to use a Multi Series NinjaScript for intrabar actions. If you would like to take such an approach, please refer to the documentation on Multi Time Frame and Instruments.

    I may also suggest to download Market Replay data or use a demo account to get the intra day data needed.

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

    IsFirstTickOfBar - https://ninjatrader.com/support/help...ttickofbar.htm

    Multi Series NinjaScript reference - https://ninjatrader.com/support/help...nstruments.htm

    Playback Connection (Market Replay data) - https://ninjatrader.com/support/help...s/set_up12.htm

    Please let me know if I may be of further assistance.
    JimNinjaTrader Customer Service

    Comment


      #3
      Jim,

      Thank you for your help. I'm now working on only 1 Minute charts displayed as Daily bars. I was able to use your example of the IsFirstTickOfBar to enter my conditional position correctly, but the exit positions still seem to execute incorrectly. It looks like maybe I'm not using your example logic right. Here is my code now.


      Code:
      private double BarThatJustClosed;
      private double CurrentTick;
      private int positionFlag;
      
      OnStateChange(){
      ...
      Calculate = Calculate.OnEachTick;
      EntriesPerDirection = 1;
      TimeInForce = TimeInForce.Day;
      BarsRequiredToTrade = 1;
      ...
      }

      Code:
      OnBarUpdate(){
      
      positionFlag = 0;
      			
      // Must have at least 1 previous bar to start
      if (CurrentBar < 1)
      return;
      			
      // Marks start of bar's first tick
      if (IsFirstTickOfBar)
      	BarThatJustClosed = Close[1];
      CurrentTick = Close[0];
      			
      			
      // Enter long position on up trend
      if (Close[1] >= Open[1]){
      	Draw.ArrowUp(this,CurrentBars[0].ToString(),false,0,(Low[0] + (-380 * TickSize)), Brushes.Lime);
      				
      	if (IsFirstTickOfBar) //if (ToTime(Time[0]) == ToTime(09, 30, 0)){
      		Print(ToDay(Time[0])+" - 093000 - Entering long position at Open: "+Open[0]);
      		EnterLong();
      	}
      				
      	positionFlag = 1;
      }
      					
      // Enter short position on down trend
      if (Close[1] < Open[1]){
      	Draw.ArrowDown(this,CurrentBars[0].ToString(),false,0,(High[0] + (380 * TickSize)), Brushes.Red);
      				
      	if (IsFirstTickOfBar){
      		Print(ToDay(Time[0])+" - 093000 - Entering short position at Open: "+Open[0]);
      		EnterShort();
      	}
      				
      	positionFlag = 2;
      }
      
      // Exit either position		
      if (ToTime(Time[0]) >= ToTime(16, 0, 0)){
      	if (positionFlag == 1){
      		Print(ToDay(Time[0])+" - 160000 - Leaving long position at Close: "+Close[0]);
      		ExitLong();
      	}
      	
              if (positionFlag == 2){
      		Print(ToDay(Time[0])+" - 160000 - Leaving short position at Close: "+Close[0]);
      		ExitShort();
      	}
      }
      }

      That chart is of the minute data for ESA Index that I have imported manually. The data consists of {yyyyMMdd HHmmss, open, high, low, close, volume}. Here's a piece of it. I don't believe it to be the fault in this strategy but maybe it is.

      .....
      20180207 092900;2686.5;2688;2684.5;2685;6954
      20180207 093000;2685;2688.25;2680.75;2687.25;17012
      20180207 093100;2687;2687;2681.25;2682.5;7763
      .....
      20180207 155900;2684;2684.25;2678.25;2680;74352
      20180207 160000;2680;2680.5;2677.75;2678.75;25614
      20180207 160100;2678.75;2679;2677;2677.5;8685
      Attached Files
      Last edited by bradcd; 02-20-2018, 11:22 AM.

      Comment


        #4
        Hello bradcd,

        The prints you have entered should give you a hint about what the issue is. I would suspect that you are not hitting your exit methods, and this can be observed if you aren't seeing prints for your exits in the output window.

        In OnBarUpdate(), you set a variable "positionFlag" to 0 that represents your position, and then you change this value after you submit an entry order. I would not recommend this approach as setting a variable on order submission does not mean you have entered the position. Additionally, if you set this variable to 0 on each OnBarUpdate() your logic will never reflect a long or short position.

        I would recommend to use the Position.MarketPosition object to determine if you are long, short or flat to control your entries and exits.

        Position.MarketPosition - https://ninjatrader.com/support/help...etposition.htm

        If you have any additional questions, please don't hesitate to write back.
        JimNinjaTrader Customer Service

        Comment


          #5
          Thanks for the quick reply. Here is an example of my output window with the Prints of the above code. It looks like it's reaching the logic and should execute as I intend.

          ....
          20180202 - 093000 - Entering long position at Open: 2807.25
          20180202 - 160000 - Leaving long position at Close: 2760.75
          20180205 - 093000 - Entering short position at Open: 2735.25
          20180205 - 160000 - Leaving short position at Close: 2642
          20180206 - 093000 - Entering short position at Open: 2589.5
          20180206 - 160000 - Leaving short position at Close: 2696.5
          20180207 - 093000 - Entering long position at Open: 2687
          20180207 - 160000 - Leaving long position at Close: 2678.75
          20180208 - 093000 - Entering short position at Open: 2680.75
          20180208 - 160000 - Leaving short position at Close: 2580.75

          I'll investigate the Position.MarketPosition method and try to modify around that. Thank you for the help.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by RookieTrader, Today, 09:37 AM
          1 response
          6 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by alifarahani, Today, 09:40 AM
          0 responses
          2 views
          0 likes
          Last Post alifarahani  
          Started by Gerik, Today, 09:40 AM
          0 responses
          1 view
          0 likes
          Last Post Gerik
          by Gerik
           
          Started by KennyK, 05-29-2017, 02:02 AM
          3 responses
          1,282 views
          0 likes
          Last Post NinjaTrader_Clayton  
          Started by AttiM, 02-14-2024, 05:20 PM
          11 responses
          184 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Working...
          X