5.11 Fisher Transform · Using Fisher Transform Indicator

来源:https://uqer.io/community/share/54b5c288f9f06c276f651a16

策略思路:

在技术分析中,很多时候,人们都把股价数据当作正态分布的数据来分析。但是,其实股价数据分布并不符合正态分布。Fisher Transformation是一个可以把股价数据变为类似于正态分布的方法。

Fisher Transformation将市场数据的走势平滑化,去掉了一些尖锐的短期振荡;利用今日和前一日该指标的交错可以给出交易信号;

例如,对于沪深300指数使用Fisher变换的结果见本文后面的具体讨论。

Fisher Transformation

  • 定义今日中间价:
  1. mid=(low+high)/2
  • 确定计算周期,例如可使用10日为周期。计算周期内最高价和最低价:
  1. lowestLow=周期内最低价, highestHigh=周期内最高价
  • 定义价变参数(其中的ratio为0-1之间常数,例如可取0.5或0.33):

5.11 Fisher Transform · Using Fisher Transform Indicator - 图1

  • 对价变参数x使用Fisher变换,得到Fisher指标:

5.11 Fisher Transform · Using Fisher Transform Indicator - 图2

  1. import quartz
  2. import quartz.backtest as qb
  3. import quartz.performance as qp
  4. from quartz.api import *
  5. import pandas as pd
  6. import numpy as np
  7. from datetime import datetime
  8. from matplotlib import pylab
  1. start = datetime(2014, 1, 1) # 回测起始时间
  2. end = datetime(2014, 12, 10) # 回测结束时间
  3. benchmark = 'HS300' # 使用沪深 300 作为参考标准
  4. universe = set_universe('SH50') # 股票池
  5. capital_base = 100000 # 起始资金
  6. refresh_rate = 1
  7. window = 10
  8. # 本策略对于window非常非常敏感!!!
  9. histFish = pd.DataFrame(0.0, index = universe, columns = ['preDiff', 'preFish', 'preState'])
  10. def initialize(account): # 初始化虚拟账户状态
  11. account.amount = 10000
  12. account.universe = universe
  13. add_history('hist', window)
  14. def handle_data(account): # 每个交易日的买入卖出指令
  15. for stk in account.universe:
  16. prices = account.hist[stk]
  17. if prices is None:
  18. return
  19. preDiff = histFish.at[stk, 'preDiff']
  20. preFish = histFish.at[stk, 'preFish']
  21. preState = histFish.at[stk, 'preState']
  22. diff, fish = FisherTransIndicator(prices, preDiff, preFish)
  23. if fish > preFish:
  24. state = 1
  25. elif fish < preFish:
  26. state = -1
  27. else:
  28. state = 0
  29. if state == 1 and preState == -1:
  30. #stkAmount = int(account.amount / prices.iloc[-1]['openPrice'])
  31. order(stk, account.amount)
  32. elif state == -1 and preState == 1:
  33. order_to(stk, 0)
  34. histFish.at[stk, 'preDiff'] = diff
  35. histFish.at[stk, 'preFish'] = fish
  36. histFish.at[stk, 'preState'] = state
  37. def FisherTransIndicator(windowData, preDiff, preFish):
  38. # This function calculate the Fisher Transform indicator based on the data
  39. # in the windowData.
  40. minLowPrice = min(windowData['lowPrice'])
  41. maxHghPrice = max(windowData['highPrice'])
  42. tdyMidPrice = (windowData.iloc[-1]['lowPrice'] + windowData.iloc[-1]['highPrice'])/2.0
  43. diffRatio = 0.33
  44. # 本策略对于diffRatio同样非常敏感!!!
  45. diff = (tdyMidPrice - minLowPrice)/(maxHghPrice - minLowPrice) - 0.5
  46. diff = 2 * diff
  47. diff = diffRatio * diff + (1.0 - diffRatio) * preDiff
  48. if diff > 0.99:
  49. diff = 0.999
  50. elif diff < -0.99:
  51. diff = -0.999
  52. fish = np.log((1.0 + diff)/(1.0 - diff))
  53. fish = 0.5 * fish + 0.5 * fish
  54. return diff, fish

