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 🙂
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
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))]
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
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!