Hi
My trading strategy alternates between long and short trades i.e. long closes and short opens at the same time and vice versa. If a long trade is closed early due to a stop loss being triggered, I want the next trade to be a short trade (and vice versa if a short trade is closed early due to a stop loss being triggered).
Please, how do I code to check whether the previous trade was long or short and whether it was exited due to a stop loss being triggered?
Something like:
IF [previous trade was long] AND [previous trade exited due to stop loss being triggered] THEN
a = 1
ENDIF
IF [previous trade was short] AND [previous trade exited due to stop loss being triggered] THEN
b = 1
ENDIF
I can then use the above a and b as conditions to enter the next trade but I do not know how to code the bits in the square brackets. Please help
Thanks
Sachin
I already used this snippet to identify what was the last order type and if it was in gain, so it could be adapted to make the opposite:
//------------------
//check profit
if strategyprofit<>strategyprofit[1] then
if positionperf(1)>0 then
if close<tradeprice(2) then
gain=-1 //was sell
elsif close>tradeprice(2) then
gain=1 //was buy
endif
else
gain=0
endif
endif
Thanks Nicolas
Is the below code valid and would it give me result of a = 1 if the previous trade was long and b =1 if the previous trade was short?
IF LONGONMARKET(TRADEINDEX(2)) = 1 THEN
a = 1
ENDIF
IF SHORTONMARKET(TRADEINDEX(2)) = 1 THEN
b = 1
ENDIF
Hi Nicolas
Thinking about it further, BARINDEX starts ascending from left to right and ONMARKET starts ascending from right to left. So is the below code correct instead of my reply earlier to give a result of a = 1 if the previous trade was long and b = 1 if the previous trade was short?
IF LONGONMARKET(BARINDEX - TRADEINDEX(2)) = 1 THEN
a = 1
ENDIF
IF SHORTONMARKET(BARINDEX - TRADEINDEX(2)) = 1 THEN
b = 1
ENDIF
Thanks
Sachin
Why don’t you GRAPH Longonmarket and GRAPH a then you will see what the code gives you on the BT results?
If you don’t know what GRAPH involves just say?
That could work but offset must be in brackets and not parenthesis !
Thanks Nicolas
Yes, I should check when to use [] or ()
Thanks GraHal
I was aware of RETURN but completely missed GRAPH. I’ll have a look and come back if I need assistance. Thanks again