yserictsai / DEEPE

DEEPE Project - PineScript
0 stars 0 forks source link

TO-DO List #1

Open yserictsai opened 1 year ago

yserictsai commented 1 year ago

DEEPE 手動交易策略

Signal Detection Function

Filter Function

Stop Loss Function

Trailing Stop Function

Strategy Evaluation Table

進階

yserictsai commented 1 year ago

完善系統後 有需要再採購 https://courses.theartoftrading.com/courses/my-indicators https://courses.theartoftrading.com/courses/ultimate-pullback-indicator

yserictsai commented 1 year ago

DEEPE 自動交易策略

3commas for Crypto

PineConnector

yserictsai commented 1 year ago

` //DEB beta version

highest = high == ta.highest(high, 8) lowest = low == ta.lowest(low, 8)

bull_bar_pattern = open > open[2] and close > close[2] and open[1] > close[1] and highest and (trend_bull_pass or anti_trend_bull_pass) bear_bar_pattern = open < open[2] and close < close[2] and open[1] < close[1] and lowest and (trend_bear_pass or anti_trend_bear_pass) `

yserictsai commented 1 year ago

` Switch options // Trend // { short_MA_direction_up = short_MA > short_MA[1] and short_MA[1] >= short_MA[2] short_MA_direction_down = short_MA < short_MA[1] and short_MA[1] <= short_MA[2] long_MA_direction_up = long_MA > long_MA[1] and long_MA[1] >= long_MA[2] long_MA_direction_down = long_MA < long_MA[1] and long_MA[1] <= long_MA[2]

bullish_MA_arrange = short_MA > long_MA bearish_MA_arrange = short_MA < long_MA

bullish_trend = short_MA_direction_up and long_MA_direction_up and bullish_MA_arrange bearish_trend = short_MA_direction_down and long_MA_direction_down and bearish_MA_arrange bullish_antitrend = short_MA_direction_up and bearish_MA_arrange and close < long_MA bearish_antitrend = short_MA_direction_down and bullish_MA_arrange and close > long_MA

BullishTrendFilterOption = switch useTrendFilter "No Trend Filter" => true "Trend" => bullish_trend "Specific antiTrend" => bullish_antitrend "Both" => bullish_trend or bullish_antitrend

BearishTrendFilterOption = switch useTrendFilter "No Trend Filter" => true "Trend" => bearish_trend "Specific antiTrend" => bearish_antitrend "Both" => bearish_trend or bearish_antitrend // }

`

yserictsai commented 1 year ago

` DEB beta version2

higherLow = low > low[2] and low[1] >= low[2] higherHigh = high > high[2] and high[1] >= high[2]

lowerLow = low < low[2] and low[1] <= low[2] lowerHigh = high < high[2] and high[1] <= high[2]

bullishMiddle = close[1] >= open[1] bearishMiddle = close[1] <= open[1]

upFib_2 = low[2] + (high[2] - low[2]) solidLevel downFib_2 = low[2] + (high[2] - low[2]) (1 - solidLevel)

upFib = low + (high - low) solidLevel downFib = low + (high - low) (1 - solidLevel)

solid_2 = (close[2] >= upFib_2 and open[2] <= downFib_2) or (open[2] >= upFib_2 and close[2] <= downFib_2) solid = (close >= upFib and open <= downFib) or (open >= upFib and close <= downFib)

candleSize_2 = high[2] - low[2] candleSize = high - low

atr = ta.atr(20) atr_2 = atr[2] atrFilter = (candleSize_2 >= atr_2) and (candleSize >= atr)

bullishDEC = higherLow and higherHigh and bearishMiddle and solid_2 and solid and atrFilter bearishDEC = lowerLow and lowerHigh and bullishMiddle and solid_2 and solid and atrFilter

`

yserictsai commented 1 year ago

Falling three Rising three

yserictsai commented 1 year ago

ph = ta.pivothigh(8, 8) pl = ta.pivotlow(8, 8) nz(ph,0.0) nz(pl,0.0)

var ph_val = 0.0 var pl_val = 0.0

if ph != 0 ph_val := ph

if pl != 0 pl_val := pl

// plot(ph) plot(ph_val, color = color.red) plot(pl_val, color = color.yellow)

yserictsai commented 1 year ago

ph = ta.pivothigh(8, 8) pl = ta.pivotlow(8, 8) nz(ph,0.0) nz(pl,0.0)

