Closing trades on weighted average price

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #204257 quote
    neddyboy
    Participant
    New

    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
    #204262 quote
    Nicolas
    Keymaster
    Master

    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
    #204304 quote
    neddyboy
    Participant
    New

    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.

    #204308 quote
    Nicolas
    Keymaster
    Master

    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.

    close-on-average-price.png close-on-average-price.png
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.

Closing trades on weighted average price


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
neddyboy @neddyboy Participant
Summary

This topic contains 3 replies,
has 2 voices, and was last updated by Nicolas
3 years, 3 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 11/16/2022
Status: Active
Attachments: 1 files
Logo Logo
Loading...