---
# System prepended metadata

title: Tradingview

---

# TradingView Script

## BuyBuyBuy

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

//@version=5

strategy(title="BuyBuyBuy", shorttitle="BuyBuyBuy", overlay=true, initial_capital=500, currency = currency.USD, default_qty_type=strategy.cash, commission_type=strategy.commission.percent, commission_value=0.05, pyramiding=4, default_qty_value=500)

// MACD
groupTrend6 = 'MACD'
fast_length = input(title = "Fast Length", defval = 12,group = groupTrend6)
slow_length = input(title = "Slow Length", defval = 26,group = groupTrend6)
src = input(title = "Source", defval = close,group = groupTrend6)
signal_length = input.int(title = "Signal Smoothing",  minval = 1, maxval = 50, defval = 9,group = groupTrend6)
sma_source = input.string(title = "Oscillator MA Type",  defval = "EMA", options = ["SMA", "EMA"],group = groupTrend6)
sma_signal = input.string(title = "Signal Line MA Type", defval = "EMA", options = ["SMA", "EMA"],group = groupTrend6)
// Calculating
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal

// 获取 Bolinger Band
groupTrend7 = 'BB'
length = input.int(20, minval=1,group = groupTrend7)
srcbb = input(close, title="Source",group = groupTrend7)
multbb = input.float(2.0, minval=0.001, maxval=50, title="StdDev",group = groupTrend7)
basisbb = ta.sma(srcbb, length)
devbb = multbb * ta.stdev(srcbb, length)
upperbb = basisbb + devbb
lowerbb = basisbb - devbb
bbrbb = (srcbb - lowerbb)/(upperbb - lowerbb)

groupTrend9 = 'Trade'

rsi=ta.rsi(close, 14)
longRSI = input.float(22, title="longRSI",group = groupTrend9)
shortRSI = input.float(90, title="shortRSI",group = groupTrend9)
longprofit_target_ratio = 1 + input.float(5, title="longprofit_target_ratio %",group = groupTrend9) * 0.01
shortprofit_target_ratio = 1 - input.float(1, title="shortprofit_target_ratio %",group = groupTrend9) * 0.01
longstoploss_ratio = 1 - input.float(30, title="longstoploss_ratio %",group = groupTrend9) * 0.01
shortstoploss_ratio = 1 + input.float(30, title="shortstoploss_ratio %",group = groupTrend9) * 0.01
bucang_ratio = 1 - input.float(1, title="bucang_ratio %",group = groupTrend9) * 0.01

longprofit_target = strategy.position_avg_price * longprofit_target_ratio
shortprofit_target = strategy.position_avg_price * shortprofit_target_ratio
longstop_target = strategy.position_avg_price * longstoploss_ratio
shortstop_target = strategy.position_avg_price * shortstoploss_ratio

avg_cost = strategy.position_avg_price
longcondition = high >= avg_cost and rsi > 90
plot(longprofit_target, title="pt", color=color.lime)
//plot(shortprofit_target, title="lt", color=color.red)
plot(avg_cost, title="ac", color=color.yellow)
plot(longstop_target, title="lst", color=color.lime)
plot(shortstop_target, title="sst", color=color.red)

barsSinceLastEntry() =>
    strategy.opentrades > 0 ? bar_index - strategy.opentrades.entry_bar_index(strategy.opentrades - 1) : na
if ta.crossunder(rsi,longRSI) and strategy.opentrades == 0 
    strategy.entry("buy", strategy.long)
if ta.crossunder(rsi,longRSI) and low <= avg_cost*bucang_ratio //and strategy.opentrades == 1 //and barsSinceLastEntry() >=1200
    strategy.entry("buy", strategy.long)
if ta.crossunder(rsi,longRSI) and low <= avg_cost*bucang_ratio //and strategy.opentrades == 2 
    strategy.entry("buy", strategy.long)
if ta.crossunder(rsi,longRSI) and low <= avg_cost*bucang_ratio //and strategy.opentrades == 3 
    strategy.entry("buy", strategy.long)
strategy.exit("buy",stop= longstop_target)
//if ta.crossover(rsi,shortRSI) and macd > 0 and bbrbb > 1.1 and strategy.opentrades == 0 
    //strategy.entry("short", strategy.short)
//if ta.crossover(rsi,shortRSI) and macd > 0 and bbrbb > 1.1 and close >= avg_cost and strategy.opentrades == 1
    //strategy.entry("short", strategy.short)
//if ta.crossover(rsi,shortRSI) and macd > 0 and bbrbb > 1.1 and close >= avg_cost and strategy.opentrades == 2
    //strategy.entry("short", strategy.short)
//if ta.crossover(rsi,shortRSI) and macd > 0 and bbrbb > 1.1 and close >= avg_cost and strategy.opentrades == 3
    //strategy.entry("short", strategy.short)
//strategy.exit("buy",stop= shortstop_target)
if high >= longprofit_target
    strategy.close("buy")
