Hello!
I’m trying to add a parameter that won’t allow my system to continue trading after 2 consecutive losses in the same day, but without quitting the system. I’ve copy pasted from a couple of old threads here, but can’t quite get it to work. This is what I’ve “come up with”
Once TradeON = 1
If IntraDayBarIndex = 0 then
TradeON = 1
Endif
if strategyprofit <> strategyprofit[1] then
if positionperf(1)<0 then
perte=perte+1
else
perte=0
endif
endif
if perte >=2 then
TradeON = 0
endif
Does anyone know what I’m doing wrong or have any ideas about how to code this?
Seems fine to me, TRADEON variable should be tested before entries:
if buycondition and TRADEON then
buy at market
endif
I’ve added TRADEON to the buy conditions like you show, but it still seems to take more positions then “allowed”. Here is an example, though i reckon it’s not enough to go on. It shouldn’t have taken the position on the 6th at 19.18 with this code, am I right?
You should add this line just after line 3 (before ENDIF):
perte=0
but this has nothing to do with your issue.
It’s a nother issue, since you never reset PERTE at the beginning of a new day, it may happen that you never enter at market again after 2 losses as STERATEGYPROFIT will never change again!
The question you asked is to stop after 2 LOSSES, not after 2 trades.
Your issue is due to using POSITIONPERF instead of STRATEGYPROFIT.
Try this code:
Once TradeON = 1
If IntraDayBarIndex = 0 then
TradeON = 1
perte = 0
Endif
if strategyprofit < strategyprofit[1] then
perte=perte+1
elsif strategyprofit > strategyprofit[1] then
perte=0
endif
if perte =2 then
TradeON = 0
endif
I believe I understand the logic of your code. Perte is set to 0 at new day and if two losses are recorded then tradeon=0. Makes sense.
But it still takes positions after 2 losses. The positions are exactly the same as with the code I first used.
It’s quite odd because if I change “if perte =2″ then to “if perte =1” it seems to work flawlessly, but then closes after 1 loss..
I think yoy haven’t added TradeON to your conditions to enter a position.
My code works like a charm:
Once TradeON = 1
If IntraDayBarIndex = 0 then
TradeON = 1
perte = 0
Endif
if strategyprofit < strategyprofit[1] then
perte = perte + 1
elsif strategyprofit > strategyprofit[1] then
perte=0
endif
if perte = 2 then
TradeON = 0
endif
IF close CROSSES OVER average[20] and not onmarket and TradeON THEN
BUY AT Market
ENDIF
IF close CROSSES UNDER average[20] and not onmarket and TradeON THEN
SELLSHORT AT Market
ENDIF
SET TARGET pPROFIT 100
SET STOP pLOSS 50
graph perte coloured(255,0,0,255)
graph TradeON coloured(0,0,255,255)
I’ve definitely added it, see that portion of my code here.
As you can see from the other picture it still takes a position after the second loss just before 20.00.
Shouldn’t it have paused after that loss?
If you write the code AFTER any entry, it will be evaluated correctly the next bar.
As lines dealing with TradeON and perte affect entries, they should be place BEFORE them.
Code is read and executed sequentially.
I was not aware of that. It’s working perfectly now.
Thanks!