13.6 VB(Excel) 下单接口

股票自动交易--VB,VBA,Excel 下单接口

股票自动交易软件助手的 Order.dll 自动下单接口不仅仅能在大智慧,通达信,飞狐等软件公式里调用,也可以在Excel 里通过VBA(Visiual basic for Application)程序里调用,Excel 在数据统计和展示上都很强大,有些交易者喜欢用excel进行量化交易,股票自动交易助手可以帮助你实现在Excel里读取持仓数据,调用股票交易接口进行交易下单。为量化交易打下基础。


C++需要的可参考C++ 和 C#调用例子或 Python 例子。

Excel读取持仓和调用交易下单接口 运行例子下载     

下载后压缩,有两个文件,StockOrderApiSample.xlsm , Order.dll, 用 Excel 打开 StockOrderApiSample.xlsm

注意:Excel要启动宏,否则不能正常运行,确保已经安装了 VBA。假如提示 “找不到 Order.dll” ,请把 Order.dll复制到 office 的安装目录。

运行结果如下

 

点击 下单按钮 ,调出下单测试界面


 

 

假如提示 “找不到 Order.dll” ,请把 Order.dll复制到 office 的安装目录。先运行excel,再从Excel菜单打开文件,不要直接双击打开文件

请把 Order.dll复制到 office 的安装目录。

VB - Excel 自动交易下单接口调用源码

 

'一个Excel VBA 读取股票持仓并可调用交易接口自动下单的例子
'需要 Order.dll ,并 配合股票自动交易助手使用

'买入股票函数
Public Declare Function Buy Lib "Order.dll" Alias "_Buy1@20" (ByVal stkCode As String, ByVal vol As Integer, ByVal price As Single, ByVal formulaNum As Integer, ByVal ZhuShouHao As Integer) As Integer

'卖出股票函数
Public Declare Function Sell Lib "Order.dll" Alias "_Sell1@20" (ByVal stkCode As String, ByVal vol As Integer, ByVal price As Single, ByVal formulaNum As Integer, ByVal ZhuShouHao As Integer) As Integer

'根据股票代码或者股票的持仓数据
Public Declare Function GetPosInfo Lib "Order.dll" Alias "_GetPosInfo@12" (ByVal stkCode As String, ByVal nType As Integer, ByVal ZhuShouHao As Integer) As Single

'获取账户资金数据
Public Declare Function GetAccountInfo Lib "Order.dll" Alias "_GetAccountInfo@12" (ByVal stkCode As String, ByVal nType As Integer, ByVal ZhuShouHao As Integer) As Single

Private Declare Sub GetAllPositionCodeA Lib "Order.dll" Alias "_GetAllPositionCodeA@12" (ByVal lpString As String, ByVal cch As Long, ByVal ZhuShouHao As Integer)

'获取所有持仓股代码
Public Function GetAllPositionCode(ByVal ZhuShouHao As Integer) As String
    Dim lpString As String
    lpString = Space(8112)
    
    GetAllPositionCodeA lpString, 8112, ZhuShouHao
    GetAllPositionCode = lpString
End Function


Public Function ToUnicode(ByVal str As String) As String
    ToUnicode = StrConv(str, vbUnicode)
End Function


Public Function FromUnicode(ByVal str As String) As String
    FromUnicode = str ' StrConv(str, vbFromUnicode)
End Function


Public Sub RefreshPosition()
    Application.ScreenUpdating = False
    
    Dim she1 As Worksheet
    Set she1 = Sheets("Sheet1")
    she1.Range("A2:G500").Clear
    strAllCodes = GetAllPositionCode(0)
    Dim codeArray As Variant
    codeArray = Split(strAllCodes, ",")
    For i = 0 To UBound(codeArray)
        nrow = i + 2
        she1.Cells(nrow, 1).NumberFormatLocal = "@"
        she1.Cells(nrow, 1) = codeArray(i)
        
        she1.Cells(nrow, 2).NumberFormatLocal = "@"
        she1.Cells(nrow, 2) = codeArray(i)
        
        she1.Cells(nrow, 3) = GetPosInfo(ToUnicode(codeArray(i)), 0, 0)
        she1.Cells(nrow, 4) = GetPosInfo(ToUnicode(codeArray(i)), 1, 0)
        
        she1.Cells(nrow, 5) = GetPosInfo(ToUnicode(codeArray(i)), 2, 0)
        
        profit = CInt(GetPosInfo(ToUnicode(codeArray(i)), 3, 0))
        she1.Cells(nrow, 5).NumberFormat = "0.00"
        she1.Cells(nrow, 6) = profit
        
        If profit > 0 Then
            she1.Cells(nrow, 7).Interior.Color = RGB(255, 0, 0)
            she1.Cells(nrow, 6).Interior.Color = RGB(255, 0, 0)
        ElseIf profit < 0 Then
            she1.Cells(nrow, 7).Interior.Color = RGB(0, 255, 0)
            she1.Cells(nrow, 6).Interior.Color = RGB(0, 255, 0)
        Else
            she1.Cells(nrow, 7).Interior.Color = RGB(255, 255, 255)
            she1.Cells(nrow, 6).Interior.Color = RGB(255, 255, 255)
        End If
            
        'she1.Cells(nRow, 7).NumberFormatLocal = "@"
        she1.Cells(nrow, 7) = str(GetPosInfo(ToUnicode(codeArray(i)), 4, 0)) + "%"
        
        she1.Cells(nrow, 8) = GetPosInfo(ToUnicode(codeArray(i)), 5, 0)
    Next
    Application.ScreenUpdating = True
End Sub

Public Sub StartTimer()
    RefreshPosition
    Application.OnTime Now + TimeValue("00:00:05"), "StartTimer"
End Sub

Public Sub EndTimer()
    Application.OnTime Now + TimeValue("00:00:01"), "StartTimer", False
End Sub


	

支持VB开放接口也是 股票自动交易助手和机智股票自动交易软件, A计划自动交易软件,小闪客自动交易软件,SNB股票自动交易伴侣等软件的区别之一。