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.
profile picture

jairofraga

#5
I've been trying for some time already, to do a timeout for the setupbar, so that it triggers not only on the next bar but on other bars, like described on some other topics and on the wiki. The problem I found is that I can't set the SetupBar properly, as the stop-loss and take profit it's not a fixed amount, and it's referenced on the candle previous to the SetupBar. Also, there is the problem that if not using any filters, a setup bar could be a valid long and a valid short, depending on where the breakout happens.

As I need the setup to be valid for, like, 5 candles after it happens, I couldn't make a proper "SetupValid" trigger, that also takes into account the conditions of a valid short and a valid long. I've managed to do a long/short setup which trigger on the next candles on a strategy that only uses enties on limit, by using a SetupValidLong and a SetupValidShort, but not on those that use entry on stops like this on, which will show me an error related to the SetupBar.

Can you check what I'm doing wrong, Eugene? I've added some more filters which were working good on the next bar entries. Thanks a lot for the help

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

Eugene

#6
Jairo, there are several issues with the code:

1. Before anything else, you have a problem with missing curly braces here:
CODE:
Please log in to see this code.

Which makes the SetupValidLong/SetupValidShort conditions to be evaluated on every bar. So this segment should be wrapped in curly braces.

2. Next, you should not make an AtStop/AtLimit entry and then let it be exited with an AtStop/AtLimit order on the same bar. The control flow of your code allows it. For the whys and wherefores (as well as solutions) please refer to the Wiki FAQ | Strategies and WealthScript > I want to test a strategy that buys and sells with stop/limit orders on the same bar. The workaround referenced in the WealthScript Programming Guide appears to be the simplest to install.

3. Finally, putting these directives where you did doesn't make sense and will reference the wrong values:
CODE:
Please log in to see this code.

They belong to the respective entry blocks, not the exit loop:

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