I am struggling to code in order to avoid re-entering of trade once set profit target is met. For instance if I am long and set profit target is met, the code shouldn’t enter a new long trade although the conditions to go long is still true. Any help?
EricParticipant
Master
Depends of the code, do you also have a sell conditon?
i use sometimes a binary indicator buy = 1 and sell = -1 then i check if that indicator < 0 for buy
Yes I do have sell condition as well
It is taking another long position because the conditions you specified are still being met, if you don’t want it to take another position then you must change the long entry condition or refine it in some way. What you haven’t stated is what you want the system to do after closing a long position, presumably you want it to be able to take another long position at some point? You can tell e.g. to take another long position if the long entry conditions are met after so many bars since the close of the last trade or after a certain amount of time has passed but you need to say what you want it to do.
I would like the system to only take another long position after it had taken a short position, i.e. no 2 successive longs or shorts entries
Assuming you have different conditions for entering longs and shorts then setting and testing a flag will do what you need, see example in attached code sample I used to test it.
Defparam Cumulateorders = false
LONG = 1
SHORT = 2
n = 1
c1 = close > Average[5](close)
c2 = close < Average[3](close)
if not onmarket then
if c1 and LastTrade <> LONG then
BUY n SHARES AT MARKET
LastTrade = LONG
endif
if c2 and LastTrade <> SHORT then
sellshort n shares at market
LastTrade = SHORT
endif
endif
c3 = close < Average[5](close)
c4 = close > Average[3](close)
if c3 then
sell at market
ENDIF
if c4 then
exitshort at market
endif
Thanks, i.t really helped. Exactly what I have been looking for.