Exit trade if entry bar is against the trend
Forums › ProRealTime English forum › ProOrder support › Exit trade if entry bar is against the trend
- This topic has 8 replies, 4 voices, and was last updated 7 years ago by
mikenew.
-
-
08/18/2017 at 6:42 AM #44160
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:
12345678910111213141516171819202122ONCE exitEntryBarDown = 0IF NOT OnMarket AND open > Average[20](close) THENBUY 1 CONTRACTS AT MARKET// Exit if candle OF the entry is against the trade - Market goes downIF open > close THEN// Market moves downexitEntryBarDown = 1SELL AT MARKET NextBarOpenENDIF// Set the stopSELL AT Average[20](close) STOPENDIFIF OnMarket THENSELL AT Average[20](close) STOPENDIFGRAPH exitEntryBarDown as "Exit on Entry bar is down"GRAPH OnMarket COLOURED (255,0,255) AS "On Market"exitEntryBarDown = 0It’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
08/18/2017 at 10:44 AM #4417108/18/2017 at 8:23 PM #44196NEXTBAROPEN 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).
08/18/2017 at 10:20 PM #44199Thanks 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.
08/18/2017 at 11:09 PM #44200Nicholas,
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 🙂
08/19/2017 at 8:10 AM #44208https://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.
08/19/2017 at 8:55 AM #44210Thanks 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.
08/19/2017 at 9:39 AM #44211You 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:
12345IF LongOnMarket THENIF open > close THEN //check if BEARishSELL AT MARKET //and close any open tradeENDIFENDIFRoberto
1 user thanked author for this post.
08/19/2017 at 9:42 AM #44212 -
AuthorPosts
Find exclusive trading pro-tools on