Uscita Stop-Limit TS

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #161700 quote
    MauroPro
    Participant
    Veteran

    In un TS che ha come “una” delle varie condizioni di uscita un uscita in stopLoss se il prezzo è sotto un certo livello – x punti, come posso scrivere la formula per far uscire il TS al raggiungimento di questo limite senza attendere la chiusura della barra (ossia che mi rispetti gli x punti)?

    Penso che dovrei utilizzare un ordine stop, ma non so come inserirlo nella condizione sotto (non uso insertPrt in quanto sarebbe una formula molto più complicata, mentre la logica dell’esempio è chiara)

    Esempio:

    marginError=10

    c1BuyExit= close<(resistenza-marginError)

    c2Buy Exit=….

    cLongExit= c1BuyExit and c2BuyExit

    //———————————

    If cLongExit then

    sell 1 contract at market

    endif

     

    GRAZIE

     

    Grazie

    #161701 quote
    MauroPro
    Participant
    Veteran

    L’ordine stop deve essere solo per c1BuyExit, NON per c2BuyExit

    #161719 quote
    robertogozzi
    Moderator
    Master

    Devi calcolare lo SL in forma di prezzo quando entri in posizione ed inserire un ordine pendente STOP ad ogni barra finché sei a mercato.

    Quando non sei a mercato azzera il prezzo di stoploss che avevi calcolato:

    If Not OnMarket then
       StopLoss = 0
    Endif
    If MieCondizioni then
       Buy .....
       StopLoss = close - X * pipsize
    Endif
    If StopLoss > 0 then
       Sell at StopLoss STOP
    Endif
    #161727 quote
    MauroPro
    Participant
    Veteran

    Grazie per la risposta Roberto, ma essendo condizioni raggruppate non mi sembra così facile come nel tuo esempio da singola condizione. Dovrei inserire l’ordine STOP nella condizione long: c1BuyExit (che in pratica è uno SL) e nella condizione short: c1SellExit).

    Se non è troppo complesso prova ad inserirlo nella parte del codice sotto riportato, mi servirebbe più come esempio per future prove che per il singolo caso riportato. Grazie

    marginError=6
    //----------------------------------------------------------------        PATTERN
    greenZone=vcH>vcH2
    violetZone=vcH<vcH2
    //----------------------------------------------------------------                               
    c1Buy=greenZone                                                                     //Buy
    c2Buy=(close[1]<bkL[1] and close>bkL) OR (close[1]<bkH[1] and close>bkH)
    c3Buy=high<upperChannelLine and high[1]<upperChannelLine[1]
    c1BuyExit=close<(vcL2-marginError)                                                  //Buy Exit
    c2BuyExit= vcH<vcH2 and close>tradePrice(1)and myMacd <= myKama
    c3BuyExit= vcH<vcH2 and close<tradePrice(1)
    //---------------
    c1Sell=violetZone                                                                   //Sell   
    c2Sell=(close[1]>bkH[1] and close<bkH) OR (close[1]>bkL[1] and close<bkL)
    c3Sell= low>lowerChannelLine and low[1]>lowerChannelLine[1]
    c1SellExit=close>(vcH2+marginError)                                                 //Sell
    c2SellExit=vcL>vcL2 and close<tradePrice(1) and mymacd >= myKama
    c3SellExit=vcL>vcL2 and close>tradePrice(1)
    // ------------------------------------------------------------------                               CONDIZIONI RAGGRUPPATE
    cLongEntry=c1Buy and c2Buy and c3Buy and limitHour
    cShortEntry=c1Sell and c2Sell and c3Sell and limitHour
    
    cLongExit=c1BuyExit or c2BuyExit or c3BuyExit
    cShortExit=c1SellExit or c2SellExit or c3SellExit
    // -------------------------------------------------------------------                               
    IF  cLongEntry  THEN
    BUY 1 CONTRACTS AT MARKET
    ENDIF
    IF   cLongExit then
    SELL 1 CONTRACTS AT MARKET
    endif
    If  cShortEntry   THEN
    SELLSHORT 1 CONTRACTS AT MARKET
    ENDIF
    If cShortExit then
    EXITSHORT 1 CONTRACTS AT MARKET
    ENDIF
    //----------------------------------------------------------------                                
    graph greenZone coloured(0, 238, 0)
    graph violetZone coloured(238, 130, 238)
    //----------------------------------------------------------------
    #161728 quote
    MauroPro
    Participant
    Veteran

    In pratica alla riga 9 e 16 ( non so perchè quando si inserisce un codice con insertPrt non si vedono i numeri sulla sinistra).

    #161730 quote
    robertogozzi
    Moderator
    Master

    Si, però devi inserirlo tra la riga 30 e la 31:

    c1BuyExit=close-X*pipsize-MarginError*pipsize

    lo stesso per il sell tra la 33 e la 34:

    c1SellExit=close+X*pipsize+MarginError*pipsize

    e, all’inizio, li azzeri quando non sei a mercato.

    Alla fine, prima di GRAPH, piazzi uno dei due ordini STOP (dipende se sei Long o Short) come ho fatto io.

    #161731 quote
    MauroPro
    Participant
    Veteran

    Ho provato questo ma non funziona (non esce in STOP)

    If not onMarket then
    c1BuyExit=0
    c1SellExit=0
    endif
    //--------------------------------------------------------------------
    IF  cLongEntry  THEN
    BUY 1 CONTRACTS AT MARKET
    c1BuyExit=close-1*pipsize-MarginError*pipsize
    ENDIF
    IF   cLongExit then
    SELL 1 CONTRACTS AT MARKET
    endif
    //------------------------------------------------------------------
    If  cShortEntry   THEN
    SELLSHORT 1 CONTRACTS AT MARKET
    c1SellExit=close+1*pipsize+MarginError*pipsize
    ENDIF
    If cShortExit then
    EXITSHORT 1 CONTRACTS AT MARKET
    ENDIF
    //----------------------------------------------------------------
    If c1BuyExit>0 then
    sell at c1BuyExit STOP
    endif
    if c1SellExit>0 then
    exitShort at c1SellExit STOP
    endif
    
    //----------------------------------------------------------------
    graph greenZone coloured(0, 238, 0)
    graph violetZone coloured(238, 130, 238)
    #161732 quote
    MauroPro
    Participant
    Veteran

    In questo esempio in cui il TS entra long (freccia blu) si dovrebbe  uscire in SL  al prezzo della linea dove c’è la freccia verde in cui si perdono 6 punti (marginError) dalla linea viola tratteggiata (vcL2)  

    Image-001-2.jpg Image-001-2.jpg
    #161734 quote
    robertogozzi
    Moderator
    Master

    Usa GRAPH o, meglio, GRAPHONPRICE con le due variabili c1BuyExit e c1SellExit.

    #161736 quote
    robertogozzi
    Moderator
    Master

    Verifica che le due variabili non vengano modificate DOPO l’entrata a mercato, perché vedo che non hai messo AND Not OnMarket alle righe 6 e 14.

    #161737 quote
    MauroPro
    Participant
    Veteran

    Ecco con :

    graph greenZone coloured(0, 238, 0)
    graph violetZone coloured(238, 130, 238)

    graphOnPrice c1BuyExit
    graphOnPrice c1SellExit

    Image-001-3.jpg Image-001-3.jpg
    #161768 quote
    MauroPro
    Participant
    Veteran

    Ho provato, ma non mi funziona. Me lo puoi correggere (solo parte long per semplicità)?

    marginError=6
    //----------------------------------------------------------------       
    greenZone=vcH>vcH2
    //----------------------------------------------------------------                                
    c1Buy=greenZone
    c2Buy=(close[1]<bkL[1] and close>bkL) OR (close[1]<bkH[1] and close>bkH)
    c3Buy=high<upperChannelLine and high[1]<upperChannelLine[1]
    c1BuyExit=close<(vcL2-marginError)
    c2BuyExit= vcH<vcH2 and close>tradePrice(1)and myMacd <= myKama
    c3BuyExit= vcH<vcH2 and close<tradePrice(1)
    
    cLongEntry=c1Buy and c2Buy and c3Buy and limitHour
    cShortEntry=c1Sell and c2Sell and c3Sell and limitHour
    
    // -------------------------------------------------------------------
    IF  cLongEntry and not onMarket THEN
    BUY 1 CONTRACTS AT MARKET
    c1BuyExit=close-1*pointSize-MarginError*pointSize
    ENDIF
    IF   cLongExit then
    SELL 1 CONTRACTS AT MARKET
    endif
    
    If c1BuyExit>0 then
    sell at c1BuyExit STOP
    endif
    #161783 quote
    MauroPro
    Participant
    Veteran

    Ho risolto in questo modo, puoi controllare se va bene?

    marginError=6*pointSize
    
    IF  not onMarket and cLongEntry THEN
    BUY 1 CONTRACTS AT MARKET
    stopLossBuy=vcL2-marginError
    ENDIF
    IF   cLongExit then
    SELL 1 CONTRACTS AT MARKET
    endif
    If stopLossBuy>0 then
    sell at stopLossBuy STOP
    endif
    //------------------------------------------------------------------
    If  not onMarket and cShortEntry THEN
    SELLSHORT 1 CONTRACTS AT MARKET
    stopLossSell=vcH2+marginError
    ENDIF
    If cShortExit then
    EXITSHORT 1 CONTRACTS AT MARKET
    ENDIF
    if c1SellExit>0 then
    exitShort at stopLossSell STOP
    endif
    #161789 quote
    robertogozzi
    Moderator
    Master

    Si, hai riassegnato alle due precedenti variabili un valore logico (io le avevo cambiate in stop loss), usandone altre due per lo stop loss.

    L’aggiunta di OnMarket alle condizioni garantisce che le righe 5 e 16 non siano eseguite quando già a mercato (questo perché DEFPARAM CumulateOrders=FALSE impedisce di accumulare posizioni ignorando BUY/SELLSHORT, ma tutte le eventuali altre istruzioni che ci sono entro IF…ENDIF vengono comunque eseguite).

    #161792 quote
    MauroPro
    Participant
    Veteran

    Ok perfetto, ti chiedo un altra cosa al volo che non mi funziona.

    Puoi controllare se va bene questo codice formale per aprire una posizione contraria di  un Ts  che è uscito da una posizione long con profitto e permangono ancora certe condizioni.

    Esempio:
    cLongEntry=  if not onMarket and c1 and c2 and c3
    cLongExit =  if longOnMarket and c4                                 //(es.: c4=close >average[50](close)
    ———
    cShortEntry = if (not onMarket and onMarket[1]) and  c4 and positionPerf(1)>0

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

Uscita Stop-Limit TS


ProOrder: Trading Automatico & Backtesting

New Reply
Author
author-avatar
MauroPro @mauropro Participant
Summary

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

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