Bonjour,
Ci-joint un petit exercice de pyramidage sur le DAX en UT=15min (short only pour les tests dans le contexte actuel…)
DEFPARAM FLATAFTER = 213000
noEntryBeforeTime = 091500
timeEnterBefore = time >= noEntryBeforeTime
noEntryAfterTime = 213000
timeEnterAfter = time < noEntryAfterTime
daysForbiddenEntry = OpenDayOfWeek = 1 OR OpenDayOfWeek = 3 OR OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// Variables
ordersize = 2
TS = 25
SL = 50
TP = 100
//Condition to go short
myMACD = MACD[12,26,9](close)
short = myMACD crosses over 0
//first order + //let's add another stop order while price continue to go lower (more than 15 points)
IF (COUNTOFPOSITION<=ordersize) AND short AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THEN
SELLSHORT ordersize CONTRACTS AT MARKET
SELLSHORT ordersize/2 CONTRACTS AT (CLOSE-30) STOP//should I write close instead of PositionPrice here ?
ENDIF
If ShortOnMarket THEN
SELL AT (POSITIONPRICE-TP) LIMIT
SELL AT (POSITIONPRICE+SL) STOP
ENDIF
//let's add another order while price continue to go lower (more than 15 points)
IF ShortOnMarket AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry and (COUNTOFPOSITION<ordersize*1.5) THEN
SELLSHORT ordersize/2 CONTRACTS AT (TRADEPRICE(1)-30) STOP
ENDIF
//trailing stop
trailingstop = TS//Best 30
//resetting variables when no trades are on market
if not onmarket then
MINPRICE = close
priceexit = 0
endif
//case SHORT order
if shortonmarket then
MINPRICE = MIN(MINPRICE,close) //saving the MFE of the current trade
if positionprice-MINPRICE>=trailingstop*pointsize then //if the MFE is higher than the trailingstop then
priceexit = MINPRICE+trailingstop*pointsize //set the exit price at the MFE + trailing stop price level
endif
endif
//exit on trailing stop price levels
if onmarket and priceexit>0 then
EXITSHORT AT priceexit STOP
SELL AT priceexit STOP
endif
Je ne suis pas loin de ce que je souhaite faire, mais je ne parviens pas à limiter le nombre de positions prises par le système à 3. (les 2 positions initiales et la position additionnelle)
J’ai un problème de compréhension de l’instruction COUNTOFPOSITION visiblement…
Votre aide me sera précieuse.
D’avance merci,
COUNTOFPOSITION est négatif pour les positions de vente à découvert et positif pour les achats. Il faut donc prendre en considération cette donnée dans ta condition de comparaison. Ou alors utiliser ABS(COUNTOFPOSITION) tout simplement.
Il y a aussi COUNTOFSHORTSHARES.
Merci beaucoup Nicolas.
Ce n’est pas encore parfait mais je progresse grâce à ton précieux support.
Je me heurte de nouveau à cette difficulté liée au fait que je souhaite placer (dans le même temps que mon premier ordre de vente) un ordre de vente STOP 6 points en dessous (pour préparer ma pyramide).
Ca fonctionne si je rentre (close-6) mais j’aurais préféré utiliser (positionprice-6) pour considérer le spread et le slippage éventuel à l’ouverture de la bougie… Malheureusement dans ce cas le système est immédiatement arrêté sur le motif que j’ai essayé de placer un ordre avec un STOP ou une limite négative….
Puis-je contourner ce problème stp ?
DEFPARAM FLATAFTER = 213000
noEntryBeforeTime = 091500
timeEnterBefore = time >= noEntryBeforeTime
noEntryAfterTime = 213000
timeEnterAfter = time < noEntryAfterTime
daysForbiddenEntry = OpenDayOfWeek = 1 OR OpenDayOfWeek = 3 OR OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// Variables
ordersize = 2
TS = 10
SL = 15
TP = 35
//Condition to go short
myMACD = MACD[12,26,9](close)
short = myMACD < myMACD[1]
//first order + //let's add another stop order while price continue to go lower (more than 15 points)
IF (abs(COUNTOFPOSITION)<ordersize) AND short AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THEN
SELLSHORT ordersize CONTRACTS AT MARKET
MARK1=close
SELLSHORT ordersize/2 CONTRACTS AT (close-6) STOP//should I write close instead of PositionPrice here ?
EXITSHORT AT MARK1-TP LIMIT
EXITSHORT AT MARK1+SL STOP
ENDIF
If ShortOnMarket and abs(COUNTOFPOSITION)=2 THEN
EXITSHORT AT MARK1-TP LIMIT
EXITSHORT AT MARK1+SL STOP
ENDIF
//let's add another order while price continue to go lower (more than 15 points)
IF ShortOnMarket AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry and (abs(COUNTOFPOSITION)<ordersize*1.5) THEN
SELLSHORT ordersize/2 CONTRACTS AT (TRADEPRICE(1)-6) STOP
MARK2=(2*MARK1+CLOSE)/3
EXITSHORT AT MARK1-TP LIMIT
EXITSHORT AT MARK1+SL STOP
ENDIF
If ShortOnMarket and abs(COUNTOFPOSITION)=3 THEN
EXITSHORT AT MARK2-TP LIMIT
EXITSHORT AT MARK2+SL STOP
ENDIF
//trailing stop
trailingstop = TS//Best 30
//resetting variables when no trades are on market
if not onmarket then
MINPRICE = close
priceexit = 0
endif
//case SHORT order
if shortonmarket then
MINPRICE = MIN(MINPRICE,close) //saving the MFE of the current trade
if positionprice-MINPRICE>=trailingstop*pointsize then //if the MFE is higher than the trailingstop then
priceexit = MINPRICE+trailingstop*pointsize //set the exit price at the MFE + trailing stop price level
endif
endif
//exit on trailing stop price levels
if onmarket and priceexit>0 then
EXITSHORT AT priceexit STOP
SELL AT priceexit STOP
endif
En essayant avec TRADEPRICE ?
Merci de ta réponse Nicolas.
De mémoire çà conduit à une fermeture immédiate de la stratégie et de la position avec la même erreur que POSITIONPRICE. Mais je vais réessayer. Seulement il semble qu’il y a qq bugs sur PRT DEMO ce matin (suite au chgt d’heure ?). Bref, je vais patienter un peu. A bientôt.
En effet TRADEPRICE ne sera connu qu’au Close suivant..
Ok, je comprends le principe. Du coup, je vais me contenter de l’approximation tradeprice=close pour la première barre après prise de position et affiner ensuite.
MERCI