R05Participant
Veteran
Gent.mi, avrei bisogno di individuare il massimo e il minimo che va dalle 9.00 alle 12.00 per poi utilizzarli alle 15.30 con l’apertura dell’america in brakout.
Ho trovato questi codici ma, usando il timeframe ad 1 minuto, mi fanno aprire le operazioni ogni minuto.
IF (TIME >= 090000) AND (TIME <= 120000) THEN
b=High
c=Low
ENDIF
if intradaybarindex=0 then
valeurhaute=0
valeurbasse=close*100
endif
tcondition = time>090000 and time<=100000
if tcondition then
valeurhaute = max(valeurhaute,high)
valeurbasse = min(valeurbasse,low)
endif
Mi aiutereste?
Eccolo:
IF Time = 090000 THEN
b = high
c = low
ENDIF
IF (TIME >= 090000) AND (TIME <= 120000) THEN
b = max(b,High)
c = min(c,Low)
ENDIF
R05Participant
Veteran
Grazie Roberto gentilissimo. Ho messo giù il seguente sistema ma mi fa aprire sempre tante operazioni. Io utilizzo come time frame per entrare il minuto.
BegTime=143000
EndTime=163000
IF Time = 090000 THEN
b = high
c = low
ENDIF
IF (TIME >= 090000) AND (TIME <= 120000) THEN
b = max(b,High)
c = min(c,Low)
ENDIF
//---------------------------------------------------------------------------------------
If Time >= BegTime and Time <= EndTime then
Buy 1 contract at b stop
endif
If Time >= BegTime and Time <= EndTime then
SellShort 1 contract at c stop
endif
set stop ploss 15
set target pprofit 9
Ti allego un’immagine.
Il problema principale è che tu esegui sempre ordini STOP, ma il prezzo varia e può essere anche migliorativo (ad esempio se ti entra a B, esce, a quel punto il tuo ordine STOP viene preso LIMIT o a mercato, quindi entra immediatamente).
Prova a modificare le righe 14-20 con:
If Time >= BegTime and Time <= EndTime and Not OnMarket then
if close < b then
Buy 1 contract at b stop
else
Buy 1 contract at b limit
endif
endif
If Time >= BegTime and Time <= EndTime abd Not OnMarket then
if close > c then
SellShort 1 contract at c stop
else
SellShort 1 contract at c limit
endif
endif
Resta sempre il problema che, in esecuzione (non in backtest) il prezzo di entrata deve essere a mercato, oppure deve rispettare la distanza minima che il broker ha stabilito per ciascun strumento.
Se, ad esempio, per il DAX il broker ha stabilito che il prezzo d’entrata non deve essere inferiore/superiore a 6 pips da quello corrente, se non rientra in questo intervallo l’ordine viene eseguito a mercato (o respinto).
R05Participant
Veteran
Si Roberto ho modificato il sistema nel modo seguente e sembra andare bene. L’unico problema è che mi fa entrare più volte.
BegTime=143000
EndTime=163000
IF Time = 090000 THEN
b = high
c = low
ENDIF
IF (TIME >= 090000) AND (TIME <= 120000) THEN
b = max(b,High)
c = min(c,Low)
ENDIF
//---------------------------------------------------------------------------------------
If Time >= BegTime and Time <= EndTime and Not OnMarket then
if close < b then
Buy 1 contract at b stop
endif
endif
If Time >= BegTime and Time <= EndTime and Not OnMarket then
if close > c then
SellShort 1 contract at c stop
endif
endif
set stop ploss 15
set target pprofit 9
Però ora che ci penso posso risolverlo in questo modo: dico al sistema di fare una sola operazione al giorno (che poi è quello che dovrebbe fare) e solo se prende lo stop fare il rientry. Ora ci lavoro, perchè dovrei avercelo già il codice. Ti aggiorno
R05Participant
Veteran
Ecco qui Roberto, dovrei aver risolto. Fa solo un’operazione al giorno, tranne nel caso in cui la prima operazione è andata in stop, in tal caso mi farà il rientry.
BegTime=143000
EndTime=163000
IF Time = 090000 THEN
b = high
c = low
ENDIF
IF (TIME >= 090000) AND (TIME <= 120000) THEN
b = max(b,High)
c = min(c,Low)
ENDIF
//---------------------------------------------------------------------------------------
if intradaybarindex=0 then
tradethisday=0
else
if barindex=tradeindex then
tradethisday=1
endif
endif
// questo è il rientry
IF NOT ONMARKET THEN
IF POSITIONPERF(1)<0 THEN
BUY 1 CONTRACTS AT b stop
ENDIF
IF POSITIONPERF(1)<0 then
SELLSHORT 1 CONTRACTS at c stop
ENDIF
ENDIF
If Time >= BegTime and Time <= EndTime and Not OnMarket and tradethisday=0 then
if close < b then
Buy 1 contract at b stop
endif
endif
If Time >= BegTime and Time <= EndTime and Not OnMarket and tradethisday=0 then
if close > c then
SellShort 1 contract at c stop
endif
endif
set stop ploss 15
set target pprofit 9
Le righe 23-30 non mi sembrano corrette, le riscriverei così:
IF StrategyProfit < StrategyProfit[1] THEN
tradethisday=0
ENDIF
Perché POSITIONPERF ti da il risultato momentaneo di una posizione aperta, mentre STRATEGYPROFIT dopo la chiusura.
R05Participant
Veteran
Gent.mo Roberto avrei un dubbio. Il codice seguente fa fare al sistema una sola operazione al giorno. Il mio dubbio è:
- ho un sistema che ha questa dicitura che opera la mattina
- ho un altro sistema che ha questa dicitura ma che opera il pomeriggio
- ho altri sistemi che girano contemporaneamente ai precedenti, però non hanno questa dicitura
Se il primo sistema quello della mattina fa la sua operazione, gli altri si fermano o continuano in modo autonomo?
if intradaybarindex=0 then
tradethisday=0
else
if barindex=tradeindex then
tradethisday=1
endif
endif
Il primo che entra, quindi quello della mattina, blocca tutto.
Se vuoi che il trading riparta ad un certo punto della giornata, senza aspettare il giorno successivo, devi rimettere la variabile a 0, ad esempio ad una certa ora, oppure quando si verifica un incrocio di medie o l’RSI entra o esce da una certa zona, ecc…
Esempio 1:
IF Time >= 143000 THEN //riparti dalle 14:30
tradethisday=0
ENDIF
Esempio 2:
IF Rsi[14](close) CROSSES OVER 70 THEN //riparti quando RSI entra in IperComprato
tradethisday=0
ENDIF
Esempio 3:
IF average[10,0](close) CROSSES OVER average[50,0](close) THEN //riparti quando le due medie incrociano al rialzo
tradethisday=0
ENDIF
R05Participant
Veteran
Ok grande Roberto grazie, lo inserisco subito.
R05Participant
Veteran
Gent.mo Roberto ho inserito il codice suddetto sulla seguente strategia, però dopo la prima operazione il sistema si è bloccato. Non so se ho sbagliato qualcosa.
//-------------------------------------------------------------------------
// Codice principale : coach supertrendmultrame 2
//-------------------------------------------------------------------------
// Definizione dei parametri del codice
DEFPARAM CumulateOrders = False // Posizioni cumulate disattivate
// Il sistema cancellerà tutti gli ordini in attesa e chiuderà tutte le posizioni a 0:00. Dopo l'orario "Flat Before" non saranno piazzati nuovi ordini o posizioni.
DEFPARAM FLATBEFORE = 090000
// Cancellare tutti gli ordini in attesa e chiudere tutte le posizioni all'orario "Flat After"
DEFPARAM FLATAFTER = 220000
ONCE varlow=5 //number of candle for trailing stop long
ONCE varhigh=5 //number of candle for trailing stop short
timeframe(30 minutes,updateonclose)
// Condizioni per entrare su posizioni long
indicator1 = SuperTrend[3,10]
if indicator1 < close then
coloreverde = indicator1
colorerosso = 0
endif
// Condizioni per entrare su posizioni short
indicator1 = SuperTrend[3,10]
if indicator1 > close then
colorerosso = indicator1
coloreverde = 0
endif
timeframe(default) //time frame 1 minuto
// Condizioni per entrare su posizioni long
IF close<coloreverde and close>colorerosso then //riparti dalle 14:30
tradethisday=0
ENDIF
IF low<coloreverde and close>coloreverde and tradethisday=0 THEN
BUY 1 CONTRACT AT high + 1 stop
ENDIF
if longonmarket then
exitlow=lowest[varlow](low) //exit level for long position
sell at exitlow - 2 stop
endif
// Condizioni per entrare su posizioni long
IF high>colorerosso and close<colorerosso and tradethisday=0 THEN
SELLSHORT 1 CONTRACT AT low - 1 stop
ENDIF
if shortonmarket then
exithigh=highest[varhigh](high) //exit level for short position
exitshort at exithigh + 2 stop
endif
// trailing stop function
trailingstart = 10 //20 trailing will start @trailinstart points profit
trailingstep = 0 //12 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
CAND = BarIndex - TradeIndex
IF newSL > 0 AND CLOSE[1] > HIGHEST[CAND](CLOSE) THEN
newSL = CLOSE[1] - TrailingStart*PipSize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL = 0 AND TRADEPRICE(1) - CLOSE[1] >= TrailingStart*PipSize THEN
newSL = TRADEPRICE(1) - TrailingStep*PipSize
ENDIF
//next moves
CAND = BarIndex - TradeIndex
IF newSL > 0 AND CLOSE[1] < LOWEST[CAND](CLOSE) THEN
newSL = CLOSE[1] + TrailingStart*PipSize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL > 0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
// Stop e target
SET STOP pLOSS 100
SET TARGET pPROFIT 100
Per prima cosa vedo che non hai mai messo tradethisday=0, a cosa ti serve se non lo usi?
Per il resto a me funziona regolarmente e mi ha aperto e chiuso molte posizioni.
R05Participant
Veteran
Si Roberto non so se ho capito comunque ti estrapolo la parte in cui c’è tradethisday=0. Può essere che il sistema si è bloccato per un altro motivo. Nel backtest non dà problemi. Non saprei.
IF close<coloreverde and close>colorerosso then //riparti dalle 14:30
tradethisday=0
ENDIF
IF low<coloreverde and close>coloreverde and tradethisday=0 THEN
BUY 1 CONTRACT AT high + 1 stop
ENDIF
if longonmarket then
exitlow=lowest[varlow](low) //exit level for long position
sell at exitlow - 2 stop
endif
// Condizioni per entrare su posizioni long
IF high>colorerosso and close<colorerosso and tradethisday=0 THEN
SELLSHORT 1 CONTRACT AT low - 1 stop
ENDIF