I propose this code which takes a position on trend rebounds at the 32.2% and 61.8% Fibonacci level with a Williams% R confirmation, and the Vwap Weekly as a trend indicator. I created this backtest on the CAC 40 in 30Min time frame But I think that this strategy can be applied to other assets and also be optimized.
The details are in the code in comments to help understanding.
Thank you
//================================================//
// ========== PARAMETRE GENERAUX ================ //
//================================================//
DEFPARAM CumulateOrders = false
// REINVESTISSEMENT DES GAINS
REINV = 1 // 0 = NON / 1 = OUI
IF REINV = 0 THEN
Capital = CapitalInit
ELSIF REINV = 1 THEN
Capital = CapitalInit + (strategyprofit*2/3)
ENDIF
//================================================//
// ========== PARAMETRES DES POSITIONS ========== //
//================================================//
// MONEY MANAGEMENT // 3 VARIABLES A PARAMETRER //
Capitalinit = 10000 // CAPITAL INITIAL
RisquePourc = 1.5 // RISQUE PAR TRADE EN %
DSL = 46 // DISTANCE AU STOP LOSS
RisquePartrade = capital*RisquePourc*0.01
RisqueParContrat = DSL*pipsize
N = RisquePartrade / RisqueParContrat
IF N < 1 then N = 1 endif // PLAGES HORRAIRES DE TRADING // CtimeAchat1 = time >= 080000 and time < 223000
//================================================//
// ========== PARAMETRE DES INDICATEURS ========= //
//================================================//
// RETRACEMENT DE FIBONACCI ET LOCALISATION DES ZONES // 1 VARIABLE A PARAMETRER //
NbCycle = 54 // NOTE : LE NOMBRE DE BOUGIE PRECEDENTE CORRESPOND A UN CYCLE , EXEMPLE 40 BOUGIES DE 30 MIN REPRESENTENT UN CYCLE DE 20H00 //
hh = highest[NbCycle](high) // hh est le plus haut des X bougies précédentes
ll = lowest[NbCycle](low) // LL est le plus bas des X bougies précédentes
amplitudefibo = ((hh - ll)/ll)*100 // L'amplitude est égale au plus haut des NBP précédentes
RT382 = ll+(hh-ll)*0.382 // Niveau de retracement 38,2%
RT618 = ll+(hh-ll)*0.618 // Niveau de retracement 61,8%
RT788 = ll+(hh-ll)*0.788 // Niveau de retracement 78,8%
// LES V-WAP YEARLY , MONTHLY ET DAYLY //
//calcule de la période
if DayOfWeek=0 or (dayofweek[1]=5 and dayofweek<>5) then
weekbar=barindex
endif
if month<>month[1] then
monthbar=barindex
endif
if year<>year[1] then
yearbar=barindex
endif
once dWeekly=1
once dMonthly=1
once dYearly=1
dWeekly = max(dWeekly, barindex-weekbar)
dMonthly = max(dMonthly, barindex-monthbar)
dYearly = max(dYearly, barindex-yearbar)
VWAPweekly = SUMMATION[dWeekly](volume*typicalprice)/SUMMATION[dWeekly](volume)
VWAPmonthly = SUMMATION[dMonthly](volume*typicalprice)/SUMMATION[dMonthly](volume)
VWAPyearly = SUMMATION[dYearly](volume*typicalprice)/SUMMATION[dYearly](volume)
// LE Volatility index //
PTotETMoving=20
PeriodET = 2 //"Ecart type"
PeriodTotET = 18 //"Période de recherche",minval=0)
PeriodEMA= 3 //"Période EMA",minval=0, type=input.integer)
BMax= 0.8 //"Borne Maximum",minval=0, type=input.float)
BMin= 0.2 //"Borne Minimum",minval=0, type=input.float)
ET = std[PeriodET]
ETmaxpostTotET=highest[PeriodTotET](ET)
if (PTotETMoving<PeriodTotET+PeriodET and barindex>PeriodET) then
PTotETMoving= PTotETMoving+1
if ETmaxPeriodET+PeriodTotET) then
ETmax=ETmaxpostTotET
endif
Volat=(ET/ETmax)
MMExp=average[PeriodEMA,1](volat)
//williams %R//
Catest = williams[22]<williams(close) and williams < -31 //================================================// // ======== LES CONDITIONS D'ENTREE LONG ======== // //================================================// //Les condition de Momentum// CaMom1 = hh > VWApWeekly // CONDITION MOMENTUM 1 :le cours cloture au dessus de la VWAP monthly
CaMom2 = VWAPWEEKLY > VWAPWEEKLY[21] // La Vwap weekly est ascendante : la vwap weekly cloture au dessus de la vwap weekly a 24 périodes
CaMom3 = MMExp < 0.2 // Le marché est toujours en zone d'accumulation : La Moyenne mobile exponentiel est au dessous de la borne Minimum Bmin //les conditions de localisation// //achat sur REBOND premier arrêt 38,2% de retracement// CaLoc4 = amplitudefibo > 0.8 //la hauteur minumum du mouvement de référence en pourcentage
CaLoc5 = amplitudefibo < 1.4 //La hauteur maximum du mouvement de référence en pourcentage
//ENTREE LONG SUR PREMIER ARRET //
//Les Conditions de figure //
CaFiG6 = LOWEST[1](low) < RT382 AND CLOSE[1] > RT382 //La bougie précédente croise les 38,2% de retracement mais cloture au dessus
//stoploss et take profit //
takeprofit = 2.02
Stoploss = (tradeprice*takeprofit/100)*0.5
If stoploss > DSL Then
stoploss = DSL
endif
If CtimeAchat1 AND CaMom1 AND CaMom2 AND CaMom3 AND CaLoc4 AND CaLoc5 AND CaFig6 and Catest THEN
BUY n SHARES AT MARKET
set target %profit takeprofit
set stop loss Stoploss ptrailing stoploss*1.02
ENDIF
//ENTREE LONG SUR RELOAD ZONE //
// les conditions de Momentum
CaMom1bis = hh > VWApWeekly // CONDITION MOMENTUM 1 :le cours cloture au dessus de la VWAP monthly
CaMom2bis = VWAPWEEKLY > VWAPWEEKLY[54] // La Vwap weekly est ascendante : la vwap weekly cloture au dessus de la vwap weekly a 24 périodes
CaMom3bis = MMExp < 0.18 // Le marché est toujours en zone d'accumulation : La Moyenne mobile exponentiel est au dessous de la borne Minimum Bmin CaLoc4bis = amplitudefibo > 0.8
CaLoc5bis = amplitudefibo < 1.4
//Les Conditions de figure //
CaFiG6bis = LOWEST[1](low) < RT618 AND CLOSE[1] > RT618 //La bougie précédente croise les 61,8% de retracement mais cloture au dessus
CaFig7bis = open>close
//stoploss et take profit //
takeprofitbis = 2.51
Stoplossbis = (tradeprice*takeprofit/100)*0.4
If stoplossbis > DSL Then
stoplossbis = DSL
endif
If CtimeAchat1 AND CaMom1bis AND CaMom2bis AND CaMom3bis AND CaLoc4bis AND CaLoc5bis AND CaFig6bis AND CaFig7bis and catest THEN
BUY n SHARES AT MARKET
set target %profit takeprofitbis
set stop loss Stoplossbis ptrailing stoplossbis*1.54
ENDIF
//===============================================//
// ======= LES CONDITIONS D'ENTREE SHORT ======= //
//===============================================//
//achat sur REBOND premier arrêt 38,2% de retracement//
//Les condition de Momentum//
CvMom1 = hh < VWApWeekly[75] // CONDITION MOMENTUM 1 :le cours cloture en dessous de la VWAP monthly
CvMom2 = VWAPWEEKLY < VWAPWEEKLY[22] // La Vwap weekly est ascendante : la vwap weekly cloture en dessous de la vwap weekly a 22 périodes CvMom3 = MMExp > 0.27 // Le marché est toujours en zone d'accumulation : La Moyenne mobile exponentiel est au dessous de la borne Minimum Bmin
CvWill = williams[9]<williams(close) //les conditions de localisation// CvLoc4 = amplitudefibo > 0.8 //la hauteur minumum du mouvement de référence en pourcentage
CvLoc5 = amplitudefibo < 1.95 //La hauteur maximum du mouvement de référence en pourcentage //ENTREE LONG SUR PREMIER ARRET SHORT// //Les Conditions de figure // CvFiG6 = HIGHEST[1](HIGH) > RT618 AND CLOSE[1] < RT618 //La bougie précédente croise les 38,2% de retracement mais cloture EN DESSOUS //stoploss et take profit // takeprofit = 1.98 StoplossV = (tradeprice*takeprofit/100)*0.44 If stoplossV > DSL Then
stoplossV = DSL
endif
If CtimeAchat1 AND CvMom1 and CvMom2 AND CvMom3 AND CvWill AND CvLoc4 AND CvLoc5 AND CvFig6 THEN
sellshort n SHARES AT MARKET
set target %profit takeprofit
set stop loss StoplossV ptrailing stoplossV*1.08
ENDIF
//achat sur REBOND premier arrêt 61.8% de retracement//
//Les condition de Momentum//
CvMom1Vbis = hh < VWApWeekly[64] // CONDITION MOMENTUM 1 :le cours cloture en dessous de la VWAP monthly
CvMom2Vbis = VWAPWEEKLY < VWAPWEEKLY[21] // La Vwap weekly est ascendante : la vwap weekly cloture en dessous de la vwap weekly a 22 périodes
CvMom3Vbis = MMExp < 0.49 // Le marché est toujours en zone d'accumulation : La Moyenne mobile exponentiel est au dessous de la borne Minimum Bmin
CvWillVbis = williams[43]<williams(close) and williams > -23
//les conditions de localisation//
CvLoc4Vbis = amplitudefibo > 0.8 //la hauteur minumum du mouvement de référence en pourcentage
CvLoc5Vbis = amplitudefibo < 1.3 //La hauteur maximum du mouvement de référence en pourcentage //ENTREE LONG SUR PREMIER ARRET SHORT// //Les Conditions de figure // CvFiG6Vbis = HIGHEST[1](HIGH) > RT382 AND CLOSE[1] < RT382 //La bougie précédente croise les 61.2% de retracement mais cloture EN DESSOUS //stoploss et take profit // takeprofitVbis = 2.57 StoplossVbis = (tradeprice*takeprofit/100)*0.44 If stoplossVbis > DSL Then
stoplossVbis = DSL
endif
If CtimeAchat1 AND CvMom1Vbis AND CvMom2Vbis AND CvMom3Vbis AND CvWillVbis AND CvLoc4Vbis AND CvLoc5Vbis AND CvFig6Vbis THEN
sellshort n SHARES AT MARKET
set target %profit takeprofitVbis
set stop loss StoplossVbis ptrailing stoplossVbis*1.1
ENDIF