//if close <= shortprofit_target
    //strategy.close("short")
```

### Notes

## ETH
```
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © BrandonVay

//@version=5
strategy("ETH View", overlay=true)

len1 = input.int(5, minval=1, title="EMA1")
src1 = input(close, title="Source")
offset1 = input.int(title="Offset", defval=0, minval=-500, maxval=500)
ema1 = ta.ema(src1, len1)
plot(ema1, title="EMA1", color=color.yellow, offset=offset1)

len2 = input.int(34, minval=1, title="EMA2")
src2 = input(close, title="Source")
offset2 = input.int(title="Offset", defval=0, minval=-500, maxval=500)
ema2 = ta.ema(src2, len2)
plot(ema2, title="EMA2", color=color.red, offset=offset2)

len3 = input.int(99, minval=1, title="EMA3")
src3 = input(close, title="Source")
offset3 = input.int(title="Offset", defval=0, minval=-500, maxval=500)
ema3 = ta.ema(src3, len3)
plot(ema3, title="EMA", color=color.blue, offset=offset3)

len4 = input.int(200, minval=1, title="EMA4")
src4 = input(close, title="Source")
offset4 = input.int(title="Offset", defval=0, minval=-500, maxval=500)
ema4 = ta.ema(src4, len4)
plot(ema4, title="EMA", color=color.lime, offset=offset4)

len5 = input.int(400, minval=1, title="EMA_Special")
src5 = input(close, title="Source")
offset5 = input.int(title="Offset", defval=0, minval=-500, maxval=500)
ema5 = ta.ema(src5, len5)
plot(ema5, title="EMA_Special", color=color.purple, offset=offset5)

// 获取 BB

//indicator(title = "Bollinger Bands %B", shorttitle = "BB %B", format=format.price, precision=2, timeframe="", timeframe_gaps=true)
length = input.int(20, minval=1)
srcbb = input(close, title="Source")
multbb = input.float(2.0, minval=0.001, maxval=50, title="StdDev")
basisbb = ta.sma(srcbb, length)
devbb = multbb * ta.stdev(srcbb, length)
upperbb = basisbb + devbb
lowerbb = basisbb - devbb
bbrbb = (srcbb - lowerbb)/(upperbb - lowerbb)
//plot(bbrbb, "Bollinger Bands %B", color=#26A69A)
//band1 = hline(1, "Overbought", color=#787B86, linestyle=hline.style_dashed)
//hline(0.5, "Middle Band", color=color.new(#787B86, 50))
//band0 = hline(0, "Oversold", color=#787B86, linestyle=hline.style_dashed)
//fill(band1, band0, color=color.rgb(38, 166, 154, 90), title="Background")

// Inputs {
theme = input.string('Light','Color Theme',options=['Dark','Light'])
groupEMA = 'Moving Averages'
length1 = input.int(5, 'MA 1', group = groupEMA)
length2 = input.int(20, 'MA 2', group = groupEMA)
length3 = input.int(40,'MA 3', group = groupEMA)
length4 = input.int(60,'MA 4', group = groupEMA)
length5 = input.int(80,'MA 5', group = groupEMA)
length6 = input.int(100,'MA 6', group = groupEMA)
length7 = input.int(120,'MA 7', group = groupEMA)
length8 = input.int(140,'MA 8', group = groupEMA)
length9 = input.int(160,'MA 9', group = groupEMA)
length10 = input.int(180,'MA 10', group = groupEMA)
length11 = input.int(200,'MA 11 (long term)', group = groupEMA)

groupTrend = 'Trend'
rocLength = input.int(14,'ROC length (Trend sensitivity)', group = groupTrend)
BullsignalPercentage = input.int(9,'Reversal sensitivity (1 to 9)',minval = 1,maxval = 9, group = groupTrend)
BearsignalPercentage = input.int(9,'Reversal sensitivity (1 to 9)',minval = 1,maxval = 9, group = groupTrend)
//}

ma1 = ta.sma(close,length1)
ma2 = ta.sma(close,length2)
ma3 = ta.sma(close,length3)
ma4 = ta.sma(close,length4)
ma5 = ta.sma(close,length5)
ma6 = ta.sma(close,length6)
ma7 = ta.sma(close,length7)
ma8 = ta.sma(close,length8)
ma9 = ta.sma(close,length9)
ma10 = ta.sma(close,length10)
ma11 = ta.sma(close,length11)

roc1 = ta.roc(ma1,rocLength)
roc2 = ta.roc(ma2,rocLength)
roc3 = ta.roc(ma3,rocLength)
roc4 = ta.roc(ma4,rocLength)
roc5 = ta.roc(ma5,rocLength)
roc6 = ta.roc(ma6,rocLength)
roc7 = ta.roc(ma7,rocLength)
roc8 = ta.roc(ma8,rocLength)
roc9 = ta.roc(ma9,rocLength)
roc10 = ta.roc(ma10,rocLength)
roc11 = ta.roc(ma11,rocLength)
rocsp = ta.roc(ema3,rocLength)

