Ho creato una strategia basata su dei livelli. Funziona correttamente ma non riesco a fargli fare due entrate nella stessa giornata. Mi spiego meglio. Supponiamo si attivi la condizione long. Questa puo’ andare a TP o prendere SL o raggiungere altro livello (quello per lo short). Io vorrei che se va long e va a TP e una volta flat se si attiva nella stessa giornata la condizione short entra anche su quella e viceversa. Ovviamente non si deve attivare nella stessa giornata una stessa posizione due volte (due long o due short no). Inoltre se la posizione è long e non raggiune ne TP ne SL ma si attiva la condizione short va in stop and reverse (questa dovrebbe già funzionare cosi). Spero di essermi spiegato.
//-------------------------------------------------------------------------
// Main code : Lunch
//-------------------------------------------------------------------------
// common rules
DEFPARAM CUMULATEORDERS = false
DEFPARAM PRELOADBARS = 10000
if IntradayBarIndex = 0 then
lx=0
sx=0
endif
once upper=0
once lower=0
//starttime = 080000
endtime = 120000
finaltime= 220000
If Time = endtime then
upper=highest[48](high)
lower=lowest[48](low)
dif=round(abs((lower-upper)*pipsize*pointvalue))
dif3=round(dif/3)
Rialzo= upper-dif3
Ribasso=lower+dif3
endif
// positionsize and stops
positionsize = 1
sl = 0.2 // % Stoploss
pt = 0.3// % Profit Target
ts = 0.35 // % MFETrailing
ONCE closetime = 240000 // greater then 23.59 means it continues position overnight
ONCE closetimeFriday=173000
tt1 = time >= endtime
tt2 = time <= finaltime
tradetime = tt1 and tt2
DayForbidden = 0 // 0=sunday
df = dayofweek <> dayforbidden
// setup number of trades intraday
if IntradayBarIndex = 0 then
longtradecounter = 0
Shorttradecounter = 0
Tradecounter=0
endif
// general criteria
GeneralCriteria = tradetime and df
// trade criteria
tcLong = countoflongshares < 1 and longtradecounter < 1 and tradecounter <1
tcShort = countofshortshares < 1 and shorttradecounter < 1 and tradecounter <1
if close > rialzo then
lx=1
else
lx=0
endif
if close < ribasso then
sx=1
else
sx=0
endif
// long entry
If GeneralCriteria then
if lx and tcLong then
buy positionsize contract at market
longtradecounter=longtradecounter + 1
tradecounter=tradecounter+1
endif
endif
// short entry
If GeneralCriteria then
if sx and tcShort then
sellshort positionsize contract at market
shorttradecounter=shorttradecounter + 1
tradecounter=tradecounter+1
endif
endif
MFETrailing=1
WTrailing=1
// MFETrailing
if MFETrailing then
trailingstop = (tradeprice/100)*ts
if not onmarket then
MAXPRICE = 0
MINPRICE = close
priceexit = 0
endif
if longonmarket then
MAXPRICE = MAX(MAXPRICE,close)
if MAXPRICE-tradeprice(1)>=trailingstop*pipsize then
priceexit = MAXPRICE-trailingstop*pipsize
endif
endif
if shortonmarket then
MINPRICE = MIN(MINPRICE,close)
if tradeprice(1)-MINPRICE>=trailingstop*pipsize then
priceexit = MINPRICE+trailingstop*pipsize
endif
endif
If onmarket and WTrailing=0 and priceexit>0 then
sell at market
exitshort at market
endif
endif
// exit larry williams
If WTrailing and MFETrailing then
count=1
i=0
j=i+1
tot=0
while count<4 do
tot=tot+1
if (low[j]>=low[i]) and (high[j]<=high[i]) then
//inside bar
j=j+1
else
count=count+1
i=i+1
J=i+1
endif
wend
basso=lowest[tot](low)
alto=highest[tot](high)
if close>alto[1] then
ref=basso
endif
if close<basso[1] then
ref=alto
endif
if onmarket and priceexit>0 then
if high<ref then
sell at market
endif
If low>ref then
exitshort at market
endif
endif
endif
// exit at closetime
If onmarket then
if time >= closetime then
sell at market
exitshort at market
endif
endif
// exit friday at set closetime
if onmarket then
if (CurrentDayOfWeek=5 and time>=closetimefriday) then
sell at market
exitshort at market
endif
endif
// build-in exit
SET TARGET %PROFIT pt
SET STOP %LOSS sl
Per evitare che possa entrare più volte nella stessa direzione ho aggiunto le variabili GoLong e GoShort che se avranno valore 1 consentiranno di entrare a mercato, altrimenti no. Ad inizio giornata si resettano al valore di default 1 per riprendere la normale operatività.
La linea 24, oppure la 29 della mia versione, devi variarla così (divisione al posto della moltiplicazione per trasformare il prezzo in pips, mentre si usa la moltiplicazione per trasformare i pips in prezzo, come nel trailing stop):
dif=round(abs((lower-upper)/pipsize*pointvalue))
Ad ogni modo adesso mi sembra funzioni (l’ho provato sul mini EurUsd a 30 minuti), provalo:
//-------------------------------------------------------------------------
// Main code : Lunch
//-------------------------------------------------------------------------
// common rules
DEFPARAM CUMULATEORDERS = false
DEFPARAM PRELOADBARS = 10000
ONCE GoLong = 1
ONCE GoShort = 1
if IntradayBarIndex = 0 then
lx=0
sx=0
GoLong = 1
GoShort = 1
endif
once upper=0
once lower=0
//starttime = 080000
endtime = 120000
finaltime= 220000
If Time = endtime then
upper=highest[48](high)
lower=lowest[48](low)
dif=round(abs((lower-upper)/pipsize*pointvalue))
dif3=round(dif/3)
Rialzo= upper-dif3
Ribasso=lower+dif3
endif
// positionsize and stops
positionsize = 1
sl = 0.2 // % Stoploss
pt = 0.3// % Profit Target
ts = 0.35 // % MFETrailing
ONCE closetime = 240000 // greater then 23.59 means it continues position overnight
ONCE closetimeFriday=173000
tt1 = time >= endtime
tt2 = time <= finaltime
tradetime = tt1 and tt2
DayForbidden = 0 // 0=sunday
df = dayofweek <> dayforbidden
// setup number of trades intraday
if IntradayBarIndex = 0 then
longtradecounter = 0
Shorttradecounter = 0
Tradecounter=0
endif
// general criteria
GeneralCriteria = tradetime and df
// trade criteria
tcLong = countoflongshares < 1 and longtradecounter < 1 and tradecounter <1
tcShort = countofshortshares < 1 and shorttradecounter < 1 and tradecounter <1
if close > rialzo then
lx=1
else
lx=0
endif
if close < ribasso then
sx=1
else
sx=0
endif
// long entry
If GeneralCriteria then
if lx and tcLong and GoLong then
buy positionsize contract at market
GoLong = 0
longtradecounter=longtradecounter + 1
tradecounter=tradecounter+1
endif
endif
// short entry
If GeneralCriteria then
if sx and tcShort and GoShort then
sellshort positionsize contract at market
GoShort = 0
shorttradecounter=shorttradecounter + 1
tradecounter=tradecounter+1
endif
endif
MFETrailing=1
WTrailing=1
// MFETrailing
if MFETrailing then
trailingstop = (tradeprice/100)*ts
if not onmarket then
MAXPRICE = 0
MINPRICE = close
priceexit = 0
endif
if longonmarket then
MAXPRICE = MAX(MAXPRICE,close)
if MAXPRICE-tradeprice(1)>=trailingstop*pipsize then
priceexit = MAXPRICE-trailingstop*pipsize
endif
endif
if shortonmarket then
MINPRICE = MIN(MINPRICE,close)
if tradeprice(1)-MINPRICE>=trailingstop*pipsize then
priceexit = MINPRICE+trailingstop*pipsize
endif
endif
If onmarket and WTrailing=0 and priceexit>0 then
sell at market
exitshort at market
endif
endif
// exit larry williams
If WTrailing and MFETrailing then
count=1
i=0
j=i+1
tot=0
while count<4 do
tot=tot+1
if (low[j]>=low[i]) and (high[j]<=high[i]) then
//inside bar
j=j+1
else
count=count+1
i=i+1
J=i+1
endif
wend
basso=lowest[tot](low)
alto=highest[tot](high)
if close>alto[1] then
ref=basso
endif
if close<basso[1] then
ref=alto
endif
if onmarket and priceexit>0 then
if high<ref then
sell at market
endif
If low>ref then
exitshort at market
endif
endif
endif
// exit at closetime
If onmarket then
if time >= closetime then
sell at market
exitshort at market
endif
endif
// exit friday at set closetime
if onmarket then
if (CurrentDayOfWeek=5 and time>=closetimefriday) then
sell at market
exitshort at market
endif
endif
// build-in exit
SET TARGET %PROFIT pt
SET STOP %LOSS sl
Grazie…ho inserito l’impostazione della variabile golong/goshort all’interno della routine IntradayBarIndex cosi si resetta ogni giorno..altrimenti nei giorni successivi non entrava piu’ (la strategia l’ho testata germany cash 1 euro time frame 5 minuti)
Poi ho fatto altra versione che nello stesso giorno puo’ entrare sia long che short ma solo una volta per ciascuna direzione.
//-------------------------------------------------------------------------
// Main code : Lunch
//-------------------------------------------------------------------------
// common rules
DEFPARAM CUMULATEORDERS = false
DEFPARAM PRELOADBARS = 10000
if IntradayBarIndex = 0 then
GoLong = 1
GoShort = 1
lx=0
sx=0
endif
once upper=0
once lower=0
//starttime = 080000
endtime = 120000
finaltime= 220000
If Time = endtime then
upper=highest[48](high)
lower=lowest[48](low)
dif=round(abs((lower-upper)/pipsize*pointvalue))
dif3=round(dif/3)
Rialzo= upper-dif3
Ribasso=lower+dif3
endif
// positionsize and stops
positionsize = 1
sl = 0.2 // % Stoploss
pt = 0.3// % Profit Target
ts = 0.35 // % MFETrailing
ONCE closetime = 240000 // greater then 23.59 means it continues position overnight
ONCE closetimeFriday=173000
tt1 = time >= endtime
tt2 = time <= finaltime
tradetime = tt1 and tt2
DayForbidden = 0 // 0=sunday
df = dayofweek <> dayforbidden
// setup number of trades intraday
if IntradayBarIndex = 0 then
longtradecounter = 0
Shorttradecounter = 0
Tradecounter=0
endif
// general criteria
GeneralCriteria = tradetime and df
// trade criteria
tcLong = countoflongshares < 1 and longtradecounter < 1 and tradecounter <1
tcShort = countofshortshares < 1 and shorttradecounter < 1 and tradecounter <1
if close > rialzo then
lx=1
else
lx=0
endif
if close < ribasso then
sx=1
else
sx=0
endif
// long entry
If GeneralCriteria then
if lx and tcLong and GoLong then
buy positionsize contract at market
GoLong = 0
longtradecounter=longtradecounter + 1
tradecounter=tradecounter+1
endif
endif
// short entry
If GeneralCriteria then
if sx and tcShort and GoShort then
sellshort positionsize contract at market
GoShort = 0
shorttradecounter=shorttradecounter + 1
tradecounter=tradecounter+1
endif
endif
MFETrailing=1
WTrailing=1
// MFETrailing
if MFETrailing then
trailingstop = (tradeprice/100)*ts
if not onmarket then
MAXPRICE = 0
MINPRICE = close
priceexit = 0
endif
if longonmarket then
MAXPRICE = MAX(MAXPRICE,close)
if MAXPRICE-tradeprice(1)>=trailingstop*pipsize then
priceexit = MAXPRICE-trailingstop*pipsize
endif
endif
if shortonmarket then
MINPRICE = MIN(MINPRICE,close)
if tradeprice(1)-MINPRICE>=trailingstop*pipsize then
priceexit = MINPRICE+trailingstop*pipsize
endif
endif
If onmarket and WTrailing=0 and priceexit>0 then
sell at market
exitshort at market
endif
endif
// exit larry williams
If WTrailing and MFETrailing then
count=1
i=0
j=i+1
tot=0
while count<4 do
tot=tot+1
if (low[j]>=low[i]) and (high[j]<=high[i]) then
//inside bar
j=j+1
else
count=count+1
i=i+1
J=i+1
endif
wend
basso=lowest[tot](low)
alto=highest[tot](high)
if close>alto[1] then
ref=basso
endif
if close<basso[1] then
ref=alto
endif
if onmarket and priceexit>0 then
if high<ref then
sell at market
endif
If low>ref then
exitshort at market
endif
endif
endif
// exit at closetime
If onmarket then
if time >= closetime then
sell at market
exitshort at market
endif
endif
// exit friday at set closetime
if onmarket then
if (CurrentDayOfWeek=5 and time>=closetimefriday) then
sell at market
exitshort at market
endif
endif
// build-in exit
SET TARGET %PROFIT pt
SET STOP %LOSS sl
//-------------------------------------------------------------------------
// Main code : Lunch
//-------------------------------------------------------------------------
// common rules
DEFPARAM CUMULATEORDERS = false
DEFPARAM PRELOADBARS = 10000
if IntradayBarIndex = 0 then
GoLong = 1
GoShort = 1
lx=0
sx=0
endif
once upper=0
once lower=0
//starttime = 080000
endtime = 120000
finaltime= 220000
If Time = endtime then
upper=highest[48](high)
lower=lowest[48](low)
dif=round(abs((lower-upper)/pipsize*pointvalue))
dif3=round(dif/3)
Rialzo= upper-dif3
Ribasso=lower+dif3
endif
// positionsize and stops
positionsize = 1
sl = 0.2 // % Stoploss
pt = 0.3// % Profit Target
ts = 0.35 // % MFETrailing
ONCE closetime = 240000 // greater then 23.59 means it continues position overnight
ONCE closetimeFriday=173000
tt1 = time >= endtime
tt2 = time <= finaltime
tradetime = tt1 and tt2
DayForbidden = 0 // 0=sunday
df = dayofweek <> dayforbidden
// setup number of trades intraday
// general criteria
GeneralCriteria = tradetime and df
// trade criteria
if close > rialzo then
lx=1
else
lx=0
endif
if close < ribasso then
sx=1
else
sx=0
endif
// long entry
If GeneralCriteria then
if lx and GoLong then
buy positionsize contract at market
GoLong = 0
endif
endif
// short entry
If GeneralCriteria then
if sx and GoShort then
sellshort positionsize contract at market
GoShort = 0
endif
endif
MFETrailing=1
WTrailing=1
// MFETrailing
if MFETrailing then
trailingstop = (tradeprice/100)*ts
if not onmarket then
MAXPRICE = 0
MINPRICE = close
priceexit = 0
endif
if longonmarket then
MAXPRICE = MAX(MAXPRICE,close)
if MAXPRICE-tradeprice(1)>=trailingstop*pipsize then
priceexit = MAXPRICE-trailingstop*pipsize
endif
endif
if shortonmarket then
MINPRICE = MIN(MINPRICE,close)
if tradeprice(1)-MINPRICE>=trailingstop*pipsize then
priceexit = MINPRICE+trailingstop*pipsize
endif
endif
If onmarket and WTrailing=0 and priceexit>0 then
sell at market
exitshort at market
endif
endif
// exit larry williams
If WTrailing and MFETrailing then
count=1
i=0
j=i+1
tot=0
while count<4 do
tot=tot+1
if (low[j]>=low[i]) and (high[j]<=high[i]) then
//inside bar
j=j+1
else
count=count+1
i=i+1
J=i+1
endif
wend
basso=lowest[tot](low)
alto=highest[tot](high)
if close>alto[1] then
ref=basso
endif
if close<basso[1] then
ref=alto
endif
if onmarket and priceexit>0 then
if high<ref then
sell at market
endif
If low>ref then
exitshort at market
endif
endif
endif
// exit at closetime
If onmarket then
if time >= closetime then
sell at market
exitshort at market
endif
endif
// exit friday at set closetime
if onmarket then
if (CurrentDayOfWeek=5 and time>=closetimefriday) then
sell at market
exitshort at market
endif
endif
// build-in exit
SET TARGET %PROFIT pt
SET STOP %LOSS sl