I am wondering how to put down two things in code:
1: Go short if today’s close is a new 4-day high
2. Close the short position if today’s close is a new 4-day low
Help would be much appreciated!
DEFPARAM PRELOADBARS = 100
DEFPARAM Cumulateorders = False
s1 = (close = highest[4](close))
exit1 = (close = lowest[4](close))
size = 1
If s1 THEN
sellshort size contracts at market
ENDIF
If exit1 and shortonmarket then
exitshort at market
ENDIF
Hope this should help
Go short if today’s close is a new 4-day high
Bit difficult to be 100% certain what you mean by this. Do you mean that today’s close has to be higher than the last three closes (which is what Harrys has coded) or do you mean that today’s close has to be higher than the highest high of the last three candles?
For the latter your conditions would need to be:
s1 = close > highest[3](high[1])
exit1 = close < lowest[3](low[1])
Ok good question, maybe I didn’t specify it well enough.
I want today’s close to be higher than the last 3 days’ closes so I guess Harrys code is what I want.
But why is there equal signs and not ‘<‘ and ‘>’ signs?
Try this version: 😉
s1 = close > highest[3](close[1])
exit1 = close < lowest[3](close[1])
s1 = close is higher than the higher close of the last 3 days
exit1 = close is lower than the lower close of the last 3 days
Harry’s code (lines 4-5) does its job correctly, current close MUST be >= than the previous 3 candles.
Vonasi’s code does the same job with a slightly different approach.
I can’t edit from my mobile.
Vonasi’s code does something different.
Nicolas’code has just a slightly different approach.
Harrys code does the job by obtaining the value of the highest close in the last four candles and comparing it to the current close. It assumes that if they are the same value then this close must be the highest. It does not however allow for the fact that one or more of the closes could have identical values in which case your entry criteria is not strictly being met. Nicolas’ version and my version do allow for this possibility. Nicolas’ does it with the last three closes which is what you want whereas my version just does the same thing with the last three highs.
Thank you guys for the help and you Vonasi for the explanation.