Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- strategy("CopyTrading Script", overlay = true, initial_capital = 110, default_qty_type = strategy.commission.cash_per_order, default_qty_value = 300, commission_value = 0.055)
- timePeriod = time > timestamp("UTC", 2023, 8, 9, 0, 0, 0)
- // STs (Strategies)
- // ST1 - Double Moving Average Proximity
- st1FastMaLength = input(22, "Fast MA Length", group = "ST1")
- st1SlowMaLength = input(46, "Slow MA Length", group = "ST1")
- st1FastMaType = input.string("SMA", "Fast MA Type", ["SMA", "EMA"], group = "ST1")
- st1SlowMaType = input.string("SMA", "Slow MA Type", ["SMA", "EMA"], group = "ST1")
- proximityPercentageMultiplier = input(310, "Proximity % Multiplier", group = "ST1")
- st1FastMaTypeFunc(source, length) =>
- if st1FastMaType == "SMA"
- ta.sma(source, length)
- else
- if st1FastMaType == "EMA"
- ta.ema(source, length)
- st1FastMa = st1FastMaTypeFunc(close, st1FastMaLength)
- st1SlowMaTypeFunc(source, length) =>
- if st1SlowMaType == "SMA"
- ta.sma(source, length)
- else
- if st1SlowMaType == "EMA"
- ta.ema(source, length)
- st1SlowMa = st1SlowMaTypeFunc(close, st1SlowMaLength)
- maProximityPercentage = ((st1SlowMa - st1FastMa) / close) * 100
- maProximity = maProximityPercentage * proximityPercentageMultiplier
- float st1BuyConf = 0
- float st1SellConf = 0
- if maProximity < 0
- st1BuyConf := 100 + maProximity
- if maProximity > 0
- st1SellConf := 100 - maProximity
- // ST2 - Double Moving Average Crossover
- st2FastMaLength = input(7, "Fast MA Length", group = "st2")
- st2SlowMaLength = input(22, "Slow MA Length", group = "st2")
- st2FastMaType = input.string("SMA", "Fast MA Type", ["SMA", "EMA"], group = "st2")
- st2SlowMaType = input.string("SMA", "Slow MA Type", ["SMA", "EMA"], group = "st2")
- st2FastMaTypeFunc(source, length) =>
- if st2FastMaType == "SMA"
- ta.sma(source, length)
- else
- if st2FastMaType == "EMA"
- ta.ema(source, length)
- st2FastMa = st2FastMaTypeFunc(close, st2FastMaLength)
- st2SlowMaTypeFunc(source, length) =>
- if st2SlowMaType == "SMA"
- ta.sma(source, length)
- else
- if st2SlowMaType == "EMA"
- ta.ema(source, length)
- st2SlowMa = st2SlowMaTypeFunc(close, st2SlowMaLength)
- st2BuyConf = 0
- st2SellConf = 0
- if ta.crossover(st2FastMa, st2SlowMa)
- st2BuyConf := 100
- if ta.crossunder(st2FastMa, st2SlowMa)
- st2SellConf := 100
- // ST3 - Supertrend
- SPTatrPeriod = input(35, "Supertrend ATR Length", group = "st3")
- SPTfactor = input(3.0, "Supertrend Factor", group = "st3")
- [superTrend, direction] = ta.supertrend(SPTfactor, SPTatrPeriod)
- st3BuyConf = 0
- st3SellConf = 0
- if direction < 0
- st3BuyConf := 100
- if direction > 0
- st3SellConf := 100
- // Managers
- // ST (Strategies) Manager
- st1Weight = input(0.3, "ST1", group = "Weight Options")
- st2Weight = input(0.3, "ST2", group = "Weight Options")
- st3Weight = input(0.4, "ST3", group = "Weight Options")
- st1BuyRltv = st1BuyConf * st1Weight
- st2BuyRltv = st2BuyConf * st2Weight
- st3BuyRltv = st3BuyConf * st3Weight
- st1SellRltv = st1SellConf * st1Weight
- st2SellRltv = st2SellConf * st2Weight
- st3SellRltv = st3SellConf * st3Weight
- globalBuyConf = st1BuyRltv + st2BuyRltv + st3BuyRltv
- globalSellConf = st1SellRltv + st2SellRltv + st3SellRltv
- // Global Manager
- longConfTreshold = input(94.0, "Long Confidence Treshhold", group = "Confidence")
- shortConfTreshold = input(94.0, "Short Confidence Treshhold", group = "Confidence")
- // Pietro shit starts here, be careful!
- //Order value
- orderValue = input(300.0, "Trade Value", group = "Trade")
- contracts = orderValue / close
- roundedContracts = (math.floor(contracts * 1000)) / 1000
- inLongTrade = strategy.position_size > 0
- inShortTrade = strategy.position_size < 0
- notInTrade = not inLongTrade and not inShortTrade
- //Backtesting
- backtestYN = input.string("No", "Backtest?", ["Yes", "No"], group = "Backtest")
- //Long trade
- if backtestYN == "Yes"
- if globalBuyConf > longConfTreshold
- strategy.close("short")
- strategy.entry("long", strategy.long, roundedContracts)
- else
- if globalBuyConf > longConfTreshold and timePeriod
- strategy.close("short")
- strategy.entry("long", strategy.long, roundedContracts)
- //short trade
- if backtestYN == "Yes"
- if globalSellConf > shortConfTreshold
- strategy.close("long")
- strategy.entry("short", strategy.short, roundedContracts)
- else
- if globalSellConf > shortConfTreshold and timePeriod
- strategy.close("long")
- strategy.entry("short", strategy.short, roundedContracts)
- //Aesthetics
- plot(st1FastMa, "ST1 Fast MA", color.yellow, 2)
- plot(st1SlowMa, "ST1 Slow MA", color.green, 2)
- plot(st2FastMa, "ST2 Fast MA", color.red, 2)
- plot(st2SlowMa, "ST2 Slow MA", color.yellow, 2)
- directionNegative = direction < 0
- plot(superTrend, "SuperTrend Line", directionNegative ? color.green : color.red, 2)
- tbl = table.new(position.top_right, 2, 2, color.black, color.white, 2)
- table.cell(tbl, 0, 0, "Buy Confidence:", text_color = color.white)
- table.cell(tbl, 0, 1, "Sell Confidence:", text_color = color.white)
- table.cell(tbl, 1, 0, str.tostring(globalBuyConf), text_color = color.white)
- table.cell(tbl, 1, 1, str.tostring(globalSellConf), text_color = color.white)
Advertisement
Add Comment
Please, Sign In to add comment