Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Entry and Exit Timeframe Issues

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

    Entry and Exit Timeframe Issues

    Hello,

    I'm attempting to create a strategy that holds the position for 1 day (1 bar since I'm on the daily timeframe). More specifically, I want to hold a long position all day on Tuesday if Monday is red -- from market open to market close. I'm able to get the entry to work, but the exit seems to be an issue for the current state of my code. Are there any functions along the lines of "BuyAtOpen", "ExitOnClose", or "SetExitOnClose". Perhaps I'm simply thinking about the problem incorrectly. Below is the code I have thus far. Thanks for the help!


    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;

    // Set 1
    if ((Times[0][0].DayOfWeek == DayOfWeek.Monday) && (Close[0] < Open[0]))
    EnterLong(1, @"TuesdayLong");
    if (BarsSinceEntryExecution() == 0)
    ExitLong();

    }

    It appears as if my final line of code is delaying the exit to the opening of the final day. I'm assuming there is an easy fix to this I'm overlooking.​






    #2
    If these are DAILY bars, then you can't enter at the open of Tuesday and exit at the close of Tuesday in this way. You would need to use intraday bars or a tick replay to do this on an intraday basis because when this code is running in a back-test on a DAILY bar, and it knows Monday's a down day, it can place orders to be executed at the OPEN of Tuesday if they're a market order, but it can't place an order to be simulated at the close without intraday data.
    Bruce DeVault
    QuantKey Trading Vendor Services
    NinjaTrader Ecosystem Vendor - QuantKey

    Comment


      #3
      Thanks Bruce!

      If I'm forced to go the intraday route, how would I evaluate the daily metrics I care about. For example, how would I know monday as a whole is a red day? How would I write code to enter at open on the first tuesday bar. Thanks!




      Comment


        #4
        Have you looked at the current day OHLC indicator? it does this same thing (tells you what the daily bar is like during the day on an intraday chart).
        Bruce DeVault
        QuantKey Trading Vendor Services
        NinjaTrader Ecosystem Vendor - QuantKey

        Comment


          #5
          I havent used the OHLC indicator -- Can you point me in the direction of an example usage?




          Comment


            #6
            Hi mkohrs14, thanks for posting. The script would need to evaluate

            if ((Times[0][0].DayOfWeek == DayOfWeek.Tuesday) && (PriorDayOHLC().PriorClose[0] < PriorDayOHLC().PriorOpen[0]))
            {
            Print(Times[0][0].DayOfWeek);
            Print("Prior day is a red bar");
            }


            You can also use Print to print out this data if you are unsure of the data your strategy is processing, using Print will be the best way to debug and make adjustments to code you have written.




            Kind regards,
            -ChrisL​

            Comment


              #7
              Thanks Chris!

              I'm noticing a common issue I'm having with my overall methodology. I think the following would clear up a lot for me. What would the logic look like to simply buy and hold for 1 bar (regardless of timeframe)?

              The condition that triggers the buy is simple, but the exit seems to be causing me problems.

              I appreciate the assistance.




              Comment


                #8
                Hi mkohrs14, You can do this by keeping track of the CurrentBar value e.g.

                Code:
                private int savedBar = -1;
                
                OnBarUpdate()
                {
                    if(CurrentBar == savedBar+1 && Position.MarketPosition == MarketPosition.Long)
                    {
                        ExitLong();
                        savedBar = -1;
                    }
                
                    if(<entry condition>)
                    {
                        savedBar = CurrentBar;
                        EnterLong();
                    }
                }

                Comment


                  #9
                  I appreciate the help, Chris!

                  Is there a way to exit on the bar's close rather than the next bar's open?




                  Comment


                    #10
                    Hi mkohrs14, that would add on a new level of complexity because then you would need to run the script OnEachTick and use some way to determine how close to the end of the bar you are willing to close the position at. This can vary based on the bar type you are using e.g. for time based bars it would be quite straight forward, but for Tick based bar types the end of the bar is a little more uncertain. See also the PercentComplete property of the bar:

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by NullPointStrategies, Yesterday, 05:17 AM
                    0 responses
                    64 views
                    0 likes
                    Last Post NullPointStrategies  
                    Started by argusthome, 03-08-2026, 10:06 AM
                    0 responses
                    139 views
                    0 likes
                    Last Post argusthome  
                    Started by NabilKhattabi, 03-06-2026, 11:18 AM
                    0 responses
                    75 views
                    0 likes
                    Last Post NabilKhattabi  
                    Started by Deep42, 03-06-2026, 12:28 AM
                    0 responses
                    45 views
                    0 likes
                    Last Post Deep42
                    by Deep42
                     
                    Started by TheRealMorford, 03-05-2026, 06:15 PM
                    0 responses
                    50 views
                    0 likes
                    Last Post TheRealMorford  
                    Working...
                    X