Semi BOT XAU/USD Code sans erreur mais pas fonctionnel

Forums ProRealTime forum Français Support ProOrder Semi BOT XAU/USD Code sans erreur mais pas fonctionnel

Viewing 5 posts - 1 through 5 (of 5 total)
  • #250119

    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

     

    #250121

    supprimer la variable orderSent depuis les sections BUY et SELLSHORT.

    Insérez ces lignes immédiatement avant la section // === TAKE PROFITS MANUELS ===:

     

     

    1 user thanked author for this post.
    #250123

    Merci pour votre réponse.

    Trois points à noter.

    • le backtest fonctionne bien.
    • Lorsque j’entre en position long et que le prix redescend pour remonter au prix d’entrée, je rachète la même taille de lot…il faudrait ajouter une ligne qui permette de rester sur un seul ordre d’achat ou de vente.
    • Le code ne fonctionne pas sur le papertrading. je n’ai pas compris pourquoi.

    Si vous aviez une réponse.

    Merci

    #250124

    J’ai pu tester avec le papertrading (je pense qu’il fallait supprimer les fenêtres backtesting).

    Je note que mon trade ne prend qu’un lot de XAU/USD, je pense qu’il y a une erreur dans le calcul et l’application de la taille de la position…

     

    #250125

    vous avez fait le backtest en  quelle unité ?

    si vous voulez tester la taille de position en bas du code ajouter

     

     

    1 user thanked author for this post.
Viewing 5 posts - 1 through 5 (of 5 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login