Morning gap strategy
Author: tradeoholic
Creation Date: 4/1/2021 3:49 AM
profile picture

tradeoholic

#1
Hi,

I'm trying to get involved into some basic programming in WL. I wanted to code the Morning gap strategy (EOD) for DAX index (^GDAXI). The strategy consists of four cases. The code and the description shows the first case, when the yesterday's candle was a growing candle and the today's open is between the yesterday's low and yesterday's open. (I try to attach a picture of it). For this case there are two exit conditions, that is described below.

My problem is that according to the conditions the program should close the position by all means today at yesterday's open or at today's close. So the 'Bars Held' column in the 'Trades' list should show maximum 2. But when I run the script I see in this column bigger numbers then 2. What's wrong, why does it hold the positions for more days ?

Entry:
- if yesterday's candle is a growing candle (yesterday's close is above yesterday's open):
- then if today's open is between yesterday's low and yesterday's open
- then buy at today's open
Exit 1:
- if today's high is above yesterday's open
- then sell today if today's price reaches yesterday's open
Exit 2:
-if today's high is below yesterday's open
- then sell today at today's close

CODE:
Please log in to see this code.

profile picture

tradeoholic

#2
Or I think, the 'Bars Held' should be only 1 in this case.
profile picture

Cone

#3
Nice job. Nearly perfect.

Re: BarsHeld
You don't have a condition to always exit after 1 bar. You have 2 specific exit conditions - if neither occurs, you'll continue to hold the position.

The other thing that you're missing is that you cannot evaluate data for a bar that doesn't exist yet. You're saying "today and yesterday", when really the code is "tomorrow (bar + 1) and what happened today (bar)".

When the loop gets to the last bar, Open/High/Low/Close/Volume[bar + 1] will throw an error, because you can't see the future. (This is by design - it's an error to peek into the future.) Consequently, for a strategy like this, you have to stop the loop before the last bar to avoid this error:

CODE:
Please log in to see this code.


That's the quick fix - keeping in mind there's no way a strategy that stops before processing the last bar can give you an Alert to buy or sell.

You can do a little better if you use a data provider that supports GetSessionOpen() (see QuickRef), but you still have a problem with the exit because the script also needs to know "tomorrow's high", i.e., High[bar + 1], which you can't know until the end of the day.

If you find that you like the strategy and want to trade it live, you could convert it to an intraday strategy and use 1 minute bars. BuyAtMarket would just be 1 minute late, and, you'd have to decide the high of the day 1 minute early, but this would be then a practical system to trade.
profile picture

tradeoholic

#4
OK, I changed every 'bar' to 'bar-1', and every 'bar+1' to 'bar'. Now the error message disappeared. (See below)

But I think the code doesn't make what I would like to. The strategy should trade the gaps between today's open and yesterday's open.
I was drawing all the possible exit cases on the picture that I attached (exits.png). I want the exits to happen always on the same day
as the entries, so every position will be closed on the day when it was opened. If it is so, the 'Bars Held' sholud always be 1.

I attached another picture (example.png) where you can see an entry (on 29.01.2021.) and an exit (on 16.02.2021.) that belongs together (on Yahoo's ^GDAXI). It lasted several days until the exit occured, but it should have to be closed on the same day as the entry. (Because the
today's price reached yesterday's open).

(There are 4 entry cases and 8 exit cases that belong to the complete strategy, but I can't expect you to study all through. If I understand the
code for the Case 1, I will be able to write the code for the another cases.)

CODE:
Please log in to see this code.



profile picture

Cone

#5
QUOTE:
I changed every 'bar' to 'bar-1', and every 'bar+1' to 'bar'.
It was okay before, it's just that you have to stop the loop early with the minor adjustment to the for loop. Either way, you cannot process data from a bar that does not exist yet.

QUOTE:
...the 'Bars Held' should always be 1.

The exit conditions that you gave are:

Exit 1:
- if today's high is above yesterday's open
- then sell today if today's price reaches yesterday's open
Exit 2:
-if today's high is below yesterday's open
- then sell today at today's close

What should happen if neither one of these conditions occur? Sell at the close? In this case, we just forget about Exit 2?

Edit - just looked at your pictures.. yes, Exit 2 should sell at the close always, i.e., with no condition

To sell on the same bar, you need to process the logic for both buy and sell on the same bar... hang on...
profile picture

Cone

#6
After looking at it in detail, now I see what you were doing :)

Here's the Cadillac version, all set with GetSessionOpen (if you have a data provider that supports it) -

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

tradeoholic

#7
Thank you,

that's it what I wanted. But unfortunately it is not tradeable. It is a full minus strategy on DAX, although it has 75% win rates (without commissions). As an analysis good to know that the gap according to the strategy will be filled with 75% probablity, but the many little wins will be eaten up by the few big losses.
profile picture

Cone

#8
I call that a success! That's why you should test everything. :)
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).