Buy the following day if price rises above the high of the signal bar
Author: coach137
Creation Date: 10/14/2019 9:34 PM
profile picture

coach137

#1
I have a simple RSI backtesting strategy that I need help expanding beyond my drag-and-drop capabilities. The setup for the signal bar is:
50 EMA is rising
50 EMA is > 200 EMA
RSI 20 is > 50
RSI 5 < 30

The part I need help with is to change the buy so that it only triggers if, on the following bar, price rises above the high of the signal bar. Here is what I have so far.

CODE:
Please log in to see this code.


Thanks for any help you can offer.
Kevin
profile picture

Cone

#2
Just make this edit...


CODE:
Please log in to see this code.
profile picture

coach137

#3
Thank you for the help. Will give that a try today.
profile picture

coach137

#4
New question.

I noticed when I use SellAtMarket that the exit is the open of the bar that triggers the sell. That is inflating the results, especially when the trade is stopped out or a break of the 50 ema occurs. It is also hurting the results when the profit target is hit. Is there a better way to code the exit strategies so that it records the price when 1 of the sell strategies occurs. They are:

X% stop is hit
X number of bars goes by
X% profit target is hit
a close below the 50 ema

I modified the above sell strategies to the following.

CODE:
Please log in to see this code.
profile picture

Eugene

#5
QUOTE:
X% stop is hit
...
X% profit target is hit

Be extra careful when pairing a stop/limit entry with an exit of the same kind - if it should take place on the same bar. This is explained in this FAQ:

I want to test a strategy that buys and sells with stop/limit orders on the same bar.

If understood your new question well and you wish to exit on the same bar when one of these 2 conditions above are met on a stop/limit order (only), your code will be subject to this. As you'll find out in the FAQ, to do it correctly you should employ one of the techniques referred to in the FAQ (i.e. a method for some 'educated guesswork' or employ a solution that peeks inside the intraday data).

On the other hand, to avoid making things complex you can simply exit with SellAtClose(bar, p).

QUOTE:
X number of bars goes by
...
a close below the 50 ema

These conditions are OK to exit on the same bar i.e. SellAtClose(bar, p).
profile picture

coach137

#6
I am backtesting daily and weekly strategies not intraday, so the X% stop and target would be something like a 10% stop and a 30% target. That would make it nearly impossible to execute both on the same bar. I will try your suggestion and see if I get more realistic results. Thanks!
profile picture

Eugene

#7
You're welcome.
profile picture

coach137

#8
When trying to access FAQ, I get an error message that says my connection is not private.

When I implemented the SellAtClose(bar,p), I got an error message on a handful of stocks. For instance, KO had one trade initiated on 1/7/1993 and is still open despite the 60 bar limit I set. The error read:

Runtime error: Bar number must be 278 or greater
at WealthLab.Strategies.MyStrategy.Execute()
at WealthLab.WealthScript.SellAtClose(Int32bar, Position pos)
at WealthLab.WealthScript.SellAtClose(Int32bar, Position pos, String signalName)
at WealthLab.WealthScript.a(int32 A_0, Int32 A_1, Int32 A_2)
profile picture

Eugene

#9
What happens is explained in this thread:

Closing all trades: Bar number must be X or greater

Consequently, the solution is to check for not selling a Position prior to having established it:

CODE:
Please log in to see this code.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).