Coding a strategy based on 2 consecutive bars
Author: baam
Creation Date: 5/7/2016 12:55 AM
profile picture

baam

#1
Hello,

I was hoping I could get some help with coding this strategy. I have some experience with programming but have not coded with C# before.

The strategy is based on two consecutive 15 minute candles:

The 1st candle is a bearish (red) candle where close is below open and the difference between high and open is greater than the difference between close and low.

The 2nd 15 minute candle’s high is greater than the high of 1st candle and closes in the upper 15% of the 1st candle’s range or higher. For example if the low of the 1st candle is 60 and high is 61 then the 2nd candle trades above 61 and closes above 60.85

The volume of the 2nd candle is >= 2.15X the volume of the 1st candle.

Ignore the 1st candle in the morning (start with the 9:45 am candle).

If above conditions are met, buy at market on the open of the 3rd candle. Sell at high of the 1st candle plus the difference between the low of the 1st two candles and the high of the 1st candle. For example if the low of the 1st candle is 60 and high is 61 and the low of the 2nd candle is 59.80 then target is 62.20

Stop loss is at one penny below the low of the 1st two candles.

Thanks in advance
profile picture

Eugene

#2
Welcome to the forums. Not sure if I understood your profit target exit which doesn't sound too clear to; feel free to correct if I'm wrong.

Here you go. Run this code on 15-min data:

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

baam

#3
Thank you Eugene!

I ran the code and do not get any trades. Do you get trades on these 2 symbols?

SRPT 5/5/16 11:15 AM
QQQ 4/27/16 2:30 PM

The previous two candles meet the rules so buy order should trigger at the candle indicated above.
profile picture

Eugene

#4
The strategy is working as expected. You can not get trades on these bars because you only buy on the 3rd bar @ 10:00am. That was your condition. Let's debug:

CODE:
Please log in to see this code.


QQQ 4/27/16 9:45am: the difference1st test fails because High-Open is equal to Close-Low.
SRPT 5/5/16 9:45am: 3 of 5 conditions fail likely due to O=H=L=C @ 9:30am. See enclosed screenshot:



At least this is what my Google Finance data indicates. Since 1/1/2016 on QQQ I get two trades (03/22 and 04/22) and three in SRPT (03/21, 04/21 and 04/26). If your compressed data is different from mine (e.g. you're looking at an Active Trader Pro chart), then we might be comparing apples to oranges (different timestamps leading to different compressed bars, FAQ > Fidelity data does not match the same symbol in Active Trader Pro.).

P.S. Meanwhile, choose a Raw Profit position size for debugging to avoid the insufficient funds factor (FAQ > I'm using 100% Equity position sizing and strategy doesn't seem to use all capital and/or there are trades not included due to insufficient capital.)
profile picture

baam

#5
Hi Eugene,

The screenshot in your post is showing 5min data on SRPT. I ran the code on 15min data as you suggested in your previous post.

Also we're looking for two consecutive bars that meet the criteria. The 1st bar that meets the conditions doesn't have to be the 10:00 AM bar (assuming the initial bar in the morning is called 9:45 bar which we're ignoring). If the 10:00 AM bar doesn't meet the criteria then it checks the next bar. A bar that meets the conditions will become the 1st bar.

Please see attachment for 15min SRPT on 5/5/2016; it's a good example of two consecutive bars meeting the conditions:

10:45 and 11:00 AM consecutive bars are shown which meet the conditions for bars with corresponding volume (meeting the condition for volume). The buy order (or alert) should trigger on the next bar at the start of 11:15 AM bar.
profile picture

Eugene

#6
QUOTE:
showing 5min data on SRPT.

Correct. This is to show the source data for the 15--min bars.

QUOTE:
The 1st bar that meets the conditions doesn't have to be the 10:00 AM bar

I was under impression that your pattern is tied to the particular pair of bars at the open i.e. "Ignore the 1st candle in the morning (start with the 9:45 am candle)." Now I see that my assumption wasn't correct. Hope this does the trick:

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

baam

#7
Thank you Eugene! Your code works perfectly!

I'd like to get sound alerts intraday each time the strategy generates a buy in paper account for now. To get sound alerts do I just add the strategy to "Strategy Monitor" and run it on a dataset?
profile picture

Eugene

#8
Yes. Audible alerts can also be triggered from Strategy window. Make sure that approrpiate checkbox is enabled in Wealth-Lab's Preferences > Sounds.

Also, check out an example of strategy-driven media playback in the User Guide > Preferences > Sounds.

P.S. Should you have general questions, don't hesitate to ask them in other matching threads (existing or new).