close half position when reach half limit and move stoploss to breakeven
Forums › ProRealTime English forum › ProOrder support › close half position when reach half limit and move stoploss to breakeven
- This topic has 21 replies, 5 voices, and was last updated 1 year ago by GraHal.
-
-
03/07/2021 at 8:42 AM #163347
hi,
can someone help me out with this? i want to close half my position when the market has reach have my limit and then move stoploss to breakeven.
12345678ONCE PerCent = 0.50 //0.50 = close half positionsIF positionperf = 0.5 AND LongOnMarket THENSELL (abs(CountOfPosition) * PerCent) AT Marketset stop ploss 0ELSIF positionperf = 0.5 AND ShortOnMarket THENEXITSHORT (abs(CountOfPosition) * PerCent) AT Marketset stop ploss 0ENDIFhere’s what i got so far
thank you
03/07/2021 at 9:02 AM #163348You cannot expect POSITIONPERF to equal 0.5% exactly. Change it to >=
Also SET STOP pLOSS 0 does not set any stop loss – it simply cancels any stop loss on the market. Use SELL or EXITSHORT AT POSITIONPRICE STOP and place this order at every bar.
1 user thanked author for this post.
03/07/2021 at 9:08 AM #163349Something like: (not tested)
1234567891011121314151617181920212223242526ONCE PerCent = 0.50 //0.50 = close half positionsif not onmarket and (your long entry conditions) thenbuy 1 contract at marketflag = 0endifif not onmarket and (your short entry conditions) thensellshort 1 contract at marketflag = 0endifIF positionperf >= 0.5 AND LongOnMarket THENSELL (abs(CountOfPosition) * PerCent) AT Marketflag = 1endifIF positionperf >= 0.5 AND ShortOnMarket THENEXITSHORT (abs(CountOfPosition) * PerCent) AT Marketflag = 1ENDIFif flag thensell at positionprice stopexitshort at positionprice stopendif1 user thanked author for this post.
03/07/2021 at 9:21 AM #163350im getting an error for line
1SELL (abs(CountOfPosition) * PerCent) AT Marketat the “AT”
03/07/2021 at 9:24 AM #16335103/07/2021 at 9:30 AM #163352does not seem to work. i got picture. if it worked then it should sell half and then breakeven with this example.
03/07/2021 at 10:02 AM #163358Yes it needs a little tweeking:
1234567891011121314151617181920212223242526ONCE PerCent = 0.50 //0.50 = close half positionsif not onmarket and (your long entry conditions) thenbuy 1 contract at marketflag = 0endifif not onmarket and (your short entry conditions) thensellshort 1 contract at marketflag = 0endifIF positionperf >= 0.5 AND LongOnMarket and not flag THENSELL (abs(CountOfPosition)) * PerCent contracts AT Marketflag = 1endifIF positionperf >= 0.5 AND ShortOnMarket and not flag THENEXITSHORT (abs(CountOfPosition) * PerCent) contracts AT Marketflag = 1ENDIFif flag thensell at positionprice stopexitshort at positionprice stopendif03/08/2021 at 1:10 AM #163397still the same result.
i tried changing the codes but the results are still the same.
1234567891011121314if not onmarket and longconditions and buyentry thenbuy 1 contracts at marketflag = 0endifONCE PerCent = 0.50 //0.50 = close half positionsIF ((close - positionprice) >= 0.5*longstoploss) AND LongOnMarket and flag = 0 THENSELL (abs(CountOfPosition) * PerCent) contracts AT Marketflag = 1ENDIFif flag = 1 thensell at positionprice stopendif03/08/2021 at 1:19 AM #163398think i got it
1234567891011121314if not onmarket and longconditions and buyentry thenbuy 1 contracts at marketflag = 0endifONCE PerCent = 0.50 //0.50 = close half positionsIF ((close - positionprice)/pipsize >= 0.5*longstoploss) AND LongOnMarket and flag=0 THENSELL (abs(CountOfPosition) * PerCent) contracts AT Marketflag = 1ENDIFif flag=1 thensell at positionprice stopendif03/08/2021 at 9:13 AM #16341503/09/2021 at 9:17 AM #163562my codes seems to work half the time. half being it close half and move stoploss to breakeven and the other one is when profit reaches half the limi, it closes half 2 times resulting closing the full trade. anyone got any ideas why?
03/09/2021 at 10:01 AM #163566As I said – I tested my code and it worked just fine.
You changed the exit price calculation so that it is no longer a percentage using POSITIONPERF – why?
It is highly likely that your problem is being created by the code that you are choosing not to show us – do you have any other stop orders in the code? Without all the information such as the value of longstoploss the instrument and time frame and the rest of the code it is very difficult for anyone to help you or see what you are saying is happening.
A screenshot is worth a thousand words.
03/09/2021 at 10:18 AM #163569heres the screen shot of what my current codes are doing on prorealtime.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061defparam CUMULATEORDERS = falsetimeframe(15mn)ema37 = ExponentialAverage[37](close)ema50 = ExponentialAverage[50](close)bar15mn = barindex//conditions to go longlongc1 = ema37 - ema50 > 2*pipsizelongc2 = high[1] < high[2] and high[2] < high[3]longc3 = (low[1] crosses under ema37[1]) or (low[1] crosses under ema50[1])longc4 = (close[1] > ema50[1]) and (close[2] > ema50[2]) and (close[3] > ema50[3])longconditions = longc1 and longc2 and longc3 and longc4if not onmarket and (longconditions ) thenprehigh15mn = high[1]prelow15mn = low[1]endiftimeframe(default)timeframe(default)once tradeON = 1mybar = bar15mnif mybar <>mybar[1] thentradeon = 1endif//long entry triggerbuyentry = (close >= prehigh15mn)spreadave = 2.5minstop = 5if not onmarket and longconditions and buyentry and tradeon thenbuy 1 contracts at markettradeon = 0flag = 0if (close - prelow15mn+spreadave*pipsize)/pipsize > minstop and (close - prelow15mn+spreadave*pipsize)/pipsize < 50 thenlongstoploss = (close - prelow15mn+spreadave*pipsize)/pipsizeelsif (close - prelow15mn+spreadave*pipsize)/pipsize <= minstop thenlongstoploss = minstopelsif (close - prelow15mn+spreadave*pipsize)/pipsize >= 50 thenlongstoploss = 50endifendiflongtarget = longstoplossset stop ploss longstoplossset target pprofit longtargetONCE PerCent = 0.50 //0.50 = close half positionsIF ((close - positionprice)/pipsize >= 0.5*longstoploss) AND LongOnMarket and not flag THENSELL (abs(CountOfPosition) * PerCent) contracts AT Marketflag = 1ENDIFif flag thensell at positionprice stopendifthis is only for going long.
03/09/2021 at 10:20 AM #163572however, on ig trading the results r different. when it reached half my limit, it close half and another half. resulting in closing full.
03/09/2021 at 10:26 AM #163573this is the screenshot with the codes u suggested. line 52 to line 55 replaced with these
1234IF positionperf >= 0.5 AND LongOnMarket and not flag THENSELL (abs(CountOfPosition)) * PerCent contracts AT Marketflag = 1endif -
AuthorPosts
Find exclusive trading pro-tools on