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

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.​
    Brandon H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by burtoninlondon, Today, 12:38 AM
    0 responses
    10 views
    0 likes
    Last Post burtoninlondon  
    Started by AaronKoRn, Yesterday, 09:49 PM
    0 responses
    14 views
    0 likes
    Last Post AaronKoRn  
    Started by carnitron, Yesterday, 08:42 PM
    0 responses
    11 views
    0 likes
    Last Post carnitron  
    Started by strategist007, Yesterday, 07:51 PM
    0 responses
    14 views
    0 likes
    Last Post strategist007  
    Started by StockTrader88, 03-06-2021, 08:58 AM
    44 responses
    3,983 views
    3 likes
    Last Post jhudas88  
    Working...
    X