Hi
I’m writing code
to exit the trade if the entry bar is against the trend. I’ve simplified it into a price crosses SMA indicates the trend. Long side only.
So if on entry it is a down bar then the strategy will exit the trade on the open of the next bar.
Here’s the code:
ONCE exitEntryBarDown = 0
IF NOT OnMarket AND open > Average[20](close) THEN
BUY 1 CONTRACTS AT MARKET
// Exit if candle OF the entry is against the trade - Market goes down
IF open > close THEN
// Market moves down
exitEntryBarDown = 1
SELL AT MARKET NextBarOpen
ENDIF
// Set the stop
SELL AT Average[20](close) STOP
ENDIF
IF OnMarket THEN
SELL AT Average[20](close) STOP
ENDIF
GRAPH exitEntryBarDown as "Exit on Entry bar is down"
GRAPH OnMarket COLOURED (255,0,255) AS "On Market"
exitEntryBarDown = 0
It’s behaving in two unexpected ways:
- It exits on the open of the bar of entry (not the next bar)
- It enters a trade even if the open is less than the SMA
See attached image.
Am I misunderstanding something about the code here?
Broker is IG
Market is XAUUSD Spot Gold A1 Contract
Dates in the image are 14th Jan 2016 to 22nd Jan 2016
Cool! You just learned me a new commando. I wasn’t aware of nextbaropen. Nice! Thanks.
Have a look at your line 6. Should you have there one < close? Now you are exiting on an upbar.
NEXTBAROPEN is depreciated keyword that does nothing in the recent probuilder language version. So at line 9, you are exiting your BUY order at the same moment than its open.
For your second problem, please consider that at line 3, you are comparing the Open of the bar of when the code is read (at Close).
Thanks Nicholas
The NextBarOpen is in the current user manual for probacktest I down loaded last week v5.1.2-20170404 .. no mention of it being deprecated. How do we feed this back to the PRT team?
I’ll have a look at the second issue and comment.
Nicholas,
Your comment:
For your second problem, please consider that at line 3, you are comparing the Open of the bar of when the code is read (at Close).
OK so I get it …. my code says this bar’s open>AV after the close and it will enter on the open of the next bar….
I don’t see any way to implement what I want “IF open crosses moving average then buy on this open”
Unless there’s a openNextBar variable which I can’t find …. or a BUY 1 THISOPEN which seems just as rare 🙂
https://www.prorealtime.com/en/changelog_probacktest_10_1
The variable “OpenOfNextBar” contained the opening price of the bar following the current one. This variable was removed because it is impossible to know this price in an automatic trading situation. This instruction should not be confused with “NextBarOpen” which is an instruction that can be used to place an order at the opening price of the next bar and is still available as shown in the last section.
But “NextBarOpen” doesn’t help in your situation and it is useless in code since orders are always launch at the next bar open.
Thanks Again Nicolas. I’ll need to look at a different backtesting tool for this strategy … I’m thinking Python Backtrader https://github.com/mementum/backtrader combined with Pyfolio for basket testing.
You order is placed at some bar, which is the entry bar (conditions were met when the previous bar closed), so you’ll be able to tell the entry bar went the wrong way only when it closes.
So if you place this code at the very beginning of your strategy it should do:
IF LongOnMarket THEN
IF open > close THEN //check if BEARish
SELL AT MARKET //and close any open trade
ENDIF
ENDIF
Roberto