// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
//Condition To Ensure That Only One Transaction Can Happen Per Bar – for example on any 30 min bar this condition is meant to stop the exit of a Long AND an initiation of a Short during the same 30 min period. Not fully convinced if this is coded correctly for this purpose.
ONCE BarCount = 0
ONCE TradeON = 1 //1=trading allowed 0=trading disbaled
IF LongOnMarket OR ShortOnMarket AND Not LongOnMarket[1] AND Not ShortOnMarket[1] THEN
TradeON = 0 //disable trading
BarCount = 0 //reset counter
ENDIF
IF Not LongOnMarket And Not ShortOnMarket THEN //Increase counter when not on market
BarCount = BarCount + 1
ENDIF
IF BarCount > 1 THEN
TradeON = 1
ENDIF
// Conditions to enter long positions
c1 = (close < open)
IF c1 AND TradeON THEN
BUY 1 SHARES AT MARKET
ENDIF
// Conditions to exit long positions
c2 = (close > open)
IF c2 THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
c3 = (close > open)
IF c3 AND TradeOn THEN
SELLSHORT 1 SHARES AT MARKET
ENDIF
// Conditions to exit short positions
c4 = (close < open)
IF c4 THEN
EXITSHORT AT MARKET
ENDIF
I’ve used this strategy on Bloomberg over 30 years of FX data (30 min bars) and it works very well and would now like to add it to PRT but am unsure of if I have coded it correctly and would very much appreciate your feedback.
To confirm, this is what the strategy is meant to do on FX pairs with 30 min bars:
- Enter Long at the Next Open when the 30 min bar Closes < its Open
- Exit Long at the Next Open when the 30 min bar Closes > its Open
- Enter Short at the Next Open when the 30 min bar Closes > its Open
- Exit Short at the Next Open when the 30 min bar Closes < its Open
A few questions:
Is the code specified correctly for this Entry and Exit rules listed above ?
Is the code correct for FX trades ? Also, how could it be modified if it was to be used on stock indices (esp. DAX)?
Critically of all, is the “timer” condition I have specified at the top of the code correct (I doubt it). Effectively, the strategy I wish to add to PRT should only allow one transaction per 30 min bar (so for example, if a bar closes>open, on the next bar, the strategy should EITHER Exit a Long (if one exists) OR Enter a Short (but not both exit a long and enter a short at the same time).
Thank you very much in advance for your help.
Replace lines 19-44 with:
c1 = (close < open)
c2 = (close > open)
IF c1 AND TradeON THEN
If ShortOnMarket Then
EXITSHORT AT MARKET
Elsif not LongOnMarket Then
BUY 1 SHARES AT MARKET
ENDIF
ENDIF
IF c2 AND TradeON THEN
If LongOnMarket Then
SELL AT MARKET
Elsif not ShortOnMarket Then
SELLSHORT 1 SHARES AT MARKET
ENDIF
ENDIF
The use of BarCount is useful if you use multiple and lower TFs. On a 30-minute TF no more than 1 trade can occur on the same bar.
Thank you Roberto, really appreciate your help.
Actually my code is logically incorrect, since it does not allow closing open trades when TredeON=0. While this condition should not allow to OPEN new positions, it shouldn’t forbid open positions from being CLOSEd:
c1 = (close < open)
c2 = (close > open)
IF c1 THEN
If ShortOnMarket Then
EXITSHORT AT MARKET
Elsif not LongOnMarket AND TradeON Then
BUY 1 SHARES AT MARKET
ENDIF
ENDIF
IF c2 THEN
If LongOnMarket Then
SELL AT MARKET
Elsif not ShortOnMarket AND TradeON Then
SELLSHORT 1 SHARES AT MARKET
ENDIF
ENDIF