var ph_val = 0.0 var pl_val = 0.0

if ph != 0 ph_val := ph

if pl != 0 pl_val := pl

// plot(ph) plot(ph_val, color = color.red) plot(pl_val, color = color.yellow)

yserictsai commented 1 year ago

bullish_signal = bull and MACD_crossover and RSI_crossover bearish_signal = bear and MACD_crossunder and RSI_crossunder

rr = input.float(title = "RR Ratio", defval=1.0) lookback = input.int(title = "SL Lookback bar", defval = 7) SL_atrlen = input.int(title = "SL ATR Length", defval = 13) SL_ticks = input.int(title = "SL Ticks", defval = 50) factor = input.float(title = "supertrend factor", defval = 3) SL_supertrendlen = input.int(title = "SL SuperTrend Length", defval = 13)

var win_price = 0.0 var lose_price = 0.0 var long_SL = 0.0 var short_SL = 0.0 var long_flag = 0 var short_flag = 0

SL_type = input.string(defval = "ATR", options = ["ATR", "fixed ticks", "swingHL", "SuperTrend"])

SL_ATR(lookback, SL_atrlen) => atr = ta.atr(SL_atrlen) recentLow = ta.lowest(low, lookback) recentHigh = ta.highest(high, lookback) long_loseprice = recentLow - atr short_loseprice = recentHigh + atr [long_loseprice, short_loseprice]

SL_swingHL(lookback) => recentLow = ta.lowest(low, lookback) recentHigh = ta.highest(high, lookback) long_loseprice = recentLow short_loseprice = recentHigh [long_loseprice, short_loseprice]

SL_fixT(src, SL_ticks) => long_loseprice = src - (syminfo.mintick SL_ticks) short_loseprice = src + (syminfo.mintick SL_ticks) [long_loseprice, short_loseprice]

SL_superTrend(src, factor, SL_supertrendlen) => [sup, dir] = ta.supertrend(factor, SL_supertrendlen) long_loseprice = src - sup short_loseprice = src + sup [long_loseprice, short_loseprice]

if SL_type == "ATR" [long_loseATR, short_loseATR] = SL_ATR(lookback, SL_atrlen) long_SL := long_loseATR short_SL := short_loseATR

if SL_type == "swingHL" [long_loseSW, short_loseSW] = SL_swingHL(lookback) long_SL := long_loseSW short_SL := short_loseSW

if SL_type == "fixed ticks" [long_loseticks, short_loseticks] = SL_fixT(src, SL_ticks) long_SL := long_loseticks short_SL := short_loseticks

if SL_type == "SuperTrend" [long_losesup, short_losesup] = SL_superTrend(src, factor, SL_supertrendlen) long_SL := long_losesup short_SL := short_losesup

intrade = (long_flag == 1 or short_flag == 1) intradeBar = ta.barssince(intrade)

if bullish_signal and not intrade lose_price := long_SL long_flag := 1 win_price := close + (close - lose_price) * rr

if bearish_signal and not intrade lose_price := short_SL short_flag := 1 win_price := close - (lose_price - close) * rr

win_trade = (not bullish_signal or not bearish_signal) and ((close >= win_price and long_flag == 1) or (close <= win_price and short_flag == 1)) lose_trade = (not bullish_signal or not bearish_signal) and ((close <= lose_price and long_flag == 1) or (close >= lose_price and short_flag == 1))

if win_trade or lose_trade long_flag := 0 short_flag := 0

var win_count = 0 var lose_count = 0

if win_trade win_count += 1

if lose_trade lose_count += 1

bgcolor(win_trade ? color.new(color.blue, 90) : na) bgcolor(lose_trade ? color.new(color.red, 90) : na)

plot((long_flag != 0 or short_flag != 0) ? win_price : na, color=color.green, style=plot.style_linebr, title="Profit Target") plot((long_flag != 0 or short_flag != 0) ? lose_price : na, color=color.red, style=plot.style_linebr, title="Stop Loss")

plot(win_count, color = color.black, display = display.status_line) plot(lose_count, color = color.red, display = display.status_line)

yserictsai commented 1 year ago

