Hello everyone!
I hope you are well.
I am coding a very simple code that sells or buys on the first cross of the Monthly PP. However I am not able to get it right. In the following code I can’t mange to get the correct Monthly PP…
Can someone help…? 🙂
Thanks!
//-------------------------------------------------------------------------
// Code principal : PPM avec trailing stop
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
// Code principal : MonSystème
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
// Code principal : PPM avec trailing stop
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
// Code principal : PPM avec trailing stop
//-------------------------------------------------------------------------
DEFPARAM Preloadbars = 10000
// Cumul des positions désactivé
DEFPARAM CumulateOrders = False
//************************************************************************
//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 = 080000
// Annule tous les ordres en attente et ferme toutes les positions à l'heure "FLATAFTER"
DEFPARAM FLATAFTER = 170000
// 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 = 080000
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 = 170000
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
//************************************************************************
If Month<>Month[1] then
monthlyHigh = Highest[max(1,BarIndex - lastMonthBarIndex)](High)[1]
monthlyLow = Lowest[max(1,BarIndex - lastMonthBarIndex)](Low)[1]
lastMonthBarIndex = BarIndex
pp = (monthlyHigh + monthlyLow + Close[1]) / 3
endif
graph pp
If Month<>Month[1] then
Premierpassage=0
Endif
//************************************************************************
// Conditions pour ouvrir une position acheteuse
c1 = (close 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 55
ENDIF
// Conditions pour ouvrir une position en vente à découvert
c2 = (close 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 55
ENDIF
If Premierpassage=0 and c1 or c2 then
Premierpassage=1
endif
//************************************************************************
//trailing stop function
trailingstart = 15 //trailing will start @trailinstart points profit
trailingstep = 5 //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
As English is the language you chose to use, this topic is moved from french proorder forum to english proorder forum, please see rules in yellow box below created in order to make sure the forums remain tidy for everybody’s ease of use, thank you
Sorry, my bad. Thanks Noobywan!
Make sure line 2 of the indicator reads:
Once mode = 0
since you are using MODE 0.
Thanks @Robertogozzi. However I can’tfigure out what you mean… If I add it to line 2 it gets me an error…
How do you know the values differ?
I think because the indicator (by Nicolas) from which you copied the formula plots a different value, doesn’t it? Well… the predefined mode in that indicator is 1, while your code uses mode 0, change mode in your code or the initial default setting in the indicator.
For instance, when I write “graph pp”, in August, the Monthly PP was 12624.33 and the instruction above that I use in the code give me a different value, whatever the mode of calculation… I can’t find the real Monthly PP…
Do you have suggestions?
Here is another version of the code that I tried but no evolution. Still wrong calculation of the monthly PP
//-------------------------------------------------------------------------
// Code principal : PPM avec trailing stop
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
// Code principal : MonSystème
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
// Code principal : PPM avec trailing stop
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
// Code principal : PPM avec trailing stop
//-------------------------------------------------------------------------
DEFPARAM Preloadbars = 10000
// Cumul des positions désactivé
DEFPARAM CumulateOrders = False
//************************************************************************
//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 = 080000
// Annule tous les ordres en attente et ferme toutes les positions à l'heure "FLATAFTER"
DEFPARAM FLATAFTER = 170000
// 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 = 080000
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 = 170000
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
//************************************************************************
timeframe(5 minutes,updateonclose)
mode = 1
If Month<>Month[1] then
monthlyHigh = Highest[BarIndex - lastMonthBarIndex](High)[1]
monthlyLow = Lowest[BarIndex - lastMonthBarIndex](Low)[1]
lastMonthBarIndex = BarIndex
If mode = 0 then
pp = (monthlyHigh + monthlyLow + Close[1]) / 3
Elsif mode = 1 then
pp = (Open + monthlyHigh + monthlyLow + Close[1]) / 4
Elsif mode = 2 then
pp = (monthlyHigh + monthlyLow + Close[1]*2) / 4
Else
pp = (Open*2 + monthlyHigh + monthlyLow) / 4
Endif
Endif
timeframe(default)
Graph pp
If Month<>Month[1] then
Premierpassage=0
Endif
//************************************************************************
// Conditions pour ouvrir une position acheteuse
c1 = (close 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 55
ENDIF
// Conditions pour ouvrir une position en vente à découvert
c2 = (close 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 55
ENDIF
//************************************************************************
//trailing stop function
trailingstart = 15 //trailing will start @trailinstart points profit
trailingstep = 5 //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
As asked you before, why do you say it is incorrect?
What is the correct value (and how is it calculated)?
It’s calculated with PRT Monthly PP. I compare what my code tells with the PRT ones
I just found that it calculates it not when we change month exactly. I calculate it about 1 hour after we change month. Do you know why?
OpenMonth (not Month) should be used, replace line 40 with:
OpenMonth<>OpenMonth[1]
Thanks @Robertogozzi, it helps a bit I think but didn’t solve the problem…
Do you have a Monthly PP code that works for you in 1 minute timeframe ?
Here is a picture of what it gives me and what’s real in PRT…
Your code above works fine, but uou’ll have to OpenMonth throughout your code, not just line 40. And you have to use enough bars (I used 100K 1-minute units).