Finding a price at a certain time yesterday.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #76679 quote
    robert123robert123
    Participant
    Master

    I am trying to write a code that will find a price at a certain time yesterday…or the day before…etc , does anyone know how this is done?

    TIA

     

    Robert

    #76682 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    You’ll have to use FOR…NEXT (or WHILE…WEND). It’s very time consuming!

    If you are on a hourly chart, to retrieve the closing price of yesterday at 090000 you may write (not tested):

    MyClose = 0
    n = 47          //47 hours back
    i = 1
    WHILE i < n DO
       IF OpenDay[i] <> OpenDay THEN  //when it's yesterday....
          IF TIME[i] = 090000 THEN    //... and it's my desired time...
             MyClose = close[i]       //... save CLOSE and...
             BREAK                    //... exit loop...
          ENDIF
       ENDIF
       i = i + 1                      //... else keep scanning next bar
    WEND

    now MyClose will retain yesterday’s CLOSE price at 090000.

    Of course if you’re on a 1-minute chart the lookback period will greatly increase making it difficult to run!

    #76683 quote
    robert123robert123
    Participant
    Master

    Roberto, thank you for your speedy reply, and help.

    Robert

    #76687 quote
    VonasiVonasi
    Moderator
    Master

    The only other way is to calculate how many bars between the time now and the time yesterday or the day before but this becomes slightly complicated when you go from day 1 to day 7 and when you want to go back more than 24 hours. Knowing the number of bars back you could then simply use close[n] to get the closing price.

    #76688 quote
    robert123robert123
    Participant
    Master

    Vonasi- Thank you

    #76692 quote
    VonasiVonasi
    Moderator
    Master

    Here is an example of what I mean;

    defparam calculateonlastbars = 200
    
    mytime = 120000
    
    n = (24 - (mytime / 10000)) + (opentime / 10000)
    
    a = close[n]
    
    return a as " close at mytime yesterday"
    

    This will tell you what the closing price was at ‘mytime’ yesterday on an hourly chart. Now all you have to do is adapt it for whatever time frame you want to use it on and add in a calculation to know how many bars to add on for the number of extra days back you might want to go. Good luck! 🙂

    Obviously it does not work on Monday or Sunday due to the quantity of bars on Sundays.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

Finding a price at a certain time yesterday.


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
robert123 @robert123 Participant
Summary

This topic contains 5 replies,
has 3 voices, and was last updated by VonasiVonasi
8 years, 1 month ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 07/25/2018
Status: Active
Attachments: No files
ProRealCode ProRealCode
Loading...