找回密碼
 註冊
搜索
查看: 10851|回復: 7

集成Amibroker與Python COM創建複雜的交易系統

[複製鏈接]
發表於 14-4-6 15:04 | 顯示全部樓層 |閱讀模式
本帖最後由 IBM2012 於 14-4-6 16:39 編輯

創建複雜的交易系統,我們需要有一個複雜的數學模型但amibroker設計高度複雜的數學模型也不是那麼容易。但是我們仍然可以Python幫助建立複雜的數學模型。Python是類似C,C + +的語言,它具有第三方模塊對金融電腦相連,並實施統計模型。一些第三方模塊NumPy,SciPy使用來構建複雜的模型。
所以,如果我們能夠整合Amibroker與Python那麼很可能它就像創建一個對Amibroker複雜的數學模型。
COM 用於使能在大範圍的編程語言的進程間通信和動態對象創建。使用Python的COM,我們可以創造一個Amibroker傳遞變量和複雜 ​​的計算,用Python編程的幫助下完成Amibroker和Python之間的接口。與Amibroker OLE對象進行進程間通信,如下圖所示Python的COM服務器通信。


安裝Python
1)下載和安裝Python2.7
2)設置環境變量,現在轉到開始 - > 我的電腦 - >右鍵單擊並選擇屬性 - > 高級設置 - >轉到高級選項,並在系統變量輸入變量名:PYTHONPATH和變量值
C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;


在這裡,由AFL的代碼上集成Amibroker與Python使用IIR濾波器的例子。無限脈衝響應(IIR)是應用在電子濾波器最常用的多線性時不變系統。
要執行的步驟
1)下載Python-AFL設置並將其解壓縮
2)複製文件iir.py到\ \ python2.7 \ \ BIN \\ folder中。並用命令執行該文件的python iir.py如下圖所示
3)複製的Python AFL.afl文件,粘貼文件在\\Amibroker\\Formulas\\Basic Charts Folder
4)打開一個新的空白圖表和應用的Python AFL。你應該可以看到IIR(無限脈衝響應)和DEMA線,其中IIR被計算與Python的COM,如下圖所示



  1. import pythoncom
  2. from win32com.server.exception import COMException
  3. import win32com.server.util
  4. import win32com.client.dynamic
  5. import win32gui
  6. import random


  7. class PythonUtilities(object):
  8.     _public_methods_=['IIR2']

  9.     #
  10.     # Use pythoncom.CreateGuid() 2/1/08
  11.     #               
  12.     _reg_clsid_="{25F32489-9E84-4D9F-8AEB-44DC9B528405}"
  13.     _reg_desc_ =   "Utilities for Extending Amibroker AFL"
  14.     _reg_progid_ = "PyAFL"
  15.     _readonly_attrs_ = ['revision']

  16.    # class attributes can be defined and used to transfer values from AFL

  17.     _public_attrs_=['NDepVar','ArrayLen','ProbArray',
  18.                     'X_Open','X_High','X_Low','X_Close',
  19.                     'X_Volume','X_OI','Log']

  20.     Log=""  # Log may be a  string use to pass text to AFL
  21.    
  22.     def IIR2(self,InList,coef0,coef1,coef2):
  23.         # example infinite impulse response filter from Amibroker docs
  24.         #
  25.         self.revision="version 1"
  26.         OutList=[]
  27. ##
  28. ##        print type(InList),type(coef0)
  29. ##        print coef0,coef1,coef2
  30. ##        
  31.         InList=list(InList)
  32.         
  33.         if len(InList)==0:
  34.             return OutList
  35.         OutList=InList[0:2]
  36.         for index in range(2,len(InList)):
  37.            
  38. OutList.append(coef0*InList[index]+coef1*InList[index-1]+coef2*InList[index-2])
  39.         if OutList is None:
  40.             return
  41.         else:
  42.             return OutList

  43.         
  44.    
  45.         
  46. if __name__=='__main__':
  47.     print "Registering COM Server "
  48.     import win32com.server.register
  49.     win32com.server.register.UseCommandLine(PythonUtilities)

  50. # uncomment out if you want to unregister com server

  51. ##    from win32com.server.register import UnregisterServer
  52. ##    UnregisterServer(PythonUtilities._reg_clsid_)
  53. ##    print "Com server unregistered."   

  54. #############################################End Python Code
  55. ####################################


  56. //----------------------------------------------------------------------------------------Start
  57. AFL Code
  58. --------------------------------------------------------------------------

  59. MyObj=CreateObject("PyAFL");  // link to python com server

  60. Coef0=Param("Coef0",0.2,-5,5);
  61. Coef1=Param("Ceof1",1.2,-5,5);
  62. Coef2=Param("Coef2",-0.4,-5);

  63. // COM object interface cannot deal with null value, set to zero to be sure

  64. Close=Nz(Close);

  65. // use IIR2 method in com server to calculate smoothed value

  66. SmValue = MyObj.IIR2(Close,Coef0,Coef1,Coef2);


  67. Plot(C,"Close",Color=colorBlue,style=styleLine);
  68. Plot(SmValue,"IIR2 smooth",Color=colorDarkRed,style=styleLine);


  69. _SECTION_BEGIN("DEMA");
  70. P = ParamField("Price field",-1);
  71. Periods = Param("Periods", 15, 2, 200, 1, 10 );
  72. Plot( DEMA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ),
  73. ParamStyle("Style") );
  74. _SECTION_END();
複製代碼

評分

參與人數 4金錢 +13 收起 理由
勞倫斯 + 4
Fesimi + 2 感謝分享
heavenweaver + 2 太強了
kilroy + 5 感謝分享

查看全部評分

發表於 14-4-8 13:04 | 顯示全部樓層
本帖最後由 heavenweaver 於 14-4-8 13:07 編輯

iirpy.jpg
Python-AFL code
Python-AFL.zip (1.62 KB, 下載次數: 786)
回復

使用道具 舉報

發表於 14-4-8 21:00 | 顯示全部樓層
本帖最後由 joshsmi 於 14-4-8 21:01 編輯

@IBM2012, that code you have posted is not your one! FYI, it is from the AmiBroker User Library provided by Bruce Peterson. BTW this "... 但amibroker設計高度複雜的數學模型也不是那麼容易。" is rather nonsense.
回復

使用道具 舉報

發表於 14-4-8 21:58 | 顯示全部樓層
如果複雜的數學能夠賺錢那數學家不就都是億萬富翁?
回復

使用道具 舉報

發表於 14-4-8 21:58 | 顯示全部樓層
真是強阿~~~~
回復

使用道具 舉報

發表於 15-3-11 12:41 | 顯示全部樓層
以python的速度... 不要把AB的速度搞慢了喲
回復

使用道具 舉報

發表於 25-2-15 21:41 | 顯示全部樓層
謝謝!
「嘗試成功自古無!」 - 陸放翁
「自古成功在嘗試!」 - 胡適
不試怎知效果如何?謝謝。
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則


Archiver|手機版|站長信箱|廣告洽詢|COCO研究院

GMT+8, 25-9-3 07:42

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回復 返回頂部 返回列表