Hi,
This is my first post here! I’m trying to backtest an idea where I calculate and measure the different parts of the candlesticks. I want to calculate the lower wick and if it is bigger than the higher wick it’s an entry signal. However it doesent seem to be working, it gives random results. This is the code:
body=close-open
if body>0 then
bodytop=close
bodybottom=open
else
bodytop=open
bodybottom=close
endif
shadowtop=high-bodytop
shadowbottom=bodybottom-low
Hammer= Shadowbottom > Shadowtop
Exit= (BarIndex - TradeIndex) = 1
IF Hammer THEN
BUY 1 CONTRACTS AT MARKET
ENDIF
If LongOnMarket THEN
SELL AT MARKET
ENDIF
I have created an indicator with the same calculation of the wick and this seem to be working fine, see attached picture. You can also see in the attached picture that entries is not according to my expected behaviour. What am I missing?
Best regards
Hi. The problem with your code comes when you have two or more bars “hammer” type.
When a hammer bar appears the system buy 1 contract in the next bar open and sells after 1 bar.
If you have 2 consecutive hammer bars the second one doesn’t exits for the system.
In order to avoid it we can change the code like this:
IF Hammer and not onmarket THEN
BUY 1 CONTRACT AT MARKET
ENDIF
if onmarket then
if hammer then
buy 1 contract AT MARKET
sell 1 contract at market
else
sell at market
ENDIF
endif
if there are 3 consecutive candles with hammer type you should be on market 3 bars.
Is the Timeframe your displaying in image, the same as the running backtest code , image suggests no.
What was your expected behaviour for the result.
Thank you very much for your replies!
It still doesn’t seem to work though. The picture is the same timeframe as the backtest.
What I want to achieve is to have a backtest that buys when the wick under the body is x times as big as the wick above the body. I have updated my code with the input from above and also added the criteria that the wick under the body should be 3 times bigger than the one above in order for it to be easier to spot if it works or not.
body=close-open
if body>0 then
bodytop=close
bodybottom=open
else
bodytop=open
bodybottom=close
endif
shadowtop=high-bodytop
shadowbottom=bodybottom-low
Hammer= Shadowbottom > 3 * Shadowtop
Exit= (BarIndex - TradeIndex) = 1
IF Hammer and not onmarket THEN
BUY 1 CONTRACT AT MARKET
ENDIF
if onmarket then
if hammer then
buy 1 contract AT MARKET
sell 1 contract at market
else
sell at market
ENDIF
endif
Try this,
Comment out lines 15, 22 and 23 by placing // at the beginning of these lines
15. Exiting after 1 bar seems to go against staying in.
22,23 If in market, buying and then selling same qty is the same as staying in market at current qty.
25. Then only sell if in market and closing bar is not a hammer.
The logic appears to be ,
If not on market, Buy at Market on a Hammer,
If on market sell Market on NOT hammer
Thank you, I have tried this but with no luck. There seems to something else that is off… I have made the code even more basic to prove the point -> buy all times condition is met. Result is that the system is buying at correct points mostly but sometimes it’s off. Sometimes it indicates two orders same day but with different execution dates (see picture). There must be something that is not in sync?
body=close-open
if body>0 then
bodytop=close
bodybottom=open
else
bodytop=open
bodybottom=close
endif
shadowtop=high-bodytop
shadowbottom=bodybottom-low
Hammer= Shadowbottom > 3 * Shadowtop
IF Hammer THEN
BUY 1 CONTRACT AT MARKET
ENDIF
JSParticipant
Senior
Hi,
What you show (two orders/day) probably only happens on Friday…?
Yes, that is correct – it’s on a friday. Why is it that way? It shows entry the date day after (saturday), that must be wrong?
JSParticipant
Senior
Hi,
Is IG that is “out of sync”…
IG has a very nasty “Sunday candle”…
What happens is: buy on Friday and then again on Sunday and then again on Monday… And when the option “don’t show weekend dates” is enabled, then the Sunday order shifts to Friday, hence the two orders on one day…
Thank you very much for that insight, explaines a whole lot for my strategies.. Thanks again!