本文转载自微信公众号《区块链研究实验室》,作者连三丰。转载本文请联系区块链研究实验室公众号。有很多方法可以判断谁是牛市或熊市,其中之一是AdaPerspectiveIndicator的“BearsandBullsPowerIndicator”,它使用一个简单的公式来近似表示这种隐含的技术强度。今天,我们将对指标进行编码回测,然后从客观的角度进行判断。在GurayIndexMEAs数字买卖压力量和两个直方图上创建看涨和看跌力量指数,其中一个称为BullsPower,另一个称为BearsPower。直方图根据以下公式计算:EMA变量指的是指数移动平均线,这是一种移动平均线,它对最接近的值赋予更多权重。可以使用以下函数计算指数移动平均线:defma(Data,lookback,what,where):foriinrange(len(Data)):try:Data[i,where]=(Data[i-lookback+1:i+1,what].mean())exceptIndexError:passreturnDatadefema(Data,alpha,lookback,what,where):#alphaisthesmoothingfactor#windowshelookbackperiod#whatisthecolumntthatneedstohaveitsaveragecalculated#whereiswheretoputtheexponentialmovingaveragealpha=alpha/(lookback+1.0)beta=1-alpha#FirstvalueisasimpleSMAData=ma(Data,lookback,what,where)#CalculatingfirstEMAData[lookback+1,where]=(Data[lookback+1,what]*alpha)+(Data[lookback,where]*beta)#CalculatingtherestofEMAforiinrange(lookback+2,len(Data)):try:Data[i,where]=(Data[i,what]*alpha)+(Data[i-1,where]*beta)exceptIndexError:passreturnDataEURUSDinthefirstpaneland50periods的上面的牛熊指标显示了EURUSD小时数据,第二个面板有一个50周期的牛熊指标。在继续处理指标代码之前,我们必须编写两个简单的函数:defdeleter(Data,index,times):foriinrange(1,times+1):Data=np.delete(Data,index,axis=1)returnDatadefjump(Data,jump):Data=Data[jump:,]returnData现在,我们准备好编码了。请记住准备好OHLC阵列。defbull_bear_power(Data,lookback,what,high,low,where):#AddingtherequiredcolumnsData=adder(Data,3)#CalculatingtheexponentialmovinaverageData=ema(Data,2,lookback,what,where)#CalculatingtheBullPowerData[:,where+1]=数据数据[:,high]-Data[:,where]#CalculatingtheBearPowerData[:,where+2]=Data[:,where]-Data[:,low]#DeletinginitialemptyrowsData=jump(Data,lookback)returnDataUSDCHF在第一个面板上,并且是一个50周期的牛熊指标。要对上面的图进行编码,我们可以使用以下函数:defindicator_plot_double_bull_bear(Data,name='',name_ind='',window=250):fig,ax=plt.subplots(2,figsize=(10,5))Chosen=Data[-window:,]foriinrange(len(Chosen)):ax[0].vlines(x=i,ymin=Chosen[i,2],ymax=Chosen[i,1],color='黑色',linewidth=1)ax[0].grid()foriinrange(len(Chosen)):ax[1].vlines(x=i,ymin=0,ymax=Chosen[i,6],color='绿色',linewidth=1)ax[1].vlines(x=i,ymin=Chosen[i,7],ymax=0,color='red',linewidth=1)ax[1].grid()ax[1].axhline(y=0,color='black',linewidth=0.5,linestyle='--')#上面的代码考虑columns6和7分别抑制BullPower和BearPower。回测简单策略与任何适当的研究方法一样,目标是回测指标,并能够亲眼看看它是否值得添加到我们现有的交易框架中。请注意,以下仅在过去10年的10种货币对的一个时间范围内进行了回溯测试。这可能不是该策略的最佳时间框架,但我们只是想找到一种“几乎适合所有人”的一刀切策略。简化条件,我们将使用基于主观障碍的逆势方法:当BullPower指标达到-0.001且前两个值大于0.001时,做多(买入)。持仓直到收到新信号(平仓)。只要BearsPower指标达到0.001(前两个值都低于0.001),就做空(卖出)。持仓直到收到新信号(平仓)。欧元/美元信号图表。defsignal(Data,bull_power,bear_power,buy,sell):foriinrange(len(Data)):ifData[i,bull_power]
