does anyone know how to code a stop loss as you would use it when you trade manually. For example, the system is placing a trade and the stop is set to be 5 points above or below latest high/low depending of long/short position. To simplify we could say that we want the stop below/above N periods max or min price.
Hi!
Like this?
Slong = Highest [20](high)
Sshort = Lowest [20](low)
If longonmarket then
Set stop loss Slong
Endif
I did not test it since I am mobile. It’s justa Donchian Channel that saves the recent highs and lows.
Yes! thanks! But I think Highest and Lowest needs to change place? If I’m long I want the stop to be att lowest and if I’m short I want the stop to be at highest?
As the stop is tied to highs / lows then the stop level would auto-recalculate at the end of every bar?
When we get multi-timeframe I hope we will be able to have stops recalc on, for example, a 1 Min bar where the System may be running on a 1H bar?
GraHal
@Victor: Yep, the high and low in the stop loss should be reversed. Did it actually work or is Grahal’s asdumption correct? In this case you need to put the stop loss command inside your buy/sell if statement.
If it is already working you need to check what would happen if the market crashes in the very first bar you are in the market. Maybe my snippet above will leave you without a stop because “if longonmarket” is not true when they entry is triggered.
Thanks Derek and Grahal, but the code should be syntaxed like this instead:
//stoploss for long position
If longonmarket then
//calculate the long stoploss (difference between the open price of the trade and the lowest low)
Slong = Tradeprice-Lowest [20](low)
Set stop loss Slong
Endif
//stoploss for short position
if shortonmarket then
//calculate the short order stoploss (difference between the open price of the trade and the highest high)
Sshort = Highest [20](high) - Tradeprice
Set stop loss Sshort
Endif
“Set stop loss” do not use price level, but points amount, so we need to calculate the distance between the order open price and the price level where we want the stoploss to be set.
Hallo, Weiß jemand, ob die SL hier mitziehen? Das sollten sie nicht tun. Aber wenn ich damit mein System berechne, ändern sich die Ergebnisse immer im Backtest. Ich berechne lange, kurze Änderungen. Wenn ich kurz berechne, ändern sich die Ergebnisse für lang wieder. obwohl mein System ein umgekehrtes System ist. was ist das? Wie kann ich das ändern?
Hello, does anyone know whether the SL will pull along? They shouldn’t do that. But when I use it to calculate my system, the results always change in the back test. I calculate long, short changes. If I calculate briefly, the results change again for a long time. although my system is an inverse system. What is it? How can I change this?
@
phoentzs
Only post in the
language of the forum that you are posting in. For example English only in the English speaking forums and French only in the French speaking forums.
Thank you 🙂
It’s not clear what you want.
Post your code to help further.
Excuse me please.
here is my simple code. If I change SL and TP in the long order, the results also change in the short in the backtest. and vice versa.
//EurUsd H1
DEFPARAM CumulateOrders = false
defparam preloadbars = 1000
//daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// VALEURS A DEFINIR
REINV = 0 // 0 = pas de réinvestissement / 1 = réinvestissement des gains
LEVIER = 1 // nombre de contrats par défaut
CAPITALinit = 3000 // Capital initial, à définir pour le réinvestissement des gains
IF REINV = 0 THEN
n = LEVIER
ELSIF REINV = 1 THEN
CAPITAL = CAPITALinit + strategyprofit
n = (CAPITAL / CAPITALinit)*levier
IF n < 1 THEN
n=1
ENDIF
ENDIF
n = round(n)
Nlong = n
Nshort = n
myEMA = ExponentialAverage[21](close) //45 15
myEMA2 = ExponentialAverage[89](close)
c1 = myema crosses over myEMA2
c2 = myema crosses under myEMA2
//IF Time >= 080000 AND Time <= 190000 THEN
IF c1 THEN // not onmarket and
BUY Nlong shares at market
Slong = Tradeprice-Lowest [1](low) //5
Set stop loss Slong
set target profit slong*2
ENDIF
IF c2 THEN //not onmarket and
sellshort Nshort shares at market
Sshort = Highest [1](high) - Tradeprice //5
Set stop loss Sshort
set target profit sshort*2
ENDIF
//endif
if longonmarket and c2 then //or c111
sell at market
endif
if shortonmarket and c1 then //or c222
exitshort at market
endif
if time = 220000 and dayofweek=5 then //and dayofweek=5
sell at market
EXITSHORT at market
endif
Why? what do i have to change?
Replace line 38 with:
IF c1 and not onmarket THEN
and line 46 with:
IF c2 and not onmarket THEN
otherwise when C1 or C2 are true the block IF…ENDIF will be executed and values changed accordingly, despite no further trades would be opened, having accumulation been disabled at line 4.
Unfortunately, the values are still changing. When I calculate long, the short values change in the backtest and vice versa. Something is wrongly coded somewhere. Can someone help me?
//EurUsd H1
DEFPARAM CumulateOrders = false
defparam preloadbars = 1000
//daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// VALEURS A DEFINIR
REINV = 0 // 0 = pas de réinvestissement / 1 = réinvestissement des gains
LEVIER = 1 // nombre de contrats par défaut
CAPITALinit = 3000 // Capital initial, à définir pour le réinvestissement des gains
IF REINV = 0 THEN
n = LEVIER
ELSIF REINV = 1 THEN
CAPITAL = CAPITALinit + strategyprofit
n = (CAPITAL / CAPITALinit)*levier
IF n < 1 THEN
n=1
ENDIF
ENDIF
n = round(n)
Nlong = n
Nshort = n
myEMA = ExponentialAverage[21](close) //45 15
myEMA2 = ExponentialAverage[89](close)
c1 = myema crosses over myEMA2
c2 = myema crosses under myEMA2
//IF Time >= 080000 AND Time <= 190000 THEN
IF c1 and not onmarket THEN // not onmarket and
BUY Nlong shares at market
Slong = (Tradeprice-Lowest [1](low)) //Tradeprice
Set stop loss Slong
set target profit slong*1.5
ENDIF
IF c2 and not onmarket THEN //not onmarket and
sellshort Nshort shares at market
Sshort = (Highest [1](high) - Tradeprice) //Tradeprice
Set stop loss Sshort
set target profit sshort*1.6
ENDIF
//endif
if longonmarket and c2 then
sell at market
endif
if shortonmarket and c1 then
exitshort at market
endif
if time = 220000 and dayofweek=5 then
sell at market
EXITSHORT at market
endif
The addition of
And Not OnMarket is useless, it doesn’t change anything.
Slong never changes while
LongOnMarket and
Sshort never changes while
ShortOnMarket.
There is an error in your code, in lines 40 and 48 you are using TRADEPRICE, but it’s not known yet, it’s the previous entry (or exit) price, since the current price will only be known when the candle closes. It must be replaced with CLOSE.
Lines 55-61 are actually not needed, since you code, as is, is doing Stop & Reverse. Anyway, if you want to use them, move them to line 34.
I added several GRAPH instruction at the end, so that you can check those values candle by candle in the variable window of the backtest.
This is the modified code:
//EurUsd H1
DEFPARAM CumulateOrders = false
defparam preloadbars = 1000
//daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// VALEURS A DEFINIR
REINV = 0 // 0 = pas de réinvestissement / 1 = réinvestissement des gains
LEVIER = 1 // nombre de contrats par défaut
CAPITALinit = 3000 // Capital initial, à définir pour le réinvestissement des gains
IF REINV = 0 THEN
n = LEVIER
ELSIF REINV = 1 THEN
CAPITAL = CAPITALinit + strategyprofit
n = (CAPITAL / CAPITALinit)*levier
IF n < 1 THEN
n=1
ENDIF
ENDIF
n = round(n)
Nlong = n
Nshort = n
myEMA = ExponentialAverage[21](close) //45 15
myEMA2 = ExponentialAverage[89](close)
c1 = myema crosses over myEMA2
c2 = myema crosses under myEMA2
//if longonmarket and c2 then
//sell at market
//endif
//
//if shortonmarket and c1 then
//exitshort at market
//endif
//IF Time >= 080000 AND Time <= 190000 THEN
IF c1 THEN // not onmarket and
BUY Nlong shares at market
Slong = (Close - Lowest [1](low)) //Tradeprice
Set stop loss Slong
set target profit slong*1.5
ENDIF
IF c2 THEN //not onmarket and
sellshort Nshort shares at market
Sshort = (Highest [1](high) - Close) //Tradeprice
Set stop loss Sshort
set target profit sshort*1.6
ENDIF
//endif
if time = 220000 and dayofweek=5 then
sell at market
EXITSHORT at market
endif
graph Slong
graph Sshort
graph c1
graph c2
graph OnMarket