Hello every one ! 😀
I wanted to try something with the PP4H. I’ve done this code, I think it is quite nice but not long of backtest so…!
This code is simply the entry of position on the 4h pp between 9:05 and 12:50. By the close(2) crossing it for the first time of the day with a trailing stop 🙂
Tell me what you think about it and if you think that something should be change/optimize 😉
@Paul, maybe this could interest you as I think you like this kind of code 🙂
//-------------------------------------------------------------------------
// Code principal : PP4h avec trailing stop
//-------------------------------------------------------------------------
// Définition des paramètres du code
// Cumul des positions désactivé
DEFPARAM CumulateOrders = False
DEFPARAM PRELOADBARS = 100000000
//************************************************************************
//Horaires de trading
// Annule tous les ordres en attente et ferme toutes les positions à 0:00, puis empêche toute création d'ordre avant l'heure "FLATBEFORE".
DEFPARAM FLATBEFORE = 090000
// Annule tous les ordres en attente et ferme toutes les positions à l'heure "FLATAFTER"
DEFPARAM FLATAFTER = 173000
// Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position avant l'heure spécifiée
noEntryBeforeTime = 090500
timeEnterBefore = time >= noEntryBeforeTime
// Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position après l'heure spécifiée
noEntryAfterTime = 125000
timeEnterAfter = time < noEntryAfterTime
// Empêche le système de placer de nouveaux ordres sur les jours de la semaine spécifiés
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
//************************************************************************
// Calcul Points Pivots 4h
Int = (OpenTime[1] < 010000 AND OpenTime > 010000) OR (OpenTime[1] < 050000 AND OpenTime > 050000) OR (OpenTime[1] < 090000 AND OpenTime > 090000) OR (OpenTime[1] < 130000 AND OpenTime > 130000) OR (OpenTime[1] < 170000 AND OpenTime > 170000) OR (OpenTime[1] < 210000 AND OpenTime > 210000) OR (Openday <> Openday[1] AND DayOfWeek < DayOfWeek[1])
IF (OpenTime Mod 40000 = 10000) OR Int THEN
myLastHigh = myHigh
myLastLow = myLow
myLastClose = Close[1]
myHigh = High
myLow = Low
Premierpassage=0
ELSE
myHigh = Max(myHigh, High)
myLow = Min(myLow, Low)
ENDIF
PP = (myLastHigh + myLastLow + myLastClose) / 3
// Conditions pour ouvrir une position acheteuse
c1 = (close[2] CROSSES UNDER pp)
IF c1 AND timeEnterBefore AND timeEnterAfter AND Premierpassage=0 AND not daysForbiddenEntry THEN
BUY 1 CONTRACT AT MARKET
Premierpassage=1
SET STOP pLOSS 40
ENDIF
// Conditions pour ouvrir une position en vente à découvert
c2 = (close[2] CROSSES OVER pp)
IF c2 AND timeEnterBefore AND timeEnterAfter AND Premierpassage=0 AND not daysForbiddenEntry THEN
SELLSHORT 1 CONTRACT AT MARKET
Premierpassage=1
SET STOP pLOSS 40
ENDIF
//************************************************************************
//trailing stop function
trailingstart = 5 //trailing will start @trailinstart points profit
trailingstep = 1 //trailing step to move the "stoploss"
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart THEN
newSL = tradeprice(1)+trailingstep
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep THEN
newSL = newSL+trailingstep
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart THEN
newSL = tradeprice(1)-trailingstep
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep THEN
newSL = newSL-trailingstep
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
What does Line 27 in your code (copied below) mean / achieve please?
Mod / Modulo is the remainder after a division?
OpenTime Mod 40000 = 10000
Exactly @GraHal! 🙂
It’s the way of calculation of this PP that I found to be the most accurate and effective 🙂
PaulParticipant
Master
Hi Tanou
Can you provide a screenshot of backtest/chart with this?
Here it is @Paul 🙂
I don’t know if it is possible to ask PRT to get more historical data on this UT
PaulParticipant
Master
Thnx, I’ve found that “int” parameter doesn’t make a change if it’s added or not. Could be different today. But the limited data and the number of trades doesn’t make things easy to test. Maybe put it in demo and let’s see how it works out.
That’s exactly what I’ve done @Paul 😉
Hey Tanou,
I’ve tried using your calculation code for 4h PP but I still find some discrepancies now and again. For example, today, October 29th, the real 4h PP from 9 to 13 is supposed to be 26 756.9 on DJI. Your algo found 26 752.07.
Would you have any idea as to why?