Gap Fill
Author: NPresutti
Creation Date: 6/1/2009 10:30 PM
profile picture

NPresutti

#1
New to Welath-lab, trying to get any help here to figure out how to build code to buy ABC on the open if it gaps down x% from previous day's close, and when filled to place conditional OCO to either sell at 2% stop loss or sell limit @ previous day's closing price whichever fills first...any help would be greatly appreciated, thanks guys
profile picture

Eugene

#2
What you describe is already present in the bundled strategy "Gap Filler" - except for the absense of a stop. So the change is minimal: open the strategy and replace the line starting with SellAtLimit... with these 2 lines:
CODE:
Please log in to see this code.

By the way, welcome to our Wealth-Lab 5 Wiki where you can find examples of actual gap systems:
System Building | Gap trading strategies
profile picture

rmunoz

#3
Also new to Wealth-Lab, trying to build a gap strategy to do as follows:

- Buy if gap > x% from prior day's Close, when filled
- Sell at a) if price moves down y% (stop loss), or [y% loss]
b) if price moves up z% then set stop loss to Open, or [zero loss/gain]
c) if price hits previous days closing price, or [x % gain]
d) at end of trading day, whichever occurs first [some % gain]

Apprciate any guidance on the code to insert/replace in the default Gap Filler strategy... thanks.
profile picture

Eugene

#4
I'm afraid it's not easily possible to insert/replace some code in the Gap Filler strategy to do this. You'll need:
a) intraday data
b) modify the strategy accordingly.

Here's why: to buy when gap is filled, you'll need a stop order, but using daily data makes it impossible to find out which event came first -- your stop loss "a", profit target "c", breakeven stop "b" or your setup i.e. filling the gap.
profile picture

Cone

#5
Give this a try on a SPY intraday chart, for example -

CODE:
Please log in to see this code.


Edit: removed some tab spacing
profile picture

rmunoz

#6
Thanks... I'm now seeing trades entered even when the target gap % down from prior day's close is not met.
Can you give the code another look?
profile picture

Cone

#7
The code looks for a gap up by the specified percentage, and then opens a limit buy at yesterday´s close. Gap down is ignored because your specifications didn't say anything about the direction of the gap.
profile picture

rmunoz

#8
Sorry Cone... my bad. I was following the prior thread that was referring to a down gap.
Hope this is clearer. The strategy is:

Buy at Open if gaps down > x% from prior day's Close.
When filled, Sell at whichever occurs first below:
a) if price moves down y% [stop loss]
b) if price moves up z%, then set stop loss to Open [breakeven]
c) if price hits prior day's Close price [gap fill]
d) at end of trading day. [close of day]

profile picture

Cone

#9
Working with the opening price has its challenges and it's not necessarily feasible to realize those "buy at open" trades since you can't determine how much of a gap occurred until after the opening trade already occurred. There will be delays getting your order to the market, so you're unlikely to get the opening price anyway.

The following strategy assumes that you use a 1-minute interval chart to place the order after the close of the first minute. If buying into a large opening gap down, you might find that 1-minute delay works to your advantage; the difference is recorded in the entry signalName. A negative value indicates profit that you missed by delaying 1 minute, whereas positive value indicates extra profit realized by the delay. You can analyze that data offline.

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

rmunoz

#10
Excellent Cone... thank you. Good point on the limitation in trying buy at the Open price - understood.
This was Part A of the overall strategy. Now, part B is the complimentary but standalone strategy that enters the reverse ETF.
Appreciate your help one last time on the code of the B strategy below:

Buy if gaps up > x% from prior day's Close AND if price goes up by y% from the Open
When filled, do whichever occurs first:
a) if price moves down z% from entry, Sell [stop loss]
b) if price moves up z% from entry, then set stop loss to Buy price [breakeven]
c) at end of trading day, Sell [close of day]
profile picture

rmunoz

#11
Correction below:

Buy if gaps up > x% from prior day's Close AND if price goes up by y% from the Open
When filled, do whichever occurs first:
a) if price moves down z% from entry, Sell [stop loss]
b) if price moves up w% from entry, then set stop loss to Buy price [breakeven]
c) at end of trading day, Sell [close of day]
profile picture

rmunoz

#12
Ok, to the point... thanks.
What code can I insert in a gap strategy to buy only if the gap is > X% AND the price is Y% higher from today's open?
profile picture

Cone

#13
Sorry I don't have time to continually modify strategies for you, but try to look at the pieces I've already given you:

pctGapToday is the gap (in percentage)
Open[firstBarToday] is today's opening price
Close[bar] is the most-recent closing price
profile picture

rmunoz

#14
Understood... appreciate the guidance on my newbie questions. I'll try and work it from here.
profile picture

jul1123

#15
Hi there,

I'm interested in that same strategy. I have figured out how to automate it on TDAmeritrade's StrategyDesk, and now would like to try it here.

I've tried to plug in what was given above into a strategy builder, but couldn't get it to work.

I'm not a programmer, but I have the exact code to execute my strategy on TDAmeritrade. It took me a long time to figure out how to do this on TDA, but it seems, even with the help above, this programming language is beyond me. I've tried to read the instruction manual, but it felt like I was reading a foreign language. I'm not opposed to learning new things, but I think I've reached my limit here. However, I did manage to make this strategy work on TDA, so I'm not fully hopeless.

The reason why I came here is because on TDAmeritrade's Strategy Desk the code I have, even though StrategyDesk says it should work, sometimes malfunctions and doesn't buy when it's supposed to at Open, and sometimes it buys when it's not supposed to. That's why I'm here on WLP, to see if I can get this simple strategy to work here.

If anybody would, it would be great if they could translate it for me. I would very much appreciate it.

Entry:

CODE:
Please log in to see this code.


The idea is to buy the stock if it gaps down 1.5% or more from the prior day's close.

Exit:

CODE:
Please log in to see this code.


The idea is to exit either at a -3% stop loss, or, if the stock moves up to +2% from open and then goes back down to Open, to sell at Open (breakeven), or, if neither one of those two conditions happens, to sell as close as possible to EOD.

Your help and insight are appreciated. If nobody here is available to help me with this, please let me know where I might be able to go to get what I have above translated to C# (I believe that's the language for WLP, no?).

Thank you.

Julian



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).