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
How about …
If abs(TradePrice - Close) > 0 Then
Sell at Market
ExitShort at Market
Endif
To 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:
IF PositionPerf > 0 THEN
Thank 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.
smpParticipant
Average
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
Endif
Be warned that
InProfit = (close – tradeprice) * pipsize
should read
InProfit = (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:
timeCloseTradeInProfit = 163000
if time = timeCloseTradeInProfit Then
InProfit = (tradeprice - close) * pipsize //SHORT trades
if LongOnMarket THEN
InProfit = (close – tradeprice) * pipsize //LONG trades
endif
If InProfit > 0 THEN
Sell at market
ExitShort at Market
Endif
endif
smpParticipant
Average
Hi,
The code was indeed just for long trades. It’s been working perfectly daily for me in active Algo’s on DAX and FTSE.
If you look at your code and mine, you are saying the same as I!
It 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!