Il codice di Roberto fa SEMPRE 1 operazione.
Il codice di Phoentzs fa SEMPRE 2 operazioni.
//30.03.2022 200000k
//US500 M5 Spread 0.6
//German Time
defparam preloadbars = 10000
defparam CUMULATEORDERS = false
//MM
ONCE LotSize = 5 //starting number of lots/contracts
ONCE MinLots = 5 //minimum lot size required by the broker
ONCE MaxLots = 10 //999 max lots allowed (to be also set in AutoTradung)
ONCE Rise = 5 //numebe of lots to increment
ONCE Fall = 5 //number of lots to decrement
IF StrategyProfit > StrategyProfit[1] THEN
LotSize = LotSize + Rise
ELSIF StrategyProfit < StrategyProfit[1] THEN
LotSize = LotSize - Fall
ENDIF
LotSize = min(MaxLots,max(MinLots,LotSize)) //check both Maximum and Minimum
//LotSize = 5
timeframe(15minute, updateonclose)
RangeMAD5 = average[480,0](close) //Wochentrend Daily MA5
RangelongD5 = close > RangeMAD5
RangeshortD5 = close < RangeMAD5
RangeMAD20 = average[1920,0](close) //"Monatstrend" Daily MA20
RangelongD20 = RangeMAD5 > RangeMAD20
RangeshortD20 = RangeMAD5 < RangeMAD20
MA1 = average[x1,0](close)
MA2 = average[x2,0](close)
//MA3 = average[x3,0](close)
MA4 = average[x5,1](close)
MA5 = average[x6,1](close)
longA = MA1 > MA2 //and MA2 > MA3 //and MA2 > MA2[1]
shortA = MA4 < MA5 //and MA5 < MA5[1]
MAL1 = average[5,0](close)
MAL2 = average[15,0](close) //15
longB = MAL1 crosses over MAL2
shortB = MAL1 crosses under MAL2
timeframe(default)//M5
long = RangelongD5 and longB and longA //and RangelongD20
short = RangeshortD5 and shortB and shortA //and RangeshortD20
Exit1 = RangeshortD5
Exit2 = RangelongD5
// trading window
ONCE BuyTime = 110000
ONCE SellTime = 213000
ONCE BuyTime2 = 150000
ONCE SellTime2 = 213000
// position management
IF Time >= buyTime AND Time <= SellTime THEN
If long then //not onmarket and
BUY LotSize CONTRACT AT market
SET STOP %LOSS hl
SET TARGET %PROFIT gl
EndIf
endif
IF Time >= buyTime2 AND Time <= SellTime2 THEN
If short then
sellshort LotSize CONTRACT AT market
SET STOP %LOSS hs
SET TARGET %PROFIT gs
EndIf
endif
If longonmarket and Exit1 then
sell at market
endif
If shortonmarket and Exit2 then
exitshort at market
endif
if time = 223000 then //223000
//sell at market
exitshort at market
endif
if time = 225500 and dayofweek=5 then //225500
sell at market
exitshort at market
endif
////////////////////////////////////////
// %trailing stop function incl. cumulative positions
once trailingstoptype = 1
if trailingstoptype then
//====================
trailingpercentlong = startl // %
trailingpercentshort = start // %
once acceleratorlong = stepl // typically tst*0.1
once acceleratorshort= step // typically tss*0.1
ts2sensitivity = 2 // [1] close [2] high/low [3] low/high [4] typicalprice
//====================
once steppercentlong = (trailingpercentlong/10)*acceleratorlong
once steppercentshort = (trailingpercentshort/10)*acceleratorshort
if onmarket then
trailingstartlong = positionprice*(trailingpercentlong/100)
trailingstartshort = positionprice*(trailingpercentshort/100)
trailingsteplong = positionprice*(steppercentlong/100)
trailingstepshort = positionprice*(steppercentshort/100)
endif
if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
newsl = 0
mypositionprice = 0
endif
positioncount = abs(countofposition)
if newsl > 0 then
if positioncount > positioncount[1] then
if longonmarket then
newsl = max(newsl,positionprice * newsl / mypositionprice)
else
newsl = min(newsl,positionprice * newsl / mypositionprice)
endif
endif
endif
if ts2sensitivity=1 then
ts2sensitivitylong=close
ts2sensitivityshort=close
elsif ts2sensitivity=2 then
ts2sensitivitylong=high
ts2sensitivityshort=low
elsif ts2sensitivity=3 then
ts2sensitivitylong=low
ts2sensitivityshort=high
elsif ts2sensitivity=4 then
ts2sensitivitylong=(typicalprice)
ts2sensitivityshort=(typicalprice)
endif
if longonmarket then
if newsl=0 and ts2sensitivitylong-positionprice>=trailingstartlong then
newsl = positionprice+trailingsteplong + 0.2
endif
if newsl>0 and ts2sensitivitylong-newsl>=trailingsteplong then
newsl = newsl+trailingsteplong
endif
endif
if shortonmarket then
if newsl=0 and positionprice-ts2sensitivityshort>=trailingstartshort then
newsl = positionprice-trailingstepshort
endif
if newsl>0 and newsl-ts2sensitivityshort>=trailingstepshort then
newsl = newsl-trailingstepshort
endif
endif
if barindex-tradeindex>1 then
if longonmarket then
if newsl>0 then
sell at newsl stop
endif
if newsl>0 then
if low crosses under newsl then
sell at market
endif
endif
endif
if shortonmarket then
if newsl>0 then
exitshort at newsl stop
endif
if newsl>0 then
if high crosses over newsl then
exitshort at market
endif
endif
endif
endif
mypositionprice = positionprice
endif
if (shortonmarket and newsl > 0) or (longonmarket and newsl>0) then
if positioncount > positioncount[1] then
if longonmarket then
newsl = max(newsl,positionprice * newsl / mypositionprice)
endif
if shortonmarket then
newsl = min(newsl,positionprice * newsl / mypositionprice)
endif
endif
endif
//////////////////////////////////////////////////////////////
Caro @MauroPro
, visto che ho postato questa strategia prima… Ti ricordi? MA Croce? Qui la strategia con MM Qui il codice funziona.
Ciao Phoetzs, grazie per l’esempio. Stavo facendo delle prove con il tuo codice. Ho visto che funziona in questo modo:
A) Raddoppia la posizione dopo un guadagno (come fa il tuo codice “Multi SP500”)
IF StrategyProfit > StrategyProfit[1] THEN
LotSize = LotSize + Rise
ELSIF StrategyProfit < StrategyProfit[1] THEN
LotSize = LotSize – Fall
ENDIF
B) Raddoppia la posizone dopo una perdita (MARTINGALA):
IF StrategyProfit > StrategyProfit[1] THEN
LotSize = LotSize – Rise
ELSIF StrategyProfit < StrategyProfit[1] THEN
LotSize = LotSize + Fall
ENDIF
Vorrei però aggiungere nel codice B (Martingala), come stava provando Roberto, il flag: RADDOPPIO, per fare in modo che il TS usi due contratti solo dopo la PRIMA perdita, mentre il codice che hai postato mantiene due contratti anche se ci sono più operazioni in perdita consecutive (vd foto).
Siccome però funziona, Roberto potrebbe aggiungere il suo flag RADDOPPIO in questo codice.
Ovviamente puoi anche ottimizzare “Rise” e “Fall” (1-5 entrambi nell’ottimizzatore).
Per la codifica di livello superiore, come Flag, Roberto deve intervenire. Non ne ho idea.
Roberto puoi inserire in questo codice il flag per raddoppiare la posizione in perdita soltanto la prima volta? (vedi immagine allegata sopra)
defParam cumulateOrders = false //(test DAX 15m)
ONCE LotSize = 1 //starting number of lots/contracts
ONCE MinLots = 1 //minimum lot size required by the broker
ONCE MaxLots = 2 //999 max lots allowed (to be also set in AutoTradung)
ONCE Rise = 1 //numebe of lots to increment
ONCE Fall = 1 //number of lots to decrement
IF StrategyProfit > StrategyProfit[1] THEN
LotSize = LotSize - Rise
ELSIF StrategyProfit < StrategyProfit[1] THEN
LotSize = LotSize + Fall
ENDIF
LotSize = min(MaxLots,max(MinLots,LotSize)) //check both Maximum and Minimum
//--------------
avg50 = average[50,0](close)
if close crosses over avg50 then
buy lotSize contract at market
endif
if longOnMarket and close < (avg50 - 50*pointSize) then
sell lotSize contract at market
endif
//-------------------------------------
set target pProfit 500
set stop pLoss 200
//--------------------------------------------
Ne ho uno in più. Mi è venuto in mente solo dopo aver letto la tua frase… Basta girare il “>”. Poi fa quasi quello che vuoi. Ho appena provato una strategia diversa… stessa performance ma riduce il drawdown.
IF StrategyProfit < StrategyProfit[1] THEN //erhöht bei Verlust
LotSize = LotSize + Rise
ELSIF StrategyProfit > StrategyProfit[1] THEN
LotSize = LotSize – Fall
ENDIF
Questo che hai postato è il codice martingala che raddoppia la posizione in caso di perdita (come il mio caso B sopra).
Nel tuo “TS Multi” NON usavi il martingala, ma raddoppiavi la posizione in caso di guadagno (che è un altra tecnica).
Giusto. Rilancio a MaxPosition se vinco. Dopo aver parlato con te, ho scoperto che se rilanci in caso di perdita e torni a MinPosition in caso di vittoria, il drawdown del sistema diminuisce. Che a quanto pare è meglio. Questo sarebbe l’ultimo codice che ho postato. Ma penso che funzioni allo stesso modo del tuo codice B. Scritto in modo diverso.
Il vero Martingala raddoppia la posizione OGNI volta che si perde ( e porta ad azzerare i conti in quanto il capitale a disposizione non è infinito!)
Il codice che ho postato per Roberto, che utilizza la tua formula (strategyProfit < StrategyProfit[1] e lotSize +1) è un Martingala che raddoppia una volta la posizione e la mantiene raddoppiata fino che non capita un guadagno…)
Il codice che ho postato sopra e che vorrei che Roberto modificasse con il suo flag dovrebbe raddoppiare solo per la prima perdita la posizione e poi, anche se capitano perdite consecutive, utilizzare sempre un contratto.
Poi c’è il tuo codice Multi che invece raddoppia una posizione vincente e la mantiene raddoppiate per tutte le vincite consecutive fino a che non capita una perdita.
Sono onestamente curioso di sapere come si comporta il codice con il “flag”.
Perdonami se mi sono intromesso qui.
Aspettiamo Roberto che sui flags è un mago!
Ho cambiato una riga ONCE iniziale, più la verifica del raddoppio.
Adesso sembra segua perfettamente la regola del raddoppio solo quando c’è un risultato POSITIVO seguito da uno NEGATIVO (+-), negli altri casi (++ oppure –, oppure -+) non raddoppia.
defParam cumulateOrders = false //(test DAX 15m)
once orderSize = 1
once raddoppio = 1
if StrategyProfit < StrategyProfit[1] then
IF Raddoppio = 1 THEN
orderSize = orderSize * 2
Raddoppio = 0
ELSE
orderSize = 1
ENDIF
elsif StrategyProfit > StrategyProfit[1] THEN
orderSize = 1
Raddoppio = 1
endif
//--------------
avg50 = average[50,0](close)
if close crosses over avg50 then
buy orderSize contract at market
endif
if longOnMarket and close < (avg50 - 50*pointSize) then
sell orderSize contract at market
endif
//-------------------------------------
set target pProfit 500
set stop pLoss 200
//--------------------------------------------
graph ordersize coloured(255,0,0,255)
graph Raddoppio coloured(0,0,255,255)
graph StrategyProfit < StrategyProfit[1] coloured(0,0,0,150)
Quello scritto da phoentzs va bene, il suo scopo è incrementare di uno o diminuire di uno in caso di perdita o di guadagno, rispettando i limiti di 1 e 2 (senza la regola +-)
Per modificarlo devo farlo uguale al mio, non ha molto senso.
Ciao Roberto, ho provato ed ora funziona.
Per raddoppiare la posizione (la prima volta) non di un trade perdente, ma vincente (anti martingala) ho visto (e funziona) che basta invertire < e > nelle rughe 5 e 12.
Volevo apportare un ultima modifica nel seguente TS anti martingala di prova: attendere prima di raddoppiare (la prima volta soltanto) la posizione vincente non un trade vincente, ma due trade vincenti (quindi si raddoppierebbe dopo -++).
Pensavo che bastava scrivere nella riga 5 : if strategyProfit > strategyProfit[1] and strategyProft[1] > strategyProfit[2], ma non funziona (tutte le op. tornano ad essere di un contratto), sai come si corregge?