Buongiorno, vorrei sapere se è possibile codificare la seguente situazione. Vorrei che il sistema (che opera nell’intraday) dopo un certo numero di stop consecutivi si fermasse e che si riavviasse solo dopo che si è verificata una nuova condizione (ossia che c’è stato un trade positivo ad esempio). Ovviamente per sapere se negli eventuali trade che il sistema avrebbe fatto (se non lo avessi stoppato) arriva un trade positivo (quindi parliamo a distanza anche di diversi giorni o settimane) il sistema dovrebbe in un certo senso continuare a fare i trade virtualmente e “riaccendersi” live quando finalmente il trade vincente arriva. In pratica replicare la situazione in cui se una sistema non funziona, lo spegni, poi ovviamente dovresti continuare a monitorarlo in paper trading, e riavviarlo poi manualmente quando hai deciso che diciamo “torna a funzionare”. Si può automatizzare qualcosa del genere? Ho visto altri esempi in cui con la funzione quit, dopo un certo numero di trade, il sistema può essere interrotto ma poi bisogna riavviarlo manualmente. Grazie
Si, si può fare. Quando arrivano gli stop si simulano le operazioni (che non saranno al 100% identiche al reale, a causa di slippage, volatilità del mercato, ecc…). Esempio:
ONCE MaxLosses = 3
ONCE TradingON = 1
ONCE myLosses = 0
ONCE SL = 0.5 //0.5%
ONCE TP = 4.5 //1.5%
Sma = average[20,0](close)
IF TradingON THEN
// variables to be used for simulations
OnMarketStatus = 0
LongOnMarketStatus = 0
ShortOnMarketStatus = 0
// conditions to enter real trades
myLongConditions = close CROSSES OVER Sma AND Not OnMarket
myShortConditions = close CROSSES UNDER Sma AND Not OnMarket
IF myLongConditions THEN
BUY 1 CONTRACT AT MARKET
ELSIF myShortConditions THEN
SELLSHORT 1 CONTRACT AT MARKET
ENDIF
SET STOP %LOSS SL
SET TARGET %PROFIT TP
IF StrategyProfit <> StrategyProfit[1] THEN
IF StrategyProfit > StrategyProfit[1] THEN
myLosses = 0
ELSIF StrategyProfit < StrategyProfit[1] THEN
myLosses = myLosses + 1
IF myLosses = MaxLosses THEN
TradingON = 0
ENDIF
ENDIF
ENDIF
ELSE
myLosses = 0
// --- simulation starts here after MaxLosses consecutive losses
//
// conditions to enter simulated trades
myLongConditions = close CROSSES OVER Sma AND Not OnMarketStatus
myShortConditions = close CROSSES UNDER Sma AND Not OnMarketStatus
LongTradeStarted = 0
ShortTradeStarted = 0
IF myLongConditions THEN
// Long simulated trade
LongTradeStarted = 1
LongOnMarketStatus = 1
ShortOnMarketStatus = 0
mySL = close - (close * SL / 100)
myTP = close + (close * TP / 100)
ELSIF myShortConditions THEN
// Short simulated trade
ShortTradeStarted = 1
LongOnMarketStatus = 0
ShortOnMarketStatus = 1
mySL = close + (close * SL / 100)
myTP = close - (close * TP / 100)
ENDIF
OnMarketStatus = LongOnMarketStatus OR ShortOnMarketStatus
IF OnMarketStatus THEN
IF LongOnMarketStatus THEN
// check Long simulated trade
IF low <= mySL THEN
LongOnMarketStatus = 0 //it's not a profitable trade, DO NOTHING and keep simulating
ELSIF high >= myTP THEN
LongOnMarketStatus = 0 //it's a profitable trade, resume Real trading
TradingON = 1
ENDIF
ELSIF ShortOnMarketStatus THEN
// check Short simulated trade
IF high >= mySL THEN
ShortOnMarketStatus = 0 //it's not a profitable trade, DO NOTHING and keep simulating
ELSIF low <= myTP THEN
ShortOnMarketStatus = 0 //it's a profitable trade, resume Real trading
TradingON = 1
ENDIF
ENDIF
OnMarketStatus = LongOnMarketStatus OR ShortOnMarketStatus
ENDIF
// --- Simulation ends ehere
ENDIF
//graph TradingON = 1 AS "Real Trading"
//graph TradingON = 0 AS "Simulated Trading"
//graph LongTradeStarted AS "LONG trade entered" coloured("Green")
//graph ShortTradeStarted AS "SHORT trade entered" coloured("Red")
//graph myLosses = MaxLosses AS "Stop Real Trading" coloured ("Cyan")
//graph StrategyProfit > StrategyProfit[1] coloured("Green")
//graph StrategyProfit < StrategyProfit[1] coloured("Red")
//graph myLosses