Hi
How would the code look like if i want the following outcome
- Don’t enter the trade when the conditions come true, if the last trade taken by the system was a losing trade
Thankfull for any help!
Yngve
Try this one, it should work. It will disable only ONE trade after any loss, not all trades (this is what you asked, insn’t it?):
DEFPARAM CumulateOrders = false
ONCE LosingTrade = 0 //initialize variable to allow trading
Cond = close CROSSES OVER Average[10] //condition to buy
IF POSITIONPERF(1) < 0 THEN //if the last one was a losing trade...
IF LosingTrade = 0 THEN //... and yhis is the firsr trade afterwards....
LosingTrade = 1 //... then disable trading
ELSE
Cond = 0 //if one losing trade already occurred do not trade and...
LosingTrade = 0 //... enable next trades
ENDIF
ENDIF
IF Cond THEN //Buy if condition is met (unless disabled)
BUY AT MARKET
LosingTrade = 0 //set variable to its initial value
ENDIF
SET TARGET pPROFIT 50
SET STOP pLOSS 20
I have found a logic bug, because I forgot to take note that one trade had already been skipped (after being skipped) despite conditions.
So I added the variable “TradeSkipped” :
DEFPARAM CumulateOrders = false
ONCE LosingTrade = 0 //initialize variable to allow trading
ONCE TradeSkipped = 0 //initialize variable to mark a trade as skipped (after a loss)
Cond = close CROSSES OVER Average[10] //condition to buy
IF POSITIONPERF(1) < 0 THEN //if the last one was a losing trade...
IF TradeSkipped = 0 THEN
IF LosingTrade = 0 THEN //... and this is the firsr trade afterwards....
LosingTrade = 1 //... then disable trading
ELSE
Cond = 0 //if one losing trade already occurred do not trade and...
LosingTrade = 0 //... enable next trades
TradeSkipped= 1 //mark this trade as skipped
ENDIF
ENDIF
ENDIF
IF Cond THEN //Buy if condition is met (unless disabled)
BUY AT MARKET
LosingTrade = 0 //set variable to its initial value
TradeSkipped = 0
ENDIF
SET TARGET pPROFIT 50
SET STOP pLOSS 20
It should work fine now. Let me know after testing it. Sorry for the inconvenience.
Roberto
Thanks again @robertogozzi!
It did reduce the number of consecutive losses on the strategy. But overall the performance was not improved. But thank you for the help!!!