Codage pour Backtest croisement de moyennes mobiles et breakeven

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #165629 quote
    maxlys
    Participant
    Senior

    Bonjour a tous,

    j’utilise un indicateur avec flèches (bleu pour achat et rouge pour vente) qui me spécifie mon passage d’ordre.

    J’ai tenté de le backtester à la main mais sur plusieurs UT et actifs, cela est vraiment contraignants et très long.

    Ne connaissant pas la programmation pour réaliser du backtet automatique, je fais appel à une âme charitable.

    Dans ce code de backtest automatique j’aimerais avoir les variables suivantes :

    TP fixe = x

    Stop fixe = x

    Et le top serait d’avoir un passage au breakeven a une valeur que je défini

     

    Exemple

    TP = 8 points

    Stop = 8 points

    Breakeven = 4 points

     

    voici le code de mon indicateur et les conditions :

    Achat=0
    Vente=0
    MM20=exponentialaverage[20](close)
    MM50=exponentialaverage[50](close)
    MM80 = average[80](close)
    MM200 = average[200]
    //ACHAT
    C1 = (MM20 CROSSES OVER MM50) and  MM80 > MM200
    //VENTE
    C2 = (MM20 CROSSES UNDER MM50) and MM80 < MM200
    IF C1 then
    drawarrowup(barindex, low-2*pipsize) coloured(0,0,255)
    ELSIF C2 Then
    drawarrowdown(barindex, high+2*pipsize) coloured(255,0,0)
    Endif
    
    
    Return

    Merci par avance de votre aide

    #165632 quote
    Nicolas
    Keymaster
    Master

    Tu peux retourner les conditions C1 et C2 dans ton indicateur, tu seras alors en mesure de l’ajouter dans l’assistant de création pour les programmes de trading automatique :

    return c1 as "achat", c2 as "vente"
    #165640 quote
    maxlys
    Participant
    Senior

    Merci Nicolas.

    Voici ce que j’ai fait avec l’assistant de création (ici le backtest est pourri mais on verras plus tard pour optimiser).

    Je n’ai pas su trouver comment placer la fonction breakeven. Ici par exemple, j’ai mis un TP à 45 points, j’aimerais que lorsque nous arrivon a 20 points le stop remonte a BE.

    Comment ajouter cette fonction ?

    Merci

    // Définition des paramètres du code
    DEFPARAM CumulateOrders = False // Cumul des positions désactivé
    // 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 = 091500
    // Annule tous les ordres en attente et ferme toutes les positions à l'heure "FLATAFTER"
    DEFPARAM FLATAFTER = 173000
    
    // 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
    
    // Conditions pour ouvrir une position acheteuse
    indicator1 = ExponentialAverage[20](close)
    indicator2 = ExponentialAverage[50](close)
    c1 = (indicator1 CROSSES OVER indicator2)
    indicator3 = Average[80](close)
    indicator4 = Average[200](close)
    c2 = (indicator3 > indicator4)
    
    IF (c1 AND c2) AND not daysForbiddenEntry THEN
    BUY 1 CONTRACT AT MARKET
    ENDIF
    
    // Conditions pour fermer une position acheteuse
    indicator5 = ExponentialAverage[20](close)
    indicator6 = ExponentialAverage[50](close)
    c3 = (indicator5 CROSSES UNDER indicator6)
    indicator7 = Average[80](close)
    indicator8 = Average[200](close)
    c4 = (indicator7 < indicator8)
    
    IF c3 AND c4 THEN
    SELL AT MARKET
    ENDIF
    
    // Stops et objectifs
    SET STOP pLOSS 30
    SET TARGET pPROFIT 45
    
    #165644 quote
    Nicolas
    Keymaster
    Master

    Pour ajouter un breakeven, voir cette fonction : https://www.prorealcode.com/blog/learning/breakeven-code-automated-trading-strategy/

    Le code complet :

    startBreakeven = 30 //how much pips/points in gain to activate the breakeven function?
    PointsToKeep = 5 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
    
    //reset the breakevenLevel when no trade are on market
    IF NOT ONMARKET THEN
    breakevenLevel=0
    ENDIF
    // --- BUY SIDE ---
    //test if the price have moved favourably of "startBreakeven" points already
    IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN
    //calculate the breakevenLevel
    breakevenLevel = tradeprice(1)+PointsToKeep*pipsize
    ENDIF
    //place the new stop orders on market at breakevenLevel
    IF breakevenLevel>0 THEN
    SELL AT breakevenLevel STOP
    ENDIF
    // --- end of BUY SIDE ---
    // --- SELL SIDE ---
    //test if the price have moved favourably of "startBreakeven" points already
    IF SHORTONMARKET AND tradeprice(1)-close>=startBreakeven*pipsize THEN
    //calculate the breakevenLevel
    breakevenLevel = tradeprice(1)-PointsToKeep*pipsize
    ENDIF
    //place the new stop orders on market at breakevenLevel
    IF breakevenLevel>0 THEN
    EXITSHORT AT breakevenLevel STOP
    ENDIF
    // --- end of SELL SIDE ---
    #165702 quote
    maxlys
    Participant
    Senior

    Merci Nicolas

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.

Codage pour Backtest croisement de moyennes mobiles et breakeven


ProOrder : Trading Automatique & Backtests

New Reply
Author
author-avatar
maxlys @maxlys Participant
Summary

This topic contains 4 replies,
has 2 voices, and was last updated by maxlys
4 years, 11 months ago.

Topic Details
Forum: ProOrder : Trading Automatique & Backtests
Language: French
Started: 03/29/2021
Status: Active
Attachments: No files
Logo Logo
Loading...