Apertura trade dopo numero barre con condizione

Viewing 15 posts - 1 through 15 (of 24 total)
  • Author
    Posts
  • #108905 quote
    carlo__
    Participant
    Average

    Ciao a tutti

    Domanda semplice per chi vuole imparare come me 🙂

    Dovrei istruire il proorder a aprire un trade dopo un x numero di barre, SOLO SE il prezzo non ha toccato un certo livello all’interno di quel range x di barre.

    Avevo pensato ad un ciclo loop sul quale però non sono molto pratico. Mi fate un esempio di codice che potrei usare?

     

    Grazie

    #108923 quote
    robertogozzi
    Moderator
    Master

    Non credo serv aun loop, ad ogni modo puoi fare un esempio (testo, non occorrono immagini)?

    #108924 quote
    carlo__
    Participant
    Average

    Un esempio può essere alla presenza di un pattern candlestick e identificazione del target

    If Bullsih Engulfing = 1 then
    Close = My Target
    Endif
    
    

    Poi un comando di apertura trade (es. Buy At Market) ma solo dopo 10 barre dalla identificazione del pattern. Il trade non si apre se il prezzo nelle X barre (10, nell’esempio) ha toccato “MyTarget”

    L’alternativa è usare un comando a tempo…invece di 10 barre potrebbe essere 50 min (nel caso del TimeFrame a 5 min)

    Probabilmente il comando barindex potrebbe aiutarmi ma non so incastrarlo con l’eccezione del “tocco” che annulla il trade.

     

    Grazie

    #108937 quote
    robertogozzi
    Moderator
    Master

    La tua riga dovrebbe essere:

    MyTarget = close

    Il codice è questo (non l’ho provato):

    ONCE Conto = 0
    // verificare che, se il conto è già iniziato, non abbia superato 10 o che il prezzo non abbia toccato MyTarget
    IF Conto > 0 Then
       Conto = Conto + 1
       If Conto > 10 Or high >= MyTarget Then
          Conto    = 0
    	  MyTarget = 0
       Endif
    Endif
    // una volta a mercato azzerare il conteggio e NyTarget
    If OnMarket Then
       Conto    = 0
       MyTarget = 0
    Endif
    // quando si verificano le condizione settare MyTarget ed iniziare il conteggio
    If BullsihEngulfing = 1 then
       MyTarget = close
       Conto    = 1
    Endif
    // entrare a mercato se già non lo si è, se il conteggio è 10 e se Mytarget non è stato toccato
    IF MyTarget > 0 AND Not OnMarket And Conto = 10 Then
       Buy 1 Contract At Market
       Set Target pProfit 100
       Set Stop   pLoss   50
    Endif
    #108945 quote
    carlo__
    Participant
    Average

    Grazie Roberto. Puoi però cortesemente dirmi come riesco a “fissare” il target proprio quando esso è uguale al close del pattern come l’esempio?

    Tu hai settato un Target Profit a 100 punti, in realtà “MyTarget” è uguale proprio al close del pattern “Bullish Engulfing”

    Sto facendo un po di prove…per quanto effettivamente il codice pare apra la posizione dopo 10 barre (certificate le condizioni date), purtroppo poi esce praticamente dal trade all’apertura della barra successiva:

    If MyTarget > 0 AND Not OnMarket And Conto = 10 Then
    Buy 1 Contracts at market
    ELSIF MyTarget > 0 AND NOT ONMARKET And Conto = 10 Then
    SellShort 1 Contracts at market
    ENDIF
     
    
    //----- Trade Closing Conditions
    IF LONGONMARKET THEN
    SELL AT MyTarget Limit
    ELSIF SHORTONMARKET THEN
    EXITSHORT AT MyTarget Limit
    ENDIF
    #108949 quote
    robertogozzi
    Moderator
    Master

    Il tuo codice entra sempre LONG perché le condizioni sono identiche e quella LONG è la prima.

    Perché sono identiche?

    Quale dev’essere il tuo target?

    #108951 quote
    carlo__
    Participant
    Average

    Ciao Roberto

    Vedi anche il “sellshort” perchè sto adattando il codice a specchio sul pattern inverso…mi sono dimenticato di toglierlo nel post precedente.

    Il punto è che il trade esce sempre alla candela successiva (immagine), quando in realtà il target è pari al prezzo di close del pattern esattamente come ho riportato nell’esempio al terzo post.

    Infatti non capisco come mai nel tuo codice ci siano sia MyProfit = Close e sia Set Target Profit

     

    Come faccio a istruire proorder affinchè il target sia solo il close del pattern “Bullish Engulfing”?  In altre parole ancora:

    • La seconda candela (close) che compone il pattern Bullish Engulfing rappresenta il target;
    • Identificato il pattern ci deve essere una istruzione che mette in standby il trade, che si attiva solo dopo N candele dal pattern;
    • Se prima delle N candele il prezzo ritocca il close del pattern Engulfing, il segnale è annullato e si ricomincia il controllo per l’identificazione di un successivo pattern.
    • Se le condizioni vengono rispettate (quindi sono trascorse 10 candele e il prezzo in queste 10 candele non ha mai ritestato il close del pattern) il trade si attiva e come target ha il close della seconda candela del pattern.

     

    Spero di aver chiarito ogni dubbio e grazie in anticipo

    Spero di esser stato piu chiaro.

    Cattura-5.jpg Cattura-5.jpg
    #108955 quote
    robertogozzi
    Moderator
    Master

    Il mio è un esempio, tu metti quello che vuoi. Se il tuo target è MyTarget dovrai indicare la distanza tra il prezzo d’entrata e MyTarget. Devi indicarla in prezzo, così com’è, se usi SET TARGET PROFIT, esempio:

    SET TARGET PROFIT abs(close - MyTarget)

    oppure in Pips se usi SET TARGET pPROFIT:

    SET TARGET PROFIT abs(close - MyTarget) / pipsize

    Il perché esce non so dirtelo. Per capirlo mi serve il codice completo in modo da replicare il backtest (su Dax a 5 minuti, hai detto).

    #108956 quote
    carlo__
    Participant
    Average

    Ciao Roberto

    Quello che sto facendo è in larga parte per uso didattico e imparare, comunque ti riporto l’intero codice, verifica se ti torna. Basta che torni al mio post precedente per capire cosa vorrei che accadesse. Attualmente il principale problema è che, verificate le condizoni, il trade chiude subito.

    DEFPARAM Cumulateorders=False 
    
    Bullish = close > open 
    Bearish = open > close 
    
    //identificazione PATTERN 
    
    IF Bullish then 
    BullPattern = 1 
    MyProfitBull=Close 
    ELSE BullPattern=0 
    ENDIF 
    
    IF Bearish then 
    BearPattern = 1 
    MyProfitBear=Close
    ELSE BearPattern=0 
    ENDIF 
    
    // verificare che, se il conto è già iniziato, non abbia superato la soglia o che il prezzo non abbia toccato i target 
    
    ONCE Conto = 0 
    IF Conto > 0 Then 
    Conto = Conto + 1 
    If Conto > 7 Or high >= MyProfitBull Then 
    Conto = 0 
    MyProfitBull = 0 
    elsif Conto > 7 or low <= MyProfitBear then 
    Conto = 0 
    MyProfitBear = 0 
    Endif 
    Endif 
    
    // una volta a mercato azzerare il conteggio e azzerare MyProfitBull/MyProfitBear 
    
    If OnMarket Then 
    Conto = 0 
    MyProfitBull = 0 
    MyProfitBear = 0 
    Endif 
    
    // quando si verificano le condizioni settare MyProfitLOng/MyProfitShort ed iniziare il conteggio 
    
    If (BullPattern=1) then 
    MyProfitBull = Close[0] 
    Conto = 1 
    ELSIF (BearPattern=1) then 
    MyProfitBear = Close[0] 
    Conto = 1 
    Endif
    
     // entrare a mercato se già non lo si è, se il conteggio è pari alla soglia e se MyProfitBull/MYProfitBear non sono stati toccati 
    
    If MyProfitBull > 0 AND Not OnMarket And Conto = 7 Then 
    Buy 1 Contracts at market 
    ELSIF MyProfitBear > 0 AND NOT ONMARKET And Conto = 7 Then 
    SellShort 1 Contracts at market ENDIF 
    
    //----- Trade Closing Conditions 
    
    IF LONGONMARKET THEN 
    SELL AT MyProfitBull Limit 
    ELSIF SHORTONMARKET THEN 
    EXITSHORT AT MyProfitBear Limit 
    ENDIF
    #109107 quote
    robertogozzi
    Moderator
    Master

    Non mi apre nessuna operazione sul DAX a 5 minuti, tu dove l’hai provato?

    #109114 quote
    carlo__
    Participant
    Average

    S&P 500 future ma quel codice ne rappresenta una sintesi. La logica che vorrei correggere è la stessa comunque ti incollo il codice intero sotto:

     

    DEFPARAM Cumulateorders=False
    
    Bullish = close > open
    Bearish = open > close
    Body    = (abs(open - close) > 0.5 * pipsize)
    UpperSHGreen = High > Close
    LowerSHRed =  Low < Close
    
    
    //------ Trading Periods Parameters
    
    If (Time >=153000 AND Time <=154000) OR (Time < 091500 OR Time > 210000)OR (Month = 12 AND Day > 15) OR (Month = 8 AND Day >10) THEN
    NoPattern = 1
    ELSE
    NoPattern = 0
    ENDIF
    
    // ----- ombra assente sull'Apertura - identificazione PATTERN
    
    IF Bullish[1] and Bearish and (Open=High) and (Body[1]) AND LowerSHRed AND (NoPattern=0) THEN
    RedNSH=1
    MyProfitLong=Open
    ELSE
    RedNSH=0
    ENDIF
    
    
    
    IF Bearish[1] and Bullish and (Low=Open) and (Body[1]) AND UpperSHGreen AND (NoPattern=0)THEN
    GreenNSH=1
    MyProfitShort=Open
    ELSE
    GreenNSH=0
    ENDIF
    
    
    
    //----- Counter Bars per apertura posizione
    
    
    
    
    // verificare che, se il conto è già iniziato, non abbia superato la soglia o che il prezzo non abbia toccato MyTarget
    
    ONCE Conto = 0
    
    IF Conto > 0 Then
    Conto = Conto + 1
    If Conto > 7 Or high >= MyProfitLong Then
    Conto    = 0
    MyProfitLong = 0
    elsif Conto > 7 or low <= MyProfitShort then
    Conto    = 0
    MyProfitShort = 0
    Endif
    Endif
    
    // una volta a mercato azzerare il conteggio e azzerare MyProfitLong/MyProfitShort
    If OnMarket Then
    Conto    = 0
    MyProfitLong = 0
    MyProfitShort = 0
    Endif
    
    // quando si verificano le condizioni settare MyProfitLOng/MyProfitShort ed iniziare il conteggio
    
    If (GreenNSH=1) then
    MyProfitShort = Open[0]
    Conto    = 1
    ELSIF (RedNSH=1) then
    MyProfitLong = Open[0]
    Conto    = 1
    Endif
    
    // entrare a mercato se già non lo si è, se il conteggio è pari alla soglia e se MyProfitLong/MYProfitShort non sono stati toccati
    
    If MyProfitLong > 0 AND Not OnMarket And Conto = 7 Then
    Buy 1 Contracts at market
    ELSIF MyProfitShort > 0 AND NOT ONMARKET And Conto = 7 Then
    SellShort 1 Contracts at market
    ENDIF
     
    
    //----- Trade Closing Conditions
    IF LONGONMARKET THEN
    SELL AT MyProfitLong Limit
    ELSIF SHORTONMARKET THEN
    EXITSHORT AT MyProfitShort Limit
    ENDIF
    
    
    IF ONMARKET AND (BarIndex - TradeIndex >= 40) then
    EXITSHORT AT MARKET
    SELL AT MARKET
    ENDIF
    
    #109123 quote
    robertogozzi
    Moderator
    Master

    Ho modificato un pò il codice, spostando anche qualche riga.

    Sembra funzionare.

    Ho aggiunto alla fine le istruzioni GRAPH e GRAPHONPRICE affinché tu possa monitorare, nell’apposito riquadro evidenziato nella foto, candela per candela, i valori di tutte le variabili che t’interessano, anche colorandole diversamevte. GRAPH serve per visualizzare nel riquadro delle variabili quelle che non hanno attinenza col prezzo, mentre GRAPHONPRICE quelle che si vedono meglio visualizzandole SUL prezzo:

    DEFPARAM Cumulateorders=False
    
    ONCE Conto    = 0
    ONCE MaxBarre = 7
     
    Bullish = close > open
    Bearish = open > close
    Body    = (abs(open - close) > 0.5 * pipsize)
    UpperSHGreen = High > Close
    LowerSHRed =  Low < Close
     
    //------ Trading Periods Parameters
    
    If Not OnMarket AND OnMarket[1] Then
    Conto         = 0
    MyProfitLong  = 0
    MyProfitShort = 0
    Endif
     
    If (Time >=153000 AND Time <=154000) OR (Time < 091500 OR Time > 210000)OR (Month = 12 AND Day > 15) OR (Month = 8 AND Day >10) THEN
    NoPattern     = 1
    Conto         = 0
    MyProfitLong  = 0
    MyProfitShort = 0
    ELSE
    NoPattern = 0
    ENDIF
    
    //----- Counter Bars per apertura posizione
    
    // verificare che, se il conto è già iniziato, non abbia superato la soglia o che il prezzo non abbia toccato MyTarget
     
    IF Conto > 0 Then
    Conto = Conto + 1
    IF Not OnMarket Then
    If Conto > MaxBarre Or high >= MyProfitLong Then
    Conto    = 0
    MyProfitLong = 0
    elsif Conto > MaxBarre or low <= MyProfitShort then
    Conto    = 0
    MyProfitShort = 0
    Endif
    Endif
    Endif
    
    // ----- ombra assente sull'Apertura - identificazione PATTERN
     
    IF Bullish[1] and Bearish and (Open=High) and (Body[1]) AND LowerSHRed AND (NoPattern=0) THEN
    //RedNSH=1
    MyProfitLong=Open
    Conto    = 1
    //ELSE
    //RedNSH=0
    ENDIF
     
     
     
    IF Bearish[1] and Bullish and (Low=Open) and (Body[1]) AND UpperSHGreen AND (NoPattern=0)THEN
    //GreenNSH=1
    MyProfitShort=Open
    Conto    = 1
    //ELSE
    //GreenNSH=0
    ENDIF
     
    // una volta a mercato azzerare il conteggio e azzerare MyProfitLong/MyProfitShort
    If OnMarket Then
    Conto    = 0
    //MyProfitLong = 0
    //MyProfitShort = 0
    Endif
     
    // quando si verificano le condizioni settare MyProfitLOng/MyProfitShort ed iniziare il conteggio
     
    //If (GreenNSH=1) then
    //MyProfitShort = Open[0]
    //Conto    = 1
    //ELSIF (RedNSH=1) then
    //MyProfitLong = Open[0]
    //Conto    = 1
    //Endif
     
    // entrare a mercato se già non lo si è, se il conteggio è pari alla soglia e se MyProfitLong/MYProfitShort non sono stati toccati
     
    If MyProfitLong > 0 AND Not OnMarket And Conto = MaxBarre Then
    Buy 1 Contracts at market
    ELSIF MyProfitShort > 0 AND NOT ONMARKET And Conto = MaxBarre Then
    SellShort 1 Contracts at market
    ENDIF
     
     
    //----- Trade Closing Conditions
    IF LONGONMARKET THEN
    SELL AT MyProfitLong Limit
    ELSIF SHORTONMARKET THEN
    EXITSHORT AT MyProfitShort Limit
    ENDIF
     
     
    IF ONMARKET AND (BarIndex - TradeIndex >= 40) then
    EXITSHORT AT MARKET
    SELL AT MARKET
    ENDIF
    graph NoPattern
    graphonprice MyProfitLong  coloured(0,0,255,255)
    graphonprice MyProfitShort coloured(0,255,0,255)
    graph Conto
    graph MaxBarre
    graph OnMarket
    carlo__ thanked this post
    x-1.jpg x-1.jpg
    #109139 quote
    carlo__
    Participant
    Average

    Grazie mille, un ultimissima cosa. Hai notato che il sistema non apre posizioni short? Possibile?

    #109141 quote
    robertogozzi
    Moderator
    Master

    Non ci avevo fatto caso.

    Sostituisci la linea 58 con:

    IF Bearish[1] and Bullish and (Open=Low) and (Body[1]) AND UpperSHGreen AND (NoPattern=0)THEN
    #109144 quote
    carlo__
    Participant
    Average

    Niente da fare!! Eppure mi sembra tutto ok nello script……veramente strano adesso mi guardo per bene gli indicatori che mi hai messo per controllare la situazione sul grafico…

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

Apertura trade dopo numero barre con condizione


ProOrder: Trading Automatico & Backtesting

New Reply
Author
author-avatar
carlo__ @carlo__ Participant
Summary

This topic contains 23 replies,
has 2 voices, and was last updated by robertogozzi
6 years, 4 months ago.

Topic Details
Forum: ProOrder: Trading Automatico & Backtesting
Language: Italian
Started: 09/30/2019
Status: Active
Attachments: 7 files
Logo Logo
Loading...