code to close a position when in profit
Forums › ProRealTime English forum › General trading discussions › code to close a position when in profit
- This topic has 7 replies, 4 voices, and was last updated 1 year ago by robertogozzi.
Tagged: positionperf, POSITIONPRICE, Profit, tradeprice
-
-
04/22/2023 at 2:55 PM #213606
Hello to the community!
Could any of the coding wizards write some simple code for me? Here is my request:
If I have an open position I would like to close this position at the close of the first candle in profit, whether long or short.
That is all
Best wishes
Jamed
04/22/2023 at 3:01 PM #213607How about …
1234If abs(TradePrice - Close) > 0 ThenSell at MarketExitShort at MarketEndif2 users thanked author for this post.
04/22/2023 at 6:18 PM #213615To make the code work with both Long and Short trades or in case of accumulation of orders or when partial closing of positions is used, a more accurate line 1 should read:
1IF PositionPerf > 0 THEN2 users thanked author for this post.
04/22/2023 at 10:17 PM #213620Thank you kindly, GraHal and Roberto. This is a great community! I sincerely appreciate how all you guys help with coding for ProRealTime, long may it continue.
1 user thanked author for this post.
04/24/2023 at 9:33 AM #213660If you want to try and max out on a profit into a close, for example, FTSE100 and DAX40 close at 16:30hrs you could also use this;
timeCloseTradeInProfit = 163000
if time = timeCloseTradeInProfit Then
InProfit = (close – tradeprice) * pipsize
If InProfit > 0 THEN
Sell at market
ExitShort at Market
Endif04/24/2023 at 9:47 AM #213662Be warned that
1InProfit = (close – tradeprice) * pipsizeshould read
1InProfit = (close – tradeprice) / pipsizethe first one will convert a price difference of, say 0.0040, into 0.00000040 pips (for FX pairs), the latter will convert it into 40 pips.
In addition it calculates the profit for only LONG trades, while you coded exiting also the SHORT trades and one final ENDIF is missing.
It should be coded this way:
1234567891011timeCloseTradeInProfit = 163000if time = timeCloseTradeInProfit ThenInProfit = (tradeprice - close) * pipsize //SHORT tradesif LongOnMarket THENInProfit = (close – tradeprice) * pipsize //LONG tradesendifIf InProfit > 0 THENSell at marketExitShort at MarketEndifendif1 user thanked author for this post.
04/24/2023 at 12:06 PM #21367104/24/2023 at 4:13 PM #213688It works simply because you only trade DAX and FTSE, which have a price to pip ratio of 1:1, but it doesn’t make it a portable code. You may not need it, but many other may!
-
AuthorPosts
Find exclusive trading pro-tools on