Hi:
I have a problem in which the back-test takes multiple orders in one bar. Mt system is a pyramiding strategy that takes additional positions every time price goes up by 2ATR however, it seems to take on multiple orders on the same bar every time there is a large candle and/or ATR goes up significantly. From the code, it should be taking one position per candle only.
Can anyone tell me what’s wrong? Thank you.
Ruben
Because all the conditions for a new order to open are exactly the same: Close>open+ATR (same Open, same Close and same ATR).
Not quite because there is no problem on the previous 3 transactions (in the attachment). It happens on other instruments as well, the code works well unless there is a large candle, it is only on the large candles that it does this, it executes multiple orders at exactly the same price. However, from the code, it should be recording the close of the candle as a reference for the next transaction. It should also be processing every candle only once.
Even if all conditions were the same you would see trades on every candle, not multiple transactions on the same candle at the same price. The IF/ENDIF conditions should prevent that shouldn’t it?
Here’s the code.
Has anyone seen this before? Thanks in advance
// Definition of code parameters
DEFPARAM CumulateOrders = True // Cumulating positions activated
DEFPARAM NoCashUpdate = False //For backtest only - pg 22
// Conditions to enter long positions
indicator1 = ExponentialAverage[8](close)
indicator2 = ExponentialAverage[50](close)
c1 = (indicator1 CROSSES OVER indicator2)
Equity = 50000
Risk = Equity*0.01
FXinAUD=1.50 //Exchange rate for quote currency in to AUD (1 equals = ??AUD)
FXrate = 1/FXinAUD //Add line ABOVE for non-AUD contracts with the FX rate of quote currency in AUD
ContractSize = 10000
ATRdistance = AverageTrueRange[14](close)*2
Risk1 = ATRdistance*ContractSize //Risk per contract in quote currency
ContractRisk = Risk1*Fxrate
Pos1 = Risk/ContractRisk
IF Not OnMarket THEN
Transactions = 0
ENDIF
//Long entry
IF Not LongOnMarket AND c1 THEN
InitialStop = low
BuyPrice = Open
BUY Pos1 CONTRACT AT MARKET
Transactions = Transactions + 1
ENDIF
//Pyramiding
IF Transactions=1 AND Close>=(BuyPrice+AverageTrueRange[14](close)*2) AND LongOnMarket THEN
BUY Pos1 CONTRACTS AT MARKET
BuyPrice2 = Open
Transactions = Transactions + 1
ENDIF
IF Transactions=2 AND Close>=(BuyPrice2+AverageTrueRange[14](close)*2) AND LongOnMarket THEN
BUY Pos1 CONTRACTS AT MARKET
BuyPrice3 = Open
Transactions = Transactions + 1
ENDIF
IF Transactions=3 AND Close>=(BuyPrice3+AverageTrueRange[14](close)*2) AND LongOnMarket THEN
BUY Pos1 CONTRACTS AT MARKET
BuyPrice4 = Open
Transactions = Transactions + 1
ENDIF
IF Transactions=4 AND Close>=(BuyPrice4+AverageTrueRange[14](close)*2) AND LongOnMarket THEN
BUY Pos1 CONTRACTS AT MARKET
BuyPrice5 = Open
Transactions = Transactions + 1
ENDIF
IF Transactions=5 AND Close>=(BuyPrice5+AverageTrueRange[14](close)*2) AND LongOnMarket THEN
BUY Pos1 CONTRACTS AT MARKET
BuyPrice6 = Open
Transactions = Transactions + 1
ENDIF
IF Transactions=6 AND Close>=(BuyPrice6+AverageTrueRange[14](close)*2) AND LongOnMarket THEN
BUY Pos1 CONTRACTS AT MARKET
BuyPrice7 = Open
Transactions = Transactions + 1
ENDIF
IF Transactions=7 AND Close>=(BuyPrice7+AverageTrueRange[14](close)*2) AND LongOnMarket THEN
BUY Pos1 CONTRACTS AT MARKET
BuyPrice8 = Open
Transactions = Transactions + 1
ENDIF
IF Transactions=8 AND Close>=(BuyPrice8+AverageTrueRange[14](close)*2) AND LongOnMarket THEN
BUY Pos1 CONTRACTS AT MARKET
BuyPrice9 = Close
Transactions = Transactions + 1
ENDIF
IF Transactions=9 AND Close>=(BuyPrice9+AverageTrueRange[14](close)*2) AND LongOnMarket THEN
BUY Pos1 CONTRACTS AT MARKET
BuyPrice10 = Close
Transactions = Transactions + 1
ENDIF
IF Transactions=10 AND Close>=(BuyPrice10+AverageTrueRange[14](close)*2) AND LongOnMarket THEN
BUY Pos1 CONTRACTS AT MARKET
BuyPrice11 = Close
Transactions = Transactions + 1
ENDIF
IF Transactions=11 AND Close>=(BuyPrice11+AverageTrueRange[14](close)*2) AND LongOnMarket THEN
BUY Pos1 CONTRACTS AT MARKET
BuyPrice12 = Close
Transactions = Transactions + 1
ENDIF
IF Transactions=12 AND Close>=(BuyPrice12+AverageTrueRange[14](close)*2) AND LongOnMarket THEN
BUY Pos1 CONTRACTS AT MARKET
BuyPrice13 = Close
Transactions = Transactions + 1
ENDIF
IF Transactions=13 AND Close>=(BuyPrice13+AverageTrueRange[14](close)*2) AND LongOnMarket THEN
BUY Pos1 CONTRACTS AT MARKET
BuyPrice14 = Close
Transactions = Transactions + 1
ENDIF
IF Transactions=14 AND Close>=(BuyPrice14+AverageTrueRange[14](close)*2) AND LongOnMarket THEN
BUY Pos1 CONTRACTS AT MARKET
BuyPrice15 = Close
Transactions = Transactions + 1
ENDIF
IF Transactions=15 AND Close>=(BuyPrice15+AverageTrueRange[14](close)*2) AND LongOnMarket THEN
BUY Pos1 CONTRACTS AT MARKET
ENDIF
// Conditions to exit long positions
ATRx = AverageTrueRange[14](close)*2
StopLoss = CALL "2ATR trailing stop"
IF LongOnMarket AND close <= Buyprice + ATRx AND InitialStop>StopLoss THEN
SELL AT InitialStop STOP
ELSE
SELL AT StopLoss STOP
ENDIF
Yes , it appears on large candle, when the Close is superior to previous “BuyPrice” with 2 ATR, so it never happens on small candle (because ATR is small).
The problem is that each time you open an order you increase the “transactions” variable and this is the only variable tested to open a new order (because your Close>Buyprice+ATR*2 condition is always the same) and since the code is read from to to bottom, you add continuously new order (from line 32 to 119).
Hi Nicholas:
The “BuyPrice” is also supposed to change after every pyramid trade: Buyprice2…Buyprice3… is this variable being ignored?
BTW, thanks for your reply. Is there a way to make it execute each pyramid only once? (I tried “ONCE” in combination with IF but it doesn’t work)
Nicholas:
Don’t worry, I solved it.
Cheers.
No, the BuyPrice don’t change because you are constantly storing the same value on the same candle. You can use TRADEPRICE instead, which refer to the real open price of the last N order.