src = input(title = "Calculate source", defval = close) rr = input.float(title = "RR Ratio", defval=1.0) lookback = input.int(title = "SL Lookback bar", defval = 7) SL_atrlen = input.int(title = "SL ATR Length", defval = 13) SL_ticks = input.int(title = "SL Ticks", defval = 50) factor = input.float(title = "supertrend factor", defval = 3) SL_supertrendlen = input.int(title = "SL SuperTrend Length", defval = 13)

var win_price = 0.0 var lose_price = 0.0 var long_SL = 0.0 var short_SL = 0.0 var long_flag = 0 var short_flag = 0

SL_type = input.string(defval = "ATR", options = ["ATR", "fixed ticks", "SuperTrend"])

SL_ATR(src, lookback, SL_atrlen) => atr = ta.atr(SL_atrlen) recentLow = ta.lowest(src, lookback) recentHigh = ta.highest(src, lookback) long_loseprice = recentLow - atr short_loseprice = recentHigh + atr [long_loseprice, short_loseprice]

SL_fixT(src, SL_ticks) => long_loseprice = src - (syminfo.mintick SL_ticks) short_loseprice = src + (syminfo.mintick SL_ticks) [long_loseprice, short_loseprice]

SL_superTrend(src, factor, SL_supertrendlen) => [sup, dir] = ta.supertrend(factor, SL_supertrendlen) long_loseprice = src - sup short_loseprice = src + sup [long_loseprice, short_loseprice]

if SL_type == "ATR" [long_loseATR, short_loseATR] = SL_ATR(src, lookback, SL_atrlen) long_SL := long_loseATR short_SL := short_loseATR

if SL_type == "fixed ticks" [long_loseticks, short_loseticks] = SL_fixT(src, SL_ticks) long_SL := long_loseticks short_SL := short_loseticks

if SL_type == "SuperTrend" [long_losesup, short_losesup] = SL_superTrend(src, factor, SL_supertrendlen) long_SL := long_losesup short_SL := short_losesup

bullish_signal = hammer bearish_signal = star

intrade = (long_flag == 1 or short_flag == 1) intradeBar = ta.barssince(intrade)

if bullish_signal and not intrade lose_price := long_SL long_flag := 1 win_price := close + (close - lose_price) * rr

if bearish_signal and not intrade lose_price := short_SL short_flag := 1 win_price := close - (lose_price - close) * rr

win_trade = (not bullish_signal or not bearish_signal) and ((close >= win_price and long_flag == 1) or (close <= win_price and short_flag == 1)) lose_trade = (not bullish_signal or not bearish_signal) and ((close <= lose_price and long_flag == 1) or (close >= lose_price and short_flag == 1))

if win_trade or lose_trade long_flag := 0 short_flag := 0

var win_count = 0 var lose_count = 0

if win_trade win_count += 1

if lose_trade lose_count += 1

bgcolor(win_trade ? color.new(color.blue, 90) : na) bgcolor(lose_trade ? color.new(color.red, 90) : na)

plot(long_flag, color = color.black, display = display.status_line) plot(short_flag, color = color.yellow, display = display.status_line)

plot((long_flag != 0 or short_flag != 0) ? win_price : na, color=color.green, style=plot.style_linebr, title="Profit Target") plot((long_flag != 0 or short_flag != 0) ? lose_price : na, color=color.red, style=plot.style_linebr, title="Stop Loss")

plot(win_count, color = color.black, display = display.status_line) plot(lose_count, color = color.red, display = display.status_line)

yserictsai commented 1 year ago

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © ystsai

//@version=5 // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © ystsai //@version=5 indicator("DEEPE Detection testing", overlay=true)

// General Input Settings // { var general = "General Settings" // // 用於設定畫圖是否依照Real Time Bar變動 disableRepaint = input.bool(title="Disable Repainting?", tooltip="General Settings", defval=true) // } // Indicator Function() // { // MA ma(source, length, TrendMAType) => switch TrendMAType "SMA" => ta.sma(source, length) "EMA" => ta.ema(source, length) "HMA" => ta.hma(source, length) "WMA" => ta.wma(source, length) "VWMA" => ta.vwma(source, length) => e1 = ta.ema(source, length) e2 = ta.ema(e1, length) 2 * e1 - e2

// // 用於設定的參數 var param = "General Settings"

