该策略基于质数震荡指标来判断市场趋势,并据此构建长短仓位。质数震荡指标计算价格附近的最近质数与价格的差值,正值表示多头趋势,负值表示空头趋势。该策略可以捕捉价格震荡时隐藏的趋势信息,对于突破交易有指导意义。
该策略首先定义一个PrimeNumberOscillator函数,传入参数为价格和allowedPercent。该函数会在价格正负allowedPercent的范围内寻找最接近价格的质数,并返回两者的差值。差值大于0表示多头趋势,小于0表示空头趋势。
然后在策略中,调用PrimeNumberOscillator函数计算xPNO值。根据xPNO正负判断仓位方向,乘以reverseFactor来确定最终的交易方向。根据交易方向开仓做多做空。
该策略主要依赖质数震荡指标判断趋势方向。指标本身比较粗糙,需要结合其他因素来验证交易信号。但它基于数学原理,可以提供一定的客观指引。
该策略基于质数震荡原理判断趋势方向,实现简单,逻辑清晰。但质数震荡本身存在一定局限,需谨慎使用。可以通过組合其他技术指标来验证信号,控制交易风险。该策略为数学交易策略的典型代表,对于学习和研究具有一定参考价值。
/*backtest start: 2023-10-02 00:00:00 end: 2023-11-01 00:00:00 period: 1h basePeriod: 15m exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=2 //////////////////////////////////////////////////////////// // Copyright by HPotter v1.0 29/03/2018 // Determining market trends has become a science even though a high number or people // still believe it’s a gambling game. Mathematicians, technicians, brokers and investors // have worked together in developing quite several indicators to help them better understand // and forecast market movements. // // Developed by Modulus Financial Engineering Inc., the prime number oscillator indicates the // nearest prime number, be it at the top or the bottom of the series, and outlines the // difference between that prime number and the respective series. // // You can change long to short in the Input Settings // WARNING: // - For purpose educate only // - This script to change bars colors. //////////////////////////////////////////////////////////// PrimeNumberOscillator(price, percent) => res = 0 res1 = 0 res2 = 0 for j = price to price + (price * percent / 100) res1 := j for i = 2 to sqrt(price) res1 := iff(j % i == 0 , 0, j) if res1 == 0 break if res1 > 0 break for j = price to price - (price * percent / 100) res2 := j for i = 2 to sqrt(price) res2 := iff(j % i == 0 , 0, j) if res2 == 0 break if res2 > 0 break res := iff(res1 - price < price - res2, res1 - price, res2 - price) res := iff(res == 0, res[1], res) res strategy(title="Prime Number Oscillator Backtest") percent = input(5, minval=0.01, step = 0.01, title="Tolerance Percentage") reverse = input(false, title="Trade reverse") xPNO = PrimeNumberOscillator(close, percent) pos = iff(xPNO > 0, 1, iff(xPNO < 0, -1, nz(pos[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) barcolor(possig == -1 ? red: possig == 1 ? green : blue ) clr = iff(xPNO > 0, green, red) p1 = plot(xPNO, color=blue, title="KPO") p2 = plot(0, color=black, title="0") fill(p1,p2,color=clr)