Both sides inside bar breakout with one side canceling the other
Author: jairofraga
Creation Date: 11/7/2019 11:23 AM
profile picture

jairofraga

#1
Hello guys,

I'm new here and testing Wealth-Lab on trial mode, to check if strategies I use elsewhere can be implemented here. I'm trying to implement a strategy that I already use on other platforms, but couldn't do it here yet.

I would like a strategy that:

- The 1st bar is a big bar, from 0 to 3x (custom parameter) the range (high-low) of the average range of the last 15 bars

- The 1st bar is a big volume bar, from 0 to 3x (custom parameter) the volume of the 20-period Volume moving average

- The 2nd bar is an inside bar.

- The 2nd bar has a range (high-low) of 3 to 10 ticks (custom parameter).

- The 2nd bar has a volume of at most 50% of the volume of the 1st bar, or even less.

- The entry must occur the next bar that the high of the inside bar + tick is reached (giving a long entry), or that the low of the inside bar - tick is reached (giving a short entry).

- If it's a long entry, the stop-loss is located at the low of the inside bar - tick. And the target is the range (high-low) of the 1st bar mutiplied by 1 to 4x (custom parameter) added to the entry point.

- If it's a short entry, the stop-loss is located at the high of the inside bar + tick. And the target is the range (high-low) of the 1st bar mutiplied by 1 to 4x (custom parameter) subtracted to the entry point.

- If target or stop-loss isn't achieved the trade is closed at hour 16:40 the same day (working with intraday data).

- I would like to use a filter (as a custom parameter), which doesn't enter a long if Slow Stochastic (8,3) isn't over 80 or not entering a short if Slow Stochastic (8,3) is below 20.


I managed to do some part of the code already, but what I'm having a bit more hard time to do is the cancelling of a short stop entry if long stop entry is reached and vice-versa. I'm not sure if this is possible on Wealth-lab, as other platforms take an assumption that if both short entry and long entry are reached on the same candle, the entry ocurred at the nearest point of the inside bar close price, be it short or long.

If that isn't possible, an entry following the side of the big 1st bar would do.

Thanks a lot.


profile picture

Eugene

#2
Hi Jairo,

Welcome to the forum. Here's my quick take at your strategy. Note that your "2nd bar" is just "bar" in our terminology and your "first bar" is "bar-1". For speed's sake I skipped an optimizable parameter from your list of many but it should be trivial for you to get it coded by following the examples in the WealthScript Programming Guide (Programming Trading Strategies > Strategy Parameters).

CODE:
Please log in to see this code.

As you see the long side gets precedence in this code. But there's hardly a way to know what entry comes first in live trading at bar+1.


QUOTE:
I'm not sure if this is possible on Wealth-lab, as other platforms take an assumption that if both short entry and long entry are reached on the same candle, the entry ocurred at the nearest point of the inside bar close price, be it short or long.

My understanding is that you already use intraday data so it shouldn't be much of an issue.

QUOTE:
I managed to do some part of the code already, but what I'm having a bit more hard time to do is the cancelling of a short stop entry if long stop entry is reached and vice-versa.

Making assumptions about same-bar stop/limit orders puts a strategy on pretty shaky ground if applied to EOD bars. When a strategy employs Limit (and Stop) orders on the same bar, backtesting it in a realistic manner cannot go without some prerequisite as per this FAQ: I want to test a strategy that buys and sells with stop/limit orders on the same bar.


Let me know if you have some questions or concerns.
profile picture

jairofraga

#3
Thanks a lot Eugene! I just corrected the inside bar code which had a error on the "Low" check. Also added the parameters correctly to make an optimization.

One thing I couldn't correct, as it gave me index errors, was replacing the truerange code, as I use in fact high - low of each candle instead of the true range of the last candles.
profile picture

Eugene

#4
You're welcome.
QUOTE:
I just corrected the inside bar code which had a error on the "Low" check.

Code above fixed.

QUOTE:
as I use in fact high - low of each candle instead of the true range of the last candles.

Here's how:
CODE:
Please log in to see this code.


P.S. And since the code uses an "unstable" indicator (ATR), make this change to put it in accordance with the WealthScript Programming Guide > Indicators > Stability of Indicators:

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