Combo Backtest 123 Reversal & Signal To Noise

HPotter · strategy · 65 行 · 点赞 208 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=4
////////////////////////////////////////////////////////////
//  Copyright by HPotter v1.0 196/01/2021
// This is combo strategies for get a cumulative signal. 
//
// First strategy
// This System was created from the Book "How I Tripled My Money In The 
// Futures Market" by Ulf Jensen, Page 183. This is reverse type of strategies.
// The strategy buys at market, if close price is higher than the previous close 
// during 2 days and the meaning of 9-days Stochastic Slow Oscillator is lower than 50. 
// The strategy sells at market, if close price is lower than the previous close price 
// during 2 days and the meaning of 9-days Stochastic Fast Oscillator is higher than 50.
//
// Second strategy
// The signal-to-noise (S/N) ratio. 
// And Simple Moving Average.
//
// WARNING:
// - For purpose educate only
// - This script to change bars colors.
////////////////////////////////////////////////////////////
Reversal123(Length, KSmoothing, DLength, Level) =>
    vFast = sma(stoch(close, high, low, Length), KSmoothing) 
    vSlow = sma(vFast, DLength)
    pos = 0.0
    pos := iff(close[2] < close[1] and close > close[1] and vFast < vSlow and vFast > Level, 1,
	         iff(close[2] > close[1] and close < close[1] and vFast > vSlow and vFast < Level, -1, nz(pos[1], 0))) 
	pos
 
SignalToNoise(length) =>
    StN = 0.0
    for i = 1 to length-1
        StN := StN + (1/close[i])/length
    StN := -10*log(StN)
 
StN(length,Smooth) =>
    pos = 0.0
    StN = SignalToNoise(length)
    SMAStN = sma(StN, Smooth)
    pos := iff(SMAStN[0] > StN[0] , -1,
    	     iff(SMAStN[0] < StN[0], 1, 0)) 
    pos
 
strategy(title="Combo Backtest 123 Reversal & Signal To Noise", shorttitle="Combo", overlay = true)
Length = input(14, minval=1)
KSmoothing = input(1, minval=1)
DLength = input(3, minval=1)
Level = input(50, minval=1)
//-------------------------
lengthStN = input(title="Days", type=input.integer, defval=21, minval=2)
SmoothStN =  input(title="Smooth", type=input.integer, defval=29, minval=2)
reverse = input(false, title="Trade reverse")
posReversal123 = Reversal123(Length, KSmoothing, DLength, Level)
posStN = StN(lengthStN,SmoothStN)
pos = iff(posReversal123 == 1 and posStN == 1 , 1,
	   iff(posReversal123 == -1 and posStN == -1, -1, 0)) 
possig = iff(reverse and pos == 1, -1,
          iff(reverse and pos == -1 , 1, pos))	   
if (possig == 1) 
    strategy.entry("Long", strategy.long)
if (possig == -1)
    strategy.entry("Short", strategy.short)	 
if (possig == 0) 
    strategy.close_all()
barcolor(possig == -1 ? #b50404: possig == 1 ? #079605 : #0536b3 )

← 返回列表