Daily Returns on lower tf

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #215755 quote
    ashehzi
    Participant
    Average

    Hi,

    I am using 1h timeframe for the chart but using daily timeframe to calculate daily returns e.g.

    On 1h chart

    timeframe(daily, updateonClose)
    DailyVolatility = STD[252](close)
    timeframe(default)

    but the results are not consistent.

    My objective is the following:

    I want to calculate daily volatility of an instrument based on last 252 days but running in lower timeframe as I need candle closures at specific times of the day which I cannot do on daily TF. Anyway even If i run above code on daily timeframe, the result seems to be wrong.

    From excel, I’ve the following:

    1. Get daily closes for last 252 days/candles ==> DailyClosesArray
    2. Get daily returns e.g. Close(i+1)/Close(i) – 1 ==> DailyReturnsArray
    3. DailyVolatility = stddev (DailyReturnsArray)

    Initially I thought If I just do above mentioned PRT code, I can achieve same but it seems to be not working. Do you’ve any suggestion/solution in mind please?

    Thanks and really appreciated.

    Ash

    Capture.png Capture.png
    #215759 quote
    JS
    Participant
    Senior

    Hi,

    DailyVolatility = Std[252](Close) is the standard deviation over the last 252 days (TF Daily) so you calculate the standard deviation over a time period of a (trade) year.

    The daily returns can be calculated with the daily time frame:

    DailyReturn = (Close – Close[1]) is the difference in “Close” between today and yesterday (in points)

    DailyReturn% = (Close-Close[1])/Close[1]*100  (DailyReturn in %)

    The standard deviation of the “DailyReturn” over the last 252 trading days:

    DailyReturnVolatility = Std[252](DailyReturn%)

    TimeFrame(Daily, UpdateOnClose)
    DailyReturn = (Close-Close[1])/Close[1]*100 //DailyReturn in %
    DailyReturnVolatility = Std[252](DailyReturn) //Standaard deviation of the DailyReturn based on 252 trading days
    
    Return DailyReturn as "DailyReturn", DailyReturnVolatility as "DailyReturnVolatility"
    ashehzi thanked this post
    #215789 quote
    ashehzi
    Participant
    Average
    Thanks. But the problem is how can I get accurate values on 1h timeframe. It never computes properly. The chart attached is 1h while the bottom shows the DailyReturnVolt indicator using below code:
    TimeFrame(Daily, UpdateOnClose)
    DailyReturn = (Close-Close[1])/Close[1]*100 //DailyReturn in %
    DailyReturnVolatility = Std[252](DailyReturn) //Standaard deviation of the DailyReturn based on 252 trading days
     
    Return DailyReturn as "DailyReturn", DailyReturnVolatility as "DailyReturnVolatility"
    
    Now If I change chart interval to daily, it returns the values which is understandable but this isn’t what I want, I want same result on lower tf.
    NASDAQ-1-hour-2023_06_07-18h19.png NASDAQ-1-hour-2023_06_07-18h19.png NASDAQ-Daily-2023_06_07-18h21.png NASDAQ-Daily-2023_06_07-18h21.png
    #215793 quote
    robertogozzi
    Moderator
    Master
    Since one day is 24 hours, 252 trading days require at least 252*24 (i.e. over 6K) 1-hour units on the chart. If you have chosen less than those required, result may not be as inaccurate.
    #215829 quote
    ashehzi
    Participant
    Average
    Please ignore the attachments as they got posted. I know that I need to have these many not only on the chart but I also use “defparam CALCULATEONLASTBARS=8000” but still I’m unable to get the results and that was the reason of my earlier post.
    NASDAQ-1-hour-2023_06_07-18h19-1.png NASDAQ-1-hour-2023_06_07-18h19-1.png NASDAQ-Daily-2023_06_07-18h21-1.png NASDAQ-Daily-2023_06_07-18h21-1.png
    #215832 quote
    ashehzi
    Participant
    Average
    see attachment where I’ve highlighted the 10K units and also the code below for the indicator    
    defparam drawonlastbaronly=true
    defparam CALCULATEONLASTBARS=8000
    
    TimeFrame(Daily, UpdateOnClose)
    DailyReturn = (Close-Close[1])/Close[1]*100 //DailyReturn in %
    DailyReturnVolatility = Std[252](DailyReturn) //Standaard deviation of the DailyReturn based on 252 trading days
    TimeFrame(Default)
    
    Return DailyReturn as "DailyReturn", DailyReturnVolatility as "DailyReturnVolatility"
    
    NAS_1h_10kUnits.png NAS_1h_10kUnits.png
    #215836 quote
    Nicolas
    Keymaster
    Master
    While it works properly on DAX 1-hour timeframe, it is not on NASDAQ with “updateonclose” but do without it. Here is the code i’m using:
    TimeFrame(Daily)
    DailyReturn = (Close-Close[1])/Close[1]*100 //DailyReturn in %
    DailyReturnVolatility = Std[252](DailyReturn) //Standaard deviation of the DailyReturn based on 252 trading days
    
    Return DailyReturn as "DailyReturn", DailyReturnVolatility as "DailyReturnVolatility"
    #215840 quote
    ashehzi
    Participant
    Average
    I’m ok to use updateonClose as it is returning some values now e.g. My DailyReturnsVolt indicator is like below:
    defparam CALCULATEONLASTBARS=10000
    
    TimeFrame(Daily, UpdateOnClose)
    YesterDailyReturn = (Close[1]-Close[2])/Close[2]*100 //DailyReturn in %
    DailyReturnVolatility = Std[252](YesterDailyReturn) //Standaard deviation of the DailyReturn based on 252 trading days
    TimeFrame(Default)
    
    Return YesterDailyReturn as "DailyReturn", DailyReturnVolatility as "DailyReturnVolatility"
    I tried above code in my automated trading test and it gives error on line 5 (which is the “TimeFrame(Daily, UpdateOnClose)”) line from the above indicator. Below is a simple test and it fails.
    // Conditions to enter long positions
    myDailyReturn, myDailyReturnVolatility = CALL "DailyReturnsVolt"[0, 2] 
    
    if time = 220000 then
    cashclose = close
    endif
    
    myLong1 = cashclose * (1 - myDailyReturnVolatility/4/100)
    
    IF NOT LongOnMarket then // AND YourConditions THEN
    BUY 1 CONTRACTS AT myLong1 limit
    SET STOP LOSS 40
    SET TARGET PROFIT 60
    ENDIF
    Above fails with parsing error on line 5 (basically timeframe(daily…)) from the indicator. See attached.
    ParsingErrorAuto.png ParsingErrorAuto.png
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.

Daily Returns on lower tf


Platform Support: Charts, Data & Broker Setup

New Reply
Author
author-avatar
ashehzi @ashehzi Participant
Summary

This topic contains 7 replies,
has 4 voices, and was last updated by ashehzi
2 years, 8 months ago.

Topic Details
Forum: Platform Support: Charts, Data & Broker Setup
Language: English
Started: 06/07/2023
Status: Active
Attachments: 7 files
Logo Logo
Loading...