5.11 Fisher Transform · Using Fisher Transform Indicator - 图3

沪深300指数上使用Fisher Transformation

  • 对最近半年的沪深300进行Fisher变换,得到的指标能够比较温和准确反映出指数的变化
  1. from CAL.PyCAL import *
  2. # DataAPI.MktIdxdGet返回pandas.DataFrame格式
  3. index = DataAPI.MktIdxdGet(indexID = "000001.ZICN", beginDate = "20140501", endDate = "20140901")
  1. index.head()
indexIDtradeDatetickersecShortNameexchangeCDpreCloseIndexopenIndexlowestIndexhighestIndexcloseIndexturnoverVolturnoverValueCHGCHGPct
0000001.ZICN2014-05-051上证综指XSHG2026.3582022.1782007.3512028.9572027.3537993339500600934877360.9950.00049
1000001.ZICN2014-05-061上证综指XSHG2027.3532024.2562021.4852038.7052028.0387460941100575481108500.6850.00034
2000001.ZICN2014-05-071上证综指XSHG2028.0382023.1522008.4512024.6312010.083743601920057558051925-17.955-0.00885
3000001.ZICN2014-05-081上证综指XSHG2010.0832006.8532005.6852036.9412015.2747786539300595293655465.1910.00258
4000001.ZICN2014-05-091上证综指XSHG2015.2742016.5012001.3002020.4542011.135762242440057505383717-4.139-0.00205
  1. def FisherTransIndicator(windowData, preDiff, preFish, state):
  2. # This function calculate the Fisher Transform indicator based on the data
  3. # in the windowData.
  4. minLowPrice = min(windowData['lowestIndex'])
  5. maxHghPrice = max(windowData['highestIndex'])
  6. tdyMidPrice = (windowData.iloc[-1]['lowestIndex'] + windowData.iloc[-1]['highestIndex'])/2.0
  7. diffRatio = 0.5
  8. diff = (tdyMidPrice - minLowPrice)/(maxHghPrice - minLowPrice) - 0.5
  9. diff = 2 * diff
  10. if state == 1:
  11. diff = diffRatio * diff + (1 - diffRatio) * preDiff
  12. if diff > 0.995:
  13. diff = 0.999
  14. elif diff < -0.995:
  15. diff = -0.999
  16. fish = np.log((1 + diff)/(1 - diff))
  17. if state == 1:
  18. fish = 0.5 * fish + 0.5 * fish
  19. return diff, fish
  1. window = 10
  2. index['diff'] = 0.0
  3. index['fish'] = 0.0
  4. index['preFish'] = 0.0
  5. for i in range(window, index.shape[0]):
  6. windowData = index.iloc[i-window : i]
  7. if i == window:
  8. diff, fish = FisherTransIndicator(windowData, 0, 0, 1)
  9. index.at[i,'preFish'] = 0
  10. index.at[i,'diff'] = diff
  11. index.at[i,'fish'] = fish
  12. else:
  13. preDiff = index.iloc[i-1]['diff']
  14. preFish = index.iloc[i-1]['fish']
  15. diff, fish = FisherTransIndicator(windowData, preDiff, preFish, 1)
  16. index.at[i,'preFish'] = preFish
  17. index.at[i,'diff'] = diff
  18. index.at[i,'fish'] = fish
  19. Plot(index, settings = {'x':'tradeDate','y':'closeIndex', 'title':u'沪深300指数历史收盘价'})
  20. Plot(index, settings = {'x':'tradeDate','y':['fish', 'preFish'], 'title':u'沪深300指数Fisher Transform Indicator'})

5.11 Fisher Transform · Using Fisher Transform Indicator - 图4

5.11 Fisher Transform · Using Fisher Transform Indicator - 图5

  • 上图中的蓝色曲线表示Fisher指标,绿色曲线表示前一日的Fisher指标,两个指标的交错可以给出沪深300指数涨跌情况的信号