Above added as Log 206 to here …
Snippet Link Library
Also a Note added re the LOADS of TS in this Topic
Hi,
first a humble thanks for sharing all these cool codes!
I’ve been looking for a suitable protect-your-money-system and have been playing around with some Money Management-, Stop Loss-, Trailing Stop-, Breakeven-, Profit Target-systems and so on. There’s a lot of topics of the same subject with similar methods and I feel a bit overwhelmed of all the information. So I figured, why not ask someone who actually knows 🙂
So Paul and other Jedi-coders. If you would rekommend a setup of protect-your-money-system for a padawan, what would it be and why? I would love to hear your thoughts about MM, SL, TS, BE, PT and such.
Keep up the awesome work! I hope to contribute in the future…
Hi Paul, thanks very much for posting this. Are these to be used in conjunction with the code you posted earlier, April 2019? or they’re designed to work independently of that?
PaulParticipant
Master
@keewee Thanks! My advise, don’t use too many things. Check if something works as you think it should work (display the lines). Only work on mm if you already have a succesfull proven code. Don’t use a big stoplos (depended on timeframe) as in live trading they will occur more frequent then in an optimised backtest. Trailing stop there are a few, like posted before which used %. A.t.m. I prefer an atr based trailing stop. Also the atr breakeven is a nice edition. a pt is sometimes usefull. Too many things though lead quickly to over optimisation!
@nonetheless It’s different. The ones from april use %, this one uses atr. Also the % based uses the underlaying value to make it work on different assets. Probably it can work together if there are no code overlapping, but it’s not the goal. I thought the atr breakeven would be a nice addition, but not worth of a topic so that’s why I posted it here.
Thanks for the clarification, I thought that was prob the case. Going back to the underlaying code, would you mind having a look at this strategy to see if I’ve applied it correctly?
(BTW, how does it feel to be a Jedi-Coder? sounds pretty cool to me. Personally, I’m struggling to get beyond the Jar Jar Binks level)
// Definition of code parameters
DEFPARAM CumulateOrders = false // Cumulating positions deactivated
DEFPARAM preloadbars = 5000
//Money Management DOW
MM = 0 // = 0 for optimization
if MM = 0 then
positionsize=1
ENDIF
if MM = 1 then
ONCE startpositionsize = .4
ONCE factor = 10 // factor of 10 means margin will increase/decrease @ 10% of strategy profit; factor 20 = 5% etc
ONCE factor2 = 20 // tier 2 factor
ONCE margin = (close*.005) // tier 1 margin value of 1 contract in instrument currency; change decimal according to available leverage
ONCE margin2 = (close*.01)// tier 2 margin value of 1 contract in instrument currency; change decimal according to available leverage
ONCE tier1 = 55 // DOW €1 IG first tier margin limit
ONCE maxpositionsize = 550 // DOW €1 IG tier 2 margin limit
ONCE minpositionsize = .2 // enter minimum position allowed
IF Not OnMarket THEN
positionsize = startpositionsize + Strategyprofit/(factor*margin)
ENDIF
IF Not OnMarket THEN
IF startpositionsize + Strategyprofit/(factor*margin) > tier1 then
positionsize = (((startpositionsize + (Strategyprofit/(factor*margin))-tier1)*(factor*margin))/(factor2*margin2)) + tier1 //incorporating tier 2 margin
ENDIF
IF Not OnMarket THEN
if startpositionsize + Strategyprofit/(factor*margin) < minpositionsize THEN
positionsize = minpositionsize //keeps positionsize from going below allowed minimum
ENDIF
IF (((startpositionsize + (Strategyprofit/(factor*margin))-tier1)*(factor*margin))/(factor2*margin2)) + tier1 > maxpositionsize then
positionsize = maxpositionsize// keeps positionsize from going above IG tier 2 margin limit
ENDIF
ENDIF
ENDIF
ENDIF
once enableSL = 1 // stop loss
once enablePT = 1 // profit target
once enableTS = 1 // trailing stop
once enableBE = 1 // breakeven stop
once displaySL = 1 // stop loss
once displayPT = 1 // profit target
once displayTS = 1 // trailing stop
once displayBE = 1 // breakeven stop
SL = 1.9 // % stop loss
PT = 2.3 // % profit target
TS = 0.26 // % trailing stop
BESG = 0.25 // % break even stop gain
BESL = 0.00 // % break even stop level
// underlaying security / index / forex
// profittargets and stoploss have to match the lines
// 0.01 FOREX [i.e. GBPUSD=0.01]
// 1.00 SECURITIES [i.e. aapl=1 ;
// 100.00 INDEXES [i.e. dax=100]
// 100=XAUUSD
// 100=CL US Crude
// DAX=100
underlaying=100
// reset at start
if intradaybarindex=0 then
longtradecounter=0
shorttradecounter=0
endif
pclong = longtradecounter<1
pcshort = shorttradecounter<1
TIMEFRAME(2 hours,updateonclose)
Period= 495
inner = 2*weightedaverage[round( Period/2)](typicalprice)-weightedaverage[Period](typicalprice)
HULLa = weightedaverage[round(sqrt(Period))](inner)
c1 = HULLa > HULLa[1]
c2 = HULLa < HULLa[1]
indicator1 = SuperTrend[8,6]
c3 = (close > indicator1)
c4 = (close < indicator1)
ma = average[60,3](close)
c11 = ma > ma[1]
c12 = ma < ma[1]
//Stochastic RSI | indicator
lengthRSI = 15 //RSI period
lengthStoch = 9 //Stochastic period
smoothK = 10 //Smooth signal of stochastic RSI
smoothD = 5 //Smooth signal of smoothed stochastic RSI
myRSI = RSI[lengthRSI](close)
MinRSI = lowest[lengthStoch](myrsi)
MaxRSI = highest[lengthStoch](myrsi)
StochRSI = (myRSI-MinRSI) / (MaxRSI-MinRSI)
K = average[smoothK](stochrsi)*100
D = average[smoothD](K)
c13 = K>D
c14 = K<D
TIMEFRAME(30 minutes,updateonclose)
indicator5 = Average[2](typicalPrice)
indicator6 = Average[7](typicalPrice)
c15 = (indicator5 > indicator6)
c16 = (indicator5 < indicator6)
TIMEFRAME(15 minutes,updateonclose)
indicator2 = Average[4](typicalPrice)
indicator3 = Average[8](typicalPrice)
c7 = (indicator2 > indicator3)
c8 = (indicator2 < indicator3)
ma2 = average[25,1](close)
c17 = ma2 > ma2[1]
c18 = ma2 < ma2[1]
Periodc= 23
innerc = 2*weightedaverage[round( Periodc/2)](typicalprice)-weightedaverage[Periodc](typicalprice)
HULLc = weightedaverage[round(sqrt(Periodc))](innerc)
c9 = HULLc > HULLc[1]
c10 = HULLc < HULLc[1]
TIMEFRAME(10 minutes)
indicator1a = SuperTrend[2,7]
c19 = (close > indicator1a)
c20 = (close < indicator1a)
TIMEFRAME(default)
//Stochastic RSI | indicator
lengthRSIa = 3 //RSI period
lengthStocha = 6 //Stochastic period
smoothKa = 9 //Smooth signal of stochastic RSI
smoothDa = 3 //Smooth signal of smoothed stochastic RSI
myRSIa = RSI[lengthRSIa](close)
MinRSIa = lowest[lengthStocha](myrsia)
MaxRSIa = highest[lengthStocha](myrsia)
StochRSIa = (myRSIa-MinRSIa) / (MaxRSIa-MinRSIa)
Ka = average[smoothKa](stochrsia)*100
Da = average[smoothDa](Ka)
c23 = Ka>Da
c24 = Ka<Da
ma3 = average[15,3](close)
c21 = ma3 > ma3[1]
c22 = ma3 < ma3[1]
Periodb= 15
innerb = 2*weightedaverage[round( Periodb/2)](typicalprice)-weightedaverage[Periodb](typicalprice)
HULLb = weightedaverage[round(sqrt(Periodb))](innerb)
c5 = HULLb > HULLb[1]and HULLb[1]<HULLb[2]
c6 = HULLb < HULLb[1]and HULLb[1]>HULLb[2]
// Conditions to enter long positions
IF pclong and c1 AND C3 AND C5 and c7 and c9 and c11 and c13 and c15 and c17 and c19 and c21 and c23 THEN
BUY positionsize CONTRACT AT MARKET
longtradecounter=longtradecounter+1
ENDIF
// Conditions to enter short positions
IF pcshort and c2 AND C4 AND C6 and c8 and c10 and c12 and c14 and c16 and c18 and c20 and c22 and c24 THEN
SELLSHORT positionsize CONTRACT AT MARKET
shorttradecounter=shorttradecounter+1
ENDIF
//================== exit in profit
if longonmarket and C6 and c8 and close>positionprice then
sell at market
endif
If shortonmarket and C5 and c7 and close<positionprice then
exitshort at market
endif
//==============exit at loss
if longonmarket AND c2 and c6 and close<positionprice then
sell at market
endif
If shortonmarket and c1 and c5 and close>positionprice then
exitshort at market
endif
// to set & display stoploss
if enableSL then
set stop %loss SL
if displaysl then
if not onmarket then
sloss=0
elsif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
sloss=0
endif
if onmarket then
if longonmarket then
sloss=tradeprice(1)-((tradeprice(1)*SL)/underlaying)*pointsize
endif
if shortonmarket then
sloss=tradeprice(1)+((tradeprice(1)*SL)/underlaying)*pointsize
endif
endif
graphonprice sloss coloured(255,0,0,255) as "stoploss"
//sloss=sloss
endif
endif
// to set & display profittarget
if enablePT then
set target %profit PT
if displaypt then
if not onmarket then
ptarget=0
elsif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
ptarget=0
endif
if onmarket then
if longonmarket then
ptarget=tradeprice(1)+((tradeprice(1)*PT)/underlaying)*pointsize
endif
if shortonmarket then
ptarget=tradeprice(1)-((tradeprice(1)*PT)/underlaying)*pointsize
endif
endif
graphonprice ptarget coloured(121,141,35,255) as "profittarget"
//ptarget=ptarget
endif
endif
// trailing stop
if enableTS then
trailingstop = (tradeprice/100)*TS
if not onmarket then
maxprice=0
minprice=close
priceexit=0
endif
if ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
maxprice=0
minprice=close
priceexit=0
endif
if longonmarket then
maxprice=max(maxprice,close)
if maxprice-tradeprice(1)>=(trailingstop) then
priceexit=maxprice-(trailingstop/(underlaying/100))*pointsize
endif
endif
if shortonmarket then
minprice=min(minprice,close)
if tradeprice(1)-minprice>=(trailingstop) then
priceexit=minprice+(trailingstop/(underlaying/100))*pointsize
endif
endif
if longonmarket and priceexit>0 then
sell at priceexit stop
endif
if shortonmarket and priceexit>0 then
exitshort at priceexit stop
endif
if displayTS then
graphonprice priceexit coloured(0,0,255,255) as "trailingstop"
endif
endif
// break even stop
if enableBE then
if not onmarket then
newsl=0
endif
if ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
newsl=0
endif
if longonmarket then
if close-tradeprice(1)>=(((tradeprice(1)/100)*BESG)/(underlaying/100))*pointsize then
newsl=tradeprice(1)+(((tradeprice(1)/100)*BESL)/(underlaying/100))*pointsize
endif
endif
if shortonmarket then
if tradeprice(1)-close>=(((tradeprice(1)/100)*BESG)/(underlaying/100))*pointsize then
newsl=tradeprice(1)-(((tradeprice(1)/100)*BESL)/(underlaying/100))*pointsize
endif
endif
if longonmarket and newsl>0 then
sell at newsl stop
endif
if shortonmarket and newsl>0 then
exitshort at newsl stop
endif
if displayBE then
graphonprice newsl coloured(244,102,27,255) as "breakevenstop"
endif
endif
graph (positionperf*100)coloured(0,0,0,255) as "positionperformance"
PaulParticipant
Master
It was a very cool way of saying! Still got a lot of work to do to feel like that!
I’ve loaded up your code on the dji 5min and looking at the lines, it looks correct!
You have trailingstop & the breakeven almost go active at the same level, which makes the breakeven not effective.
how about increasing the trailingstop slightly and using the breakeven to reduce the stoploss?
(optimised without spread)
try activate ts at 0.5%, besg at 0.3 and besl at -0.3 and still >90% winchance.
or ts at 0.5, besg at 0.3 and besl at 0.05%
that’s also great with 97% winchance.
Excellent, thanks ever so much. I haven’t even begun to play with the numbers as I wanted to be sure I had it set up right (just copied in what I had) .
And your ATR TS should also work as a variation, replacing the %TS?
i cannot run the code because the instruction “Graph”…may somebody help me? thank you
You just have to rem out any line that says graph, or graphonprice etc. For example:
//graphonprice newsl coloured(244,102,27,255) as "breakevenstop"
endif
endif
//graph (positionperf*100)coloured(0,0,0,255) as "positionperformance"
PaulParticipant
Master
And your ATR TS should also work as a variation, replacing the %TS?
yes but i’am not too sure about the atr trailingstop combined with % Breakeven, as both have something in common, like newsl.
Hi!
I am backtesting “Vectorial us100 v5” and want to test it on different instruments. On some instrument it runs the backtest as it should. On others it stops taking positions after a few trades or makes no trades at all.
This is a part of the original code:
// used for sl/pt/ (not used for ts); not to be optimized!
// forex use 0.01; securities use 1, index use 100
// profittargets and stoploss have to match the lines with displaysl/tp/ts set to 1
underlaying=100
If I for example try Forex with the setting 0.01 instead of 100 it still doesn´t work.
“profittargets and stoploss have to match the lines with displaysl/tp/ts set to 1”
Is this the problem and what does it mean to match the lines?
PaulParticipant
Master
@OboeOpt match the lines means what it says.
if a stoploss is x% and you have the entryprice, the red line should exactly be there where you have your stoploss. If displayed at another (crazy) level, then you need to adjust the underlaying value.
Sometimes a strategy isn’t suitable for every market on any timeframe, because of range or volume etc.
You can post your itf file , with info on which market & timeframe , so I can quickly look if you put in right
Thanks for your answer Paul!
I´m just experimenting with
https://www.prorealcode.com/wp-content/uploads/2019/07/vectorial-us100-v5.0p-5m.itf
For example gbp/usd and other Forex mini with underlaying=0.01 I don´t get any trades (no other Changes to the code).
Same problem for stocks with underlaying=1. Works for most index with value 100.
I use IG as broker and timeframe 5 minutes.
PaulParticipant
Master
oke had a look on gbp/usd.
to test if the core code is right, I removed ts/sl/pt etc and and just used a default small stoploss&pt.
The core code of the vectorial dax doesn’t create a trade so it has nothing to do with the exit criteria which use the underlaying value.
Ok, so there is no Quick fix to change just to scan through instruments looking for candidates where vectorial can work?
Is there anything in perticular in the core code that does that no signal is triggered for some instruments? Just tested several Swedish stocks and didn´t get any trades.