Errore in creazione strategia per backtest

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #209814 quote
    davidator
    Participant
    Junior

    Buongiorno,

    Sto imparando e provando a testare alcune strategie. Utilizzo un indicatore personalizzato, quando imposto le opzioni per il probacktest ricevo il seguente errore

    – “La funzione “Engulfing 2.0” richiamata da “Il Mio Sistema” dà 0 valori ma il tuo codice ne ha bisogno di 1.”

    L’indicatore colora sul grafico la candela con le condizioni dell’indicatore. La logica di come vorrei impostare il codice è: ” Se l’indicatore è maggiore della chiusura di ieri, LONG”.

    Vi allego il codice e un screenshot.

    Grazie in anticipo per l’aiuto

    // Definizione dei parametri del codice
    DEFPARAM CumulateOrders = False // Posizioni cumulate disattivate

    // Condizioni per entrare su posizioni long
    indicator1 = CALL “Engulfing 2.0”
    c1 = (indicator1[1] > DClose(1)[1])

    IF c1 THEN
    BUY 1 CONTRACT AT MARKET
    ENDIF

    #209832 quote
    robertogozzi
    Moderator
    Master

    Probabilmete quell’inbdicatore non restituisce nessun valore (se non ha niente alla destra di RETURN).
    Per essere più precisi servirebbe il codice di quell’indicatore (ed anche il file ITF).

    davidator thanked this post
    #209867 quote
    davidator
    Participant
    Junior

    Ecco il codice dell’indicatore ed il relativo file ITF

     

    // ENGULFING (TradingView 4) (Engulfing Look-back Alert)
    //
    //https://www.tradingview.com/script/lfEO7LvU-Engulfing-Look-back-Alert/
    //
    //Engulfing Candle Definition:
    //
    //- Bullish Engulfing: Trade BELOW the prior candle’s LOW and CLOSE ABOVE the prior candle’s HIGH.
    // Previous candle can be an up ( bullish ) or a down ( bearish ) candle
    //- Bearish Engulfing: Trade ABOVE the prior candle’s HIGH and CLOSE BELOW the prior candle’s LOW.
    // Previous candle can be an up ( bullish ) or a down ( bearish ) candle
    //
    //Features:
    //
    //- Set the look-back period for engulfing candle high and low -> default = 1; e.g. Did the bullish
    // eng candle trade below the lows of the last 3 candles and trade above the highs of the last 4
    // candles? Set the input values accordingly
    LookBack = 1 //1 lookback period (default)
    bullHiHis = highest[LookBack](high[1])
    bullLoLos = lowest[LookBack](low[1])
    bearHiHis = highest[LookBack](high[1])
    bearLoLos = lowest[LookBack](low[1])
    //===============================================
    //BULLISH ENGULFING
    //===============================================
    //Candle must close above prev candle high AND candle low is lower than the lowest low for the
    //look-back period (bullLowLows) AND candle high is higher than look-back period for the highest
    //high (bullHiHighs)
    BullishEngulfing = (close > high[1]) and (low < bullLoLos) and (high > bullHiHis)
    //
    //===============================================
    //BEARISH ENGULFING
    //===============================================
    //Candle must close BELOW prev candle low AND candle high is higher than the highest high for the
    // look-back period (bearHiHighs) AND candle low is lower than look-back period for the lowest low
    // (bearLowLows)
    BearishEngulfing = (close < low[1]) and (high > bearHiHis) and (low < bearLoLos)
    //
    IF BullishEngulfing THEN
    DrawCandle(Open,High,Low,Close) coloured(255,255,0,255) //Giallo
    //DrawText(“↓”,BarIndex,high+range,Dialog,Bold,30) coloured(255,255,0,255)
    ELSIF BearishEngulfing THEN
    DrawCandle(Open,High,Low,Close) coloured(0,0,255,255) //Blù
    //DrawText(“↓”,BarIndex,high+range,Dialog,Bold,30) coloured(0,0,255,255)
    ENDIF
    return

    #209876 quote
    robertogozzi
    Moderator
    Master

    Infatti, non restituisce nessun dato, stampa solo sul grafico .

    Si può modificare, cosa vuoi che ti restituisca?

    davidator thanked this post
    #209917 quote
    davidator
    Participant
    Junior

    Intanto vorrei poter fare un backtest utilizzando la chiusura della candela come ingresso, specificando se rialzista o ribassista. Per le altre condizioni non dovrei avere problemi in quanto utilizzerei indicatori standard.

    Grazie infinite e buon fine settimana

    #209936 quote
    robertogozzi
    Moderator
    Master

    Per sapere se una candela è rialzista basta che la chiusura sia maggiore dell’apertura, viceversa è ribassista. Devi solo decidere se ignorare quelle che hanno apertura e chiusura identiche oppure se farle rientrare in uno dei due gruppi, oppure lasciarle in un terzo gruppo separato.

    A che ti serve un indicatore?

    #209942 quote
    davidator
    Participant
    Junior

    Buongiorno Roberto,

    Non mi riferivo a qualsiasi candela ma intendevo che utilizzando il mio indicatore (Engulfing 2.0), che indica le candele engulfing con dei criteri specifici, ho solo bisogno di dividerle in rialzista o ribassista. Già funziona in questo modo quando si applica al grafico ma vorrei applicarlo al backtest.

    #210086 quote
    robertogozzi
    Moderator
    Master

    Eccolo. Questo devi metterlo SOTTO al grafico per vedere l’istogramma:

    // ENGULFING  (TradingView 4)  (Engulfing Look-back Alert)
    //
    //https://www.tradingview.com/script/lfEO7LvU-Engulfing-Look-back-Alert/
    //
    //Engulfing Candle Definition:
    //
    //- Bullish Engulfing: Trade BELOW the prior candle's LOW and CLOSE ABOVE the prior candle's HIGH.
    //  Previous candle can be an up ( bullish ) or a down ( bearish ) candle
    //- Bearish Engulfing: Trade ABOVE the prior candle’s HIGH and CLOSE BELOW the prior candle’s LOW.
    //  Previous candle can be an up ( bullish ) or a down ( bearish ) candle
    //
    //Features:
    //
    //- Set the look-back period for engulfing candle high and low -> default = 1; e.g. Did the bullish
    //  eng candle trade below the lows of the last 3 candles and trade above the highs of the last 4
    //  candles? Set the input values accordingly
    LookBack  = 1                                      //1  lookback period (default)
    bullHiHis = highest[LookBack](high[1])
    bullLoLos = lowest[LookBack](low[1])
    bearHiHis = highest[LookBack](high[1])
    bearLoLos = lowest[LookBack](low[1])
    //===============================================
    //BULLISH ENGULFING
    //===============================================
    //Candle must close above prev candle high AND candle low is lower than the lowest low for the
    //look-back period (bullLowLows) AND candle high is higher than look-back period for the highest
    //high (bullHiHighs)
    BullishEngulfing = (close > high[1]) and (low < bullLoLos) and (high > bullHiHis)
    //
    //===============================================
    //BEARISH ENGULFING
    //===============================================
    //Candle must close BELOW prev candle low AND candle high is higher than the highest high for the
    // look-back period (bearHiHighs) AND candle low is lower than look-back period for the lowest low
    // (bearLowLows)
    BearishEngulfing = (close < low[1]) and (high > bearHiHis) and (low < bearLoLos)
    //
    Segnale = 0
    //
    IF BullishEngulfing THEN
    //DrawCandle(Open,High,Low,Close)                  coloured(255,255,0,255) //Giallo
    //DrawText("↓",BarIndex,high+range,Dialog,Bold,30) coloured(255,255,0,255)
    Segnale = 1
    ELSIF BearishEngulfing THEN
    //DrawCandle(Open,High,Low,Close)                  coloured(0,0,255,255)   //Blù
    //DrawText("↓",BarIndex,high+range,Dialog,Bold,30) coloured(0,0,255,255)
    Segnale = -1
    ENDIF
    return Segnale AS "1=Rialzista, -1=Ribassista",0 AS"Zero"
    davidator thanked this post
    #210122 quote
    davidator
    Participant
    Junior

    Buongiorno, funziona alla perfezione.

    Vorrei impostare uno stop loss nel momento in cui il prezzo tocchi il minimo (se LONG) o il massimo (se SHORT) della candela di ingresso. E’ possibile con la creazione semplificata?

    Grazie

    #210134 quote
    robertogozzi
    Moderator
    Master

    No, con la creazione semplificata puoi indicare solo i punti di stop, devi metterlo manualmente.

    davidator thanked this post
    #210244 quote
    davidator
    Participant
    Junior

    Come avevo già scritto l’indicatore funziona, ho però scoperto che ci sono dei prolemi col timeframe giornaliero, infatti il backtest apre delle posizioni che non dovrebbe.

    I principali timeframe intraday come 15min. / 1H.  e anche il timeframe 1 Settimana funzionano bene.

    Cosa sbaglio? Come potrei risolvere?

    Grazie!

    #210277 quote
    robertogozzi
    Moderator
    Master

    Posta il codice funzionante (nel senso che contenga eventuali indicatori custom e con tutte le variabili indicate) ed indicami lo strumento ed il timeframe utilizzato, nonché qualche operazione errata o mancata (data e ora).

    Solo così è possibile controllarlo. Se riesci allega il file ITF della strategia.

    davidator thanked this post
    #210320 quote
    davidator
    Participant
    Junior

    Per semplificare ho rimosso altre condizioni e ho solo lasciato quella di acquisto (LONG). Questo stesso codice funziona con altri timeframe, ad esempio 15min., 1H., 1 Settimana. Ho provato diversi strumenti e il risultato è uguale.

    Strumento : DAX

    Timeframe : Daily

    Esempi di trades che “non” dovrebbero aprirsi :

    • 24 Gennaio
    • 14 Febbraio

    DEFPARAM CumulateOrders = False // Posizioni cumulate disattivate

    // Condizioni per entrare su posizioni long
    indicator1, ignored = CALL “Engulfing Istogramma”
    c1 = (indicator1 > 0)

    IF c1 THEN
    BUY 1 CONTRACT AT MARKET
    ENDIF

    // Stop e target
    SET STOP pLOSS 75
    SET TARGET pPROFIT 75

    #210482 quote
    robertogozzi
    Moderator
    Master

    Si aprono perché c’è un pattern ENGULFING Rialzista.

    Forse t’inganno le freccette che vedi sotto, ma quelle sono sulla candela al cui inizio entra a mercato, in quanto la strategia viene eseguita immediatamente alla chiusura della candela che fa l’engulfing e che è quella che determina le condizioni d’entrata.

    davidator thanked this post
    #210496 quote
    davidator
    Participant
    Junior

    Credo di aver individuato il problema. Nel mio grafico rimuovo i dati del week-end quindi vedo un grafico diverso dal tuo, però il backtest tiene conto di quelle candele anche se non le visualizzo. Questo spiega perchè riscontro il problema solo sul timeframe giornaliero.

    E’ possibile ignorarle o incorporarle con quelle del lunedì?

    Grazie e buon fine settimana

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

Errore in creazione strategia per backtest


ProOrder: Trading Automatico & Backtesting

New Reply
Author
author-avatar
davidator @davidator Participant
Summary

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

Topic Details
Forum: ProOrder: Trading Automatico & Backtesting
Language: Italian
Started: 02/16/2023
Status: Active
Attachments: 5 files
Logo Logo
Loading...