sourceMA = input(defval=close, title="source for MA", group=param) TrendMAType = input.string(title="Choose Trend MA Type", defval="SMA", options=["SMA", "EMA", "HMA", "WMA", "VWMA"], tooltip="Choose a Trend MA method", group=param) shortMALength = input.int(title="Short MA Length", defval=20, group=param) longMALength = input.int(title="Long MA Length", defval=60, group=param) atrLengthforInc = input.int(title="ATR Length for indicator", defval=13, group=param) ATRindicetor = ta.atr(atrLengthforInc) shortMA = ma(sourceMA, shortMALength, TrendMAType) longMA = ma(sourceMA, longMALength, TrendMAType) supertrendFactor = input.float(title="Super Trend Factor", defval=2.618, group=param) supertrendATRLength = input.int(title="Super Trend ATR Length", defval=13, group=param) [supertrend, direction] = ta.supertrend(supertrendFactor, supertrendATRLength) bufferPoint_mintick = math.round_to_mintick(supertrend)

plot(shortMA, color=color.orange) plot(longMA, color=color.blue) plot(ATRindicetor, color=color.black, display = display.status_line)

// }

// --- --- --- ---// var aroonFilter = "Aroon Indicator Filter Settings" useAroonFilter = input.bool(title = "Use Aroon Filter", defval=true, tooltip="Turns on/off the Aroon filter", group=aroonFilter)

length = input.int(14, minval=1, group=aroonFilter) upper = 100 (ta.highestbars(high, length) + length)/length lower = 100 (ta.lowestbars(low, length) + length)/length bull_cross = ta.crossover(upper, lower) bull_cross_barssince = ta.barssince(bull_cross) bear_cross = ta.crossunder(upper, lower) bear_cross_barssince = ta.barssince(bear_cross)

aroonUpTrendFilter(bool useAroonFilter = false) => bull_trend = (upper > lower) and (bull_cross_barssince >= 2) not useAroonFilter or bull_trend

aroonDownTrendFilter(bool useAroonFilter = false) => bear_trend = (upper < lower) and (bear_cross_barssince >= 2) not useAroonFilter or bear_trend

aroonUpTrend = aroonUpTrendFilter(useAroonFilter) aroonDownTrend = aroonDownTrendFilter(useAroonFilter)

//// ---- //// var supertrendFilter = "SuperTrend Filter Settings" useSuperTrendFilter = input.bool(title = "Use SuperTrend Filter", defval=true, tooltip="Turns on/off the SuperTrend filter", group=supertrendFilter)

superUpTrendFilter(bool useSuperTrendFilter = false) => bullish_trend = direction < 0 aboveSuperTrend = low > supertrend breaktrend = (high > supertrend and direction > 0) not useSuperTrendFilter or (bullish_trend and aboveSuperTrend) or breaktrend superDownTrendFilter(bool useSuperTrendFilter = false) => bearish_trend = direction > 0 belowSuperTrend = high < supertrend breaktrend = (low < supertrend and direction < 0) not useSuperTrendFilter or (bearish_trend and belowSuperTrend) or breaktrend

superUpTrend = superUpTrendFilter(useSuperTrendFilter) superDownTrend = superDownTrendFilter(useSuperTrendFilter)

// High timeframe supertrend filter // var HTsupertrendFilter = "High Timeframe Super Trend Filter Settings"

useHTsuperTrendFilter = input.bool(title="Use High TimeFrame bullish Trend Filter", defval=true, tooltip="Turns on/off the High TimeFrame bullish Trend filter", group=HTsupertrendFilter) HTATRLength = input.int(title="High Timeframe ATR Length", defval=13, group=HTsupertrendFilter) HTsupertrendFactor = input.float(title="High Timeframe Super Trend Factor", defval=2.618, group=HTsupertrendFilter)

HTsupertrendPeriod = if timeframe.period == "60" "240" else if timeframe.period == "30" "60" else timeframe.period

[HTSupertrend, HTDirection] = request.security(syminfo.tickerid, HTsupertrendPeriod, ta.supertrend(HTsupertrendFactor, HTATRLength), ignore_invalid_symbol=true)

HTsuperUpTrendFilter(bool useHTsuperTrendFilter = false) => bullish_trend = HTDirection < 0 not useHTsuperTrendFilter or bullish_trend

HTsuperDownTrendFilter(bool useHTsuperTrendFilter = false) => bearish_trend = HTDirection > 0 not useHTsuperTrendFilter or bearish_trend

