---
# System prepended metadata

title: 221026 政大區研社

---

# 221026 政大區研社

```
//@version=5
indicator(title="Moving Average Convergence Divergence", shorttitle="MACD", timeframe="", timeframe_gaps=true)
// Getting inputs
fast_length = input(title="Fast Length", defval=12)
slow_length = input(title="Slow Length", defval=26)
src = input(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 9)
sma_source = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])
// Plot colors
col_macd = input(#2962FF, "MACD Line  ", group="Color Settings", inline="MACD")
col_signal = input(#FF6D00, "Signal Line  ", group="Color Settings", inline="Signal")
col_grow_above = input(#26A69A, "Above   Grow", group="Histogram", inline="Above")
col_fall_above = input(#B2DFDB, "Fall", group="Histogram", inline="Above")
col_grow_below = input(#FFCDD2, "Below Grow", group="Histogram", inline="Below")
col_fall_below = input(#FF5252, "Fall", group="Histogram", inline="Below")
// 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
hline(0, "Zero Line", color=color.new(#787B86, 50))
plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))
plot(macd, title="MACD", color=col_macd)
plot(signal, title="Signal", color=col_signal)



```

### 改成 Strategy

將

```
indicator(title="Moving Average Convergence Divergence", shorttitle="MACD", timeframe="", timeframe_gaps=true)
```

改成
```
strategy(title="Moving Average Convergence Divergence", shorttitle="MACD")
```

最下面加上
```
if ta.crossover(macd, signal)
    strategy.entry("Long1", strategy.long, comment="MACD X 做多")
    strategy.exit("Close Long1","Long1",stop=strategy.position_avg_price*0.98,trail_price=strategy.position_avg_price*1.05,trail_offset=strategy.position_avg_price*0.02,comment="做多 止盈 5% / 止損2%")
if ta.crossover(signal, macd)
    strategy.entry("Short1", strategy.short, comment="MACD X 做空")
    strategy.exit("Close Short1","Short1",stop=strategy.position_avg_price*1.02,trail_price=strategy.position_avg_price*0.95,trail_offset=strategy.position_avg_price*0.02,comment="做空 止盈 5% / 止損2%")

```



### 加上 alert
最下方加上

```
alertcondition(ta.crossover(macd, signal), title="開多")
alertcondition(ta.crossover(signal, macd), title="開空")
```