partial close when price moves against position

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

    Hi Coders and traders,

    I’m looking for a code that can do a partial close when price moves against my current position of 10 pips/ points,

    Right now, i’m using this code:

    // --- Additional Variables and Logic ---
    Savingstop = 10
    
    if not onmarket then
        AllowPartialCloseNegative = 0
        PartialCloseNegativeDone = 0  // Återställ flaggan för partiell stängning vid negativ rörelse
        minPriceNegative = 0          // Återställ för långa positioner vid negativ rörelse
        maxPriceNegative = 0          // Återställ för korta positioner vid negativ rörelse
        positionSizeNegative = 0      // Återställ positionsstorlek för negativ rörelse
        // Återställ även andra variabler om det behövs
    endif
    
    //## -- FUNKTION FÖR PARTIELL STÄNGNING VID OGYNNSAM RÖRELSE -- ##
    
    //## - KÖPORDRAR
    if longonmarket then
        // Vid nytt inträde, initiera variabler
        if not longonmarket[1] then
            minPriceNegative = close
            positionSizeNegative = size  // Sätt positionsstorleken till initial storlek
            PartialCloseNegativeDone = 0 // Säkerställ att vi kan göra en partiell stängning
        endif
    
        // Uppdatera minPriceNegative till lägsta pris sedan inträdet
        minPriceNegative = min(minPriceNegative, close)
    
        // Trigger för att börja övervaka för en partiell stängning när priset går emot positionen
        if (tradeprice - minPriceNegative >= Savingstop * pointsize) and (PartialCloseNegativeDone = 0) then
            AllowPartialCloseNegative = 1
        endif
    
        if AllowPartialCloseNegative then
            if positionSizeNegative >= size then
                contractsToCloseNegative = max(mincontracts, size / 2)
                sell contractsToCloseNegative contracts at market
                SET STOP LOSS PrimaryStopLevel
                AllowPartialCloseNegative = 0
                PartialCloseNegativeDone = 1
                positionSizeNegative = positionSizeNegative - contractsToCloseNegative
                minPriceNegative = close
            endif
        endif
    endif
    
    //## - SÄLJ KORTA ORDER
    if shortonmarket then
        if not shortonmarket[1] then
            maxPriceNegative = close
            positionSizeNegative = size
            PartialCloseNegativeDone = 0
        endif
    
        maxPriceNegative = max(maxPriceNegative, close)
    
        if (maxPriceNegative - tradeprice >= Savingstop * pointsize) and (PartialCloseNegativeDone = 0) then
            AllowPartialCloseNegative = 1
        endif
    

    The code above works when it’s almost by itself, but I want to combine it with a partial close when price mnoves with my position, and then the code above stops working.

    I want, when the price moves against my position by 10 points, I want half of my position to be sold, the rest of the position to be sold at my “Primarystop”, My primarystop works as my SL, 20 points away from my entry price.

    Anyone who can/ want to help me?

    Thank you 🙂

    #239964 quote
    Iván González
    Moderator
    Master

    Hi! take a look this code. Maybe is what you are looking for.

    ss=10
    sl=20
    DistancePartialStop=ss*pipvalue
    
    n=6
    
    avg1=average[10](close)
    avg2=average[50](close)
    
    if avg1 crosses over avg2 then
    buy n contract at market
    endif
    
    if longonmarket then
    
    if not longonmarket[1] then
    PrimaryStop=tradeprice-sl*pipvalue
    PartialStop=close-DistancePartialStop
    partial=0
    a1=255
    a2=255
    endif
    
    if (close-PartialStop)>DistancePartialStop then
    PartialStop=close-DistancePartialStop
    endif
    
    if longonmarket[1] and partial=0 and close < PartialStop[1] then
    sell COUNTOFLONGSHARES*0.5 contracts at market
    partial=1
    a1=0
    endif
    endif
    
    if not onmarket then
    a1=0
    a2=0
    endif
    
    set stop ploss sl
    set target pprofit 5*sl
    
    graphonprice PartialStop coloured(0,0,255,a1)
    graphonprice primaryStop coloured(255,0,0,a2)
    danskfilou thanked this post
    2024-11-05_10-08.png 2024-11-05_10-08.png
    #240018 quote
    Vicke1990
    Participant
    New

    Hi Ivan,

    thank you for your code, I cant get it to work however. I have a multi timeframe system, where I initially buy size = 2 contracts, and if price moves opposite way of my position I want half of my contracts = 1 to be sold, the rest to keep all other kinds of exits, including the primarystop at 20 points. the other part of the position is connected to a MFE exit.

    Would it be possible for you to write the code with 2 inital contracts and then the rest of your code? So I can copy paste?

    Im using this code for partial close when price is moving the correct way:

    defparam cumulateorders=false
     
    // --- Partial Close settings --- 
    size = 10
    mincontracts = 1
    trigger = 20
    pointback = 5
    closepercent = 20
    // ------------------------------
     
    timeframe(15 minutes,updateonclose)
    st = Supertrend[3,10]
    if not longonmarket and close crosses over st then 
     buy size contracts at market 
     set stop ploss 15 
    endif
    if not shortonmarket and close crosses under st then
     sellshort size contracts at market
     set stop ploss 15
    endif 
     
    timeframe(3 minute)
    if not onmarket then 
     AllowPartialClose=0
    endif
     
    //## -- PARTIAL CLOSE PRICE BACK FUNCTION -- ##
    //## - BUY ORDERS 
    if longonmarket then 
     //trigger for the partial closure function to start 
     if close-tradeprice>=trigger*pointsize then 
      AllowPartialClose = 1
     endif
     
     if AllowPartialClose then 
      //compute the maxprice reached
      maxprice = max(maxprice,close)
      //check to trigger a partial closure or not 
      if maxprice-close>=pointback*pointsize then 
       //close partially 
       sell max(mincontracts,size*(closepercent/100)) contracts at market 
       //reset the maxprice to the current price 
       maxprice = close
      endif 
     endif 
    endif
    //## - SELLSHORT ORDERS
    if shortonmarket then 
     //trigger for the partial closure function to start
     if tradeprice-close>=trigger*pointsize then
      AllowPartialClose = 1
     endif
     
     if AllowPartialClose then
      //compute the maxprice reached
      minprice = min(minprice,close)
      //check to trigger a partial closure or not
      if close-minprice>=pointback*pointsize then
       //close partially
       exitshort max(mincontracts,size*(closepercent/100)) contracts at market
       //reset the maxprice to the current price
       minprice = close
      endif
     endif
    endif
     
    graph st coloured(200,100,200) as "Supertrend"
    graph Close

    thank you again 🙂

    #240021 quote
    Vicke1990
    Participant
    New

    If I just copy-paste the code you provided, this is my result 🙁

    Screenshot-2024-11-06-092638.png Screenshot-2024-11-06-092638.png
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.

partial close when price moves against position


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Vicke1990 @vicke1990 Participant
Summary

This topic contains 3 replies,
has 2 voices, and was last updated by Vicke1990
1 year, 3 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 11/04/2024
Status: Active
Attachments: 2 files
Logo Logo
Loading...