HTsuperUpTrend = HTsuperUpTrendFilter(useHTsuperTrendFilter) HTsuperDownTrend = HTsuperDownTrendFilter(useHTsuperTrendFilter)

// bgcolor(HTsuperUpTrend ? color.new(color.green, 50) : na) // bgcolor(HTsuperDownTrend ? color.new(color.red, 50) : na)

// plot(HTDirection, color=color.black, display = display.) // plot(direction, color=color.red, display = display.pane)

// ATR Size Filter Settings // { var ATRsize = "ATR Size Filter Settings"

useATRFilter = input.bool(title="Use size Filter by ATR?", defval=true, tooltip="Turns on/off the ATR filter", group=ATRsize) upperSizeLimitMultipler = input.float(title="Upper ATR Limit Multipler", defval=2.618, group=ATRsize) lowerSizeLimitMultipler = input.float(title="Lower ATR Limit Multipler", defval=0.618, group=ATRsize) sizeATRLength = input.int(title="ATR Length", defval=13, group=ATRsize)

// // ATR Filter Function() sizeFilterbyATR(int sizeATRLength = 13, float upperSizeLimitMultipler = 2.618, float lowerSizeLimitMultipler = 0.618, bool useATRFilter = false) => atr = ta.atr(sizeATRLength) candleSize = high - low upperSizeLimit = atr upperSizeLimitMultipler lowerSizeLimit = atr lowerSizeLimitMultipler not useATRFilter or (candleSize >= lowerSizeLimit and candleSize <= upperSizeLimit)

// // Testing sizeFilter = sizeFilterbyATR(sizeATRLength, upperSizeLimitMultipler, lowerSizeLimitMultipler, useATRFilter) // bgcolor(sizeFilter ? color.new(color.blue, 50) : na) // }

//------------ Candle Detection Function() ----------// // Pin Bar Settings // { var pinbar = "Pin Bar Settings"

usePinBar = input.bool(title="Use Pin Bar Detection?", defval=true, tooltip="Turns on/off the Pin Bar Detection", group=pinbar) bodyLevel = input.float(title="Pin Bar Body Level", defval=0.786, group=pinbar)

// // Hammer Candle Detection Function() // { hammerCandle(float bodyLevel = 0.786, bool usebullishHammer = false) => bullbodyLevel = low + (high - low) bodyLevel hammerSolidBody = close > open ? open : close hammerSolidBody >= bullbodyLevel // } // Star Candle Detection // { starCandle(float bodyLevel = 0.786, bool usebearishStar = false) => bearbodyLevel = high - (high - low) bodyLevel starSolildBody = close < open ? open : close starSolildBody <= bearbodyLevel // }

// Double Engulfing Bar Settings // { var doubleengulfingbar = "Double Engulfing Bar Settings"

// // 用於是否使用 Double Engulfing Bar Detection的開關 useDoubleEngulfingBar = input.bool(title="Use Doulbe Engulfing Bar Detection?", defval=true, tooltip="Turns on/off the Double Engulfing Bar Detection", group=doubleengulfingbar)

// // 用於放寬Gap Double Engulfing Bar的訊號 pointAllowanceD = input.int(title="Gap Allowance of Double Engulfing Bar", defval=2, group=doubleengulfingbar) allowanceD = syminfo.mintick * pointAllowanceD

// // 用於過濾帶有過長反向影線的吞噬K 數值越大可接受用大的反向影線 等於0則為關閉 antiWickSizePercentageD = input.float(title="the Percentage of anti Wick size in DEC", defval=0.382, group=doubleengulfingbar)

// } // bullish Double Engulfing Candle Detection // 可以用兩種判斷方式來區分吞噬 1. 看是否為實體K useSolidDoubleEngulfBar 2. 設定反向影線的比例antiWickSizePercentageD 可用0.382 用2.看起來比較好 // { bullishDoubleEngulfingCandle(float allowanceD = 0.0, float antiWickSizePercentageD = 0.0) => topWickSize = math.abs(high - (close > open ? close : open)) candleSize = high - low failEngulf_1 = (close[2] >= open[2] and close[1] > open[2] and open[1] <= close[2] + allowanceD) (close[1] <= open[1] and close >= open[1] and open <= close[1] + allowanceD and close >= high[1]) and failEngulf_1 and (antiWickSizePercentageD == 0.0 or topWickSize / candleSize < antiWickSizePercentageD) // } // bearish Double Engulfing Candle Detection // { bearishDoubleEngulfingCandle(float allowanceD = 0.0, float antiWickSizePercentageD = 0.0) => bottomWickSize = math.abs(high - (close > open ? close : open)) candleSize = high - low failEngulf_1 = (close[2] <= open[2] and close[1] < open[2] and open[1] >= close[2] - allowanceD) (close[1] >= open[1] and close <= open[1] and open >= close[1] - allowanceD and close <= low[1]) and failEngulf_1 and (antiWickSizePercentageD == 0.0 or bottomWickSize / candleSize < antiWickSizePercentageD) // }

