# 布林帶策略方向

#### 定義

“策略方向”指定策略建立什麼類型的訂單。如果設定值為0，則可以做多也可以做空。如果設置為-1，則只能做空。如果設定為1，則只能做多。當商品穿過布林帶下帶下方時，布林帶策略方向進入做多，如果商品穿過上帶上方，則進入空頭。但請記住，在策略設定中，您可以將方向調整為只做多、只做空或兩者兼具。

#### 計算

##### Pine Script
```//@version=5
strategy("Bollinger Bands Strategy directed", overlay=true)
source = close
length = input.int(20, minval=1)
mult = input.float(2.0, minval=0.001, maxval=50)
direction = input.int(0, title = "Strategy Direction", minval=-1, maxval=1)
strategy.risk.allow_entry_in(direction == 0 ? strategy.direction.all : (direction < 0 ? strategy.direction.short : strategy.direction.long))
basis = ta.sma(source, length)
dev = mult * ta.stdev(source, length)
upper = basis + dev
lower = basis - dev
if (ta.crossover(source, lower))
strategy.entry("BBandLE", strategy.long, stop=lower, oca_name="BollingerBands", oca_type=strategy.oca.cancel, comment="BBandLE")
else
strategy.cancel(id="BBandLE")
if (ta.crossunder(source, upper))
strategy.entry("BBandSE", strategy.short, stop=upper, oca_name="BollingerBands", oca_type=strategy.oca.cancel, comment="BBandSE")
else
strategy.cancel(id="BBandSE")
//plot(strategy.equity, title="equity", color=color.red, linewidth=2, style=plot.style_areabr)```