Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Advice on coding - Daily strategy that can trigger intra-day

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

    Advice on coding - Daily strategy that can trigger intra-day

    Hoping to ask for some advice please on how to setup a strategy...

    I'm looking to trade a strategy which has logic similar to the following: For a basket of stocks, lets say 100 stocks, if Price for an individual stock breaks the High of the last 52 weeks, and the last 3 days have all been green candles (e.g. Close[1] > Open[1], Close[2] > Open[2], then go long immediately - without waiting for the full days bar to complete.

    To get the high of the last 52weeks I need to use Daily data and I generally choose a date range of the last 18 months. I've got this setup and all working fine - backtests run in ~60 seconds.

    The problem I'm facing is running this in real-time. If I choose OnBarClose then I will obviously be delayed in entering a trade until the next day (as the current bar being built that breaks the 52wk High isn't closed until the next day).

    If I choose OnPriceChange, then when I'm running the strategy in real-time then suddenly my last 3 days rules that work in backtesting fail in realtime, because Close[1] > Open[1], Close[2] > Open[2] no longer represent days but seconds/minutes as the bars are formed by price changes.

    I can't add an additional timeframe of minutes because backtesting and live running both requires 18 months worth of data to be processed, which is fine for daily data but not for minute data.

    I'm outlining below what I think is the logical approach to take, but hoping to ask for any thoughts/insights/opinions on whether there is a better way of solving the problem:
    *Continue to use Daily data only and OnPriceChange.
    *Break my code into 'Real-time' & 'Historical'
    *On Historical, on the last full bar store the 52wk high in a double variable, and have a bool true/false variable for whether the prior 3 days have all been green candles.
    *On Real-Time, remove any indexed bar references like Open[2] and only check if Close[0] > the 52wk High variable and whether the 3 day green candle bool is true/false

    Q1: Do you think this is the optimal approach to take?
    Q2: Regarding how to do the real-time vs historical split. I'm imagining kicking this off at say 9.15am each day and worry that some sub-daily bars coming through pre-market for the current day might get confused as historical and mess up the 3 day green candle bool, so do I need to add in a date rule each day in my code to do the 'Real-time' & 'Historical' split (E.g. if (Time[0].ToString("mmddyyyy" == 11252022) //realtime code) to ensure the 3 day green candle bool is captured correctly?

    Many thanks in advance for any help or guidance on this.

    ChainsawDR​

    #2
    Hello ChainsawDR,

    Thanks for your post.

    The approach you mentioned does sound like a possible option.

    That said, you could consider separating logic in your script between Calculate.OnBarClose and Calculate.OnPriceChange. This way, you could process some logic and do some actions on bar close and process other logic and actions on price change.

    Logic can be separated between Calculate.OnEachTick and Calculate.OnBarClose using IsFirstTickOfBar. Please note that a hosted script will inherit the Calculate mode of the script that hosts it. You can take the following approach to differentiate logic between OnBarClose and OnEachTick processing. The difference in your script would be using OnPriceChange where OnEachTick is used instead.

    Please see this reference sample which demonstrates a technique used for those who need to separate their logic to calculate some values OnEachTick/OnPriceChange and others only on the close of a bar. You will set your host script to Calculate.OnEachTick/OnPriceChange and use if(IsFirstTickOfBar) and place all code that needs to calculate once every bar within that condition check. Then place all code that you want to calculate OnEachTick/OnPriceChange outside of the IsFirstTickOfBar condition check.

    SampleEnterOnceExitEveryTick -https://ninjatrader.com/support/help...either_cal.htm

    Please let us know if we may assist further.​
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    54 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    131 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    73 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    44 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    49 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X