Hi,
I’m trying to correct the following code to close all positions on the weighted average price if there is more than one trade open. If only one trade is open it will close on the indicator signal. Any help would be greatly appreciated.
// Definition of code parameters
DEFPARAM CumulateOrders = True // Cumulating positions deactivated
// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 6
m = 0.4
ONCE positionsize = 0.5
// Conditions to enter long positions
indicator1 = williams[14](close)
c1 = (indicator1 CROSSES OVER -80)
IF NOT LONGONMARKET AND c1 AND not daysForbiddenEntry THEN
BUY positionsize PERPOINT AT MARKET
ENDIF
//let's add another order while price continue to get lower (more than 10 points) than the last order taken with a condition of 1 bars elapsed since then
IF LONGONMARKET AND TRADEINDEX>1 AND close-TRADEPRICE<-20 THEN
Buy PositionSize * (1 + (countofposition * m)) perpoint at market
endif
// Conditions to exit long positions
indicator2 = williams[14](close)
c2 = (indicator2 CROSSES UNDER -20)
IF c2 THEN
SELL AT MARKET
ENDIF
IF COUNTOFPOSITION > 0.5 AND Close > POSITIONPRICE THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
indicator3 = williams[14](close)
c3 = (indicator3 CROSSES UNDER -20)
IF NOT SHORTONMARKET AND c3 AND not daysForbiddenEntry THEN
SELLSHORT positionsize PERPOINT AT MARKET
ENDIF
//let's add another order while price continue to get higher (more than 10 points) than the last order taken with a condition of 1 bars elapsed since then
IF SHORTONMARKET AND TRADEINDEX>1 AND close-TRADEPRICE>20 THEN
Sellshort Positionsize * (1 + (ABS(countofposition) * m)) perpoint at market
endif
// Conditions to exit short positions
indicator4 = williams[14](close)
c4 = (indicator4 CROSSES OVER -80)
IF c4 THEN
EXITSHORT AT MARKET
ENDIF
IF COUNTOFPOSITION < -0.5 AND Close < POSITIONPRICE THEN
EXITSHORT AT MARKET
ENDIF
In order to close the orders basket at average price, add this code at the top of the strategy:
if abs(countofposition)>1 then
if longonmarket then
sell at positionprice limit
else
exitshort at positionprice limit
endif
endif
Thanks for the reply. I’ve added the suggestion to the code but unfortunately it still seems to be closing only on the indicator criteria being met and not the average weighted price when there is more than one trade. Do you know what is causing this?
Many thanks in advance.
This is the code i’m using, seems ok:
// Definition of code parameters
DEFPARAM CumulateOrders = True // Cumulating positions deactivated
if abs(countofposition)>1 then
if longonmarket then
sell at positionprice limit
else
exitshort at positionprice limit
endif
endif
// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 6
m = 0.4
ONCE positionsize = 0.5
// Conditions to enter long positions
indicator1 = williams[14](close)
c1 = (indicator1 CROSSES OVER -80)
IF NOT LONGONMARKET AND c1 AND not daysForbiddenEntry THEN
BUY positionsize PERPOINT AT MARKET
ENDIF
//let's add another order while price continue to get lower (more than 10 points) than the last order taken with a condition of 1 bars elapsed since then
IF LONGONMARKET AND TRADEINDEX>1 AND close-TRADEPRICE<-20 THEN
Buy PositionSize * (1 + (countofposition * m)) perpoint at market
endif
// Conditions to exit long positions
indicator2 = williams[14](close)
c2 = (indicator2 CROSSES UNDER -20)
IF c2 THEN
SELL AT MARKET
ENDIF
IF COUNTOFPOSITION > 0.5 AND Close > POSITIONPRICE THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
indicator3 = williams[14](close)
c3 = (indicator3 CROSSES UNDER -20)
IF NOT SHORTONMARKET AND c3 AND not daysForbiddenEntry THEN
SELLSHORT positionsize PERPOINT AT MARKET
ENDIF
//let's add another order while price continue to get higher (more than 10 points) than the last order taken with a condition of 1 bars elapsed since then
IF SHORTONMARKET AND TRADEINDEX>1 AND close-TRADEPRICE>20 THEN
Sellshort Positionsize * (1 + (ABS(countofposition) * m)) perpoint at market
endif
// Conditions to exit short positions
indicator4 = williams[14](close)
c4 = (indicator4 CROSSES OVER -80)
IF c4 THEN
EXITSHORT AT MARKET
ENDIF
IF COUNTOFPOSITION < -0.5 AND Close < POSITIONPRICE THEN
EXITSHORT AT MARKET
ENDIF
graphonprice positionprice
The white line on the price chart is where the orders should close when accumulating.