how to use the same variable in different timeframes

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #239329 quote
    Vicke1990
    Participant
    New

    Hi fellow traders and coders,

    I’m havning an issue where I get an error message when trying to use a the same variable in different timeframes,

    Example:

    A variable cannot be assigned in multiple different timeframes, you need to use a different variable. Timeframe 3 minutes, line 1, variable “Size” is alredy assigned in timeframe 15 minutes, updateonclose.

    Note: the same variable may be read in different timeframes by the code editor as long as it is only assigned in one timeframe.

    Is there an easy way to accomplish this? Like with an array or something?

    Example of code I would like to be able to use in different timeframe:

    
    IF OnMarket AND Not OnMarket[1] THEN
       EntryLevel     = Tradeprice
       BreakEvenLevel = EntryLevel + (PointsToKeep * pipsize)
    ENDIF
    IF abs(CountOfPosition) < abs(CountOfPosition)[1] then
       SET STOP PRICE BreakEvenLevel
    ENDIF

    Thank you 🙂

    #239333 quote
    druby
    Participant
    New

    In general variables have to have a unique name, and this crops up when using TF and using same variables names.

    I usually post fix the timeframe value to the variable , e.g.   size15m, size3m, to differentiate between them.

    You would have to reference them by their names.

    Alternatively you could bring variables out the timeframes and do calculation in default TF if suitable.

    If you put values in array you could reference by a single name and the index value which you could have as another variable.

    But this would have to be done in default timeframe area I think.

    timeframe(15mn,updateonclose)
    size15m = 15
    
    timeframe(3mn,updateonclose)
    size3m = 3
    
    timeframe(default)
    size1m = 10
    
    
    $size[0]=size1m
    $size[1]=size3m
    $size[2]=size15m
    
    if minute >= 3 and minute < 15 then
    idx = 1
    elsif minute >= 15 and minute < 30 then
    idx = 2
    else
    idx = 0
    endif
    
    size = $size[idx]
    
    graph(size15m)coloured("red",60) 
    graph(size3m)coloured("lime",60)
    graph(size)
    
    
    BUY -1 CONTRACTS AT MARKET
    
    
    robertogozzi and Iván González thanked this post
    #239343 quote
    Iván González
    Moderator
    Master
    So you can see a simple example of how using arrays does not work, even though it does not give you an error.
    timeframe(30mn)
    if rsi[14](close) crosses over 50 then
    $rsi[max(0,lastset($rsi))+1]=lastset($rsi)+1
    drawarrowdown(barindex,max(0,lastset($rsi)))
    endif
    
    
    timeframe(5mn)
    if rsi[14](close) crosses over 50 then
    $rsi[max(0,lastset($rsi))+1]=lastset($rsi)+1
    drawarrowup(barindex,max(0,lastset($rsi)))coloured("red")
    endif
    
    return $rsi[max(0,lastset($rsi))]
    #239345 quote
    druby
    Participant
    New
    Never tried using different elements,  of the same array, in different timeframes. Did a few tests in back-test and it appears that array elements are treated as different variables names, probably due to their index. I don’t know if there will be any hidden surprises, especially when running a strategy. But if solution holds up, then same variable  name used in different timeframes…ish  
    timeframe(15mn,updateonclose)
    $size[2]=15
    timeframe(3mn,updateonclose)
    $size[1]=3
    timeframe(default)
    $size[0]=10
    
    
    if minute >= 3 and minute < 15 then
    idx = 1
    elsif minute >= 15 and minute < 30 then
    idx = 2
    else
    idx = 0
    endif
    
    size = $size[idx]
    
    graph($size[2])coloured("red",60)
    graph($size[1])coloured("lime",60)
    graph($size[0])coloured("yellow",60)
    graph(size)
    
    
    BUY -1 CONTRACTS AT MARKET
    #239387 quote
    Vicke1990
    Participant
    New
    Thank you for your answer! If I understand correctly, there is no way to use the same code in different timeframes? The code I want to use is this one in a 15 min timeframe (Entry of position)
    IF OnMarket AND Not OnMarket[1] THEN
       EntryLevel     = Tradeprice
       BreakEvenLevel = EntryLevel + (PointsToKeep * pipsize)
    ENDIF
    IF abs(CountOfPosition) < abs(CountOfPosition)[1] then
       SET STOP PRICE BreakEvenLevel
    ENDIF
    Or is there a way to reference the code above in 3 minutes when the code and buying of position take place in a 15-minute timeframe? More specifically to Entrylevel and traderprice variables. If not, is it possible to reference to the 1st minute of a 15 minute bar? Found
    DClose(0) 
    yesterday, is there a similar one but for minutes? My goal, and probably the best solution would be if it would be possible to reference to the 1st minute of a 15 min bar. Is this possible? Thank you!
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.

how to use the same variable in different timeframes


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Vicke1990 @vicke1990 Participant
Summary

This topic contains 4 replies,
has 3 voices, and was last updated by Vicke1990
1 year, 3 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 10/22/2024
Status: Active
Attachments: No files
Logo Logo
Loading...