回测测试
/*backtest start: 2021-12-01 09:00:00 end: 2022-06-14 15:00:00 period: 1h basePeriod: 15m exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}] args: [["ContractType","rb888",360008]] */ // © RafaelZioni //@version=4 strategy(title = "Linear trend", overlay = true) // c = input(close, title="数据源") len = input(12, minval=1, title="周期"),off= 0,dev= input(4, "偏离") lreg = linreg(c, len, off), lreg_x =linreg(c, len, off+1) b = bar_index, s = lreg - lreg_x,intr = lreg - b*s dS = 0.0 for i=0 to len-1 dS:= dS + pow(c[i]-(s*(b-i)+intr), 2) de = sqrt(dS/(len)) up = (-de*dev) + lreg down= (de*dev) + lreg up_t = 0.0 up_t := c[1] > up_t[1] ? max(up, up_t[1]) : up down_t = 0.0 down_t := c[1] < down_t[1] ? min(down, down_t[1]) : down trend = 0 trend := c > down_t[1] ? 1: c < up_t[1] ? -1 : nz(trend[1], 1) // r_line = trend ==1 ? up_t : down_t plot(r_line) buy=crossover( c, r_line) sell=crossunder(c, r_line) plotshape(buy, style=shape.triangleup, size=size.normal, location=location.belowbar, color=color.lime) plotshape(sell, style=shape.triangledown, size=size.normal, location=location.abovebar, color=color.red) /////// Alerts ///// alertcondition(buy,title="buy") alertcondition(sell,title="sell") if buy strategy.entry("long", strategy.long) else if sell strategy.entry("short", strategy.short)