Bonjour,
Merci pour ce forum.
j’ai écris avec l’aide de chat gpt un code pour prendre des positions sur le gold papier.
L’idée est qu’avant chaque trade j’entre mon prix d’entrée, long ou court et mes TPs. Les variables sont en Gras.
Le bot fait le reste:
- calcul de la taille de la position.
- Gestion du SL
- remonte le SL au BE
Si quelqu’un pouvait jeter un œil ça serait vraiment top!
Voici le code:
// =============================
// === PARAMÈTRES À MODIFIER ===
// =============================
entryPrice = 3380 // Prix d’entrée manuel
direction = 1 // 1 = Achat, -1 = Vente
riskPercent = 3.5 // Risque en % du capital
capital = 85000 // Capital total en USD
// =============================
// === SL ET CALCULS CONTRATS ===
// =============================
slDistancePrice = 2800 // SL en USD
pipValuePerContract = 1 // Valeur d’un pip par contrat (USD)
slInPips = slDistancePrice / pipSize
slValuePerContract = slInPips * pipValuePerContract
riskAmount = capital * (riskPercent / 100)
positionSizeFloat = riskAmount / slValuePerContract
positionSize = floor(positionSizeFloat)
// Sécurité si position arrondie à 0
if positionSize < 1 then
positionSize = 1
endif
// =============================
// === TAKE PROFITS MANUELS ===
// =============================
TP1 = 3381
TP2 = 3382
TP3 = 3383
TP4 = 3384
TP5 = 3395
// === POURCENTAGES DE SORTIE PAR TP ===
tp1Pct = 0.10
tp2Pct = 0.10
tp3Pct = 0.20
tp4Pct = 0.30
tp5Pct = 0.30
// === Initialisation des variables persistantes ===
once tp1Done = 0
once tp2Done = 0
once tp3Done = 0
once tp4Done = 0
once tp5Done = 0
once beMoved = 0
slLevel = 0.0
once orderSent = 0
// =============================
// === CALCUL DU SL INITIAL ===
// =============================
if barindex = 1 then
if direction = 1 then
slLevel = entryPrice – slDistancePrice
else
slLevel = entryPrice + slDistancePrice
endif
endif
// =============================
// === ENTRÉE EN POSITION ===
// =============================
if not onmarket and orderSent = 0 then
if direction = 1 and close < entryPrice then
buy positionSize shares at entryPrice stop
orderSent = 1
endif
if direction = -1 and close > entryPrice then
sellshort positionSize shares at entryPrice stop
orderSent = 1
endif
endif
// =============================
// === GESTION DE LA POSITION ===
// =============================
if positionperf(0) <> 0 then
// Répartition des positions par TP
tp1Size = round(positionSize * tp1Pct)
tp2Size = round(positionSize * tp2Pct)
tp3Size = round(positionSize * tp3Pct)
tp4Size = round(positionSize * tp4Pct)
tp5Size = positionSize – (tp1Size + tp2Size + tp3Size + tp4Size)
// === GESTION POUR ACHAT ===
if direction = 1 then
if tp1Done = 0 and close >= TP1 then
sell tp1Size shares at market
tp1Done = 1
slLevel = entryPrice // Break Even
beMoved = 1
endif
if tp2Done = 0 and close >= TP2 then
sell tp2Size shares at market
tp2Done = 1
endif
if tp3Done = 0 and close >= TP3 then
sell tp3Size shares at market
tp3Done = 1
endif
if tp4Done = 0 and close >= TP4 then
sell tp4Size shares at market
tp4Done = 1
endif
if tp5Done = 0 and close >= TP5 then
sell tp5Size shares at market
tp5Done = 1
endif
// Stop Loss
if close <= slLevel then
sell positionSize shares at market
endif
// === GESTION POUR VENTE ===
else
if tp1Done = 0 and close <= TP1 then
exitshort tp1Size shares at market
tp1Done = 1
slLevel = entryPrice // Break Even
beMoved = 1
endif
if tp2Done = 0 and close <= TP2 then
exitshort tp2Size shares at market
tp2Done = 1
endif
if tp3Done = 0 and close <= TP3 then
exitshort tp3Size shares at market
tp3Done = 1
endif
if tp4Done = 0 and close <= TP4 then
exitshort tp4Size shares at market
tp4Done = 1
endif
if tp5Done = 0 and close <= TP5 then
exitshort tp5Size shares at market
tp5Done = 1
endif
// Stop Loss
if close >= slLevel then
exitshort positionSize shares at market
endif
endif
endif