Hi all,
Here’s my code. Strategy looks pretty good but unable to get the code to work. The idea is a range is set by a longer than average (1.5xaverage of last 20bars) bar.
A trade is then entered following a close outside of the limits (high or low) of this undecided bar.
If higher a buy trade is entered, if lower a sell trade is executed.
The stop strategy is simple and looks effective. It uses either the low of the undecided bar if a buy is activated (or the high of the undecided bar if sell is activated).
However 1 major and 1 minor problem
- The code doesnt work
- The pprofit distance needs optimising
Fine tuning/help welcomed.
Thanks
Linden
Many
//-------------------------------------------------------------------------
// Main code : Undecided Bar as baseline
//-------------------------------------------------------------------------
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
DEFPARAM Preloadbars = 1000
Defparam Flatbefore = 143000
Defparam Flatafter = 210000
// Money Management
Capital = 10000
Risk = 0.05
StopLoss = atr// VARY TO DETERMINE RISK
//Calculate contracts
equity = Capital + StrategyProfit
maxrisk = round(equity*Risk)
PositionSize = abs(round((maxrisk/StopLoss+0.01)/PointValue)*pipsize)
if PositionSize >= 100 Then
PositionSize = 100
endif
atr = AverageTrueRange[20]*1.5
// Conditions to enter long positions
c1 = High[1] - Low[1] > atr
c2 = Close[1] - Open[1] > 0.5
c3 = High[1] - Close[1] > (Close[1] - Open[1]) *6
c4 = Open[1] - Low[1] > (Close[1] - Open[1]) *6
c5 = Close[0] - Open[0] > (High[0] - Max(Open[0],Close[0]))*0.5
IF c1 and c2 and c3 and c4 and c5 then
TopofBar = high[1]
Achat = 0
endif
If achat = 0 then
if open > TopofBar then
if onmarket = 0 then
buy positionsize lots at market
endif
endif
endif
Once Downstop = 1
Once Uptarget = 1
Downstop = Low[1] - 1
Uptarget = High[1] - Close[1]
// Conditions to exit long positions
Set Target pProfit Uptarget
Sell at Downstop stop
// Conditions to enter short positions
c6 = High[1] - Low[1] > atr
c7 = Close[1] - Open[1] < -0.5
c8 = High[1] - Open[1] > (Open[1] - Close[1]) *6
c9 = Close[1] - Low[1] > (Open[1] - Close[1]) *6
c10 = Open[0] - Close[0] > (Min(Open[0],Close[0]) - Low[0])*0.5
IF c6 and c7 and c8 and c9 and c10 and onmarket = 0 then
BottomofBar = low[1]
Vente = 0
endif
If Vente = 0 then
If open < BottomofBar then
If onmarket = 0 then
sellshort positionsize Lots at market
endif
endif
endif
// Conditions to exit short positions
Once Upstop = 1
Once Downtarget = 1
Upstop = High[1] + 1
Downtarget = Close[1]- Low[1]
Set Target pProfit Downtarget
Exitshort at Upstop stop
> For clarity of messages on ProRealCode’s forums, please use the “insert code PRT” button to separate the text of the code part! Thank you! <<
Did you try to graph all your “c” conditions already? The no trading problem might come from these conditions not met all at the same time.
You should also try to GRAPH the “BottomofBar” and “TopofBar” to know if your price levels are correctly calculated.
It seems that you have copy/paste some of the codes from another strategy? I can see french words in the code 😉
You don’t need five conditions to enter a trade.
If (high-low) > averagetruerange[20]×1.5 then
upper =high
Lower =low
Rangebar=1
Endif
If close crosses over upper and rangebar=1 then
Buy 1 contract at market
Rangebar=0
Endif
Did not test the code but the idea is clear, i hope.
Instead of optimizing the pprofit you can also look for prolonged periods of contraction. If the price has been trading inside the range for X+ bars already the final breakout might be more vicious.
Or maybe look into volume confirmation to filter false signals.
Thanks for your input.
Yep I did copy and paste some code when I got stuck 😉
That looks good Derek, I’ve just got 2 questions
- Why do you need the line 9) Rangebar = 0
- The code you wrote doesnt account for a short body in the middle of a long long spike either side. How would I factor this in?
- Many thanks for your time 🙂
Hey,
- To avoid reentry after a false breakout. You can leave it out if you can work without referring to the event later in your code. Or you go testing if your strategy performs better on a later breakout attempt.
- That’s intended. Otherwise wait for confirmation of the range. See 1.
- I love simplicity in a furios world.
But now for 1.: are you referring to extra long bars with a full body and a close very near the top, or the bottom respectively? We can shortcut around both issues.
Since the market doesn’t care what timeframe we’re trading on, the top of the range is confirmed by the retracement! So, we wait for a Rangebar to occur and then look for a follow up event to differentiate a forming range from a straight forward breakout. This way, we don’t need to look at the rangebar when it occurs because they are all equal.
Here is my code (not tested):
If (high-low) > averagetruerange[20]×1.5 then
upper =high
Lower =low
Rangebar=1
Endif
If Rangebar=1 and close[2] < upper and close[1] < upper and close[0] < upper and close[0] > close[1] then //it's a 1-bar fractal below rangebar's high.
Rangetop= upper
Rangebottom=lower
Elsif Rangebar=1 and close[2] > upper and close[1] > upper and close[0] > upper and close[0] < close[1] then //it's a 1-bar fractal above rangebar's low
Endif //the range has at least some confirmation.
If close crosses over rangetop or if close crosses under rangebottom then
Buy 1 contract at market
Rangebar=0
Endif
Don’t let your eyes fool you. The initial hypothesis is more likely to be correct if there actually is a range following a rangebar before the breakout 🙂
edit: added the lower range
Ok, this should work. Replace with lines 7-11:
If Rangebar=1 and close[2] < upper and close[1] < upper and close[0] < upper and close[0] > close[1] then //it's a 1-bar fractal below rangebar's high.
Rangetop= upper
Rangebottom=lower
Elsif Rangebar=1 and close[2] > upper and close[1] > upper and close[0] > upper and close[0] < close[1] then //it's a 1-bar fractal above rangebar's low
Rangetop= upper
Rangebottom=lower
Endif //the range has at least some confirmation.
Hi Derek,
Just back from hols to find this excellent piece of help (and code)
Definitely prefer the simplicity of it – in a furious world 🙂
Many many thanks
Linden