|
本帖最後由 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,如下圖所示
- import pythoncom
- from win32com.server.exception import COMException
- import win32com.server.util
- import win32com.client.dynamic
- import win32gui
- import random
- class PythonUtilities(object):
- _public_methods_=['IIR2']
- #
- # Use pythoncom.CreateGuid() 2/1/08
- #
- _reg_clsid_="{25F32489-9E84-4D9F-8AEB-44DC9B528405}"
- _reg_desc_ = "Utilities for Extending Amibroker AFL"
- _reg_progid_ = "PyAFL"
- _readonly_attrs_ = ['revision']
- # class attributes can be defined and used to transfer values from AFL
- _public_attrs_=['NDepVar','ArrayLen','ProbArray',
- 'X_Open','X_High','X_Low','X_Close',
- 'X_Volume','X_OI','Log']
- Log="" # Log may be a string use to pass text to AFL
-
- def IIR2(self,InList,coef0,coef1,coef2):
- # example infinite impulse response filter from Amibroker docs
- #
- self.revision="version 1"
- OutList=[]
- ##
- ## print type(InList),type(coef0)
- ## print coef0,coef1,coef2
- ##
- InList=list(InList)
-
- if len(InList)==0:
- return OutList
- OutList=InList[0:2]
- for index in range(2,len(InList)):
-
- OutList.append(coef0*InList[index]+coef1*InList[index-1]+coef2*InList[index-2])
- if OutList is None:
- return
- else:
- return OutList
-
-
-
- if __name__=='__main__':
- print "Registering COM Server "
- import win32com.server.register
- win32com.server.register.UseCommandLine(PythonUtilities)
- # uncomment out if you want to unregister com server
- ## from win32com.server.register import UnregisterServer
- ## UnregisterServer(PythonUtilities._reg_clsid_)
- ## print "Com server unregistered."
- #############################################End Python Code
- ####################################
- //----------------------------------------------------------------------------------------Start
- AFL Code
- --------------------------------------------------------------------------
- MyObj=CreateObject("PyAFL"); // link to python com server
- Coef0=Param("Coef0",0.2,-5,5);
- Coef1=Param("Ceof1",1.2,-5,5);
- Coef2=Param("Coef2",-0.4,-5);
- // COM object interface cannot deal with null value, set to zero to be sure
- Close=Nz(Close);
- // use IIR2 method in com server to calculate smoothed value
- SmValue = MyObj.IIR2(Close,Coef0,Coef1,Coef2);
- Plot(C,"Close",Color=colorBlue,style=styleLine);
- Plot(SmValue,"IIR2 smooth",Color=colorDarkRed,style=styleLine);
- _SECTION_BEGIN("DEMA");
- P = ParamField("Price field",-1);
- Periods = Param("Periods", 15, 2, 200, 1, 10 );
- Plot( DEMA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ),
- ParamStyle("Style") );
- _SECTION_END();
複製代碼
|
評分
-
查看全部評分
|