Salve, ho un problema con questo codice, nel caso specifico, quando la posizione viene chiusa sulla stessa candela dove viene aperto l’ordine, nella candela successiva l’ordine viene ripetuto e non trovo il modo per bloccarlo. L’obiettivo finale di questo codice è che al verificarsi di condizioni l’ordine limit deve rimanere in essere per x candele e non replicarsi in caso di esecuzione e chiusura su stessa candela
DEFPARAM CumulateOrders = False
once x = 1
once noEntryBeforeTime = 063000
once noEntryAfterTime = 173500
bmax1 = (close[0] > open[1]) and (open[0] < close[1]) and (open[1] > close[1])
bmax2 = (high[0] > high[1]) and (low[0] < low[1]) and (close[0] > high[1])
IF NOT ONMARKET THEN
myorder = 0
parstop = 0
profitto = 0
if (gmaxbuy) then
gindice = gindice + 1
else
gindice = 0
endif
if gindice > 2 then
gmaxbuy = 0
toccato = 0
endif
if (bmax1 and bmax2) then
gmaxbuy = 1
endif
// time
// **************************************************************************************
timeEnterBefore = time > noEntryBeforeTime
timeEnterAfter = time < noEntryAfterTime
if timeEnterBefore and timeEnterAfter then
a = 1
else
a = 0
endif
//ORDINE double top bottom
// **************************************************************************************
if (a and gmaxbuy and toccato = 0) THEN //apre e rinnova ordine
doveentrare = high[gindice] - ((high[gindice] - low[gindice]) / 2) // 2 = 50% barra 1.5 = 66% 1.2 = 83%
myorder = doveentrare
if gindice < 3 then //aspetto max 2 candele
BUY x CONTRACT AT myorder limit
if gindice = 0 then //solo 1 volta stop e profit
profitto = (high[gindice] - low[gindice]) * 1 //10 //- myorder //* 2 //1.5 // rischio 1:1.5
parstop = (high[gindice] - low[gindice]) * 1.10 // 10 // * 1.2)
SET STOP pLOSS parstop
SET TARGET pPROFIT profitto
endif
endif
endif
ELSIF ONMARKET THEN
toccato = 1
//ferma tutto il venerdì alle 22:00 ***********************************************
if ((OpenDayOfWeek = 5) AND (time >= 220000)) THEN
sell at market
exitshort at market
endif
endif
// ************************ attendere sviluppo piattaforma *********************************
//5 minute TF (get the trend of the 5 minute chart)
//timeframe (1 minutes,updateonclose)
//manage long positions TRAILING STEP
//"default" timeframe (the timeframe you will launch the strategy on)
//timeframe(default)
// *****************************************************************************************
// DEBUG
//***********************************************************************************
graph toccato
graph gmaxbuy
graph gindice
graph parstop
graph myorder
graph profitto
Se un ordine entra ed esce in un’unica candela ProOrder NON se ne accorge in modo diretto, quindi non esiste un’istruzione per poterlo verificare.
Occorre aggirare il problema salvando il profitto della strategia (STRATEGYPROFIT) ad inizio di ogni giorno, quando IntraDayBarIndex=0 per intenderci, oppure quando c’è stato un trade per più di una candela e rilevabile da OnMarket, per poi verificare questo dato con STRATEGYPROFIT ad ogni candela, se è variato significa che c’è c’è stato un trade aperto/chiuso nella stessa candela e OnMarket non ti segnala niente, ovviamente.
ONCE MioProfitto = 0
IF IntraDayBarIndex = 0 OR (OnMarket[1] AND Not OnMarket) THEN //Aggiornare il profitto a inizio giornata, oppure dopo la chiusura di un Trade multicandela
MioProfitto = STRATEGYPROFIT
ENDIF
.
.
IF MioProfitto <> STRATEGYPROFIT THEN
. //c'è stato un trade iniziato e chiuso nella stessa candela
ENDIF