//@version=4 strategy("TEST MACD DEFAULT", shorttitle="MACD", overlay=true, precision=4, initial_capital=1000, default_qty_type=strategy.percent_of_equity, default_qty_value=1, currency=currency.NONE, process_orders_on_close=true, pyramiding=0) // MACD [macdLine, signalLine, _] = macd(close, 12, 26, 9) // EMA 200 ema = ema(close, 200) plot(ema, title="EMA 200", color=color.yellow, linewidth=2) //QUANTITÉ pourcentageSL = input(1., "Pourcentage de risque") / 100 facteurTP = input(1.5, "Facteur du Take Profit") currentEquity = strategy.equity // ============================================================================================================ // // =============================================TIME RANGE===================================================== // // ============================================================================================================ // // CHOISIR LES DATES i_dateFilter = input(false, "═════ Date Range Filtering ═════") i_fromYear = input(2020, "From Year", minval = 1900) i_fromMonth = input(1, "From Month", minval = 1, maxval = 12) i_fromDay = input(1, "From Day", minval = 1, maxval = 31) i_toYear = input(2020, "To Year", minval = 1900) i_toMonth = input(6, "To Month", minval = 1, maxval = 12) i_toDay = input(1, "To Day", minval = 1, maxval = 31) // CREATION DE LA CONDITION start = timestamp(i_fromYear, i_fromMonth, i_fromDay, 00, 00) finish = timestamp(i_toYear, i_toMonth, i_toDay, 23, 59) window() => (time >= start and time <= finish) or not i_dateFilter ? true : false // AFFICHER LE BACKGROUND COLORÉ showDate = input(defval = false, title = "Show Date Range", type = input.bool) bgcolor(color = showDate and window() ? color.gray : na, transp = 90) // ============================================================================================================ // // =========================================LONG POSITIONS===================================================== // // ============================================================================================================ // // LONG CONDITIONS longCondition1 = crossover(macdLine, signalLine) longCondition2 = macdLine < 0 and signalLine < 0 longCondition3 = close > ema longCondition = longCondition1 and longCondition2 and longCondition3 and strategy.opentrades == 0 and window() // VARIABLE POUR SL float longSL = na longSL := longCondition ? lowest(close, 11)[1] : longSL[1] // VARIABLES POUR TP longEntryPrice = close longTailleSL = abs(longEntryPrice - longSL) float longTP = na longTP := longCondition ? close + (facteurTP * longTailleSL) : longTP[1] // TAILLE DE LA POSITION longPositionSize = (currentEquity * pourcentageSL) / longTailleSL plotchar(longSL, "longSL", "", location = location.top) plotchar(strategy.equity, "strategy.equity", "", location = location.top) // ENTRÉE/SORTIE if longCondition strategy.entry("LONG", strategy.long, qty=longPositionSize) strategy.exit("EXIT LONG","LONG", stop=longSL, limit=longTP) // TRACER SL longPlotSL = strategy.opentrades > 0 and strategy.position_size > 0 ? longSL : na plot(longPlotSL, title='LONG STOP LOSS', linewidth=2, style=plot.style_linebr, color=color.red) // TRACER TP longPlotTP = strategy.opentrades > 0 and strategy.position_size > 0 ? longTP : na plot(longPlotTP, title='LONG TAKE PROFIT', linewidth=2, style=plot.style_linebr, color=color.green) // ============================================================================================================ // // =========================================SHORT POSITIONS==================================================== // // ============================================================================================================ // // SHORT CONDITIONS shortCondition1 = crossunder(macdLine, signalLine) shortCondition2 = macdLine > 0 and signalLine > 0 shortCondition3 = close < ema shortCondition = shortCondition1 and shortCondition2 and shortCondition3 and strategy.opentrades == 0 and window() // VARIABLE POUR SL float shortSL = na shortSL := shortCondition ? highest(close, 11)[1] : shortSL[1] // VARIABLES POUR TP shortEntryPrice = close shortTailleSL = abs(shortEntryPrice - shortSL) float shortTP = na shortTP := shortCondition ? close - (facteurTP * shortTailleSL) : shortTP[1] // TAILLE DE LA POSITION shortPositionSize = (currentEquity * pourcentageSL) / shortTailleSL // ENTRÉE/SORTIE if shortCondition strategy.entry("SHORT", strategy.short, qty=shortPositionSize) strategy.exit("EXIT SHORT","SHORT", stop=shortSL, limit=shortTP) // TRACER SL shortPlotSL = strategy.opentrades > 0 and strategy.position_size < 0 ? shortSL : na plot(shortPlotSL, title='SHORT STOP LOSS', linewidth=2, style=plot.style_linebr, color=color.orange) // TRACER TP shortPlotTP = strategy.opentrades > 0 and strategy.position_size < 0 ? shortTP : na plot(shortPlotTP, title='SHORT TAKE PROFIT', linewidth=2, style=plot.style_linebr, color=color.blue)