策略
Pair-trading-strategy
ChaoZhang · PineScript · 均值回归 · 2023-09-10 00:17:24
无说明:源文件没有中文正文。
参数
| 参数 | 默认值 | 说明 |
|---|---|---|
| v_input_1 | 20 | SMA Length |
| v_input_2 | 2 | Entry ZScore |
| v_input_3 | 2016 | Backtest Start Year |
源码(39 行)
/*backtest
start: 2022-09-03 00:00:00
end: 2023-09-09 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © femisapien
//@version=4
strategy("Pair Trade L/S", overlay=true)
source = close
smalength = input(title = "SMA Length", defval = 20, minval=1)
entryzscore = input(title = "Entry ZScore", defval = 2.0, minval=0, maxval = 50)
startYear = input(title="Backtest Start Year", type=input.integer, defval=2016, minval=1980, maxval=2100)
ma = sma(source, smalength)
dev = entryzscore * stdev(source, smalength)
upper = ma + dev
lower = ma - dev
longEntrySignal = cross(source, lower)
shortEntrySignal = cross(source, upper)
exitSignal = cross(source, ma)
afterStartDate = (time >= timestamp(syminfo.timezone, startYear,1,1, 0, 0))
if (longEntrySignal and afterStartDate)
strategy.entry("le", strategy.long, comment = "Enter Long")
if (shortEntrySignal and afterStartDate)
strategy.entry("se", strategy.short, comment="Enter Short")
if (exitSignal and afterStartDate)
strategy.close_all(true)