Spaces:
Running
Running
کد رو به زبان پاین اسکریگ ورژن6 و برای استغاده در تریدینگ ویو برام بنویس
Browse files- ssl-hybrid-bot.pine +52 -71
ssl-hybrid-bot.pine
CHANGED
|
@@ -1,84 +1,65 @@
|
|
| 1 |
//@version=6
|
| 2 |
-
strategy("SSL
|
| 3 |
|
| 4 |
-
//
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
//
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
//
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
-
//
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
riskPercent = input(2.0, title="Risk % per Trade", minval=0.1, maxval=100)
|
| 23 |
-
maxDrawdown = input(20.0, title="Max Drawdown %", minval=1, maxval=50)
|
| 24 |
-
|
| 25 |
-
// News Filter
|
| 26 |
-
filterNews = input(true, title="Filter High Impact News")
|
| 27 |
-
newsBuffer = input(30, title="Minutes Before/After News")
|
| 28 |
|
| 29 |
-
//
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
sslMALow = ta.sma(low, sslLength)
|
| 34 |
-
sslH = ta.ema(sslMAHigh, sslSmoothing)
|
| 35 |
-
sslL = ta.ema(sslMAlow, sslSmoothing)
|
| 36 |
-
sslDirection = sslH > sslL ? 1 : sslH < sslL ? -1 : 0
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
hullMA1 = request.security(syminfo.tickerid, hullTf, hullMA(hullSrc, hullPeriod))
|
| 43 |
-
hullDirection = hullMA1 > hullMA1[1] ? 1 : hullMA1 < hullMA1[1] ? -1 : 0
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
-
//
|
| 50 |
-
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
-
//
|
| 54 |
-
//
|
| 55 |
-
longCondition
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
//
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
takeProfitPips = stopLossPips * 2
|
| 63 |
|
| 64 |
-
//
|
| 65 |
-
|
| 66 |
-
if (longCondition)
|
| 67 |
-
strategy.entry("Long", strategy.long, qty=positionSize)
|
| 68 |
-
strategy.exit("Exit Long", "Long",
|
| 69 |
-
stop=close - stopLossPips,
|
| 70 |
-
limit=close + takeProfitPips,
|
| 71 |
-
trail_points=useTrailingStop ? stopLossPips : na,
|
| 72 |
-
trail_offset=useTrailingStop ? stopLossPips/2 : na)
|
| 73 |
-
|
| 74 |
-
if (shortCondition)
|
| 75 |
-
strategy.entry("Short", strategy.short, qty=positionSize)
|
| 76 |
-
strategy.exit("Exit Short", "Short",
|
| 77 |
-
stop=close + stopLossPips,
|
| 78 |
-
limit=close - takeProfitPips,
|
| 79 |
-
trail_points=useTrailingStop ? stopLossPips : na,
|
| 80 |
-
trail_offset=useTrailingStop ? stopLossPips/2 : na)
|
| 81 |
|
| 82 |
-
//
|
| 83 |
-
|
| 84 |
-
|
|
|
|
| 1 |
//@version=6
|
| 2 |
+
strategy("SSL Hybrid Advanced + Trailing Stop", overlay=true, margin_long=100, margin_short=100)
|
| 3 |
|
| 4 |
+
// Inputs
|
| 5 |
+
inputPeriod = input.int(10, title="SSL Period", minval=1)
|
| 6 |
+
inputSmoothing = input.int(3, title="Smoothing Factor", minval=1)
|
| 7 |
+
inputTrailingStop = input.float(1.5, title="Trailing Stop (%)", minval=0.1, step=0.1)
|
| 8 |
+
inputRiskReward = input.float(2.0, title="Risk/Reward Ratio", minval=1.0, step=0.1)
|
| 9 |
+
inputUseNewsFilter = input.bool(true, title="Use News Event Filter")
|
| 10 |
|
| 11 |
+
// SSL Hybrid Calculation
|
| 12 |
+
hmaFast = ta.hma(close, inputPeriod)
|
| 13 |
+
hmaSlow = ta.hma(close, inputPeriod * 2)
|
| 14 |
+
sslUp = ta.sma(hmaFast > hmaSlow ? hmaFast : hmaSlow, inputSmoothing)
|
| 15 |
+
sslDown = ta.sma(hmaFast < hmaSlow ? hmaFast : hmaSlow, inputSmoothing)
|
| 16 |
|
| 17 |
+
// Hull Trend Confirmation
|
| 18 |
+
hullTrend = ta.hma(close, inputPeriod * 3)
|
| 19 |
+
trendDirection = hullTrend > hullTrend[1] ? 1 : hullTrend < hullTrend[1] ? -1 : 0
|
| 20 |
|
| 21 |
+
// Entry Conditions
|
| 22 |
+
longCondition = close > sslUp and trendDirection == 1
|
| 23 |
+
shortCondition = close < sslDown and trendDirection == -1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
// Exit Conditions (Trailing Stop)
|
| 26 |
+
trailOffset = inputTrailingStop * 0.01 * close
|
| 27 |
+
longTrailPrice = 0.0
|
| 28 |
+
shortTrailPrice = 0.0
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
if longCondition
|
| 31 |
+
longTrailPrice := close - trailOffset
|
| 32 |
+
else
|
| 33 |
+
longTrailPrice := nz(longTrailPrice[1], close - trailOffset)
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
if shortCondition
|
| 36 |
+
shortTrailPrice := close + trailOffset
|
| 37 |
+
else
|
| 38 |
+
shortTrailPrice := nz(shortTrailPrice[1], close + trailOffset)
|
| 39 |
|
| 40 |
+
// Risk Management
|
| 41 |
+
positionSize = strategy.equity * 0.01 / (close * 0.01) // 1% risk per trade
|
| 42 |
+
takeProfitLong = close + (close - longTrailPrice) * inputRiskReward
|
| 43 |
+
takeProfitShort = close - (shortTrailPrice - close) * inputRiskReward
|
| 44 |
|
| 45 |
+
// Strategy Execution
|
| 46 |
+
if time > timestamp(2020, 1, 1, 0, 0) // Backtest period filter
|
| 47 |
+
if longCondition and (not inputUseNewsFilter or not na(security("ECONOMIC_CALENDAR", "D")))
|
| 48 |
+
strategy.entry("Long", strategy.long, qty=positionSize)
|
| 49 |
+
strategy.exit("Long Exit", "Long", stop=longTrailPrice, limit=takeProfitLong)
|
| 50 |
+
|
| 51 |
+
if shortCondition and (not inputUseNewsFilter or not na(security("ECONOMIC_CALENDAR", "D")))
|
| 52 |
+
strategy.entry("Short", strategy.short, qty=positionSize)
|
| 53 |
+
strategy.exit("Short Exit", "Short", stop=shortTrailPrice, limit=takeProfitShort)
|
| 54 |
|
| 55 |
+
// Plotting
|
| 56 |
+
plot(sslUp, color=color.new(color.green, 0), linewidth=2, title="SSL Up")
|
| 57 |
+
plot(sslDown, color=color.new(color.red, 0), linewidth=2, title="SSL Down")
|
| 58 |
+
plot(hullTrend, color=color.new(color.blue, 0), linewidth=2, title="Hull Trend")
|
|
|
|
| 59 |
|
| 60 |
+
// Background Color for Trend
|
| 61 |
+
bgcolor(trendDirection == 1 ? color.new(color.green, 90) : trendDirection == -1 ? color.new(color.red, 90) : na)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
// Alerts
|
| 64 |
+
alertcondition(longCondition, title="Long Signal", message="SSL Hybrid Long Signal")
|
| 65 |
+
alertcondition(shortCondition, title="Short Signal", message="SSL Hybrid Short Signal")
|