Buy at fixed values during price declines
Introduction
Starting from the all-time high of a stock, I want to buy with a StopBuy when the price has fallen below the all-time high minus 5% at the end of the day. The StopBuy should be exactly at the all-time high minus this 5%.
The Stoploss should be when the daily close is below the all-time high minus 10%.
The takeprofit should be at the daily close above the all-time high.
Screenshot1
Buy on price declines
If the share falls further below 10% since the all-time high per daily close, I want to buy per StopBuy at all-time high minus 10%. The stoploss should be at all-time high minus 15%, the takeprofit at all-time high minus 5%.
If the share falls below 15% from the all-time high per day close, I want to buy with StopBuy at all-time high minus 15%. Stoploss at minus 20%, takeprofit at 15%.
screenshot2
If the share continues to fall
Is bought each time after a multiple of 5% (or n%) by StopBuy, SL TP are -n%. or +n%.
The share recovers.
Will continue to buy by stopbuy at a multiple of n%. SL and TP remain at this multiple -n% and +n% respectively.
If the share returns to the all-time high after the close of the day, it is bought by StopBuy at the all-time high. SL TP again the corresponding -n% +n%.
screenshot3
If a new all-time high is created, the old all-time high is replaced by the new all-time high.
Can someone press this into a code? I’m sorry, I completely lack the means.
But it smells like good backtest results, especially with the ever-rising indices like Dow and S&P.
Can you help?
Translated with http://www.DeepL.com/Translator (free version)
Correction and Screensot 2
Buy at fixed values during price declines
Introduction
Starting from the all-time high of a stock, I want to buy with a StopBuy when the price has fallen below the all-time high minus 5% at the end of the day. The StopBuy should be exactly at the all time high minus this 5%.
The Stoploss should be when the daily close is below the all-time high minus 10%.
The takeprofit should be when the daily close is above the all-time high.
Screenshot1
Buy on price declines
If the share price falls further below 10% since the all-time high per daily close, I want to buy with StopBuy at all-time high minus 10%. The stoploss should be at all time high minus 15%, the takeprofit at all time high minus 5%.
If the share falls below 15% from the all-time high at the end of the day, I want to buy with StopBuy at the all-time high minus 15%. Stoploss at minus 20%, takeprofit at 10%. (here the correkt 10 instead 15)
screenshot2
If the share continues to fall
Is bought each time after a multiple of 5% (or n%) by StopBuy, SL TP are -n%. or +n%.
The share recovers.
Will continue to buy by stopbuy at a multiple of n%. SL and TP remain at this multiple -n% and +n% respectively.
If the share returns to the all-time high after the close of the day, is bought by StopBuy at the all-time high. SL TP again the corresponding -n% +n%.
screenshot3
If a new all-time high is created, the old all-time high is replaced by the new all-time high.
Can someone squeeze this into a code? I’m sorry, I’m completely out of resources.
However, it smells like good backtest results especially with the ever-rising indices like Dow and S&P.
Can you help?
Translated with http://www.DeepL.com/Translator (free version)
There you go:
DEFPARAM CumulateOrders = false
DEFPARAM PreLoadBars = 10000
ONCE LotSize = 1
ONCE PerCent5 = 0.05 //5%
HIGHESTprice = highest[10000](high[1]) //get the highest price as far as can be
CurDistance = HIGHESTprice - close
ChunkSize = HIGHESTprice * PerCent5
Chunks = round((CurDistance / ChunkSize) - 0.5)
Entry = HIGHESTprice - (HIGHESTprice * (Chunks * PerCent5))
c1 = Chunks > 0
c2 = Not OnMarket
IF c1 AND c2 THEN
BUY LotSize Contracts AT Entry STOP
SET STOP LOSS ChunkSize
ENDIF
IF OnMarket AND close > HIGHESTprice THEN
SELL AT Market
ENDIF
I made it so that it does not accumulate positions as I am not sure you wanted this.
Do you want to accumulate positions?
Thank you very much for the quick help. I am glad that you help me.
But unfortunately the code does not do exactly what I want to get.
For a better understanding let me underlay it with numbers
The all time high of a fictitious value is 10.000 points.
We take n=2% of 10.000 = 200 points
so we get
10.000 // alltimehigh = Highest [1000](high[1])
9.800 // alltimehigh – 2%
9.600 // alltimehigh – 4%
9.400 // ..
9.200 //..
Now it can start
If the price falls below 9,800 at the close, we will
StopBuy at 9,800
SL 9.600
TP 10,000
If the price falls below 9,600 at the close, we will
Stop Buy 9,600
SL 9,400
TP 9,800
If the price falls below 9,400 at the close, we will
Stop Buy 9.400
SL 9.200
TP 9.600
if the price rises above 9,400 at the close, the
StopBuy to 9.600
SL to 9.400
TP to 9.800
For it as above it would have to be programmed.
And I have made it so, where I have problems. Please can you look over it again and correct?
DEFPARAM CumulateOrders = false
DEFPARAM PreLoadBars = 10000
ONCE LotSize = 1
ONCE PerCent = 0.02
ath = highest[10000](high[1])
for all n = 1 to 100 do // every n and again and again // how can i program this?
Entry = close < ath - (ath*percent*n)
IF close < ath or Entry THEN
BUY AT Entry STOP
ENDIF
Exit = (close > ath - (ath*percent*n+1)) or (close < ath - (ath*percent*n-1))
IF OnMarket AND Exit THEN
SELL AT Market
ENDIF
better again, one problem left
DEFPARAM CumulateOrders = false
DEFPARAM PreLoadBars = 10000
ONCE LotSize = 1
ONCE PerCent = 0.02
ath = highest[10000](high[1])
for all n = 1 to 100 do //n = 1 or 2 or 3 or 4 or 5 or …. or 100 // here ist the problem i can´t
Entry = ath – ath*n*percent
IF close < Entry THEN
BUY AT Entry STOP
ENDIF
Exit = (close > ath – (ath*(n+1)*percent)) or (close < ath - (ath*(n-1)*percent))
IF OnMarket AND Exit THEN
SELL AT Market
ENDIF
If you want to accumulate positions why did you write DEFPARAM CumulateOrders = false?
My code doesn’t accumulate positions.
If you want to accumulate positions the code must be changed.
When you place more than one pending order, all of them could be triggered as DEFPARAM CumulateOrders only works when ProOrder has the control of operations, but once the code is executed and pending orders placed, control is left to the broker until the new candle closes.
In any case I added FOR…NEXT to place ALL pending orders (if the price has dropped 20%, 4 orders will be placed).
But it us useless, as 5%, say on Dax, is about 800 pips and a fear it can hardly trigger one order, as I have never experienced a daily drop of more than that!
DEFPARAM CumulateOrders = false
DEFPARAM PreLoadBars = 10000
ONCE LotSize = 1
ONCE PerCent5 = 0.05 //5%
HIGHESTprice = highest[10000](high[1]) //get the highest price as far as can be
CurDistance = HIGHESTprice - close
ChunkSize = HIGHESTprice * PerCent5
Chunks = round((CurDistance / ChunkSize) - 0.5)
Entry = HIGHESTprice - (HIGHESTprice * (Chunks * PerCent5))
c1 = Chunks > 0
c2 = Not OnMarket
IF c1 AND c2 THEN
EntryPrice = Entry
For i = 1 to Chunks
BUY LotSize Contracts AT EntryPrice STOP
EntryPrice = EntryPrice + ChunkSize
Next
SET STOP LOSS ChunkSize
ENDIF
IF OnMarket AND close > HIGHESTprice THEN
SELL AT Market
ENDIF
I have only today got to look here
You have my great and heartfelt thanks that you have taken my questions again.
The addition of the For…Next function helps me immensely. Thank you very much.
I’ll try to do the rest on my own. I will be back soon.
Hello again.
Here you see again my question in another form.
Can you program here that it is executed for all “n”?
defparam cumulateorders = false
once percent = 0.02 //2%
a = 100*percent
b = 100/a
n = b
// follow must be done for all n from 1 to n
timeframe (daily)
ath = highest [1000] (high [1]) //100%
c1 = ath - (ath * (n+1) * percent)
c2 = ath - (ath * n * percent)
c3 = ath - (ath * (n-1) * percent)
timeframe (default) // timeframe 1H
If close < c2 then
buy at c2 stop
endif
If close < c3 or close > c1 then
sell at market
endif
There you go:
defparam cumulateorders = false
once percent = 0.02 //2%
a = 100*percent
b = 100/a
n = b
// follow must be done for all n from 1 to n
timeframe (daily)
ath = highest [1000] (high [1]) //100%
FOR i = 1 TO n//1
c1 = ath - (ath * (i+1) * percent)
c2 = ath - (ath * i * percent)
c3 = ath - (ath * (i-1) * percent)
If close < c2 then
buy at c2 stop
endif
NEXT
timeframe (default) // timeframe 1H
If close < c3 or close > c1 then
sell at market
endif
graph a
graph b
graph n
graph i
graphonprice c1 coloured(255,0,0,255)
graphonprice c2 coloured(0,128,0,155)
graphonprice c3 coloured(9,0,255,255)
graphonprice ath
As you can see from the attached pic, when n is reached, c2 is 0.
It only works for the FIRST iteration. This means that iterations are useless. If you change line 12 to 1, nothing changes, because from the second iteration on, it nevcer enters. Bear in mind that 2% on DAX is now about 320 pips far!
Anyway, you can play with it by trying to change something.