Frattali di Williams

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #204923 quote
    albespo
    Participant
    New

    Buongiorno,

    nel poco tempo libero a disposizione e con le mie limitate conoscenze, sto cercando di riprodurre un sistema per Eur Usd con regole prese da un video su Youtube al fine di effettuare dei backtest.

    Si utilizzano i frattali di Williams, il codice l’ho trovato qui: https://www.prorealcode.com/prorealtime-indicators/bill-williams-fractals/

    Le regole sono:

    • time frame a 5 minuti.
    • Individuare il trend di “fondo” (giornaliero o settimanale), ci provo con il supertrend e la media mobile esponenziale; entrare solo in direzione del trend di fondo.
    • Entrata long: quando ci sono 2 frattali “up” con massimi crescenti e 2 frattali “down” con minimi crescenti.
    • Entrata short: quando ci sono 2 frattali “up” con massimi decrescenti e 2 frattali “down” con minimi decrescenti.
    • Stop loss: per i long “x” pips sotto il secondo frattale “down”; per gli short “x” pips sopra il secondo frattale “up”.
    • Dopo “z” pips di guadagno, mettere la posizione in breakeven. In tal caso, al rispetto delle condizioni di sopra, consentire l’apertura di altre posizioni.
    • Chiudere la posizione long quando un frattale “up” si presenta ad un livello inferiore del penultimo (viceversa per le posizioni short).

     

    Il codice che ho provato a scrivere mi da sempre… 0 operazioni! Dove sbaglio?

    //-------------------------------------------------------------------------
    // Codice principale : frattali Williams
    //-------------------------------------------------------------------------
    DEFPARAM CumulateOrders = true
    ONCE Countup = 0
    once countdw = 0
    cp = 2
    
    // trend di “fondo”
    TIMEFRAME (4 hour, updateonclose)
    st=Supertrend[3,10]
    em=ExponentialAverage[21](close)
    if close > st and close > em then
    longok = 1
    elsif close < st and close < em then
    shortok = 1
    endif
    
    TIMEFRAME (default)
    // primo frattale up
    if (High[cp] > High[cp+2]) AND (High[cp] > High[cp+1]) AND (High[cp] > High[cp-1]) AND (High[cp] > High[cp-2]) then
    LH = 1 
    countup = countup + 1
    high1 = high [cp] 
    endif
    
    // primo frattale down
    if (Low[cp] < Low[cp+2]) AND (Low[cp] < Low[cp+1]) AND (Low[cp] < Low[cp-1]) AND (Low[cp] < Low[cp-2]) then
    LL= -1
    countdw = countdw - 1
    low1 = low [cp]
    endif
    
    // secondo frattale up
    if lh = 1 then
    if (High[cp] > High[cp+2]) AND (High[cp] > High[cp+1]) AND (High[cp] > High[cp-1]) AND (High[cp] > High[cp-2]) then
    LH = lh + 1
    countup = countup + 1
    high2 = high [cp]
    endif
    endif
    
    // secondo frattale down
    if ll = -1 then
    if (Low[cp] < Low[cp+2]) AND (Low[cp] < Low[cp+1]) AND (Low[cp] < Low[cp-1]) AND (Low[cp] < Low[cp-2]) then
    LL= ll-1
    countdw = countdw - 1
    low2 = low [cp]
    endif
    endif
    
    // condizioni entrata short
    if countup = 2 and high1 > high2 and low1 > low2 then
    if longok = 1 then
    if not onmarket or cumul = 1 then
    buy at market
    set stop loss low2 - xx*pointsize
    countup = countup - 1
    endif
    endif
    endif
    
    // condizioni entrata short
    if countdw = -2 and high1 < high2 and low1 < low2 then
    if shortok = 1 then
    if not onmarket or cumul = 1 then
    sellshort at market
    set stop loss high2 + xx*pointsize
    countdw = countdw + 1
    endif
    endif
    endif
    
    // posizione a breakeven dopo zz di guadagno; se a breakeven consentire apertura altre posizioni (cumul=1)
    if longonmarket then 
    if close - TRADEPRICE >= zz then
    set stop loss tradeprice
    cumul = 1
    endif
    endif
    if shortonmarket then
    if TRADEPRICE - close>= zz then
    set stop loss tradeprice
    cumul = 1
    endif
    endif
    
    // condizioni per uscita posizione
    if longonmarket and low2 > low1 then
    sell at market
    endif
    if shortonmarket and high2 > high1 then
    exitshort at market
    endif
    
    #204924 quote
    robertogozzi
    Moderator
    Master

    Prova a modificare le righe 13-17 con queste:

    if close > st and close > em then
       longok  = 1
       shortok = 0
    elsif close < st and close < em then
       shortok = 1
       longok  = 0
    endif

    non l’ho provato.

    albespo thanked this post
    #204980 quote
    albespo
    Participant
    New

    Grazie Roberto,

    ho provato ma il risultato non cambia

    #205040 quote
    robertogozzi
    Moderator
    Master

    Oltre alla modifica di cui sopra, è necessario azzerare (stabilisci tu quando) le variabili countdw e countup, altrimenti avranno sempre valori maggiori di 2 o minori di -2, quindi le condizioni d’entrata non saranno nai verificate. Se aggiungi queste due righe alla fine del codice, vedrai i valori che hanno le due variabili candela dopo candela nella finestra delle variabili appositamente visualizzata dal backtest:

    graph countdw
    graph countup

    Inoltre metti lo Stop Loss in modo errato, ad esempio:

    set stop loss tradeprice   //oppure set stop loss low2

    inserisce lo Stop Loss al numeri di pip (punti) indicato da TradePrice, quindi molte migliaia! SET STOP LOSS richiede una differenza di prezzo (ad esempio CLOSE – LOW[1]), non un prezzo. Devi usare questa istruzione per mettere lo stop (o eventualmente il target) al prezzo indicato da TRADEPRICE:

    SET STOP PRICE TradePrice
    SET TARGET PRICE TradePrice + 100*pipsize
    albespo thanked this post
    #205231 quote
    albespo
    Participant
    New

    Grazie, credevo che le istruzioni ai righi 58 e 69 servissero a mantenere i valori della variabili countup e countdw sempre tra 1 e 2 (-1 e -2 per countdw)….

    Ho provato a inserire istruzioni per azzerare le variabili (ad esempio una volta a mercato, inserendo sempre alle righe 58 e 69 “=0”) ma niente.

    Se hai voglia di darmi ancora una mano te ne sarò grato

    #205235 quote
    robertogozzi
    Moderator
    Master

    Posta l’ultimo codice che hai usato, comprensivo delle modifiche che hai eventualmente fatto.

    #205262 quote
    albespo
    Participant
    New

    Ecco l’ultima versione (sempre 0 operazioni in backtest):

    //-------------------------------------------------------------------------
    // Codice principale : frattali Williams
    //-------------------------------------------------------------------------
    DEFPARAM CumulateOrders = true
    ONCE Countup = 0
    once countdw = 0
    once cp = 2
    
    // trend di "fondo"
    TIMEFRAME (4 hour, updateonclose)
    st=Supertrend[3,10]
    em=ExponentialAverage[21](close)
    if close > st and close > em then
    longok = 1
    shortok = 0
    endif
    if close < st and close < em then
    shortok = 1
    longok = 0
    endif
    
    TIMEFRAME (default)
    // primo frattale up
    if (High[cp] > High[cp+2]) AND (High[cp] > High[cp+1]) AND (High[cp] > High[cp-1]) AND (High[cp] > High[cp-2]) then
    LH = 1
    high1 = high [cp]
    endif
    
    // primo frattale down
    if (Low[cp] < Low[cp+2]) AND (Low[cp] < Low[cp+1]) AND (Low[cp] < Low[cp-1]) AND (Low[cp] < Low[cp-2]) then
    LL= -1
    low1 = low [cp]
    endif
    
    // secondo frattale up
    if lh = 1 then
    if (High[cp] > High[cp+2]) AND (High[cp] > High[cp+1]) AND (High[cp] > High[cp-1]) AND (High[cp] > High[cp-2]) then
    LH = lh + 1
    countup = countup + 1
    high2 = high [cp]
    endif
    endif
    
    // secondo frattale down
    if ll = -1 then
    if (Low[cp] < Low[cp+2]) AND (Low[cp] < Low[cp+1]) AND (Low[cp] < Low[cp-1]) AND (Low[cp] < Low[cp-2]) then
    LL= ll-1
    countdw = countdw - 1
    low2 = low [cp]
    endif
    endif
    
    // condizioni entrata long
    if countup = 2 and high1 > high2 and low1 > low2 then
    if longok = 1 then
    if not onmarket or cumul = 1 then
    buy at market
    set stop loss low2 - xx*pointsize
    countup = 1
    lh = lh -1
    endif
    endif
    endif
    
    if not onmarket and countup = 2 then
    countup = 0
    endif
    
    // condizioni entrata short
    if countdw = -2 and high1 < high2 and low1 < low2 then
    if shortok = 1 then
    if not onmarket or cumul = 1 then
    sellshort at market
    set stop loss high2 + xx*pointsize
    countdw = -1
    ll = ll + 1
    endif
    endif
    endif
    
    if not onmarket and countdw = -2 then
    countdw = 0
    endif
    
    // posizione a breakeven dopo zz di guadagno; se a breakeven consentire apertura altre posizioni (cumul=1)
    if longonmarket then
    if close - TRADEPRICE >= zz then
    set stop loss close - tradeprice
    cumul = 1
    endif
    endif
    if shortonmarket then
    if TRADEPRICE - close>= zz then
    set stop loss tradeprice - close
    cumul = 1
    endif
    endif
    
    // condizioni per uscita posizione
    if longonmarket and low2 > low1 then
    sell at market
    endif
    if shortonmarket and high2 > high1 then
    exitshort at market
    endif
    graph countdw
    graph countup
    
    #205272 quote
    GraHal
    Participant
    Master

    Penso che abbia a che fare con high1 e high 2, anche low1 e low2.

    cp = 2

    Il codice afferma high1 = high[cp] quindi successivamente high2 = high[cp], quindi la condizione di acquisto include high1 > high2 (e simili per low1, low 2 ecc. Ecc.). 

    High1 > high2 non è mai 1 / true se high1 = high[cp] e high2 = high[cp] dove cp = 2. 

    albespo thanked this post
    #205288 quote
    GraHal
    Participant
    Master

    Non ho potuto resistere a una giocata, quindi ho fatto high1 = high2 e low1 = low2 in entrambe le condizioni BUY e SELSHORT e la strategia quindi prende scambi.

    Mi piacerebbe vederlo avere successo perché sembra che ci sia stato molto lavoro fino a questo punto!

    #205298 quote
    GraHal
    Participant
    Master

    Mi piacerebbe vederlo avere successo

    Giusto per chiarire, sopra significa… mi piacerebbe vedere la strategia ordinata e fare operazioni redditizie.
    Il mio test re high1 = high2 ecc era proprio questo … un test per convincere la strategia a fare trading (anche se perdendo trade).

    #205331 quote
    albespo
    Participant
    New

    Penso che abbia a che fare con high1 e high 2, anche low1 e low2.

    cp = 2

    Il codice afferma high1 = high[cp] quindi successivamente high2 = high[cp], quindi la condizione di acquisto include high1 > high2 (e simili per low1, low 2 ecc. Ecc.).

    High1 > high2 non è mai 1 / true se high1 = high[cp] e high2 = high[cp] dove cp = 2.

    Grazie per la risposta. Ho provato inserendo un’altra variabile con valore = 2 e sostituendola a cp per la ricerca del secondo frattale, ma il risultato non cambia.

    Dovrei trovare un altro modo per indicare al sistema che il secondo frattale up deve essere maggiore del primo (e viceversa per quello short), ma va al di là delle mie capacità di programmazione

    #205821 quote
    robertogozzi
    Moderator
    Master

    Il problema non è in CP=2 (o 3, ecc…). Il problema, come ha osservato GraHal, è nel fatto che usi high1 > high2, dove entrambi hanno lo stesso valore, quindi uno NON può essere maggiore dell’altro!

    Se provi con:

    if countup =  2 and high1 = high2 and low1 = low2 then
    if countdw = -2 and high1 = high2 and low1 = low2 then

    funzionerà.
    Però non so se è quello che tu vuoi.

    GraHal and albespo thanked this post
    #205937 quote
    albespo
    Participant
    New

    Grazie per la risposta, ma in effetti non è quello che vorrei ottenere.

    Vorrei indicare al sistema di verificare quado ricorre questa sequenza:

    per i long, 2 frattali “up” con massimi crescenti e 2 frattali “down” con minimi crescenti.
    Per gli short, 2 frattali “up” con massimi decrescenti e 2 frattali “down” con minimi decrescenti.

    #207219 quote
    robertogozzi
    Moderator
    Master

    Ho provato, ma il codice non è completo, mi segnala che mancano alcune variabili.

    Posta il file ITF, per essere certi che sia quello completo e funzionante.

    #207736 quote
    albespo
    Participant
    New

    Ecco il file ITF

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

Frattali di Williams


ProOrder: Trading Automatico & Backtesting

New Reply
Author
author-avatar
albespo @albespo Participant
Summary

This topic contains 15 replies,
has 3 voices, and was last updated by robertogozzi
3 years ago.

Topic Details
Forum: ProOrder: Trading Automatico & Backtesting
Language: Italian
Started: 11/28/2022
Status: Active
Attachments: 2 files
Logo Logo
Loading...