ProRealCode - Trading & Coding with ProRealTime™
Buona sera.
Con il codice che allego ho un il seguente problema. Il programma piazza il pendente correttamente, se il pendente non entra a mercato, si cancella al termine della candela. Nel caso invece il pendente entri al mercato, al termine della candela viene correttamente chiuso, ma il pendente alla candela successiva non viene piazzato.
Chiedo cortesemente aiuto per risolvere la questione.
Buona serata.
DEFPARAM CumulateOrders = False
DEFPARAM PreLoadBars = 10000
////////////////////////////////////////////////////////////////////////////////////////
ONCE Capitale = 4196
ONCE LotNumber = 1
ONCE MinLots = 1
ONCE SpreadEntrata = 10 * pipsize
ONCE SpreadPendente = 10 * pipsize
//ONCE MargineEntrata = 1 * pipsize
ONCE MarginePendente = 1 * pipsize
ONCE StopMinimo = 40 * pipsize
ONCE StopMassimo = 200 * pipsize
ONCE LimiteMaxBarra = 450 * pipsize
//NumeroBarra = 0
//OrarioChiusura = 0
TradingDay = OpenDayOfWeek >= 0 AND OpenDayOfWeek <= 5 //trade only Mon. to Fri.
TradingOrario = CurrentTime <=210000 AND CurrentTime>=000000
//Testo se la barra è di alta volatilità
AltaVolatilita = range > LimiteMaxBarra
//Definisco il tipo barra
IF (close > (high + low) / 2) AND NOT AltaVolatilita THEN
TipoBarra = 1
ELSIF (close < (high + low) /2) AND NOT AltaVolatilita THEN
TipoBarra = 2
ELSIF (close > (high + low) / 2) AND AltaVolatilita THEN
TipoBarra = 3
ELSIF (close > (high + low) / 2) AND AltaVolatilita AND (high - close) > StopMassimo THEN
TipoBarra = 4
ELSIF (close < (high + low) /2) AND AltaVolatilita AND (close - low) < StopMassimo THEN
TipoBArra = 5
ELSIF (close < (high + low) /2) AND AltaVolatilita AND (close - low) > StopMassimo THEN
TipoBarra = 6
ENDIF
// Chiude tutte le posizioni alla chiusura della barra
IF (Barindex -Tradeindex) >= 1 THEN
IF ONMARKET THEN
SELL AT MARKET //exit LONG trades, if any
EXITSHORT AT MARKET //exit SHORT trades, if any
ENDIF
ENDIF
//Credo dica il profitto è dato dal capitale + il ricavato dal programma
MyProfit = Capitale + StrategyProfit
//Credo centri qualche cosa con l'aumento della size al cambio del mese
IF Month <> Month[1] THEN
LotNumber = max(MinLots, (MyProfit / Capitale))
ENDIF
IF TipoBarra = 1 AND TradingDay AND TradingOrario THEN
//PASSO = 1
StopEntrata = ((high + SpreadEntrata) - close)
EntrataPendente = close + max(StopMinimo, StopEntrata) + 1
StopPendente = (EntrataPendente - (low - SpreadPendente - MarginePendente))
//STOP CORRETTO
BUY LotNumber CONTRACT AT EntrataPendente * pipsize STOP
SET STOP pTRAILING StopPendente * pipsize
ELSIF TipoBarra = 2 AND TradingDay AND TradingOrario THEN
//PASSO = 2
StopEntrata = (close - (low - SpreadEntrata))
EntrataPendente = close - max (StopMinimo,StopEntrata) - 1
StopPendente = ((high + SpreadPendente + MarginePendente) - EntrataPendente)
//STOP CORRETTO
SELLSHORT LotNumber CONTRACT AT EntrataPendente * pipsize STOP
SET STOP pTRAILING StopPendente * pipsize
ELSIF Tipobarra = 4 AND TradingDay AND TradingOrario THEN
//PASSO = 4
EntrataPendente = high + SpreadEntrata - StopMAssimo
SELLSHORT LotNumber CONTRACT AT EntrataPendente * pipsize STOP
SELLSHORT LotNumber CONTRACT AT EntrataPendente * pipsize LIMIT
SET STOP pTRAILING StopMassimo
ELSIF TipoBarra = 6 AND TradingDay AND TradingOrario THEN
//PASSO = 6
EntrataPendente = low - SpreadEntrata + StopMAssimo
SELLSHORT LotNumber CONTRACT AT EntrataPendente * pipsize STOP
SELLSHORT LotNumber CONTRACT AT EntrataPendente * pipsize LIMIT
SET STOP pTRAILING StopMassimo
ENDIF
Mi sembra vada bene, ad esempio, il 24/04/2019 non è entrato perché era già a mercato dal giorno precedente.
Ho aggiunto GRAPH con vari valori per verificare certe variabili.
Ho fatto una verifica solo su 4-5 operazioni. Magari è un puro caso che vadano bene.
Se ce n’è qualcuna errata dimmi in che giorno e perché avrebbe dovuto entrare e non è entrata, così verifico una (o più di una) operazione specifica.
Ecco il codice modificato e con l’aggiunta di GRAPH per il debugging:
DEFPARAM CumulateOrders = False
DEFPARAM PreLoadBars = 10000
////////////////////////////////////////////////////////////////////////////////////////
ONCE Capitale = 4196
ONCE LotNumber = 1
ONCE MinLots = 1
ONCE SpreadEntrata = 10
ONCE SpreadPendente = 10
//ONCE MargineEntrata = 1
ONCE MarginePendente = 1
ONCE StopMinimo = 40
ONCE StopMassimo = 200
ONCE LimiteMaxBarra = 450
//NumeroBarra = 0
//OrarioChiusura = 0
TradingDay = OpenDayOfWeek >= 0 AND OpenDayOfWeek <= 5 //trade only Mon. to Fri.
TradingOrario = CurrentTime <=210000 AND CurrentTime>=000000
//Testo se la barra è di alta volatilità
AltaVolatilita = range > LimiteMaxBarra
//Definisco il tipo barra
IF (close > (high + low) / 2) AND NOT AltaVolatilita THEN
TipoBarra = 1
ELSIF (close < (high + low) /2) AND NOT AltaVolatilita THEN
TipoBarra = 2
ELSIF (close > (high + low) / 2) AND AltaVolatilita THEN
TipoBarra = 3
ELSIF (close > (high + low) / 2) AND AltaVolatilita AND (high - close) > StopMassimo THEN
TipoBarra = 4
ELSIF (close < (high + low) /2) AND AltaVolatilita AND (close - low) < StopMassimo THEN
TipoBArra = 5
ELSIF (close < (high + low) /2) AND AltaVolatilita AND (close - low) > StopMassimo THEN
TipoBarra = 6
ENDIF
// Chiude tutte le posizioni alla chiusura della barra
IF (Barindex -Tradeindex) >= 1 THEN
IF ONMARKET THEN
SELL AT MARKET //exit LONG trades, if any
EXITSHORT AT MARKET //exit SHORT trades, if any
ENDIF
ENDIF
//Credo dica il profitto è dato dal capitale + il ricavato dal programma
MyProfit = Capitale + StrategyProfit
//Credo centri qualche cosa con l'aumento della size al cambio del mese
IF Month <> Month[1] THEN
LotNumber = max(MinLots, (MyProfit / Capitale))
ENDIF
PASSO = 0
StopPendente = 0
StopEntrata = 0
EntrataPendente = 0
IF TipoBarra = 1 AND TradingDay AND TradingOrario THEN
PASSO = 1
StopEntrata = ((high + SpreadEntrata) - close)
EntrataPendente = close + max(StopMinimo, StopEntrata) + 1
StopPendente = (EntrataPendente - (low - SpreadPendente - MarginePendente))
//STOP CORRETTO
//BUY LotNumber CONTRACT AT EntrataPendente * pipsize STOP
BUY LotNumber CONTRACT AT EntrataPendente STOP
//SET STOP pTRAILING StopPendente * pipsize
SET STOP TRAILING StopPendente
ELSIF TipoBarra = 2 AND TradingDay AND TradingOrario THEN
PASSO = 2
StopEntrata = (close - (low - SpreadEntrata))
EntrataPendente = close - max (StopMinimo,StopEntrata) - 1
StopPendente = ((high + SpreadPendente + MarginePendente) - EntrataPendente)
//STOP CORRETTO
//SELLSHORT LotNumber CONTRACT AT EntrataPendente * pipsize STOP
SELLSHORT LotNumber CONTRACT AT EntrataPendente STOP
//SET STOP pTRAILING StopPendente * pipsize
SET STOP TRAILING StopPendente
ELSIF Tipobarra = 4 AND TradingDay AND TradingOrario THEN
PASSO = 4
EntrataPendente = high + SpreadEntrata - StopMAssimo
//SELLSHORT LotNumber CONTRACT AT EntrataPendente * pipsize STOP
SELLSHORT LotNumber CONTRACT AT EntrataPendente STOP
//SELLSHORT LotNumber CONTRACT AT EntrataPendente * pipsize LIMIT
SELLSHORT LotNumber CONTRACT AT EntrataPendente LIMIT
SET STOP TRAILING StopMassimo
ELSIF TipoBarra = 6 AND TradingDay AND TradingOrario THEN
PASSO = 6
EntrataPendente = low - SpreadEntrata + StopMAssimo
//SELLSHORT LotNumber CONTRACT AT EntrataPendente * pipsize STOP
SELLSHORT LotNumber CONTRACT AT EntrataPendente STOP
//SELLSHORT LotNumber CONTRACT AT EntrataPendente * pipsize LIMIT
SELLSHORT LotNumber CONTRACT AT EntrataPendente LIMIT
SET STOP TRAILING StopMassimo
ENDIF
//
graph Passo
graph TipoBarra
graph EntrataPendente
graph StopEntrata
graph StopPendente
Ho tolto tutti i “* pipsize”, in quanto avevi mischiato un pò le cose, per fortuna sul Nikkei, come sul Dax ed altri indici, il rapporto tra pips e prezzo è 1:1, per cui in realtà non cambia niente, ma sul Forex o altri strumenti avreste avuto risultati imprevedibili.
PIPSIZE è una costante di sistema che permete di convertire da Pips a Prezzo o viceversa.
Per convertire un numero, ad esempio 10, oppure una variabile, ad esempio StopMassimo, in Pips, ocorre scrivere:
IF high - close > 10 * pipsize THEN....
SET Stop pLOSS StopMassimo * pipsize
ovviamente una volta che un dato è convertito in pips NON si può mischiare con un prezzo o cun una differenza di prezzi (tipo RANGE), non si può scrivere:
StopMassimo = 10 * pipsize
StopPendente = CLOSE + StopMassimo //CLOSE è un prezzo, StopMassimo soni PIPS
L’operazione opposta è la conversione da Prezzo in Pips:
IF (high - close) / pipsize > 10 * pipsize then... //converto la differenza di prezzo in pips e poi la confronto con un valore espresso in pips
SET Stop pLOSS (high - close) / pipsize
L’importante è NON mischiare PIPS e PREZZI.
Un’entrata con ordine pendente NON si può fare sui pips, ma su un certo prezzo (anche se, ripeto, in questo caso la cosa è ininfluente sul risultato).
Per il trailing stop è consigliabile usare il codice dalle righe 17 a 56 a questo link https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/.
SET STOP TRAILING può dare risultati molto diversi tra backtest e reale ed ha un passo di una sola unità (predefinita e immodificabile). Gli si può solo dire quando iniziare.
Grazie Roberto.
Il programma lo faccio girare sul Japan 225 barre orarie, quando l’ordine non viene pescato allora Pro Order lo cancella in automatico e va tutto bene. Nel caso l’ordine venga pescato nella barra oraria, alla chiusura della barra (chiamiamola barra 0) la posizione viene chiusa, ma non viene messo l’ordine nella barra successiva (barra 1). Nella barra ancora successiva (barra 2) viene regolarmente messo. Può essere che se apro una posizione long, a fine barra la chiude, ma non posso aprirne una di short?
Mi chiedevo se ogni strategia debba andare in un solo verso, ovvero se ho una strategia che in base a determinati valori di mercato debba entrare long o short, debba dividere la strategia stessa in due programmi di trading.
Mi potresti dire dove posso andare a leggeri come funzionano i programmi di prorealtime, non intendo come comandi, ma come vengono eseguiti, con che frequenza e quando?
Ti ringrazio per la tua attenzione e la tua cortesia.
Buona Pasqua a te e famiglia.
I manuali disponibili li trovi qui:
https://www.prorealtime.com/en/pdf/probacktest.pdf
https://www.prorealtime.com/en/pdf/probuilder.pdf
https://www.prorealtime.com/en/pdf/proscreener.pdf
Certi dettagli particolari però li trovi solo su questo forum, facendo ricerche e studiando altri codici.
Le strategie possono entrare sia Long che Short, ma non contemporaneamente.
Quanto al discorso della mancata entrata dovresti dirmi ora e data di una barra dove non è entrato o dove non ha piazzato il pendente.
Ad ogni modo prova anche con TRUE al posto di FALSE alla riga 1.
Buongiorno.
Sono a mercato con due posizioni short, se scrivo il comando exitshort, me ne chiude una sola. Per chiudere tutte le n posizioni short aperte come posso fare?
Grazie mille.
EXITSHORT chiude tutte le posizioni Short aperte, non è ancora supportata la chiusura parziale (in ogni caso andrebbe comunque scritto quante se ne vogliono chiudere).
Sa cosa hai visto che ne è restata una aperta?
Si avevo due posizioni una aperta da due barre, e una aperta da quattro barre, mi ha chiuso solo l’ultima.
Per darti una risposta devo potere replicare esattamente il problema, per cui mi serve:
Japan 225, 30 minuti. Prima posizione entrata alle ore 15, seconda posizione entrata alle ore 15.30. Seconda posizione chiusa alle 17 (chiusura barre 16.30).
DEFPARAM CumulateOrders = True
DEFPARAM PreLoadBars = 10000
////////////////////////////////////////////////////////////////////////////////////////
ONCE Capitale = 4196
ONCE LotNumber = 1
ONCE MinLots = 1
ONCE SpreadEntrata = 15 * pipsize
ONCE StopMinimo = 40 * pipsize
ONCE StopMassimo = 200 * pipsize
ONCE LimiteMaxBarra = 450 * pipsize
BarraVerde = Open < Close
BarraRossa = Open > Close
TradingDay = OpenDayOfWeek >= 0 AND OpenDayOfWeek <= 5 //trade only Mon. to Fri.
AltaVolatilita = range > LimiteMaxBarra
//Definisco il tipo barra
IF (close >= (high + low) / 2) AND NOT AltaVolatilita THEN
TipoBarra = 1
ELSIF (close < (high + low) /2) AND NOT AltaVolatilita THEN
TipoBarra = 2
ELSIF (close >= (high + low) / 2) AND AltaVolatilita THEN
TipoBarra = 3
ELSIF (close >= (high + low) / 2) AND AltaVolatilita AND (high - close) > StopMassimo THEN
TipoBarra = 4
ELSIF (close < (high + low) /2) AND AltaVolatilita AND (close - low) < StopMassimo THEN
TipoBArra = 5
ELSIF (close < (high + low) /2) AND AltaVolatilita AND (close - low) > StopMassimo THEN
TipoBarra = 6
ENDIF
// Se a mercato Short e Barra verde chiude la posizione SHORT, se a mercato Long e Barra rossa chiude la posizione LONG
IF SHORTONMARKET AND BarraVerde THEN
EXITSHORT AT MARKET //exit SHORT trades, if any
ELSIF LONGONMARKET AND BarraRossa THEN
SELL AT MARKET //exit LONG trades, if any
ENDIF
//Credo dica il profitto è dato dal capitale + il ricavato dal programma
MyProfit = Capitale + StrategyProfit
//Credo centri qualche cosa con l'aumento della size al cambio del mese
IF Month <> Month[1] THEN
LotNumber = max(MinLots, (MyProfit / Capitale))
ENDIF
IF TipoBarra = 1 AND TradingDay AND (NOT LONGONMARKET) THEN
StopEntrata = ((high + SpreadEntrata) - close)
SELLSHORT LotNumber CONTRACT AT MARKET
SET STOP pLOSS max (StopMinimo,StopEntrata) * pipsize
ELSIF TipoBarra = 2 AND TradingDay AND ( NOT SHORTONMARKET) THEN
StopEntrata = (close - (low - SpreadEntrata))
BUY LotNumber CONTRACT AT MARKET
SET STOP pLOSS max (StopMinimo,StopEntrata) * pipsize
ELSIF TipoBarra = 3 AND TradingDay AND (NOT LONGONMARKET) THEN
StopEntrata = ((high + SpreadEntrata) - close)
SELLSHORT LotNumber CONTRACT AT MARKET
IF StopEntrata >= 210 THEN
StopEntrata = StopMassimo + 10
ENDIF
SET STOP pLOSS max (StopMinimo,StopEntrata) * pipsize
ELSIF TipoBarra = 5 AND TradingDay AND (NOT SHORTONMARKET) THEN
StopEntrata = (close - (low - SpreadEntrata))
BUY LotNumber CONTRACT AT MARKET
SET STOP pLOSS max (StopMinimo,StopEntrata) * pipsize
ENDIF
//GRAPH max (StopMinimo,StopEntrata) * pipsize
//GRAPH TipoBarra
//GRAPH SHORTONMARKET
//GRAPH LONGONMARKET
A me ne apre solo una alle 15 e la chiude alle 17, come si vede dalla foto.
Il codice è esattamente quello che hai postato, non ho cambiato niente!
E se insieme al programma sopra girava anche questo. Effettivamente ieri sera preso dalla fretta mi sono spiegato male. La prima posizione è stata aperta dal programma
che hai analizzato, mentre la seconda è stata aperta dal programma che ti allego ora. Alle 17 il programma qui allegato ha chiuso correttamente, mentre quello che tu hai analizzato ha lasciato aperta la posizione, invece di chiuderla come mi hai fatto vedere tu.
Può essere che i due programmi in qualche maniera vadano in collisione?
ONCE Capitale = 4196
ONCE LotNumber = 1
ONCE MinLots = 1
ONCE SpreadEntrata = 15 * pipsize
ONCE SpreadPendente = 15 * pipsize
ONCE MarginePendente = 1 * pipsize
ONCE StopMinimo = 63 * pipsize
ONCE StopMassimo = 200 * pipsize
ONCE LimiteMaxBarra = 450 * pipsize
BarraVerde = Close > Open
BarraRossa = Close < Open
TradingDay = OpenDayOfWeek >= 0 AND OpenDayOfWeek <= 6 //trade only Mon. to Fri.
//Testo se la barra è di alta volatilità
AltaVolatilita = range > LimiteMaxBarra
//Definisco il tipo barra
IF (close > (high + low) / 2) AND NOT AltaVolatilita THEN
TipoBarra = 1
ELSIF (close < (high + low) /2) AND NOT AltaVolatilita THEN
TipoBarra = 2
ELSIF (close > (high + low) / 2) AND AltaVolatilita THEN
TipoBarra = 3
ELSIF (close > (high + low) / 2) AND AltaVolatilita AND (high - close) > StopMassimo THEN
TipoBarra = 4
ELSIF (close < (high + low) /2) AND AltaVolatilita AND (close - low) < StopMassimo THEN
TipoBArra = 5
ELSIF (close < (high + low) /2) AND AltaVolatilita AND (close - low) > StopMassimo THEN
TipoBarra = 6
ENDIF
// Se a mercato e Barra verde esce
IF SHORTONMARKET AND BarraVerde THEN
EXITSHORT AT MARKET //exit SHORT trades, if any
ELSIF LONGONMARKET AND BarraRossa THEN
SELL AT MARKET //exit LONG trades, if any
ENDIF
//Credo dica il profitto è dato dal capitale + il ricavato dal programma
MyProfit = Capitale + StrategyProfit
//Credo centri qualche cosa con l'aumento della size al cambio del mese
IF Month <> Month[1] THEN
LotNumber = max(MinLots, (MyProfit / Capitale))
ENDIF
IF TipoBarra = 1 AND TradingDay AND (NOT SHORTONMARKET) THEN
StopEntrata = ((high + SpreadEntrata) - close)
EntrataPendente = close + max(StopMinimo, StopEntrata) + MarginePendente
StopPendente = (EntrataPendente - (low - SpreadPendente))
BUY LotNumber CONTRACT AT EntrataPendente * pipsize STOP
SET STOP pLOSS StopPendente * pipsize
ELSIF TipoBarra = 2 AND TradingDay AND (NOT LONGONMARKET) THEN
StopEntrata = (close - (low - SpreadEntrata))
EntrataPendente = close - max (StopMinimo,StopEntrata) - MarginePendente
StopPendente = ((high + SpreadPendente) - EntrataPendente)
//STOP CORRETTO
SELLSHORT LotNumber CONTRACT AT EntrataPendente * pipsize STOP
SET STOP pLOSS StopPendente * pipsize
ELSIF Tipobarra = 4 AND TradingDay AND (NOT LONGONMARKET) THEN
EntrataPendente = high + SpreadEntrata - StopMAssimo
SELLSHORT LotNumber CONTRACT AT EntrataPendente * pipsize LIMIT
SET STOP pLOSS StopMassimo
ELSIF TipoBarra = 6 AND TradingDay AND (NOT SHORTONMARKET) THEN
EntrataPendente = low - SpreadEntrata + StopMAssimo
BUY LotNumber CONTRACT AT EntrataPendente * pipsize STOP
SET STOP pLOSS StopMassimo
ENDIF
Per esempio Roberto anche ieri sera sempre sul japan 30 minuti alla barra delle 21 mi ha pescato l’ordine, però alla chiusura della barra delle 22.30 che è rossa non me lo ha chiuso, il programma è lo stesso che ho allegato appena prima
Se sono programmi diversi è normale che restino aperte.
Ogni strategia è indipendente dalle altre e ognuna di esse è all’oscuro dell’altra.
Ok quello ok. Ricapitolando, il primo programma che tu hai testato è quello che non mi ha chiuso la posizione alle 17 sul demo in modalità trading automatico.
Il secondo che girava assieme invece mi ha chiuso correttamente la posizione, è questo che non capisco.
Tu mi confermi che i miei codici sono corretti come chiusura?
Io voglio che la posizione si chiuda nel modo sotto:
se sono long esco alla prima barra rossa
se sono short esco alla prima barra verde
Grazie mille.
Non mi inserisce l'ordine pendente
This topic contains 79 replies,
has 2 voices, and was last updated by
robertogozzi
5 years, 4 months ago.
| Forum: | ProOrder: Trading Automatico & Backtesting |
| Language: | Italian |
| Started: | 04/01/2020 |
| Status: | Active |
| Attachments: | 6 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.