partial close when price moves against position
Forums › ProRealTime English forum › ProOrder support › partial close when price moves against position
- This topic has 3 replies, 2 voices, and was last updated 2 weeks ago by Vicke1990.
-
-
11/04/2024 at 9:24 PM #239957
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:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657// --- Additional Variables and Logic ---Savingstop = 10if not onmarket thenAllowPartialCloseNegative = 0PartialCloseNegativeDone = 0 // Återställ flaggan för partiell stängning vid negativ rörelseminPriceNegative = 0 // Återställ för långa positioner vid negativ rörelsemaxPriceNegative = 0 // Återställ för korta positioner vid negativ rörelsepositionSizeNegative = 0 // Återställ positionsstorlek för negativ rörelse// Återställ även andra variabler om det behövsendif//## -- FUNKTION FÖR PARTIELL STÄNGNING VID OGYNNSAM RÖRELSE -- ##//## - KÖPORDRARif longonmarket then// Vid nytt inträde, initiera variablerif not longonmarket[1] thenminPriceNegative = closepositionSizeNegative = size // Sätt positionsstorleken till initial storlekPartialCloseNegativeDone = 0 // Säkerställ att vi kan göra en partiell stängningendif// Uppdatera minPriceNegative till lägsta pris sedan inträdetminPriceNegative = min(minPriceNegative, close)// Trigger för att börja övervaka för en partiell stängning när priset går emot positionenif (tradeprice - minPriceNegative >= Savingstop * pointsize) and (PartialCloseNegativeDone = 0) thenAllowPartialCloseNegative = 1endifif AllowPartialCloseNegative thenif positionSizeNegative >= size thencontractsToCloseNegative = max(mincontracts, size / 2)sell contractsToCloseNegative contracts at marketSET STOP LOSS PrimaryStopLevelAllowPartialCloseNegative = 0PartialCloseNegativeDone = 1positionSizeNegative = positionSizeNegative - contractsToCloseNegativeminPriceNegative = closeendifendifendif//## - SÄLJ KORTA ORDERif shortonmarket thenif not shortonmarket[1] thenmaxPriceNegative = closepositionSizeNegative = sizePartialCloseNegativeDone = 0endifmaxPriceNegative = max(maxPriceNegative, close)if (maxPriceNegative - tradeprice >= Savingstop * pointsize) and (PartialCloseNegativeDone = 0) thenAllowPartialCloseNegative = 1endifThe 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 🙂
11/05/2024 at 10:08 AM #239964Hi! take a look this code. Maybe is what you are looking for.
1234567891011121314151617181920212223242526272829303132333435363738394041424344ss=10sl=20DistancePartialStop=ss*pipvaluen=6avg1=average[10](close)avg2=average[50](close)if avg1 crosses over avg2 thenbuy n contract at marketendifif longonmarket thenif not longonmarket[1] thenPrimaryStop=tradeprice-sl*pipvaluePartialStop=close-DistancePartialStoppartial=0a1=255a2=255endifif (close-PartialStop)>DistancePartialStop thenPartialStop=close-DistancePartialStopendifif longonmarket[1] and partial=0 and close < PartialStop[1] thensell COUNTOFLONGSHARES*0.5 contracts at marketpartial=1a1=0endifendifif not onmarket thena1=0a2=0endifset stop ploss slset target pprofit 5*slgraphonprice PartialStop coloured(0,0,255,a1)graphonprice primaryStop coloured(255,0,0,a2)1 user thanked author for this post.
11/06/2024 at 9:12 AM #240018Hi 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:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768defparam cumulateorders=false// --- Partial Close settings ---size = 10mincontracts = 1trigger = 20pointback = 5closepercent = 20// ------------------------------timeframe(15 minutes,updateonclose)st = Supertrend[3,10]if not longonmarket and close crosses over st thenbuy size contracts at marketset stop ploss 15endifif not shortonmarket and close crosses under st thensellshort size contracts at marketset stop ploss 15endiftimeframe(3 minute)if not onmarket thenAllowPartialClose=0endif//## -- PARTIAL CLOSE PRICE BACK FUNCTION -- ##//## - BUY ORDERSif longonmarket then//trigger for the partial closure function to startif close-tradeprice>=trigger*pointsize thenAllowPartialClose = 1endifif AllowPartialClose then//compute the maxprice reachedmaxprice = max(maxprice,close)//check to trigger a partial closure or notif maxprice-close>=pointback*pointsize then//close partiallysell max(mincontracts,size*(closepercent/100)) contracts at market//reset the maxprice to the current pricemaxprice = closeendifendifendif//## - SELLSHORT ORDERSif shortonmarket then//trigger for the partial closure function to startif tradeprice-close>=trigger*pointsize thenAllowPartialClose = 1endifif AllowPartialClose then//compute the maxprice reachedminprice = min(minprice,close)//check to trigger a partial closure or notif close-minprice>=pointback*pointsize then//close partiallyexitshort max(mincontracts,size*(closepercent/100)) contracts at market//reset the maxprice to the current priceminprice = closeendifendifendifgraph st coloured(200,100,200) as "Supertrend"graph Closethank you again 🙂
11/06/2024 at 9:28 AM #240021 -
AuthorPosts
Find exclusive trading pro-tools on