0
votes

In my script I use also a multiple price-market entry; when, in backtest, I use only one strategy.entry the relative TP/SL of strategy.exit is always correctly applied ; when I use double entry with relative double exit it happens that: the first strategy.entry and exit is correct while in the second strategy.entry it happens that the relative TP and SL of its strategy.exit are wrong because take the values of the previous strategy.exit. All the entry and exit have different id ... I can't find my mistake. Can anyone help me? Thank's a lot. I can email you details. My email is [email protected]`

//condizioni ingresso ed uscita mercato secondo MACD UP/DOWN  SU TF SETTIMANALE e  MACD UP/DOWN SU TF GIORNALIERO    
ConditionEntryL = MACDweekly>deltaweekly and crossover(MACDdaily, deltadaily) and MACDdaily < 0 and deltadaily < 0
ConditionEntryS = MACDweekly<deltaweekly and crossunder(MACDdaily, deltadaily) and MACDdaily > 0 and deltadaily > 0

//ingressi ed uscite Mercato - operazione LONG SU TF GG
strategy.entry ("MACDlong1",strategy.long, when = ConditionEntryL and TradeDateIsAllowed())
strategy.exit ("MACDlong1ex",profit = LongTP1, loss = LongSL1, when = ConditionEntryL and TradeDateIsAllowed())
strategy.entry ("MACDlong2",strategy.long, when = ConditionEntryL and TradeDateIsAllowed())
strategy.exit ("MACDlong2",profit = LongTP2, loss = LongSL2, when = ConditionEntryL and TradeDateIsAllowed())

//ingressi ed uscite Mercato - operazione SHORTSU TF GG
strategy.entry ("MACDshort1",strategy.short, when = ConditionEntryS and TradeDateIsAllowed())
strategy.exit ("MACDshort1ex", profit = ShortTP1, loss = ShortSL1, when = ConditionEntryS and TradeDateIsAllowed())
strategy.entry ("MACDshort2",strategy.short, when = ConditionEntryS and TradeDateIsAllowed())
strategy.exit ("MACDshort2ex", profit = ShortTP2, loss = ShortSL2, when = ConditionEntryS and TradeDateIsAllowed()) 

//condizioni ingresso ed uscita mercato secondo MACD UP/DOWN  TF SETTIMANALE e  MACD TF GIORNALIERO CHE INCROCIA LO ZERO
ConditionEntryL1 = MACDweekly>deltaweekly and MACDdaily>deltadaily and crossover(MACDdaily,0) and TradeDateIsAllowed()
ConditionEntryS1 = MACDweekly<deltaweekly and MACDdaily<deltadaily and crossunder(MACDdaily,0) and TradeDateIsAllowed()

//ingressi ed uscite Mercato - operazione LONG SU TF GG
strategy.entry ("MACDlongZ1",strategy.long, when = ConditionEntryL1 and TradeDateIsAllowed())
strategy.exit ("MACDlongZ1ex",profit = LongZTP1, loss = LongZSL1, when = ConditionEntryL1 and TradeDateIsAllowed())
strategy.entry ("MACDlongZ2",strategy.long, when = ConditionEntryL1 and TradeDateIsAllowed())
strategy.exit ("MACDlongZ2ex",profit = LongZTP2, loss = LongZSL2, when = ConditionEntryL1 and TradeDateIsAllowed())

//ingressi ed uscite Mercato - operazione SHORT SU TF GG
strategy.entry ("MACDshortZ1",strategy.short, when = ConditionEntryS1 and TradeDateIsAllowed())
strategy.exit ("MACDshortZ1ex", profit = ShortZTP1, loss = ShortZSL1, when = ConditionEntryS1 and TradeDateIsAllowed())
strategy.entry ("MACDshortZ2",strategy.short, when = ConditionEntryS1 and TradeDateIsAllowed())
strategy.exit ("MACDshortZ2ex", profit = ShortZTP2, loss = ShortZSL2, when = ConditionEntryS1 and TradeDateIsAllowed())
2

2 Answers

0
votes
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © greeen
//@version=4
//study("My first Script")
//plot(close)
//@version=4

strategy("MIX Strategy", "MIX", overlay=true, pyramiding=2, default_qty_type=strategy.cash, default_qty_value=100, initial_capital=200, currency=currency.USD, slippage=0, commission_type=strategy.commission.cash_per_order, commission_value=0, close_entries_rule="ANY") 

//parametri per calcolo MACD
fastLength = input(12)
slowlength = input(26)
MACDLength = input(9)

//calcolo MACD settimanale e giornaliero
MACD = ema(close, fastLength) - ema(close, slowlength)
delta = ema(MACD, MACDLength)
MACDweekly = security(syminfo.tickerid, "1W", MACD)
deltaweekly = security(syminfo.tickerid, "1W", delta)
MACDdaily = security(syminfo.tickerid, "D", MACD)
deltadaily = security(syminfo.tickerid, "D", delta)

//disegno su grafico MACDe SIGNAL
plot (MACDweekly, color = color.green)
plot (deltaweekly, color = color.orange)
plot (MACDdaily, color = color.blue)
plot (deltadaily, color = color.red)
plot (0, color = color.black)

//MACD CROSS UP SIGNAL  - TP e SL per le operazioni LONG - 2 INGRESSI
LongTP1 =5*close
LongSL1 = 4*close
LongTP2 = 15*close
LongSL2 = 12*close

//MACD CROSS DOWN SIGNAL – TP e SL per le operazioni SHORT - 2 INGRESSI
ShortTP1 = 5*close
ShortSL1 = 4*close
ShortTP2 = 15*close
ShortSL2 = 12*close

//MACD CROSS UP ZERO  - TP e SL per le operazioni LONG - 2 INGRESSI
LongZTP1 = 5*close
LongZSL1 = 4*close
LongZTP2 = 10*close
LongZSL2 = 8*close

//MACD CROSS DOWN ZERO  – TP e SL per le operazioni SHORT - 2 INGRESSI
ShortZTP1 =5*close
ShortZSL1 = 4*close
ShortZTP2 = 10*close
ShortZSL2 = 12*close

//INCROCI MEDIE EXPONENZIALI – TP e SL per le operazioni LONG/SHORT – 1 INGRESSO
EmaTP= 10*close
EmaSL = 8*close

DateFilter = input(false, "═════════════ Date Range Filtering")
FromYear = input(2019, "From Year", minval = 1900)
FromMonth = input(1, "From Month", minval = 1, maxval = 12)
FromDay = input(1, "From Day", minval = 1, maxval = 31)
ToYear = input(2020, "To Year", minval = 1900)
ToMonth = input(1, "To Month", minval = 1, maxval = 12)
ToDay = input(1, "To Day", minval = 1, maxval = 31)
FromDate = timestamp(FromYear, FromMonth, FromDay, 00, 00)
ToDate = timestamp(ToYear, ToMonth, ToDay, 23, 59)
TradeDateIsAllowed() => DateFilter ? (time >= FromDate and time <= ToDate) : true

//condizioni ingresso ed uscita mercato secondo MACD UP/DOWN  SU TF SETTIMANALE e  MACD UP/DOW N SU TF GIORNALIERO    
ConditionEntryL = MACDweekly>deltaweekly and crossover(MACDdaily, deltadaily) and MACDdaily < 0 and deltadaily < 0
ConditionEntryS = MACDweekly<deltaweekly and crossunder(MACDdaily, deltadaily) and MACDdaily > 0 and deltadaily > 0

//ingressi ed uscite Mercato - operazione LONG SU TF GG
strategy.entry ("MACDlong1",strategy.long, when = ConditionEntryL and TradeDateIsAllowed())
strategy.exit ("MACDlong1ex",profit = LongTP1, loss = LongSL1, when = ConditionEntryL and TradeDateIsAllowed())
strategy.entry ("MACDlong2",strategy.long, when = ConditionEntryL and TradeDateIsAllowed())
strategy.exit ("MACDlong2ex",profit = LongTP2, loss = LongSL2, when = ConditionEntryL and TradeDateIsAllowed())

//ingressi ed uscite Mercato - operazione SHORTSU TF GG
strategy.entry ("MACDshort1",strategy.short, when = ConditionEntryS and TradeDateIsAllowed())
strategy.exit ("MACDshort1ex", profit = ShortTP1, loss = ShortSL1, when = ConditionEntryS and TradeDateIsAllowed())
strategy.entry ("MACDshort2",strategy.short, when = ConditionEntryS and TradeDateIsAllowed())
strategy.exit ("MACDshort2ex", profit = ShortTP2, loss = ShortSL2, when = ConditionEntryS and TradeDateIsAllowed()) 

//condizioni ingresso ed uscita mercato secondo MACD UP/DOWN  TF SETTIMANALE e  MACD TF GIORNALIERO CHE INCROCIA LO ZERO
ConditionEntryL1 = MACDweekly>deltaweekly and MACDdaily>deltadaily and crossover(MACDdaily,0) and TradeDateIsAllowed()
ConditionEntryS1 = MACDweekly<deltaweekly and MACDdaily<deltadaily and crossunder(MACDdaily,0) and TradeDateIsAllowed()

//ingressi ed uscite Mercato - operazione LONG SU TF GG
strategy.entry ("MACDlongZ1",strategy.long, when = ConditionEntryL1 and TradeDateIsAllowed())
strategy.exit ("MACDlongZ1ex",profit = LongZTP1, loss = LongZSL1, when = ConditionEntryL1 and TradeDateIsAllowed())
strategy.entry ("MACDlongZ2",strategy.long, when = ConditionEntryL1 and TradeDateIsAllowed())
strategy.exit ("MACDlongZ2ex",profit = LongZTP2, loss = LongZSL2, when = ConditionEntryL1 and TradeDateIsAllowed())

//ingressi ed uscite Mercato - operazione SHORT SU TF GG
strategy.entry ("MACDshortZ1",strategy.short, when = ConditionEntryS1 and TradeDateIsAllowed())
strategy.exit ("MACDshortZ1ex", profit = ShortZTP1, loss = ShortZSL1, when = ConditionEntryS1 and TradeDateIsAllowed())
strategy.entry ("MACDshortZ2",strategy.short, when = ConditionEntryS1 and TradeDateIsAllowed())
strategy.exit ("MACDshortZ2ex", profit = ShortZTP2, loss = ShortZSL2, when = ConditionEntryS1 and TradeDateIsAllowed())

//parametri e calcolo EMA
EMAfastLength = input(8)
EMAslowLength = input(24)
EMAlongLenght = input (80)
price = close
EMAfast = ema(price, EMAfastLength)
EMAslow = ema(price, EMAslowLength)
EMAlong = ema(price, EMAlongLenght)

//condizioni ingresso ed uscita mercato secondo INCROCI UP MEDIE ESP 
if (crossover(EMAfast,EMAslow)) and EMAslow>EMAlong and crossover(MACDdaily,0) and TradeDateIsAllowed()
    strategy.entry("3EMAcrossLong", strategy.long, comment="3EMAcrossLong")
    strategy.exit ("3EMAcrossLongEx",profit = EmaTP,loss = EmaSL)

//condizioni ingresso ed uscita mercato secondo INCROCI DOWN MEDIE ESP 
if (crossunder(EMAfast, EMAslow)) and EMAslow<EMAlong and crossunder(MACDdaily,0) and TradeDateIsAllowed()
    strategy.entry("3EMAcrossShort", strategy.short, comment="3EMAcrossShort")
    strategy.exit ("3EMAcrossShortEx",profit = EmaTP,loss = EmaSL)

//disegno EMA su grafico
plot (EMAfast, color = color.green)
plot (EMAslow, color = color.orange)
plot (EMAlong, color = color.red)
0
votes

1) You can turn it on by this way:

//@version=4
strategy("***", ..., close_entries_rule = 'ANY', ...)
...

2) If you want to exit from particular exit you need to specify it through from_entry param:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © greeen
//@version=4
//study("My first Script")
//plot(close)
//@version=4

strategy("MIX Strategy", "MIX", overlay=true, pyramiding=2, default_qty_type=strategy.cash, default_qty_value=100, initial_capital=200, currency=currency.USD, slippage=0, commission_type=strategy.commission.cash_per_order, commission_value=0, close_entries_rule="ANY") 

//parametri per calcolo MACD
fastLength = input(12)
slowlength = input(26)
MACDLength = input(9)

//calcolo MACD settimanale e giornaliero
MACD = ema(close, fastLength) - ema(close, slowlength)
delta = ema(MACD, MACDLength)
MACDweekly = security(syminfo.tickerid, "1W", MACD)
deltaweekly = security(syminfo.tickerid, "1W", delta)
MACDdaily = security(syminfo.tickerid, "D", MACD)
deltadaily = security(syminfo.tickerid, "D", delta)

//disegno su grafico MACDe SIGNAL
plot (MACDweekly, color = color.green)
plot (deltaweekly, color = color.orange)
plot (MACDdaily, color = color.blue)
plot (deltadaily, color = color.red)
plot (0, color = color.black)

//MACD CROSS UP SIGNAL  - TP e SL per le operazioni LONG - 2 INGRESSI
LongTP1 =5*close
LongSL1 = 4*close
LongTP2 = 15*close
LongSL2 = 12*close

//MACD CROSS DOWN SIGNAL – TP e SL per le operazioni SHORT - 2 INGRESSI
ShortTP1 = 5*close
ShortSL1 = 4*close
ShortTP2 = 15*close
ShortSL2 = 12*close

//MACD CROSS UP ZERO  - TP e SL per le operazioni LONG - 2 INGRESSI
LongZTP1 = 5*close
LongZSL1 = 4*close
LongZTP2 = 10*close
LongZSL2 = 8*close

//MACD CROSS DOWN ZERO  – TP e SL per le operazioni SHORT - 2 INGRESSI
ShortZTP1 =5*close
ShortZSL1 = 4*close
ShortZTP2 = 10*close
ShortZSL2 = 12*close

//INCROCI MEDIE EXPONENZIALI – TP e SL per le operazioni LONG/SHORT – 1 INGRESSO
EmaTP= 10*close
EmaSL = 8*close

DateFilter = input(false, "═════════════ Date Range Filtering")
FromYear = input(2019, "From Year", minval = 1900)
FromMonth = input(1, "From Month", minval = 1, maxval = 12)
FromDay = input(1, "From Day", minval = 1, maxval = 31)
ToYear = input(2020, "To Year", minval = 1900)
ToMonth = input(1, "To Month", minval = 1, maxval = 12)
ToDay = input(1, "To Day", minval = 1, maxval = 31)
FromDate = timestamp(FromYear, FromMonth, FromDay, 00, 00)
ToDate = timestamp(ToYear, ToMonth, ToDay, 23, 59)
TradeDateIsAllowed() => DateFilter ? (time >= FromDate and time <= ToDate) : true

//condizioni ingresso ed uscita mercato secondo MACD UP/DOWN  SU TF SETTIMANALE e  MACD UP/DOW N SU TF GIORNALIERO    
ConditionEntryL = MACDweekly>deltaweekly and crossover(MACDdaily, deltadaily) and MACDdaily < 0 and deltadaily < 0
ConditionEntryS = MACDweekly<deltaweekly and crossunder(MACDdaily, deltadaily) and MACDdaily > 0 and deltadaily > 0

//ingressi ed uscite Mercato - operazione LONG SU TF GG
strategy.entry ("MACDlong1",strategy.long, when = ConditionEntryL and TradeDateIsAllowed())
strategy.exit ("MACDlong1ex",from_entry="MACDlong1",profit = LongTP1, loss = LongSL1, when = ConditionEntryL and TradeDateIsAllowed())
strategy.entry ("MACDlong2",strategy.long, when = ConditionEntryL and TradeDateIsAllowed())
strategy.exit ("MACDlong2ex",from_entry="MACDlong2",profit = LongTP2, loss = LongSL2, when = ConditionEntryL and TradeDateIsAllowed())

//ingressi ed uscite Mercato - operazione SHORTSU TF GG
strategy.entry ("MACDshort1",strategy.short, when = ConditionEntryS and TradeDateIsAllowed())
strategy.exit ("MACDshort1ex",from_entry="MACDshort1", profit = ShortTP1, loss = ShortSL1, when = ConditionEntryS and TradeDateIsAllowed())
strategy.entry ("MACDshort2",strategy.short, when = ConditionEntryS and TradeDateIsAllowed())
strategy.exit ("MACDshort2ex",from_entry="MACDshort2", profit = ShortTP2, loss = ShortSL2, when = ConditionEntryS and TradeDateIsAllowed()) 

//condizioni ingresso ed uscita mercato secondo MACD UP/DOWN  TF SETTIMANALE e  MACD TF GIORNALIERO CHE INCROCIA LO ZERO
ConditionEntryL1 = MACDweekly>deltaweekly and MACDdaily>deltadaily and crossover(MACDdaily,0) and TradeDateIsAllowed()
ConditionEntryS1 = MACDweekly<deltaweekly and MACDdaily<deltadaily and crossunder(MACDdaily,0) and TradeDateIsAllowed()

//ingressi ed uscite Mercato - operazione LONG SU TF GG
strategy.entry ("MACDlongZ1",strategy.long, when = ConditionEntryL1 and TradeDateIsAllowed())
strategy.exit ("MACDlongZ1ex",from_entry="MACDlongZ1",profit = LongZTP1, loss = LongZSL1, when = ConditionEntryL1 and TradeDateIsAllowed())
strategy.entry ("MACDlongZ2",strategy.long, when = ConditionEntryL1 and TradeDateIsAllowed())
strategy.exit ("MACDlongZ2ex",from_entry="MACDlongZ2",profit = LongZTP2, loss = LongZSL2, when = ConditionEntryL1 and TradeDateIsAllowed())

//ingressi ed uscite Mercato - operazione SHORT SU TF GG
strategy.entry ("MACDshortZ1",strategy.short, when = ConditionEntryS1 and TradeDateIsAllowed())
strategy.exit ("MACDshortZ1ex",from_entry="MACDshortZ1", profit = ShortZTP1, loss = ShortZSL1, when = ConditionEntryS1 and TradeDateIsAllowed())
strategy.entry ("MACDshortZ2",strategy.short, when = ConditionEntryS1 and TradeDateIsAllowed())
strategy.exit ("MACDshortZ2ex",from_entry="MACDshortZ2", profit = ShortZTP2, loss = ShortZSL2, when = ConditionEntryS1 and TradeDateIsAllowed())

//parametri e calcolo EMA
EMAfastLength = input(8)
EMAslowLength = input(24)
EMAlongLenght = input (80)
price = close
EMAfast = ema(price, EMAfastLength)
EMAslow = ema(price, EMAslowLength)
EMAlong = ema(price, EMAlongLenght)

//condizioni ingresso ed uscita mercato secondo INCROCI UP MEDIE ESP 
if (crossover(EMAfast,EMAslow)) and EMAslow>EMAlong and crossover(MACDdaily,0) and TradeDateIsAllowed()
    strategy.entry("3EMAcrossLong", strategy.long, comment="3EMAcrossLong")
    strategy.exit ("3EMAcrossLongEx",from_entry="3EMAcrossLong",profit = EmaTP,loss = EmaSL)

//condizioni ingresso ed uscita mercato secondo INCROCI DOWN MEDIE ESP 
if (crossunder(EMAfast, EMAslow)) and EMAslow<EMAlong and crossunder(MACDdaily,0) and TradeDateIsAllowed()
    strategy.entry("3EMAcrossShort", strategy.short, comment="3EMAcrossShort")
    strategy.exit ("3EMAcrossShortEx",from_entry="3EMAcrossShort",profit = EmaTP,loss = EmaSL)

//disegno EMA su grafico
plot (EMAfast, color = color.green)
plot (EMAslow, color = color.orange)
plot (EMAlong, color = color.red)

last test:last test