una strategia con 2 SuperTrend 1 lento 1 veloce

Viewing 12 posts - 16 through 27 (of 27 total)
  • Author
    Posts
  • #95548 quote
    robertogozzi
    Moderator
    Master

    Per scrivere il codice , utilizza il pulsante <> “insert PRT code”, per rendere il codice più leggibile. Grazie.

    Puoi trovare il codice di un trailing stop scritto da Nicolas (dalla riga 17 fino alla fine) qui https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/.

    Comunque questa è la versione modificata della tua strategia, l’ho aggiunto io:

    STveloce = SuperTrend[x,a]
    STlento = SuperTrend[y,b]
    Rialzo = (STveloce CROSSES OVER STlento)
    Ribasso = (STveloce CROSSES UNDER STlento)
    c1=y>(x*1)
    // — Quando c’è un segnale rialzista:
    // 1 – se si è già SHORT, chiudere la posizione ed entrare LONG
    // 2 – se non si è ancora a mercato aprire una posizione LONG
    //
    IF Rialzo THEN
       IF ShortOnMarket and c1 THEN
          EXITSHORT AT MARKET
          BUY 1 CONTRACT AT MARKET
       ELSIF Not OnMarket THEN
          BUY 1 CONTRACT AT MARKET
       ENDIF
    ENDIF
    // — Quando c’è un segnale ribassista:
    // 1 – se si è già LONG, chiudere la posizione ed entrare SHORT
    // 2 – se non si è ancora a mercato aprire una posizione SHORT
    //
    IF Ribasso THEN
       IF LongOnMarket and c1 THEN
          SELL AT MARKET
          SELLSHORT 1 CONTRACT AT MARKET
       ELSIF Not OnMarket THEN
          SELLSHORT 1 CONTRACT AT MARKET
       ENDIF
    ENDIF
    //************************************************************************
    //trailing stop function
    trailingstart = 20  //trailing will start @trailinstart points profit
    trailingstep  = 5   //trailing step to move the "stoploss"
     
    //reset the stoploss value
    IF NOT ONMARKET THEN
     newSL=0
    ENDIF
     
    //manage long positions
    IF LONGONMARKET THEN
    //first move (breakeven)
     IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
      newSL = tradeprice(1)+trailingstep*pipsize
     ENDIF
    //next moves
     IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
      newSL = newSL+trailingstep*pipsize
     ENDIF
    ENDIF
     
    //manage short positions
    IF SHORTONMARKET THEN
    //first move (breakeven)
     IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
      newSL = tradeprice(1)-trailingstep*pipsize
     ENDIF
    //next moves
     IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
      newSL = newSL-trailingstep*pipsize
     ENDIF
    ENDIF
     
    //stop order to exit the positions
    IF newSL>0 THEN
     SELL AT newSL STOP
     EXITSHORT AT newSL STOP
    ENDIF
    //************************************************************************

    Devi solo sostituire 20 con il numero di PIPS da dove iniziare a fare il trailing (intanto mette subito in pareggio) e 5 con il numero di PIPS da usare come passo per incrementare il profitto da salvaguardare.

    Tieni presente che, essendo parte della strategia, verrà eseguito alla chiusura della barra.

    Insert-PRT-Code.png Insert-PRT-Code.png
    #95551 quote
    Gaspare
    Participant
    Veteran

    Grazie Roberto, gentilissimo

    #126135 quote
    Gaspare
    Participant
    Veteran
    defparam cumulateorders=false
    STveloce = SuperTrend[X,A]
    STlento  = SuperTrend[Y,B]
    Rialzo   = (STveloce CROSSES OVER  STlento)
    Ribasso  = (STveloce CROSSES UNDER STlento)
    c5=adx[14] >= 18
    // --- Quando c'è un segnale rialzista:
    // 1 - se si è già SHORT, chiudere la posizione ed entrare LONG
    // 2 - se non si è ancora a mercato aprire una posizione LONG
    //
    IF Rialzo  AND c5    THEN
    IF ShortOnMarket  THEN
    EXITSHORT AT MARKET
    BUY 1 CONTRACT AT close - C * pipsize LIMIT
    ELSIF Not OnMarket THEN
    BUY 1 CONTRACT AT close - C * pipsize LIMIT       //Long
    ENDIF
    ENDIF
    // --- Quando c'è un segnale ribassista:
    // 1 - se si è già LONG, chiudere la posizione ed entrare SHORT
    // 2 - se non si è ancora a mercato aprire una posizione SHORT
    //
    IF Ribasso AND c5  THEN
    IF LongOnMarket  THEN
    SELL AT MARKET
    SELLSHORT 1 CONTRACT AT close + C * pipsize LIMIT //Short
    ELSIF Not OnMarket THEN
    SELLSHORT 1 CONTRACT AT close + C * pipsize LIMIT
    ENDIF
    ENDIF
    set stop ploss 25
    set target pprofit 25
    

    Salve Roberto, una cortesia grazie

    dopo aver messo in reale questo TS sul Mini SP500 e Spot Gold, gli ordini non venivano eseguiti,

    ma cancellati a condizioni rispettate, ma il backtest era andato bene.

    La variabile  “C” sta per quantita’ di pip in meno, per il long, e in piu’ per lo short, per avere l’eseguito.

    Grazie

    #126141 quote
    robertogozzi
    Moderator
    Master

    A me funziona perfettamente.

    #126156 quote
    Gaspare
    Participant
    Veteran

    forse non mi sono spiegato bene

    anche a me funziona bene, ho fatto il backtest bene,  e infatti l’ho messo live;

    ma nessuno eseguito, anzi cancellati ( in reale mi riferisco) o rifiutati sullo stato ordini.

    Non capisco se è dovuto alla variabile  C, che non siano ammessi ordini diversi dal valore di apertura.

    #126162 quote
    robertogozzi
    Moderator
    Master

    Devi sostituire le variabili con i valori numerici, le variabili vanno bene solo per l’ottimizzazione.

    #126165 quote
    Gaspare
    Participant
    Veteran
    defparam cumulateorders=false
    STveloce = SuperTrend[4,30]
    STlento  = SuperTrend[4.5,10]
    Rialzo   = (STveloce CROSSES OVER  STlento)
    Ribasso  = (STveloce CROSSES UNDER STlento)
    c5=adx[14] >= 18
    // --- Quando c'è un segnale rialzista:
    // 1 - se si è già SHORT, chiudere la posizione ed entrare LONG
    // 2 - se non si è ancora a mercato aprire una posizione LONG
    //
    IF Rialzo  AND c5    THEN
    IF ShortOnMarket  THEN
    EXITSHORT AT MARKET
    BUY 1 CONTRACT AT close - 5 * pipsize LIMIT
    ELSIF Not OnMarket THEN
    BUY 1 CONTRACT AT close - 5 * pipsize LIMIT       //Long
    ENDIF
    ENDIF
    // --- Quando c'è un segnale ribassista:
    // 1 - se si è già LONG, chiudere la posizione ed entrare SHORT
    // 2 - se non si è ancora a mercato aprire una posizione SHORT
    //
    IF Ribasso AND c5  THEN
    IF LongOnMarket  THEN
    SELL AT MARKET
    SELLSHORT 1 CONTRACT AT close + 5 * pipsize LIMIT //Short
    ELSIF Not OnMarket THEN
    SELLSHORT 1 CONTRACT AT close + 5 * pipsize LIMIT
    ENDIF
    ENDIF
    set stop ploss 25
    set target pprofit 25
    

    certamente, scusa, ma in live avevo attivato con i valori delle variabili.

    questo vale per il mini SP500

    #126166 quote
    Gaspare
    Participant
    Veteran
    defparam cumulateorders=false
    STveloce = SuperTrend[3,30]
    STlento  = SuperTrend[3,50]
    Rialzo   = (STveloce CROSSES OVER  STlento)
    Ribasso  = (STveloce CROSSES UNDER STlento)
    c5=adx[14] >= 18
    // --- Quando c'è un segnale rialzista:
    // 1 - se si è già SHORT, chiudere la posizione ed entrare LONG
    // 2 - se non si è ancora a mercato aprire una posizione LONG
    //
    IF Rialzo  AND c5    THEN
    IF ShortOnMarket  THEN
    EXITSHORT AT MARKET
    BUY 1 CONTRACT AT close - 3 * pipsize LIMIT
    ELSIF Not OnMarket THEN
    BUY 1 CONTRACT AT close - 3 * pipsize LIMIT       //Long
    ENDIF
    ENDIF
    // --- Quando c'è un segnale ribassista:
    // 1 - se si è già LONG, chiudere la posizione ed entrare SHORT
    // 2 - se non si è ancora a mercato aprire una posizione SHORT
    //
    IF Ribasso AND c5  THEN
    IF LongOnMarket  THEN
    SELL AT MARKET
    SELLSHORT 1 CONTRACT AT close + 3 * pipsize LIMIT //Short
    ELSIF Not OnMarket THEN
    SELLSHORT 1 CONTRACT AT close + 3 * pipsize LIMIT
    ENDIF
    ENDIF
    set stop ploss 15
    set target pprofit 15
    

    e questo si attiva per il GOLD

    #126167 quote
    Gaspare
    Participant
    Veteran

    tutti e due i TS in live non danno eseguiti;

    gli ordini vengono cancellati, come risulta dallo stato ordini

    Grazie

    #126173 quote
    Gaspare
    Participant
    Veteran

    scusa, aggiungo timeframe a 5 minuti per entrambi

    #126183 quote
    robertogozzi
    Moderator
    Master

    Non c’è scritto niente nella motivazione del rifiuto?

    Se passi col mouse sopra il triangolo di avvertenza dovrebbe esserci scritto qualcosa.

    Immagino sia un prezzo d’entrata troppo vicino all’attuale. C’è una distanza minima che deve essere rispettata, strumento per strumento (occorre vedere sul sito del broker).

    Specialmente in questo periodo di alta volatilità quella distanza può essere piuttosto alta.

    Poi c’è un aspetto di logica di programmazione, che però non influisce sul risultato. Le righe 11-30 puoi scriverle così:

    IF Rialzo  AND c5    THEN
       IF ShortOnMarket  THEN
          EXITSHORT AT MARKET
       ENDIF
       BUY 1 CONTRACT AT close - 3 * pipsize LIMIT       //Long
    ENDIF
    // --- Quando c'è un segnale ribassista:
    // 1 - se si è già LONG, chiudere la posizione ed entrare SHORT
    // 2 - se non si è ancora a mercato aprire una posizione SHORT
    //
    IF Ribasso AND c5  THEN
       IF LongOnMarket  THEN
          SELL AT MARKET
       ENDIF
       SELLSHORT 1 CONTRACT AT close + 3 * pipsize LIMIT
    ENDIF

    Prova a mettere 15 o più, al posto di 3, negli ordini pendenti.

    #126188 quote
    Gaspare
    Participant
    Veteran

    grazie Roberto, sei un amico

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

una strategia con 2 SuperTrend 1 lento 1 veloce


ProOrder: Trading Automatico & Backtesting

New Reply
Author
author-avatar
Gaspare @gaspare Participant
Summary

This topic contains 26 replies,
has 3 voices, and was last updated by Gaspare
5 years, 10 months ago.

Topic Details
Forum: ProOrder: Trading Automatico & Backtesting
Language: Italian
Started: 01/24/2019
Status: Active
Attachments: 4 files
Logo Logo
Loading...