AmerParticipant
Junior
Hello!
Is it possible to program the strategy to turn off after two losses. So if there are two losses regardless of time or if these are consecutive, the strategy is closed for that particular day.
Or if the loss is a certain amount, let’s say 50 dollars, the strategy is turned off for that particular day.
Thanks in advance and best regards!
My code will use the variable TradeON to flag periods when the strategy is freezed. Each new day trading is restored to full functionality:
MaxLosses = 2 //after 2 losses stop trading
MaxLoss = 50 //after 50 € loss stop trading
Once TradeON = 1
Once MyProfit = 0
If IntraDayBarIndex = 0 then
TradeON = 1
Tally = 0
MyProfit = StrategyProfit
Endif
If StrategyProfit < StrategyProfit[1] then
Tally = Tally + 1
If Tally = MaxLosses then
TradeON = 0
Endif
Endif
If (MyProfit - StrategyProfit) >= MaxLoss then
TradeON = 0
Endif
If MyLongConditions and TradeuON then
BUY 1 Contract at Market
Endif
If MyShortConditions and TradeON then
SELLSHORT 1 Contract at Market
Endif
Link to above code saved as Log 359 here …
Snippet Link Library
Hi!
I searched and found this thread and need some coding help. What I want do is: if the last 2 trades were losses, dont take any trade for the next 500 bars, instead of the rest of the day. How could I code that?
There you go:
ONCE Pause = 500 //wait 500 bars before trading again
ONCE Losses = 0
ONCE TradeON = 1
IF StrategyProfit > StrategyProfit[1] THEN
Losses = 0
ELSIF StrategyProfit < StrategyProfit[1] THEN
Losses = Losses + 1
LossBAR = BarIndex
IF Losses = 2 THEN
Losses = 0
TradeON = 0
ENDIF
ENDIF
IF (BarIndex -LossBAR) = Pause THEN
TradeON = 1
ENDIF
//
IF MyLongConditions AND TradeON THEN
BUY 1 CONTRACT AT MARKET
ELSIF MyShortConditions AND TradeON THEN
SELLSHORT 1 CONTRACT AT MARKET
ENDIF
Hi robertogozzi
I need some coding help. What I want do is: if the last 3 trades were winning , dont take any trade for the next 10 bars How could I code that?
There you go:
ONCE Pause = 10 //wait 10 bars before trading again
ONCE MaxWins = 3 //pause after 3 consecutive wins
ONCE Wins = 0
ONCE TradeON = 1
IF StrategyProfit > StrategyProfit[1] THEN
Wins = Wins + 1
IF Wins = MaxWins THEN
TradeON = 0
winBAR = BarIndex
Wins = 0
ENDIF
ELSIF StrategyProfit < StrategyProfit[1] THEN
Wins = 0
ENDIF
IF (BarIndex - winBAR) = Pause THEN
TradeON = 1
ENDIF
//
MyLongConditions = Not OnMarket AND Close CROSSES OVER Average[20,0](close)
MyShortConditions = Not OnMarket AND Close CROSSES UNDER Average[20,0](close)
//
IF MyLongConditions AND TradeON THEN
BUY 1 CONTRACT AT MARKET
ELSIF MyShortConditions AND TradeON THEN
SELLSHORT 1 CONTRACT AT MARKET
ENDIF
SET STOP %LOSS 0.5
SET TARGET %PROFIT 1
//graph Wins coloured("Green")
//graph TradeON coloured("Red")
thank you thats help my code alooot
I have just one more idea..
What I want do is: its like a trailing stop but not on the position it will be on the portfolio i will give you an example:
lets say i gain 1000$ and the trade is closed if he open a trade again i want him to secure 500$ from the last trade if he touch it he will shut down the system.
also Every time I win multiples of 500, I want the device to stop at a rate of 500.
This seems to work as expected:
ONCE Pause = 10 //wait 10 bars before trading again
ONCE MaxWins = 3 //pause after 3 consecutive wins
ONCE Wins = 0
ONCE TradeON = 1
ONCE GainTreshold = 1000 //Gain treshold above which a 50% profit is locked in
ONCE myStrategyProfit = 0
ONCE myGainStopLevel = 0
// exit if the locked in profit is reached
IF OnMarket AND (myGainStopLevel > 0) THEN
tempProfit = positionperf * positionprice * PipValue
IF (StrategyProfit + tempProfit) <= myGainStopLevel THEN
SELL AT MARKET
EXITSHORT AT MARKET
Quit
ENDIF
ENDIF
//check the wins and update the locked in profit, if large enough
IF StrategyProfit > StrategyProfit[1] THEN
Wins = Wins + 1
IF Wins = MaxWins THEN
TradeON = 0
winBAR = BarIndex
Wins = 0
ENDIF
//check if locked in profit has to be updated
IF StrategyProfit > 0 THEN
//set tha base for the calculations
BaseProfit = StrategyProfit[1]
//if the previous StrategyProfit was negative set the BaseProfit to 0,
//to avoid an incorrect addition, due to subtracting a negative value
IF StrategyProfit[1] < 0 THEN
BaseProfit = 0
ENDIF
Gain = StrategyProfit - BaseProfit
IF Gain >= GainTreshold THEN
//lock in additional 50% profits
myGainStopLevel = BaseProfit + round(Gain / 2)
ENDIF
ENDIF
ELSIF StrategyProfit < StrategyProfit[1] THEN
Wins = 0
ENDIF
//set trading again after pausing
IF (BarIndex - winBAR) = Pause THEN
TradeON = 1
ENDIF
//
MyLongConditions = Not OnMarket AND Close CROSSES OVER Average[20,0](close)
MyShortConditions = Not OnMarket AND Close CROSSES UNDER Average[20,0](close)
//
IF MyLongConditions AND TradeON THEN
BUY 1 CONTRACT AT MARKET
ELSIF MyShortConditions AND TradeON THEN
SELLSHORT 1 CONTRACT AT MARKET
ENDIF
SET STOP %LOSS 0.5
SET TARGET %PROFIT 1
//graph Wins coloured("Green")
//graph TradeON coloured("Red")
graph StrategyProfit coloured("Fuchsia")
graph myGainStopLevel coloured("Blue")