Istruzione IF nidificata che sembra non funzionare

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #161434 quote
    RockerGab
    Participant
    New

    Buongiorno a tutti;

    Sto chiedendo aiuto per capire perché le istruzioni “IF” annidate identificate dal commento “// Questa condizione non …”. Secondo le mie aspettative, la variabile “flagprofitLong” dovrebbe assumere il valore “1” quando la variabile c3, nella terza barra dall’inizio dello scambio, ha un valore di zero. Sfortunatamente, questo non accade. Stesso problema nel caso di Short trade con variabili associate.

    Grazie a tutti coloro che possono fornire una spiegazione.

    RockerGab

    #161437 quote
    robertogozzi
    Moderator
    Master

    Perché non basta la IF, devi vedere il valore di c3 e c4 in quella candela.

    Devi mettere c3[3], anche per c4.

    RockerGab thanked this post
    #161699 quote
    RockerGab
    Participant
    New

    Grazie “robertogozzi”. In effetti inserendo la modifica che mi hai suggerito, le variabili “flagprofitLong” e “flagprofitShort” assumono il valore “1” ma in modo contrario rispetto a quelli che sono i miei obiettivi. Io vorrei che il TS testasse nella terza barra dopo l’ingresso se è iniziato un trend oppure no. In un trade Long la variabile c3 dovrebbe avere valore “1” in presenza di 3 barre con la Media Mobile di Close a 2 barre in aumento e “0” invece quando non è così. Ora il “flagprofitLong” assume valore “1” soltanto nei trade Short in trend che fanno “scattare” la variabile “c3” al valore zero (non è sbagliato ma non è ciò che vorrei…). Ho provato a modificare il TS nel modo sotto indicato ma in questo modo le variabili “flagprofit” Long e Short restano sempre a zero.

    IF LONGONMARKET THEN
       IF (BARINDEX - TRADEINDEX[1]) = 3 THEN
          IF not c3[3] THEN
             flagprofitLong = 1
          ENDIF
       ENDIF
    ENDIF

    Spero di essere riuscito a spiegare qual è il mio intento.

    Grazie ancora per un altro suggerimento.

    RockerGab

    #161718 quote
    robertogozzi
    Moderator
    Master

    Per verificare ls barra successiva all’entrata basta che fai riferimento a c3[2], invece di c3[3].

    #162041 quote
    RockerGab
    Participant
    New

    Grazie “robertogozzi”. Mi sembra di aver compreso il nocciolo della questione ed ho modificato il mio TS invertendo la sua logica e, soprattutto, andando a testare le variabili c3 e c4 nella terza barra a partire da quella di ingresso. Allego il mio TS modificato che ora funziona nel modo che intendevo.

    defparam cumulateorders = false
    
    
    BUDGET = 10000
    
    c1 = HIGH > HIGH[1] AND LOW > LOW[1]// AND HIGH[1] > HIGH[2] AND LOW[1] > LOW[2]       PEGGIORAMENTO
    
    c2 = HIGH < HIGH[1] AND LOW < LOW[1]// AND HIGH[1] < HIGH[2] AND LOW[1] < LOW[2]       PEGGIORAMENTO
    
    c3 = AVERAGE[2](CLOSE[0]) > AVERAGE[2](CLOSE[1]) AND AVERAGE[2](CLOSE[1]) > AVERAGE[2](CLOSE[2])
    
    c4 = AVERAGE[2](CLOSE[0]) < AVERAGE[2](CLOSE[1]) AND AVERAGE[2](CLOSE[1]) < AVERAGE[2](CLOSE[2])
    
    IF c1 AND NOT SHORTONMARKET THEN
       BUY BUDGET CASH AT MARKET
       SET STOP %LOSS SL
    ENDIF
    
    IF c2 AND NOT LONGONMARKET THEN
       SELLSHORT BUDGET CASH AT MARKET
       SET STOP %LOSS SL
    ENDIF
    
    
    IF (BARINDEX - TRADEINDEX[1]) = 4 THEN
       IF c3[1] THEN                                    
          flagprofitLong = 1
       ENDIF
    ENDIF
    
    
    IF NOT ONMARKET THEN
       flagprofitLong  = 0
       flagprofitShort = 0
    ENDIF
    
    
    IF LONGONMARKET AND NOT flagprofitLong THEN
       SELL AT TRADEPRICE*(1+profitvalue)LIMIT
       flagprofitLong  = 0
    ENDIF
    
    IF (BARINDEX - TRADEINDEX[1]) = 4 THEN
       IF c4[1] THEN                                   
          flagprofitShort = 1
       ENDIF
    ENDIF
    
    
    IF SHORTONMARKET AND NOT flagprofitShort THEN
       EXITSHORT AT TRADEPRICE*(1-profitvalue)LIMIT
       flagprofitShort = 0
    ENDIF
    
    
    //************************************************************************
    
    //GRAPH c3 as "average"
    GRAPH flagprofitLong as "flagLONG"
    GRAPH flagprofitShort as "flagSHORT"
    //GRAPH BARINDEX as "bar"
    

    Alla prossima.

    RockerGab

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

Istruzione IF nidificata che sembra non funzionare


ProBuilder: Indicatori & Strumenti Personalizzati

New Reply
Author
author-avatar
RockerGab @rockergab Participant
Summary

This topic contains 4 replies,
has 2 voices, and was last updated by RockerGab
5 years, 1 month ago.

Topic Details
Forum: ProBuilder: Indicatori & Strumenti Personalizzati
Language: Italian
Started: 02/14/2021
Status: Active
Attachments: No files
Logo Logo
Loading...