R05Participant
Veteran
Buon pomeriggio, stavo facendo delle prove, partendo da una strategia, e ho ricavato il seguente codice. Vi allego anche il backtest su time frame orario (niente male da inizio anno).
Questa strategia va solo long alle 9 per chiudere in positivo alle 22 o in negativo con 30 euro di passivo.
La domanda è: come mai va solo long? Per andare anche short come faccio?
DEFPARAM FLATAFTER=215500 // replace closetime condition
RangeMultiplier=0.1
BegTime=090000
EndTime=105500
MyContracts=1
if intradaybarindex=0 then
maxSetup = 0
minSetup = 0
tradethisday=0
else
if barindex=tradeindex then
tradethisday=1
endif
endif
//individuo maz e minimo prima ora
if time = 090500 then
maxprimaora=High
minprimaora=Low
endif
if time > 090000 and time <= 090500 then
if High > maxprimaora then
maxprimaora = High
endif
if Low < minprimaora then
minprimaora = Low
endif
endif
If Time >= BegTime and Time <= EndTime and tradethisday=0 then
if maxSetup=0 then
maxSetup = maxprimaora + RangeMultiplier * (dhigh(1) - dlow(1))
slshort=minprimaora
endif
if minsetup=0 then
minSetup = minprimaora - RangeMultiplier * (dhigh(1) - dlow(1))
sllong = maxprimaora
endif
Buy mycontracts contract at maxSetup stop
Sell at slshort stop
SellShort mycontracts contract at minSetup stop
ExitShort at sllong stop
endif
set stop ploss 30
R05Participant
Veteran
Gentilissimi, nessuno mi può aiutare?
GRAPH minsetup
GRAPH maxsetup
GRAPH Close
Queste istruzioni nel codice certamente salveranno la tua vita per sapere dove c’è il problema del tuo calcolo!
L’ho provato su DAX ed EUR/USD con TF 5 minuti ed il codice funziona benissimo sia Short che Long.
R05Participant
Veteran
Si grazie, il fatto è che sul 5 minuti non è efficace, mentre invece sull’orario non è niente male, solo che su questo time frame va solo long e non riesco a capire perchè.
Aggiungendo le istruzioni indicate da Nicolas potrai vedere i valori delle variabili e renderti conto dove può essere l’errore.
Ad ogni modo è una strategia fatta per TF di 5 minuti (o 1 minuto), non per 1 ora!
Andrebbe riscritta dall’inizio, magari ci posso provare, ma devi indicarmi sul quale TF la vuoi usare e su quale strumento e dirmi le condizioni per entrare LONG e SHORT.
R05Participant
Veteran
Ti ringrazio tanto Roberto, il fatto è che avevo preso una strategia, che è quella postata, e mi stavo dilettando a cambiare dei valori, e nel fare un backtest mi è uscito uno veramente interessante ma andava solo long, sicuramente dopo la barra della prima ora.
Comunque, in generale stavo studiando delle strategie di breakout della prima ora delle 8, solo che oltre a prendere il primo breakout si può prendere anche il secondo, non solo ma vorrei vedere anche se evitando di prendere il primo breakout si prende solo il secondo e vedere come va.
Mi spiego meglio:
- se il prezzo rompe il minimo è il primo breakout (e viceversa); il secondo breakout è quello che va dal lato long e quindi quello che cerca di rompere il massimo non quello che prova a rompere nuovamente il minimo.
Ti ringrazio come sempre.
R05Participant
Veteran
Lo strumento su cui lo voglio usare è sempre il dax, mi era sfuggito prima
Prova questa strategia, su DAX, TF h1:
// Definition of code parameters
DEFPARAM CumulateOrders = False
DEFPARAM FLATBEFORE = 090000
DEFPARAM FLATAFTER = 210000
DEFPARAM PreLoadBars = 2000
// initialization of variables
ONCE nLots = 1
ONCE TradingON = 0
ONCE MaxPrice = 0
ONCE MinPrice = 0
ONCE BreakMax = 0
ONCE BreakMin = 0
ONCE BreakOutTime = 090000
ONCE BeginTrading = 090000
ONCE StopTrading = 130000
// check the correct time to trade
IF time >= BeginTrading AND time <= StopTrading THEN
TradingON = 1 //enable Trading when within desired time
ELSE
TradingON = 0 //reset variables when outside trading time
MaxPrice = 0
MinPrice = 0
ENDIF
// at BreakOut Time store LOW & HIGH for later use on BreakOut
IF time = BreakOutTime THEN
MaxPrice = high
MinPrice = low
BreakMax = 0
BreakMin = 0
ENDIF
// LONG
a = close > MaxPrice
IF BreakMax = 0 THEN
BreakMax = a //Set FIRST BreakOut (long)
ENDIF
IF a AND BreakMin AND TradingON THEN //Trade at Trading Time and when FIRST BO already occurred
BUY nLots CONTRACT AT MARKET
ENDIF
// SHORT
b = close < MinPrice
IF BreakMin = 0 THEN
BreakMin = b //Set FIRST BreakOut (short)
ENDIF
IF b AND BreakMax AND TradingON THEN //Trade at Trading Time and when FIRST BO already occurred
SELLSHORT nLots CONTRACT AT MARKET
ENDIF
// Target Profit & Stop Loss
SET target pprofit 80 //80
SET STOP pLOSS 30 //30
l’ho provata poco, verifica bene che entri solo al secondo Break Out.
R05Participant
Veteran
Gent.mo Roberto ti ringrazio tanto per la tua disponibilità, però il codice non fa quello che mi servirebbe. Entra in posizione non con il secondo breakout e poi a livelli di prezzo che non sono i massimi o minimi della prima ora.
Ho verificato le ultime due operazioni ed entra effettivamente solo al SECONDO breakout.
Per quanto riguarda il prezzo è chiaro che dopo il breakout la candela, che dura un’ora, può avere fatto anche diversi Pips, ma il trigger c’è stato. Per questo inconveniente occorre utilizzare TF inferiori, oppure piazzare ordini pendenti.
Vedo se riesco a farcela adesso a convertire la strategia con ordini pendenti.
R05Participant
Veteran
Ok grazie Roberto, gentilissimo. Mi sono fatto ingannare dal fatto che entra alla candela successiva.
Questa è una versione modificata per entrare con ordini pendenti, però è veramente poco performante. Il problema è che entra alla rottura dei Minimi/Massimi anche in corso di candela, però capita sovente che alla chiusura il prezzo abbia ritracciato e sia tornato sotto/sopra i Massimi/Minimi, ma a quel punto l’operazione è in corso e deve terminare, probabilmente in stop loss.
Quindi ha il vantaggio di non perdere Pips in entrata, ma ha quello che la candela NON è chiusa ed il prezzo non è detto vada davvero in quella direzione!
Secondo me la soluzione migliore è la prima, magari provando ad abbassare il TF.
Buon trading e buon fine settimana!
// Definition of code parameters
DEFPARAM CumulateOrders = False
DEFPARAM FLATBEFORE = 090000
DEFPARAM FLATAFTER = 210000
DEFPARAM PreLoadBars = 2000
// initialization of variables
ONCE nLots = 1
ONCE TradingON = 0
ONCE MaxPrice = 0
ONCE MinPrice = 0
ONCE BreakMax = 0
ONCE BreakMin = 0
ONCE BreakOutTime = 090000
ONCE BeginTrading = 090000
ONCE StopTrading = 130000
// check the correct time to trade
IF time >= BeginTrading AND time <= StopTrading THEN
TradingON = 1 //enable Trading when within desired time
ELSE
TradingON = 0 //reset variables when outside trading time
MaxPrice = 0
MinPrice = 0
BreakMax = 0
BreakMin = 0
ENDIF
// at BreakOut Time store LOW & HIGH for later use on BreakOut
IF time = BreakOutTime THEN
MaxPrice = high
MinPrice = low
BreakMax = 0
BreakMin = 0
ENDIF
//
IF OnMarket THEN
TradingON = 0 //reset variables after any trade
MaxPrice = 0
MinPrice = 0
BreakMax = 0
BreakMin = 0
ENDIF
// LONG
a = close > MaxPrice
IF BreakMax = 0 THEN
BreakMax = a //Set FIRST BreakOut (long)
ENDIF
IF BreakMin AND TradingON THEN //Trade at Trading Time and when FIRST BO already occurred
BUY nLots CONTRACT AT MaxPrice STOP
ENDIF
// SHORT
b = close < MinPrice
IF BreakMin = 0 THEN
BreakMin = b //Set FIRST BreakOut (short)
ENDIF
IF BreakMax AND TradingON THEN //Trade at Trading Time and when FIRST BO already occurred
SELLSHORT nLots CONTRACT AT MinPrice STOP
ENDIF
// Target Profit & Stop Loss
SET target pprofit 95 //95
SET STOP pLOSS 145 //145
//GRAPH MaxPrice
//GRAPH MinPrice
R05Participant
Veteran
Ti ringrazio tanto. Questo fine settimana faccio un pò di prove. Grazie di nuovo e buon fine settimana anche a te.