AmerParticipant
Junior
Hello. Got help with coding but it didn’t work. So I write the code and wonder how one could add coding so that the strategy stops after, for example, three losses. It does not need to be programmed for several days, but only for that time. So nothing complicated, but just stop automatically after three losses. Thanks in advance. Here comes the code:
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// The system will cancel all pending orders and close all positions at 0:00. No new ones will be allowed until after the "FLATBEFORE" time.
DEFPARAM FLATBEFORE = 100000
// Cancel all pending orders and close all positions at the "FLATAFTER" time
DEFPARAM FLATAFTER = 121500
// Prevents the system from creating new orders to enter the market or increase position size before the specified time
noEntryBeforeTime = 100000
timeEnterBefore = time >= noEntryBeforeTime
// Prevents the system from placing new orders to enter the market or increase position size after the specified time
noEntryAfterTime = 121500
timeEnterAfter = time < noEntryAfterTime
// Conditions to enter long positions
indicator1 = Stochastic[5,3](close)
indicator2 = Average[3](Stochastic[5,3](close))
c1 = (indicator1 CROSSES OVER indicator2)
IF c1 AND timeEnterBefore AND timeEnterAfter THEN
BUY 1 CONTRACT AT MARKET
ENDIF
// Stops and targets
SET STOP pLOSS 10
SET TARGET pPROFIT 10
JSParticipant
Veteran
Hi @Amer
Maybe you can incorporate this “standard” program into your code…
Once MaxLosingTrades = 3 //Set the maximum number of losing trades
Once Counter = 0 //Counter for counting the losing trades
If IntraDayBarIndex = 0 then //Every new day the counter will reset to zero
Counter = 0
EndIf
If StrategyProfit < StrategyProfit[1] then //Check if the last closed trade is a losing one
Counter = Counter + 1 //If the last trade is a losing one then update the counter
EndIf
If YourConditions and Counter < MaxLosingTrades then // When your buying conditions are valid and the number of losing trades is < 3
Buy 1 contract at Market
EndIf