code to close a position when in profit
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
Jamed04/22/2023 at 3:01 PM #21360704/22/2023 at 6:18 PM #21361504/22/2023 at 10:17 PM #21362004/24/2023 at 9:33 AM #213660
If 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 #213662
Be warned that1InProfit = (close – tradeprice) * pipsize
should read1InProfit = (close – tradeprice) / pipsize
the 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 MarketEndifendif
1 user thanked author for this post.04/24/2023 at 12:06 PM #21367104/24/2023 at 4:13 PM #213688