Closing trades on weighted average price
Forums › ProRealTime English forum › ProOrder support › Closing trades on weighted average price
- This topic has 3 replies, 2 voices, and was last updated 2 years ago by
Nicolas.
-
-
11/16/2022 at 2:31 PM #204257
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.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768// Definition of code parametersDEFPARAM CumulateOrders = True // Cumulating positions deactivated// Prevents the system from placing new orders on specified days of the weekdaysForbiddenEntry = OpenDayOfWeek = 6m = 0.4ONCE positionsize = 0.5// Conditions to enter long positionsindicator1 = williams[14](close)c1 = (indicator1 CROSSES OVER -80)IF NOT LONGONMARKET AND c1 AND not daysForbiddenEntry THENBUY positionsize PERPOINT AT MARKETENDIF//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 thenIF LONGONMARKET AND TRADEINDEX>1 AND close-TRADEPRICE<-20 THENBuy PositionSize * (1 + (countofposition * m)) perpoint at marketendif// Conditions to exit long positionsindicator2 = williams[14](close)c2 = (indicator2 CROSSES UNDER -20)IF c2 THENSELL AT MARKETENDIFIF COUNTOFPOSITION > 0.5 AND Close > POSITIONPRICE THENSELL AT MARKETENDIF// Conditions to enter short positionsindicator3 = williams[14](close)c3 = (indicator3 CROSSES UNDER -20)IF NOT SHORTONMARKET AND c3 AND not daysForbiddenEntry THENSELLSHORT positionsize PERPOINT AT MARKETENDIF//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 thenIF SHORTONMARKET AND TRADEINDEX>1 AND close-TRADEPRICE>20 THENSellshort Positionsize * (1 + (ABS(countofposition) * m)) perpoint at marketendif// Conditions to exit short positionsindicator4 = williams[14](close)c4 = (indicator4 CROSSES OVER -80)IF c4 THENEXITSHORT AT MARKETENDIFIF COUNTOFPOSITION < -0.5 AND Close < POSITIONPRICE THENEXITSHORT AT MARKETENDIF11/16/2022 at 3:06 PM #204262In order to close the orders basket at average price, add this code at the top of the strategy:
1234567if abs(countofposition)>1 thenif longonmarket thensell at positionprice limitelseexitshort at positionprice limitendifendif11/16/2022 at 10:59 PM #204304Thanks 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.
11/17/2022 at 9:30 AM #204308This is the code i’m using, seems ok:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677// Definition of code parametersDEFPARAM CumulateOrders = True // Cumulating positions deactivatedif abs(countofposition)>1 thenif longonmarket thensell at positionprice limitelseexitshort at positionprice limitendifendif// Prevents the system from placing new orders on specified days of the weekdaysForbiddenEntry = OpenDayOfWeek = 6m = 0.4ONCE positionsize = 0.5// Conditions to enter long positionsindicator1 = williams[14](close)c1 = (indicator1 CROSSES OVER -80)IF NOT LONGONMARKET AND c1 AND not daysForbiddenEntry THENBUY positionsize PERPOINT AT MARKETENDIF//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 thenIF LONGONMARKET AND TRADEINDEX>1 AND close-TRADEPRICE<-20 THENBuy PositionSize * (1 + (countofposition * m)) perpoint at marketendif// Conditions to exit long positionsindicator2 = williams[14](close)c2 = (indicator2 CROSSES UNDER -20)IF c2 THENSELL AT MARKETENDIFIF COUNTOFPOSITION > 0.5 AND Close > POSITIONPRICE THENSELL AT MARKETENDIF// Conditions to enter short positionsindicator3 = williams[14](close)c3 = (indicator3 CROSSES UNDER -20)IF NOT SHORTONMARKET AND c3 AND not daysForbiddenEntry THENSELLSHORT positionsize PERPOINT AT MARKETENDIF//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 thenIF SHORTONMARKET AND TRADEINDEX>1 AND close-TRADEPRICE>20 THENSellshort Positionsize * (1 + (ABS(countofposition) * m)) perpoint at marketendif// Conditions to exit short positionsindicator4 = williams[14](close)c4 = (indicator4 CROSSES OVER -80)IF c4 THENEXITSHORT AT MARKETENDIFIF COUNTOFPOSITION < -0.5 AND Close < POSITIONPRICE THENEXITSHORT AT MARKETENDIFgraphonprice positionpriceThe white line on the price chart is where the orders should close when accumulating.
-
AuthorPosts
Find exclusive trading pro-tools on