81 lines
3.9 KiB
Plaintext
81 lines
3.9 KiB
Plaintext
|
|
//@version=5
|
||
|
|
indicator("Pinbar trend", overlay=true)
|
||
|
|
|
||
|
|
// Strategy Settings
|
||
|
|
// strategy("Pinbar Trend Strategy", overlay=true)
|
||
|
|
|
||
|
|
// Parameters
|
||
|
|
threshold = 0.6667
|
||
|
|
riskRewardRatio = 2
|
||
|
|
|
||
|
|
// Calculate the length of upper and lower wicks
|
||
|
|
upperWickLength = high - math.max(close, open)
|
||
|
|
lowerWickLength = math.min(close, open) - low
|
||
|
|
|
||
|
|
// Input for EMA length
|
||
|
|
emaFastInput = input.int(10, title="EMA Fast", minval=1)
|
||
|
|
emaMidInput = input.int(20, title="EMA Mid", minval=1)
|
||
|
|
emaSlowInput = input.int(60, title="EMA Slow", minval=1)
|
||
|
|
emafast = ta.ema(close, emaFastInput)
|
||
|
|
emamid = ta.ema(close, emaMidInput)
|
||
|
|
emaslow = ta.ema(close, emaSlowInput)
|
||
|
|
|
||
|
|
// Input for lookback period to calculate the slope of EMA
|
||
|
|
// lookbackPeriod = input.int(5, title="Lookback Period for EMA Slope", minval=1)
|
||
|
|
|
||
|
|
// closeSlope = (close - close[lookbackPeriod]) / lookbackPeriod
|
||
|
|
|
||
|
|
// Calculate total range of the bar
|
||
|
|
totalRange = high - low
|
||
|
|
|
||
|
|
// Conditions for bearish and bullish signal bars
|
||
|
|
isBearishSignalBar = (upperWickLength >= threshold * totalRange and upperWickLength > lowerWickLength)
|
||
|
|
isBullishSignalBar = (lowerWickLength >= threshold * totalRange and lowerWickLength > upperWickLength)
|
||
|
|
|
||
|
|
// Condition to check if the bar touches the EMA line
|
||
|
|
// isTouchingEMA = (emafast >= low and emafast <= high) or (emamid >= low and emamid <= high)
|
||
|
|
isTouchingEMA = (isBearishSignalBar and ((emafast >= math.max(close, open) and emafast <= high) or (emamid >= math.max(close, open) and emamid <= high))) or (isBullishSignalBar and ((emafast <= math.min(close, open) and emafast >= low) or (emamid <= math.min(close, open) and emamid >= low)))
|
||
|
|
|
||
|
|
// Condition to check if the bar is below or above the EMA
|
||
|
|
isBelowEMA = high < emafast
|
||
|
|
isAboveEMA = low > emafast
|
||
|
|
|
||
|
|
// Final conditions to highlight bars
|
||
|
|
highlightBearishBar = isBearishSignalBar and (isTouchingEMA)
|
||
|
|
highlightBullishBar = isBullishSignalBar and (isTouchingEMA)
|
||
|
|
|
||
|
|
weakHighlightBearishBar = isBearishSignalBar and (not isTouchingEMA)
|
||
|
|
weakHighlightBullishBar = isBullishSignalBar and (not isTouchingEMA)
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// Plotting the bars
|
||
|
|
barcolor(highlightBearishBar ? color.yellow : na, title = "Bearish bar color")
|
||
|
|
barcolor(highlightBullishBar ? color.yellow : na, title = "Bullish bar color")
|
||
|
|
barcolor(weakHighlightBearishBar ? color.rgb(33, 150, 243, 40) : na, title = "Weak bearish bar color")
|
||
|
|
barcolor(weakHighlightBullishBar ? color.rgb(33, 150, 243, 40): na, title = "Weak bullish bar color")
|
||
|
|
|
||
|
|
// Adding markers for bearish and bullish bars
|
||
|
|
plotchar(series=highlightBearishBar, location=location.abovebar, char='▼', color=color.red, size=size.tiny, title = "Bearish indicator")
|
||
|
|
plotchar(series=highlightBullishBar, location=location.belowbar, char='▲', color=color.green, size=size.tiny, title = "Bullish indicator")
|
||
|
|
plotchar(series=weakHighlightBearishBar, location=location.abovebar, char='▼', color=color.rgb(255,82, 82, 60), size=size.tiny, title = "Weak bearish")
|
||
|
|
plotchar(series=weakHighlightBullishBar, location=location.belowbar, char='▲', color=color.rgb(76, 175, 80, 60), size=size.tiny, title = "Weak bullish")
|
||
|
|
|
||
|
|
|
||
|
|
// Adding alerts
|
||
|
|
alertcondition(highlightBearishBar or highlightBullishBar, title="Pinbar Signal", message='{"ticker": "{{ticker}}", "exchange": "{{exchange}}", "interval": "{{interval}}"}')
|
||
|
|
|
||
|
|
// Trading strategy for bullish signal bars
|
||
|
|
// if (highlightBullishBar and closeSlope > 0)
|
||
|
|
// longStopLoss = low
|
||
|
|
// longTakeProfit = close + (close - longStopLoss) * riskRewardRatio
|
||
|
|
// strategy.entry("Bullish Signal", strategy.long, when=highlightBullishBar and closeSlope > 0)
|
||
|
|
// strategy.exit("Take Profit/Stop Loss", "Bullish Signal", stop=longStopLoss, limit=longTakeProfit)
|
||
|
|
|
||
|
|
// // Trading strategy for bearish signal bars
|
||
|
|
// if (highlightBearishBar and closeSlope < 0)
|
||
|
|
// shortStopLoss = high
|
||
|
|
// shortTakeProfit = close - (shortStopLoss - close) * riskRewardRatio
|
||
|
|
// strategy.entry("Bearish Signal", strategy.short, when=highlightBearishBar and closeSlope < 0)
|
||
|
|
// strategy.exit("Take Profit/Stop Loss", "Bearish Signal", stop=shortStopLoss, limit=shortTakeProfit)
|