I’m trying to get a strategy to decide on weekly candles to close a position but then not actually close it until the market opens the following Monday but I get this error:
[attachment file=76601]
This is the code:
timeframe(weekly,updateonclose)
if onmarket and (exit conditions) then
sellflag = 1
endif
timeframe(default)
if sellflag = 1 and time = 143000 and dayofweek = 1 then
sell at market
sellflag = 0
endif
It seems a bit odd to not be able to set a variable in one time frame and then use it in another. At the moment I cannot find a solution which makes the MTF rather limited for improved exit times. Maybe I am missing something obvious – any ideas?
Line 7 is fine since you are simply using it, while line 9 ASSIGNs a value to a variable first used in a different TF, which is not allowed!
I think I found the solution:
timeframe(weekly,updateonclose)
c5 = (my exit conditions)
timeframe(default)
if c5 and time = 143000 and dayofweek = 1 then
sell at market
endif
So a condition can be used across time frames but a variable cannot be altered across timeframes.
I wanted to set the variable to 1 in one time frame and then use it to make a decision and then turn it off in another timeframe which is apparently not allowed. Setting a condition apparently is so I will just have to start thinking in a different way about how I code as I tend to like using ‘flags’ on or off. I guess another alternative is that I could have turned it off with:
timeframe(weekly,updateonclose)
if not onmarket then
sellflag = 0
endif
if onmarket and (exit conditions) then
sellflag = 1
endif
timeframe(default)
if sellflag = 1 and time = 143000 and dayofweek = 1 then
sell at market
endif
Same result but more code!
Actually you should not need to bother about setting a flag in a lower TF.
You’d better set a flag from within the LOWER TF when conditions in the HIGHER TF are met, since when, say, a new day start, also a new hour or minute or 4-hour TF starts!
That’s why, I guess, they require TFs to be multiple of the lowest one!
So if you want to set a flag in a hourly TF, just set/clear it on a 1-minute (or 5-minute, whatever) TF and you’ll be able to read it elsewhere, be it daily, weekly….
I fear my brain will need a little re-training. 🙂