Hier ist eine einfache Strategie für de40 und vgl

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #254094 quote
    JohnScherJohnScher
    Participant
    Veteran

    Ich frage ob es jmd in PRT-Sprache übersetzen kann

    im mt4 gab es keine Kompilierfehler

    die Idee dahinter ist wirklich einfach

    warten wir auf ein Corona 2.0

    LG

    //+——————————————————————+

    //| Expert Advisor: 5%.mq4 |

    //| Purpose: Place stacked Buy Limits below ATH – X%, TP = ATH + 1 |

    //| Logic: ATH from H1, check hourly at H1 close |

    //| Extension: ATH calculation by time window (days) |

    //+——————————————————————+

    #property strict

    #property show_inputs

    #property copyright “DKATLE 2025”

    #property link “https://www.dkatle.com”

    input string TradeComment = “%5”; // Unique text for orders

    // — Inputs

    input double Lots = 0.1; // Lot size per order

    input int Slippage = 10; // Slippage

    input int LookbackDays = 365; // Time window in days for ATH

    input int MagicNumber = 12345; // Unique ID for this EA

    // Stacking levels (percent below ATH)

    input double Level1 = 5.0;

    input double Level2 = 10.0;

    input double Level3 = 15.0;

    input double Level4 = 20.0;

    input double Level5 = 25.0;

    input double Level6 = 30.0;

    input double Level7 = 35.0;

    input double Level8 = 40.0;

    input double Level9 = 45.0;

    double lastATH = -1;

    datetime lastHourBar = 0;

    //— Calculate ATH from H1 candles within LookbackDays

    double GetLastATH(string sym, int daysBack)

    {

    int totalBars = iBars(sym, PERIOD_H1);

    if(totalBars <= 0) return -1;

    double ath = -DBL_MAX;

    datetime cutoff = TimeCurrent() – daysBack * 24 * 60 * 60;

    for(int i = 0; i < totalBars; i++)

    {

    datetime barTime = iTime(sym, PERIOD_H1, i);

    if(barTime < cutoff) break;

    double high = iHigh(sym, PERIOD_H1, i);

    if(high > ath) ath = high;

    }

    return ath;

    }

    //— Delete pending Buy Limits for current symbol & MagicNumber

    void DeletePendingBuyLimits(string sym)

    {

    for(int i = OrdersTotal() – 1; i >= 0; i–)

    {

    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

    {

    if(OrderSymbol() == sym && OrderType() == OP_BUYLIMIT && OrderMagicNumber() == MagicNumber)

    {

    if(!OrderDelete(OrderTicket()))

    Print(“OrderDelete error “, GetLastError(), ” for ticket “, OrderTicket());

    }

    }

    }

    }

    //— Check if a BuyLimit with same price already exists

    bool BuyLimitExists(string sym, double price)

    {

    for(int i = OrdersTotal() – 1; i >= 0; i–)

    {

    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

    {

    if(OrderSymbol() == sym && OrderType() == OP_BUYLIMIT && OrderMagicNumber() == MagicNumber)

    {

    if(MathAbs(OrderOpenPrice() – price) < Point*0.5)

    return true;

    }

    }

    }

    return false;

    }

    //— Price validation and normalization

    bool ValidateAndNormalizePrices(string sym, double &price, double &tp)

    {

    int digits = (int)MarketInfo(sym, MODE_DIGITS);

    double point = MarketInfo(sym, MODE_POINT);

    int stopLevel = (int)MarketInfo(sym, MODE_STOPLEVEL);

    double ask = MarketInfo(sym, MODE_ASK);

    double minDist = stopLevel * point;

    price = NormalizeDouble(price, digits);

    tp = NormalizeDouble(tp, digits);

    if(price >= ask – minDist)

    {

    Print(“Invalid BuyLimit price “, price, ” (Ask “, ask, “, minDist “, minDist, “)”);

    return false;

    }

    if(tp <= price + minDist)

    {

    tp = price + MathMax(minDist, point);

    tp = NormalizeDouble(tp, digits);

    Print(“TP adjusted to “, tp, ” (minDist “, minDist, “)”);

    }

    return true;

    }

    //— Place new stacked Buy Limits

    void PlaceStackedBuyLimits(string sym, double ath)

    {

    double baseTp = ath + MarketInfo(sym, MODE_POINT);

    double levels[9];

    levels[0] = Level1;

    levels[1] = Level2;

    levels[2] = Level3;

    levels[3] = Level4;

    levels[4] = Level5;

    levels[5] = Level6;

    levels[6] = Level7;

    levels[7] = Level8;

    levels[8] = Level9;

    for(int i = 0; i < 9; i++)

    {

    double pct = levels[i];

    if(pct <= 0.0) continue;

    double buyPrice = ath * (1.0 – pct / 100.0);

    double tp = baseTp;

    if(!ValidateAndNormalizePrices(sym, buyPrice, tp))

    {

    Print(“Skipped: Level “, pct, “% (invalid price)”);

    continue;

    }

    if(BuyLimitExists(sym, buyPrice))

    {

    Print(“Skipped: Level “, pct, “% (duplicate order at “, buyPrice, “)”);

    continue;

    }

    int ticket = OrderSend(

    sym, OP_BUYLIMIT, Lots, buyPrice, Slippage,

    0, tp,

    TradeComment + ” ATH-” + DoubleToString(pct, 1) + “% TP=ATH+1”,

    MagicNumber, 0, clrBlue

    );

    if(ticket < 0)

    {

    int err = GetLastError();

    Print(“OrderSend error “, err, ” at level “, pct, “% (price “, buyPrice, “, TP “, tp, “)”);

    }

    }

    }

    //— Initialization

    int OnInit()

    {

    string sym = _Symbol;

    lastATH = GetLastATH(sym, LookbackDays);

    if(lastATH > 0)

    {

    DeletePendingBuyLimits(sym);

    PlaceStackedBuyLimits(sym, lastATH);

    }

    else

    {

    Print(“OnInit: No valid ATH found (Symbol “, sym, “).”);

    }

    lastHourBar = iTime(sym, PERIOD_H1, 0);

    return(INIT_SUCCEEDED);

    }

    //— OnTick: check only after H1 candle closes

    void OnTick()

    {

    string sym = _Symbol;

    datetime bt = iTime(sym, PERIOD_H1, 0);

    if(bt != lastHourBar && TimeCurrent() > bt)

    {

    lastHourBar = bt;

    double ath = GetLastATH(sym, LookbackDays);

    if(ath > 0 && ath > lastATH)

    {

    lastATH = ath;

    DeletePendingBuyLimits(sym);

    PlaceStackedBuyLimits(sym, ath);

    }

    }

    }

    #254105 quote
    LucasBestLucasBest
    Participant
    Senior
    // ==============================================
    // ATH stacked buys sous ATH -X%  (version PRT)
    // - Timeframe : 1 heure (appliquer le système sur H1)
    // - ATH sur LookbackDays (~ jours * 24 barres)
    // - On empile des achats à chaque palier de % sous l'ATH
    // - TP global = ATH + 1 point
    // ==============================================
    
    DEFPARAM CumulateOrders = True
    DEFPARAM PreLoadBars = 10000
    
    // ---------- Paramètres utilisateur ----------
    
    // Taille de base par niveau
    UnitsPerLevel = 1
    
    // Fenêtre de calcul de l'ATH (en jours approx.)
    LookbackDays = 365
    
    // Niveaux en % sous l'ATH
    Level1 = 5.0
    Level2 = 10.0
    Level3 = 15.0
    Level4 = 20.0
    Level5 = 25.0
    Level6 = 30.0
    Level7 = 35.0
    Level8 = 40.0
    Level9 = 45.0
    
    // ---------- Variables internes ----------
    
    ONCE LastAth = 0
    ONCE Initialised = 0
    
    // ---------- Calcul de l'ATH sur la fenêtre LookbackDays ----------
    
    // Approximation : 1 jour ≈ 24 bougies H1
    BarsPerDay = 24
    BarsLookback = LookbackDays * BarsPerDay
    
    IF BarIndex > 0 THEN
    Ath = High
    BarsToScan = BarsLookback
    IF BarsToScan > BarIndex THEN
    BarsToScan = BarIndex
    ENDIF
    
    FOR i = 1 TO BarsToScan DO
    ThisHigh = High[i]
    IF ThisHigh > Ath THEN
    Ath = ThisHigh
    ENDIF
    NEXT
    ELSE
    Ath = High
    ENDIF
    
    // Initialisation & mise à jour de l'ATH
    IF Ath > 0 THEN
    IF Initialised = 0 THEN
    LastAth = Ath
    Initialised = 1
    ELSE
    IF Ath > LastAth THEN
    // Nouvel ATH : on remonte la référence
    LastAth = Ath
    ENDIF
    ENDIF
    ENDIF
    
    // Si l'ATH n'est pas encore initialisé, on ne fait rien
    IF Initialised = 0 THEN
    quit
    ENDIF
    
    // ---------- Calcul du TP global ----------
    
    BaseTp = LastAth + PointSize
    
    // ---------- Calcul des prix de niveaux ----------
    
    // On ne place des niveaux que si le % > 0
    
    PriceLevel1 = 0
    PriceLevel2 = 0
    PriceLevel3 = 0
    PriceLevel4 = 0
    PriceLevel5 = 0
    PriceLevel6 = 0
    PriceLevel7 = 0
    PriceLevel8 = 0
    PriceLevel9 = 0
    
    IF Level1 > 0 THEN
    PriceLevel1 = LastAth * (1 - Level1 / 100)
    ENDIF
    
    IF Level2 > 0 THEN
    PriceLevel2 = LastAth * (1 - Level2 / 100)
    ENDIF
    
    IF Level3 > 0 THEN
    PriceLevel3 = LastAth * (1 - Level3 / 100)
    ENDIF
    
    IF Level4 > 0 THEN
    PriceLevel4 = LastAth * (1 - Level4 / 100)
    ENDIF
    
    IF Level5 > 0 THEN
    PriceLevel5 = LastAth * (1 - Level5 / 100)
    ENDIF
    
    IF Level6 > 0 THEN
    PriceLevel6 = LastAth * (1 - Level6 / 100)
    ENDIF
    
    IF Level7 > 0 THEN
    PriceLevel7 = LastAth * (1 - Level7 / 100)
    ENDIF
    
    IF Level8 > 0 THEN
    PriceLevel8 = LastAth * (1 - Level8 / 100)
    ENDIF
    
    IF Level9 > 0 THEN
    PriceLevel9 = LastAth * (1 - Level9 / 100)
    ENDIF
    
    // ---------- Taille "théorique" à détenir selon la baisse vs ATH ----------
    
    // Idée :
    // - On compte combien de niveaux ont été atteints par le prix actuel (Close)
    // - Pour chaque niveau atteint, on veut UnitsPerLevel en plus
    // - On n'allège jamais sur rebond : la taille cible ne fait qu'augmenter
    
    TargetSize = 0
    
    IF PriceLevel1 > 0 AND Close <= PriceLevel1 THEN
    TargetSize = TargetSize + UnitsPerLevel
    ENDIF
    
    IF PriceLevel2 > 0 AND Close <= PriceLevel2 THEN
    TargetSize = TargetSize + UnitsPerLevel
    ENDIF
    
    IF PriceLevel3 > 0 AND Close <= PriceLevel3 THEN
    TargetSize = TargetSize + UnitsPerLevel
    ENDIF
    
    IF PriceLevel4 > 0 AND Close <= PriceLevel4 THEN
    TargetSize = TargetSize + UnitsPerLevel
    ENDIF
    
    IF PriceLevel5 > 0 AND Close <= PriceLevel5 THEN
    TargetSize = TargetSize + UnitsPerLevel
    ENDIF
    
    IF PriceLevel6 > 0 AND Close <= PriceLevel6 THEN
    TargetSize = TargetSize + UnitsPerLevel
    ENDIF
    
    IF PriceLevel7 > 0 AND Close <= PriceLevel7 THEN
    TargetSize = TargetSize + UnitsPerLevel
    ENDIF
    
    IF PriceLevel8 > 0 AND Close <= PriceLevel8 THEN
    TargetSize = TargetSize + UnitsPerLevel
    ENDIF
    
    IF PriceLevel9 > 0 AND Close <= PriceLevel9 THEN
    TargetSize = TargetSize + UnitsPerLevel
    ENDIF
    
    // On ne réduit jamais la taille : si le prix remonte,
    // on garde au moins la taille déjà détenue.
    CurrentSize = COUNTOFPOSITION
    
    IF TargetSize < CurrentSize THEN
    TargetSize = CurrentSize
    ENDIF
    
    // ---------- Entrées supplémentaires (empilement) ----------
    
    AdditionalSize = TargetSize - CurrentSize
    
    IF AdditionalSize > 0 THEN
    // On place / met à jour le TP global pour toutes les positions longues
    IF BaseTp > 0 THEN
    SET TARGET PRICE BaseTp
    ENDIF
    
    BUY AdditionalSize SHARES AT MARKET
    ENDIF
    
    robertogozzi, Iván González and JohnScher thanked this post
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

TradingView to ProRealTime Translation Center

New Reply
Author
author-avatar
JohnScher @johnscher Participant
Summary

This topic contains 1 reply,
has 2 voices, and was last updated by LucasBestLucasBest
10 months ago.

Topic Details
Forum: TradingView to ProRealTime Translation Center Forum
Started: 11/30/2025
Status: Active
Attachments: No files
ProRealCode ProRealCode
Loading...