// Pin Bar with Engulfing Settings var pinbarwithengulfingbar = "Pin Bar with Engulfing Settings" usePinBarwithEngulfingBar = input.bool(title="Use Pin Bar with Engulfing Bar Detection?", defval=true, tooltip="Turns on/off the Pin Bar with Engulfing Bar Detection", group=pinbarwithengulfingbar) pointAllowanceforPinE = input.int(title="Gap Allowance of Pin Bar with Engulfing", defval=2, group=pinbarwithengulfingbar) allowanceE = syminfo.mintick * pointAllowanceforPinE

// // Hammer Candle Detection Function() // { hammerwithEngulfing(float bodyLevel = 0.786, float allowanceE = 0.0) => bullbodyLevel = low[1] + (high[1] - low[1]) * bodyLevel hammerSolidBody = close[1] > open[1] ? open[1] : close[1] hasHammer = hammerSolidBody >= bullbodyLevel withEngulfing = (close < low[1]) and ((open + allowanceE) > (close[1] > open[1] ? close[1] : open[1])) hasHammer and withEngulfing

// } // Star Candle Detection // { starwithEngulfing(float bodyLevel = 0.786, float allowanceE = 0.0) => bearbodyLevel = high[1] - (high[1] - low[1]) * bodyLevel starSolildBody = close[1] < open[1] ? open[1] : close[1] hasStar = starSolildBody <= bearbodyLevel withEngulfing = (close > high[1]) and ((open - allowanceE) < (close[1] < open[1] ? close[1] : open[1])) hasStar and withEngulfing // } ////

hammer = usePinBar ? hammerCandle(bodyLevel) and sizeFilter and superUpTrend and HTsuperUpTrend and aroonUpTrend : na star = usePinBar ? starCandle(bodyLevel) and sizeFilter and superDownTrend and HTsuperDownTrend and aroonDownTrend : na

bullishDEC = useDoubleEngulfingBar ? bullishDoubleEngulfingCandle(allowanceD, antiWickSizePercentageD) and sizeFilter and superUpTrend and HTsuperUpTrend and aroonUpTrend: na bearishDEC = useDoubleEngulfingBar ? bearishDoubleEngulfingCandle(allowanceD, antiWickSizePercentageD) and sizeFilter and superDownTrend and HTsuperDownTrend and aroonDownTrend : na

hammerwithE = usePinBarwithEngulfingBar ? hammerwithEngulfing(bodyLevel, allowanceE) and sizeFilter[1] and superDownTrend and HTsuperUpTrend and aroonUpTrend : na starwithE = usePinBarwithEngulfingBar ? starwithEngulfing(bodyLevel, allowanceE) and sizeFilter[1] and superUpTrend and HTsuperDownTrend and aroonDownTrend: na

plotshape(bullishDEC, title="bullishDEC", style=shape.triangleup, color=color.green, location=location.belowbar) plotshape(bearishDEC, title="bearishDEC", style=shape.triangledown, color=color.red, location=location.abovebar) plotshape(hammer, title="Hammer", style=shape.triangleup, color=color.green, location=location.belowbar) plotshape(star, title="Star", style=shape.triangledown, color=color.red, location=location.abovebar) plotshape(starwithE, title="Hammer", style=shape.triangleup, color=color.green, location=location.belowbar) plotshape(hammerwithE, title="Star", style=shape.triangledown, color=color.red, location=location.abovebar) bgcolor(bullishDEC ? color.new(color.gray, 90) : na) bgcolor(bearishDEC ? color.new(color.gray, 90) : na) bgcolor(hammer ? color.new(color.gray, 90) : na) bgcolor(star ? color.new(color.gray, 90) : na) bgcolor(starwithE ? color.new(color.gray,90) : na) bgcolor(hammerwithE ? color.new(color.gray, 90) : na)