bull = ta.crossover(roc11,0)
bear = ta.crossunder(roc11,0)

ma_type = input.string(title="MA Type", defval="VWMA", options=["EMA", "SMA", "SWMA", "VWMA", "WMA"])
ma_period = input.int(title="MA Period (Length)", defval=34, minval=1)
ma_period_smoothing = input.int(title="MA Period smoothing (Length)", defval=10, minval=1)

color_positive = input(title="Positive color (Bullish)", defval=color.new(#26A69A, 50) )
color_negative = input(title="Negative color (Bearish)", defval=color.new(#EF5350, 50) )
color_hl = input(title="High & Low cloud color", defval=color.new(#808080, 80) )

show_line = input(title="Show (lines)", defval=false)
show_hl_cloud = input(title="Show (High & Low cloud)", defval=true)
show_oc_cloud = input(title="Show (Open & Close cloud)", defval=true)
rsi = ta.rsi(close, 14)
LengthMA = input.int(20, minval=1)
LengthEMA = input.int(20, minval=1)
xMA = ta.sma(close, LengthMA)
xEMA = ta.ema(xMA, LengthEMA)
//plot(xMA)
//plot(xEMA)
TP1Perc = input.float(title="Long Take Profit (%)", minval=0.0, step=0.1, defval=5, group="TP & SL")
TP2Perc = input.float(title="Long Take Profit (%)", minval=0.0, step=0.1, defval=10, group="TP & SL")
SLPerc = input.float(title="Long Stop Loss (%)", minval=0.0, step=0.1, defval=2, group="TP & SL")
percentAsPoints(pcnt) => strategy.position_size != 0 ? math.round(pcnt / 100.0 * strategy.position_avg_price / syminfo.mintick) : float(na)
percentAsPrice(pcnt) => strategy.position_size != 0 ? ((pcnt / 100.0) + 1.0) * strategy.position_avg_price : float(na)
TP1 = strategy.position_avg_price + percentAsPoints(TP1Perc) * syminfo.mintick * strategy.position_size / math.abs(strategy.position_size)
TP2 = strategy.position_avg_price + percentAsPoints(TP2Perc) * syminfo.mintick * strategy.position_size / math.abs(strategy.position_size)
SL = strategy.position_avg_price - percentAsPoints(SLPerc) * syminfo.mintick * strategy.position_size / math.abs(strategy.position_size)
entry_condition = close == SL
if roc11 >= 0 and rsi >= 50 and xMA > xEMA
    strategy.entry("Long", strategy.long)
if entry_condition and rsi >= 50
    strategy.entry("Long2", strategy.long)
//strategy.exit("TP1", from_entry="Long", qty=strategy.position_size * 0.2, limit=TP1)
//strategy.exit("TP2", from_entry="Long", qty=strategy.position_size * 0.2, limit=TP2)
if roc11 <= 0
    strategy.close("Long")
    strategy.close("Long2")
rocDisplay = table.new(position.top_right, 2, 11)
table.cell(rocDisplay, 0, 0, "roc1", text_color = color.green)
table.cell(rocDisplay, 0, 1, "roc2", text_color = color.green)
table.cell(rocDisplay, 0, 2, "roc3", text_color = color.green)
table.cell(rocDisplay, 0, 3, "roc4", text_color = color.green)
table.cell(rocDisplay, 0, 4, "roc5", text_color = color.green)
table.cell(rocDisplay, 0, 5, "roc6", text_color = color.green)
table.cell(rocDisplay, 0, 6, "roc7", text_color = color.green)
table.cell(rocDisplay, 0, 7, "roc8", text_color = color.green)
table.cell(rocDisplay, 0, 8, "roc9", text_color = color.green)
table.cell(rocDisplay, 0, 9, "roc10", text_color = color.green)
table.cell(rocDisplay, 0, 10, "roc11", text_color = color.green)
table.cell(rocDisplay, 1, 0, str.tostring(roc1), text_color = color.green)
table.cell(rocDisplay, 1, 1, str.tostring(roc2), text_color = color.green)
table.cell(rocDisplay, 1, 2, str.tostring(roc3), text_color = color.green)
table.cell(rocDisplay, 1, 3, str.tostring(roc4), text_color = color.green)
table.cell(rocDisplay, 1, 4, str.tostring(roc5), text_color = color.green)
table.cell(rocDisplay, 1, 5, str.tostring(roc6), text_color = color.green)
table.cell(rocDisplay, 1, 6, str.tostring(roc7), text_color = color.green)
table.cell(rocDisplay, 1, 7, str.tostring(roc8), text_color = color.green)
table.cell(rocDisplay, 1, 8, str.tostring(roc9), text_color = color.green)
table.cell(rocDisplay, 1, 9, str.tostring(roc10), text_color = color.green)
table.cell(rocDisplay, 1, 10, str.tostring(roc11), text_color = color.green)


```