i like to screen shares and to sort those screened out by max draw down. but i fail to write the code as i have no idea how to realise the conditions as described below:
maxdrawdown = (highest[…](close) – lowest[…](close)) / ( highest[…](close) * 100
Conditions:
- the lowest…close must be occured later than the the highest…close – it shall represent the max. loss risk if the share was bought on highest close in that period
- as soon as a new highest…close is reached after a past lowest…close, this becames valid for the maxdrawdown calculation in combination with the lowest…close occured after this new high was reached etc.
The criteria for sorting shall be the maxdrawdown occured during the last 254 days or 254 weeks.
I would appreciate your support.
Thanks in advance.
Roger
Ok, not so easy at first glance.. let me think of it so I can give a try to find you a relevant code.
So basically, you want the max spread between the overall max price and minimum price occurred in the last 254 days? Or do you want to use periods to limit the found highest and lowest prices?
I attached an illustration with some examples.
What I am looking for is a sorting criteria – i. e. max. DrawDown in the last year, max. Drawdown in the last month etc.
This is not the period just to calculate highest[period](price) minus lowest[Period](price). In this period serveral DrawDowns may occur – and I just want for each share the biggest / smallest as sorting criteria.
If its not getting clearer please let me know – than I try to explain it better.
Visually, your chart is clear and I’ve understood what you are trying to get, but how do you find tops and bottoms? Basically what are the green and red arrows?
Looking on the chart I just recognise that it is visually clear to me, but I doesn’t believe, that there could be any mathematical description for it.
So i try to simplify:
Top = highest[Period](high)
Low = lowest … (low) – AFTER the top AND before the next top = between current and next top.
Any idea if this could be realised?
You can give a try with this screener:
period = 50
top = highest[Period](close)
if top<>top[1] then
topbar=barindex
bot=1000000
endif
bot = min(bot,close)
if topbar>0 then
maxspread=max(maxspread,(top-bot)/top*100)
endif
test = maxspread>0
screener[test](maxspread)
I used Close instead of high, because it was how you explained it the first time, you can change the price serie if you want to.
And an indicator to confirm the calculation and see how and when the max spread between highest high and the low was found:
defparam calculateonlastbars=254
period = 50
top = highest[Period](close)
if top<>top[1] then
topbar=barindex
bot=1000000
endif
bot = min(bot,close)
if topbar>0 then
maxspread=max(maxspread,(top-bot)/top*100)
endif
return maxspread
Great – the indicator does exactly what I intended.
But not so the screener. The screener just takes into account the “last” maxspread fromm current high to current low. The previous highs / lows are not taken into account.
The screener does the same, always saving the highest maxspread, please verify on stocks that you have a live data subscription (not “End Of Day”). Bear in mind that ProScreener only have 254 bars of history available.