指標變策略,快速回測! 代碼及課件
## 指標快速回測 代碼
>相關的視頻學習資料在大陸版抖音,tiktok,blibli,油管,上搜索'量化金城武'即可找到
- **pine V4版本 指標變策略代碼(固定止盈止損)**
```
strategy(......)
//首先修改腳本屬性
// 开仓信号
if (做漲條件)
strategy.entry("Long", strategy.long)
if (做空條件)
strategy.entry("Short", strategy.short)
// 固定止盈止損設置
stop_loss_pct = input(title="止損 (%)", type=input.float, defval=2.0, step=0.1)
take_profit_pct = input(title="止盈 (%)", type=input.float, defval=2.0, step=0.1)
long_stop_loss = strategy.position_avg_price*(1-stop_loss_pct/100)
long_take_profit = strategy.position_avg_price*(1+take_profit_pct/100)
short_stop_loss = strategy.position_avg_price*(1+stop_loss_pct/100)
short_take_profit = strategy.position_avg_price*(1-take_profit_pct/100)
strategy.exit("Exit Long", "Long", stop=long_stop_loss, limit=long_take_profit)
strategy.exit("Exit Short", "Short", stop=short_stop_loss, limit=short_take_profit)
```
### pine V5版本指標變策略代碼(固定止盈止損)
```
strategy(......)
//首先修改腳本屬性
//止盈止損設置
take_profit_pct = input.float(defval = 2.0,title = '止盈', step=0.1)
stop_loss_pct = input.float(defval = 1.0,title = '止損', step=0.1)
//设定出场条件,算出買漲時候的止盈止損的價格
long_stop_loss = strategy.position_avg_price*(1-stop_loss_pct/100)
long_take_profit = strategy.position_avg_price*(1+take_profit_pct/100)
//當做空的時候,止盈止損的價格是不一樣的
short_stop_loss = strategy.position_avg_price*(1+stop_loss_pct/100)
short_take_profit = strategy.position_avg_price*(1-take_profit_pct/100)
//第八步逻辑执行
if 做漲條件
strategy.entry('long',strategy.long,comment = '做多') //進場做多
strategy.exit('exit','long',limit = long_take_profit, stop = long_stop_loss, comment_profit = '盈',comment_loss = '损') //設定做多的止盈止損
if 做空條件
strategy.entry('short',strategy.short,comment = '做空') //進場做空
strategy.exit('exit','short',limit = short_take_profit, stop = short_stop_loss, comment_profit = '盈',comment_loss = '损') //設定做空的止盈止損
```
2 条评论