Compound and Parital close function
Forums › ProRealTime English forum › ProOrder support › Compound and Parital close function
- This topic has 5 replies, 3 voices, and was last updated 11 months ago by robertogozzi.
-
-
11/09/2023 at 12:42 PM #223423
Hello!
I have a trading idea that I need help with.
I can’t get the partial close function to work with my compound effect, does anyone know what I’m doing wrong?
When i remove line 3 to 5 the partial close function works as it should see attached image.
thankful for any help 🙂
Compound and partial closure123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115// Definition of code parametersDEFPARAM CumulateOrders = False // Cumulating positions deactivated//amount of contract/lot/shares to open for each orderAmount=10takeprofit = PATR*AverageTrueRange[NATR](close) //takeprofit in pointsstoploss = SATR*AverageTrueRange[NATR](close) //stoploss in pointsBreakevenAt = PATR*AverageTrueRange[NATR](close)/3//percent achieved of target to move stop to entry (breakeven)PointsToKeep = 3 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)Lot2Close = Amount/2 //amount of contract/lot/shares quantity to close when breakeven occurs//reset the breakevenLevel when no trade are on marketIF NOT ONMARKET THENbreakevenLevel=0ENDIF// Prevents the system from creating new orders to enter the market or increase position size before the specified timenoEntryBeforeTime = 093000timeEnterBefore = time >= noEntryBeforeTime// Prevents the system from placing new orders to enter the market or increase position size after the specified timenoEntryAfterTime = 173000timeEnterAfter = time < noEntryAfterTime// Prevents the system from placing new orders on specified days of the weekdaysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0 OR OpenDayOfWeek = 1if currentmonth = 4 thenlongmonthcondition = 0elselongmonthcondition = 1endifif currentmonth = 1 thenshortmonthcondition = 0elseshortmonthcondition = 1endif// Conditions to enter long positionsindicator1 = Average[40,8](close) // 10,6indicator2 = Average[40,3](close) // 50,7c1 = (indicator1 crosses over indicator2)indicator3 = Average[200,1](close)if close > indicator3 thentradeonlong = 1elsetradeonlong = 0endifIF NOT LongOnMarket AND c1 and tradeonlong and timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry and longmonthcondition THENBUY amount CONTRACT AT MARKETENDIF// Conditions to enter Short positionsindicator4 = Average[42,0](close) // 90,1indicator5 = Average[29,0](close) // 50,4c2 = (indicator4 crosses over indicator5)if close < indicator3 thentradeonshort = 1elsetradeonshort = 0endifIF NOT ShortOnMarket AND c2 and tradeonshort and timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry and shortmonthcondition THENSELLSHORT amount CONTRACTS AT MARKETENDIF// Stops and targetsNATR = 14 //ATR PeriodSATR = 2 // ATR Multiplier for StopPATR = 3// ATR Multiplier for Profit//Stop and TargetSET STOP LOSS SATR*AverageTrueRange[NATR](close)SET TARGET PROFIT PATR*AverageTrueRange[NATR](close)startBreakeven = takeprofit*(BreakevenAt/100)//how much pips/points in gain to activate the breakeven function?// --- BUY SIDE ---//test if the price have moved favourably of "startBreakeven" points alreadyIF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN//calculate the breakevenLevelbreakevenLevel = tradeprice(1)+PointsToKeep*pipsizeENDIF//place the new stop orders on market at breakevenLevelIF LONGONMARKET AND breakevenLevel>0 THENSELL AT breakevenLevel STOPif countoflongshares=amount thensell Lot2Close contract at marketendifENDIF// --- end of BUY SIDE ---// --- SELL SIDE ---//test if the price have moved favourably of "startBreakeven" points alreadyIF SHORTONMARKET AND tradeprice(1)-close>=startBreakeven*pipsize THEN//calculate the breakevenLevelbreakevenLevel = tradeprice(1)-PointsToKeep*pipsizeENDIF//place the new stop orders on market at breakevenLevelIF SHORTONMARKET AND breakevenLevel>0 THENEXITSHORT AT breakevenLevel STOPif countofshortshares=amount thenexitshort Lot2Close contract at marketendifENDIF// --- end of SELL SIDE ---11/09/2023 at 12:57 PM #223426line 1-9 should be as below
123456789// Definition of code parametersDEFPARAM CumulateOrders = False // Cumulating positions deactivatedCapital = 2000Equity = Capital + StrategyProfitPosition = (0.03*equity)/stoploss//amount of contract/lot/shares to open for each orderAmount=position11/09/2023 at 5:28 PM #223449Without analysing your code really, it looks like you need to change line 3 to
1Once Capital = 2000Now you assign your capital once, and in the next call of your code (at the next bar(s)) the Capital will increase (or decrease 😉 ).
Regards,
Peter11/09/2023 at 6:28 PM #22345311/09/2023 at 6:54 PM #22345412/26/2023 at 11:22 AM #225700I added the ROUND() instruction to make sure there’s only 1 decimal place.
I also added MinLots to set a minimum number of lots to be traded (but I suggest that it be at least 2, or that the minimun lots to exit is allowed to be half that number, i.e. if you set a max 1 lotsize, then the broker must allow exiting 0.5 lots) and MinExit to exit partial poisitions.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120// Definition of code parametersDEFPARAM CumulateOrders = False // Cumulating positions deactivatedonce Capital = 2000once MinLots = 2 //minimum lotsizeonce MinExit = 1 //minimum number of lots to exitEquity = Capital + StrategyProfitPosition = max(MinLots,round((0.03*equity)/stoploss,1))//amount of contract/lot/shares to open for each orderAmount=positiontakeprofit = PATR*AverageTrueRange[NATR](close) //takeprofit in pointsstoploss = SATR*AverageTrueRange[NATR](close) //stoploss in pointsBreakevenAt = PATR*AverageTrueRange[NATR](close)/3//percent achieved of target to move stop to entry (breakeven)PointsToKeep = 3 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)Lot2Close = max(MinExit,round(Amount/2,1)) //amount of contract/lot/shares quantity to close when breakeven occurs//reset the breakevenLevel when no trade are on marketIF NOT ONMARKET THENbreakevenLevel=0ENDIF// Prevents the system from creating new orders to enter the market or increase position size before the specified timenoEntryBeforeTime = 093000timeEnterBefore = time >= noEntryBeforeTime// Prevents the system from placing new orders to enter the market or increase position size after the specified timenoEntryAfterTime = 173000timeEnterAfter = time < noEntryAfterTime// Prevents the system from placing new orders on specified days of the weekdaysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0 OR OpenDayOfWeek = 1if currentmonth = 4 thenlongmonthcondition = 0elselongmonthcondition = 1endifif currentmonth = 1 thenshortmonthcondition = 0elseshortmonthcondition = 1endif// Conditions to enter long positionsindicator1 = Average[40,8](close) // 10,6indicator2 = Average[40,3](close) // 50,7c1 = (indicator1 crosses over indicator2)indicator3 = Average[200,1](close)if close > indicator3 thentradeonlong = 1elsetradeonlong = 0endifIF NOT LongOnMarket AND c1 and tradeonlong and timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry and longmonthcondition THENBUY amount CONTRACT AT MARKETENDIF// Conditions to enter Short positionsindicator4 = Average[42,0](close) // 90,1indicator5 = Average[29,0](close) // 50,4c2 = (indicator4 crosses over indicator5)if close < indicator3 thentradeonshort = 1elsetradeonshort = 0endifIF NOT ShortOnMarket AND c2 and tradeonshort and timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry and shortmonthcondition THENSELLSHORT amount CONTRACTS AT MARKETENDIF// Stops and targetsNATR = 14 //ATR PeriodSATR = 2 // ATR Multiplier for StopPATR = 3// ATR Multiplier for Profit//Stop and TargetSET STOP LOSS SATR*AverageTrueRange[NATR](close)SET TARGET PROFIT PATR*AverageTrueRange[NATR](close)startBreakeven = takeprofit*(BreakevenAt/100)//how much pips/points in gain to activate the breakeven function?// --- BUY SIDE ---//test if the price have moved favourably of "startBreakeven" points alreadyIF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN//calculate the breakevenLevelbreakevenLevel = tradeprice(1)+PointsToKeep*pipsizeENDIF//place the new stop orders on market at breakevenLevelIF LONGONMARKET AND breakevenLevel>0 THENSELL AT breakevenLevel STOPif countoflongshares=amount thensell Lot2Close contract at marketendifENDIF// --- end of BUY SIDE ---// --- SELL SIDE ---//test if the price have moved favourably of "startBreakeven" points alreadyIF SHORTONMARKET AND tradeprice(1)-close>=startBreakeven*pipsize THEN//calculate the breakevenLevelbreakevenLevel = tradeprice(1)-PointsToKeep*pipsizeENDIF//place the new stop orders on market at breakevenLevelIF SHORTONMARKET AND breakevenLevel>0 THENEXITSHORT AT breakevenLevel STOPif countofshortshares=amount thenexitshort Lot2Close contract at marketendifENDIF// --- end of SELL SIDE ---graph Amountgraph Lot2Close -
AuthorPosts
Find exclusive trading pro-tools on