// // Alert bullish_signal = hammer or bullishDEC or starwithE bearish_signal = star or bearishDEC or hammerwithE // bearish_signal = false

source = input(title = "Calculate source", defval = hl2) rr = input.float(title = "RR Ratio", defval=1.0) lookback = input.int(title = "SL Lookback bar", defval = 7) SL_atrlen = input.int(title = "SL ATR Length", defval = 7) SL_Perc = input.float(title = "SL %", defval=0.01) SL_ticks = input.int(title = "SL Ticks", defval = 50)

var win_price = 0.0 var lose_price = 0.0 var price_range = 0.0 var long_flag = 0 var short_flag = 0

SL_type = input.string(defval = "ATR", options = ["ATR", "%", "fixed ticks"])

SL_ATR(src, lookback, SL_atrlen) => atr = ta.atr(SL_atrlen) recentLow = ta.lowest(src, lookback) recentHigh = ta.highest(src, lookback) long_loseprice = recentLow - atr short_loseprice = recentHigh + atr [long_loseprice, short_loseprice]

SL_Perc(src, SL_Perc) => long_loseprice = src (1 - SL_Perc) short_loseprice = src (1 + SL_Perc) [long_loseprice, short_loseprice]

SL_fixT(src, SL_ticks) => long_loseprice = src - (syminfo.mintick SL_ticks) short_loseprice = src + (syminfo.mintick SL_ticks) [long_loseprice, short_loseprice]

// SL_func(SL_type, src, lookback, SL_atrlen, SL_Perc, SL_ticks) => // switch SL_type // "ATR" => SL_ATR(src, lookback, SL_atrlen) // "%" => SL_Perc(src, SL_Perc) // "fixed points" => SL_fixT(src, SL_ticks)

src = (bullishDEC or starwithE) ? high[1] : (bearishDEC or hammerwithE) ? low[1] : source

var long_loseprice = 0.0 var short_loseprice = 0.0

if SL_type == "ATR" [long_loseATR, short_loseATR] = SL_ATR(src, lookback, SL_atrlen) long_loseprice := long_loseATR short_loseprice := short_loseATR

if SL_type == "%" [long_loseperc, short_loseperc] = SL_Perc(src, SL_Perc) long_loseprice := long_loseperc short_loseprice := short_loseperc

if SL_type == "fixed ticks" [long_loseticks, short_loseticks] = SL_fixT(src, SL_ticks) long_loseprice := long_loseticks short_loseprice := short_loseticks

if bullish_signal lose_price := long_loseprice long_flag := 1 if hammer win_price := close + (close - lose_price) rr else if bullishDEC // win_price := high[1] + (high[1] - lose_price) rr win_price := close + (close - lose_price) rr else if starwithE // win_price := high[1] + (high[1] - lose_price) rr win_price := close + (close - lose_price) * rr

if bearish_signal lose_price := short_loseprice short_flag := 1 if star win_price := close - (lose_price - close) rr else if bearishDEC // win_price := low[1] - (lose_price - low[1]) rr win_price := close - (lose_price - close) rr else if hammerwithE // win_price := low[1] - (lose_price - low[1]) rr win_price := close - (lose_price - close) * rr

win_trade = (high >= win_price and long_flag == 1) or (low <= win_price and short_flag == 1) lose_trade = (low <= lose_price and long_flag == 1) or (high >= lose_price and short_flag == 1)

if win_trade or lose_trade long_flag := 0 short_flag := 0

var win_count = 0 var lose_count = 0

if win_trade win_count += 1

if lose_trade lose_count += 1

bgcolor(win_trade ? color.new(color.blue, 90) : na) bgcolor(lose_trade ? color.new(color.red, 90) : na)

plot(win_count, color = color.black, display = display.status_line) plot(lose_count, color = color.red, display = display.status_line)

alertcondition(bullish_signal, title="bullish_signal", message="多方訊號v2\n{{exchange}}:{{ticker}}\n交易週期: {{interval}}\n目前價格: {{close}}\n緩衝點數: {{plot_2}}") alertcondition(bearish_signal, title="bearish_signal", message="空方訊號v2\n{{exchange}}:{{ticker}}\n交易週期: {{interval}}\n目前價格: {{close}}\n緩衝點數: {{plot_2}}")