码迷,mamicode.com
首页 > 编程语言 > 详细

python类(4)——自己造第一个轮子

时间:2018-01-25 13:05:32      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:function   pos   通过   register   init   har   +=   pca   demo   

先做简单版本,再一步步增加功能

1、简单目的:要实现这样一个功能,能够连接服务器,登录账号,查询账号委托信息,如果有委托信息,撤销委托。

属性(不同账户之间差别):账户,密码

方法(不同账户之间都要用到功能):连接服务器、登录账号、查询委托信息、撤销委托

问题1、如何在类方法中写类方法?A类有方法a、b,如何在方法b下面使用方法a呢?

class A:
    def a(self):
        print(abcd)
    def b(self):
        self.a()
yq=A()
yq.b()   #abcd

可见在方法前加入self即可。

脚本文件一:

import py_t2sdk
class StockTrade:
    def __init__(self,fund_account,password,connect=None):
        self.fund_account=fund_account
        self.password=password
        self.connect=connect
    def StockEntrustPack(self):
        pStockEntrustPack = py_t2sdk.pyIF2Packer()
        pStockEntrustPack.BeginPack()
        pStockEntrustPack.AddField("op_entrust_way", C, 1)
        pStockEntrustPack.AddField("op_station", S, 255)
        pStockEntrustPack.AddField("client_id", S, 15)
        pStockEntrustPack.AddField("input_content", C, 1)
        pStockEntrustPack.AddField("account_content", S, 30)
        pStockEntrustPack.AddField("fund_content", S, 30)
        pStockEntrustPack.AddField("fund_account", S, 11)
        pStockEntrustPack.AddField("stock_code", S, 6)
        pStockEntrustPack.AddField("entrust_amount", I, 10)
        pStockEntrustPack.AddField("entrust_price", F, 10, 3)
        pStockEntrustPack.AddField("entrust_bs", S, 1)
        pStockEntrustPack.AddField("entrust_prop", S, 3)
        pStockEntrustPack.AddField("content_type", S, 6)
        pStockEntrustPack.AddField("password", S, 10)
        pStockEntrustPack.AddField("password_type", C)
        pStockEntrustPack.AddField("exchange_type", S, 4)
        pStockEntrustPack.AddStr(Z)  # op_entrust_way
        pStockEntrustPack.AddStr(123456)  # op_station
        pStockEntrustPack.AddStr(10080156)  # client_id
        pStockEntrustPack.AddStr(1)  # input_content
        pStockEntrustPack.AddStr(10080156)  # account_content
        pStockEntrustPack.AddStr(10080156)  # fund_content
        pStockEntrustPack.AddStr(self.fund_account)  # fund_account
        pStockEntrustPack.AddStr(601398)  # stock_code
        pStockEntrustPack.AddStr(100)  # entrust_amount
        pStockEntrustPack.AddStr(7.50)  # entrust_price
        pStockEntrustPack.AddStr(1)  # 1买入 2 卖出    #entrust_bs
        pStockEntrustPack.AddStr(0)  # entrust_prop
        pStockEntrustPack.AddStr(0)  # content_type
        pStockEntrustPack.AddStr(self.password)  # password
        pStockEntrustPack.AddStr(2)  # password_type
        pStockEntrustPack.AddStr(1)  # exchange_type  0资金 1上海 2深圳
        pStockEntrustPack.EndPack()
        return pStockEntrustPack

    def ConnectServer(self):
        config = py_t2sdk.pyCConfigInterface()
        config.Load(t2sdk.ini)
        self.connect = py_t2sdk.pyConnectionInterface(config)
        pCallBack = py_t2sdk.pyCallbackInterface(pyCallBack, pyCallBack)
        pCallBack.InitInstance()
        ret = self.connect.Create2BizMsg(pCallBack)
        # ret = connect.Create(pCallBack)
        if ret != 0:
            print(creat faild!!)
            exit()
        ret = self.connect.Connect(9000)
        if ret != 0:
            print(connect faild:)
            ErrMsg = self.connect.GetErrorMsg(ret)
            print(ErrMsg)
            exit()
        print(YES)

    def Login(self):
        # pLoginPack = GetLoginPack()
        # pAccountPack = GetAccountPack()
        pLoginPack = self.StockEntrustPack()
        pBizMsg = py_t2sdk.pyIBizMessage()
        # pBizMsg.SetFunction(10001)
        pBizMsg.SetFunction(332255)
        # pBizMsg.SetFunction(333002)
        iPacklen = pLoginPack.GetPackLen()
        iPackBuff = pLoginPack.GetPackBuf()
        pBizMsg.SetPacketType(0)
        pBizMsg.SetContent(iPackBuff, iPacklen)
        # ret = connect.SendBiz(10001,pLoginPack,1)
        ret = self.connect.SendBizMsg(pBizMsg, 1)
        print(ret)

    def QueryEntrust(self):
        pass

    def CheckEntrust(self):
        pass

    def QueryAccountCash(self):
        pass

    def GetEntrustInfo(self):
        pass

    def StockEntrust(self):
        pass

    def StockTrade(self):
        pass

#报错 注册超时。

解决:应该是连接多了,第一次连接上了,继续第二次再连接会报注册超时,连接一次就好了,不能多连。此外,应该说如果连接断了,t2sdk会自动连接上去。

#问题:没有触发回调

解决:因为主程序在触发回调之前进程结束了,可以在主程序后面加上sleep(100)

 

2、增加功能:

原来:要实现这样一个功能,能够连接服务器,登录账号,查询账号委托信息,如果有委托信息,撤销委托。

新增:查询账户资金、查询账户持仓

思考:1、我看登录账号不是必须流程,连接服务器后可以直接查询账号信息、或查询持仓等操作(不用先登录账号)

可以直接查询,这个可以在后台设置。

2、有很多功能之间的差别在于功能号不同,可以设置不同功能号为类中方法的参数,一个类方法即可解决。

3、原来想用字典来存储数据,估计不行。因为有些数据是重复的,但是字典只保留一个。

因为字典中键值对是一一对应的。

技术分享图片

下面是字典保留信息:
{‘init_date‘: ‘20180124‘, ‘fund_account‘: ‘10080156‘, ‘batch_no‘: ‘700011‘, ‘entrust_no‘: ‘700011‘, ‘exchange_type‘: ‘1‘, ‘stock_account‘: ‘S010080156‘, ‘stock_code‘: ‘601398‘, ‘entrust_bs‘: ‘1‘, ‘entrust_price‘: ‘7.380‘, ‘entrust_amount‘: ‘100.00‘, ‘business_amount‘: ‘0.00‘, ‘business_price‘: ‘0.000‘, ‘report_no‘: ‘700011‘, ‘report_time‘: ‘155330‘, ‘entrust_type‘: ‘0‘, ‘entrust_status‘: ‘2‘, ‘entrust_time‘: ‘155330‘, ‘entrust_date‘: ‘20180124‘, ‘entrust_prop‘: ‘0‘, ‘stock_name‘: ‘工商银行‘, ‘position_str‘: ‘000000004‘, ‘entrust_price2‘: ‘7.380‘, ‘cancel_info‘: ‘‘, ‘entrust_way‘: ‘7‘, ‘business_balance‘: ‘0.00‘, ‘withdraw_amount‘: ‘0.00‘, ‘curr_milltime‘: ‘155330129‘, ‘report_milltime‘: ‘155330135‘, ‘order_id‘: ‘700011‘, ‘orig_order_id‘: ‘700011‘}
------------------------------------------------------------

认真研究后发现可以通过字典加DataFrame格式来保存数据,现在有个问题如下:多结果集?该如何处理?

技术分享图片

咨询后:现在这边产品暂时没有多结果集,恒生资管部门那边会用到。

4、存储在DataFrame中打印不出来,不知道是不是设置缓存不够原因?

 

5、在交易类中写解包功能函数,用来存储服务器返回数据,是否可以在回调函数中使用主函数中的交易类的函数?

不这样做呢?还有哪些方法?我现在的问题是主函数如何获得回调函数中返回数据?以前是在回调函数中直接打印出来,没有传到主函数。

 

6、运行时回调函数没有打印出DataFrame,什么原因?

代码一:在回调函数中打印

# coding=utf-8
import py_t2sdk
import pandas as pd
from pandas import Series,DataFrame

def PrintUnpack(lpUnpack):
    iDataSetCount = lpUnpack.GetDatasetCount()
    index = 0
    while index < iDataSetCount :
        lpUnpack.SetCurrentDatasetByIndex(index)
        iRowCount = lpUnpack.GetRowCount()
        RowIndex = 0
        while RowIndex  < iRowCount:
            iColCount = lpUnpack.GetColCount()
            iColIndex = 0
            while iColIndex < iColCount:
                ColType = lpUnpack.GetColType(iColIndex)
                if ColType == S:
                    print(lpUnpack.GetColName(iColIndex) + : + lpUnpack.GetStrByIndex(iColIndex))
                elif ColType == I:
                    print(lpUnpack.GetColName(iColIndex) + : + str(lpUnpack.GetIntByIndex(iColIndex)))
                elif ColType == C:
                    print(lpUnpack.GetColName(iColIndex) + : + lpUnpack.GetCharByIndex(iColIndex))
                elif ColType == F:
                    print(lpUnpack.GetColName(iColIndex) + : + str(lpUnpack.GetDoubleByIndex(iColIndex)))
                iColIndex += 1
            lpUnpack.Next()
            RowIndex += 1
        index += 1

def StoreUnpack(lpUnpack):
    iDataSetCount = lpUnpack.GetDatasetCount();print(iDataSetCount:,iDataSetCount)
    index = 0
    while index < iDataSetCount :
        print(index:,index)
        StoreUnpackDataFrame=DataFrame()
        lpUnpack.SetCurrentDatasetByIndex(index)
        iRowCount = lpUnpack.GetRowCount();print(iRowCount:,iRowCount)
        RowIndex = 0
        while RowIndex  < iRowCount:
            print(RowIndex:,RowIndex)
            StoreUnpack = {}  #存放一列的数据
            iColCount = lpUnpack.GetColCount();print(iColCount:,iColCount)
            iColIndex = 0
            while iColIndex < iColCount:
                print(iColIndex:,iColCount)
                ColType = lpUnpack.GetColType(iColIndex)
                if ColType == S:
                    print(lpUnpack.GetColName(iColIndex) + : + lpUnpack.GetStrByIndex(iColIndex))
                    StoreUnpack.update({lpUnpack.GetColName(iColIndex):lpUnpack.GetStrByIndex(iColIndex)})
                elif ColType == I:
                    print(lpUnpack.GetColName(iColIndex) + : + str(lpUnpack.GetIntByIndex(iColIndex)))
                    StoreUnpack.update({lpUnpack.GetColName(iColIndex): lpUnpack.GetStrByIndex(iColIndex)})
                elif ColType == C:
                    print(lpUnpack.GetColName(iColIndex) + : + lpUnpack.GetCharByIndex(iColIndex))
                    StoreUnpack.update({lpUnpack.GetColName(iColIndex): lpUnpack.GetStrByIndex(iColIndex)})
                elif ColType == F:
                    print(lpUnpack.GetColName(iColIndex) + : + str(lpUnpack.GetDoubleByIndex(iColIndex)))
                    StoreUnpack.update({lpUnpack.GetColName(iColIndex): lpUnpack.GetStrByIndex(iColIndex)})
                iColIndex += 1
            StoreUnpackRow = DataFrame(StoreUnpack, index=RowIndex)
            StoreUnpackDataFrame=pd.concat((StoreUnpackDataFrame,StoreUnpackRow))
            lpUnpack.Next()
            RowIndex += 1
        index += 1
    print(StoreUnpackDataFrame)
    return StoreUnpackDataFrame

class pyCallBack:
  def __init__(self):
    print(init)
  def OnConnect(self):
    print(OnConnect)
  def OnSafeConnect(self):
    print(OnSafeConnect)
  def OnRegister(self):
    print(OnRegister)
  def OnSent(self):
    print(OnSent)
  def OnClose(self):
    print(OnClose)
  def OnReceivedBiz(self,hResult,hSend,sBuff,iLenght):
    print(OnReceivedBiz)
    if hResult == 0 or hResult == 1:
      lppUnpack = py_t2sdk.pyIF2UnPacker()
      lppUnpack.SetPackBuff(sBuff,iLenght)
      PrintUnpack(lppUnpack)
    else:
      print(sBuff)
  def OnReceivedBizEx(self,hResult,hSend,sBuff,iLenght):
    print(OnReceivedBizEx)
    if hResult == 0 or hResult == 1:
      lppUnpack = py_t2sdk.pyIF2UnPacker()
      lppUnpack.SetPackBuff(sBuff,iLenght)
      PrintUnpack(lppUnpack)
    else:
      print(sBuff)    
  def OnReceivedBizMsg(self,hResult,hSend,sBuff,iLenght):
    print(OnReceivedBizMsg)
    if hResult == 0 or hResult == 1:
      lpBizMsg = py_t2sdk.pyIBizMessage()
      lpBizMsg.SetBuff(sBuff,iLenght)
      print(funcid:,lpBizMsg.GetFunction())
      lppUnpack = py_t2sdk.pyIF2UnPacker()
      iPackBuff,iPackLen = lpBizMsg.GetContent()
      lppUnpack.SetPackBuff(iPackBuff,iPackLen)
      #PrintUnpack(lppUnpack)
      StoreUnpack(lppUnpack)
      print(------------------------------------------------------------)
      #StoreUnpack=StoreUnpack(lppUnpack)
      # print(‘StoreUnpack:‘,StoreUnpack)
    else:
      print(sBuff)  

D:\Anaconda3\python.exe E:/UFXAPIForPython0119/demo/strategy_test.py
init
OnSafeConnect!
OnRegister!
OnConnect
OnSafeConnect
OnRegister
YES
1
OnReceivedBizMsg!

Process finished with exit code 0

代码二:在主函数中打印

# def PrintUnpack(lpUnpack):
    #     iDataSetCount = lpUnpack.GetDatasetCount()
    #     index = 0
    #     while index < iDataSetCount:
    #         lpUnpack.SetCurrentDatasetByIndex(index)
    #         iRowCount = lpUnpack.GetRowCount()
    #         RowIndex = 0
    #         while RowIndex < iRowCount:
    #             StoreUnpack = {}  # 存放一列的数据
    #             iColCount = lpUnpack.GetColCount()
    #             iColIndex = 0
    #             while iColIndex < iColCount:
    #                 ColType = lpUnpack.GetColType(iColIndex)
    #                 if ColType == ‘S‘:
    #                     print(lpUnpack.GetColName(iColIndex) + ‘:‘ + lpUnpack.GetStrByIndex(iColIndex))
    #                     StoreUnpack.update({lpUnpack.GetColName(iColIndex): lpUnpack.GetStrByIndex(iColIndex)})
    #                 elif ColType == ‘I‘:
    #                     print(lpUnpack.GetColName(iColIndex) + ‘:‘ + str(lpUnpack.GetIntByIndex(iColIndex)))
    #                     StoreUnpack.update({lpUnpack.GetColName(iColIndex): lpUnpack.GetStrByIndex(iColIndex)})
    #                 elif ColType == ‘C‘:
    #                     print(lpUnpack.GetColName(iColIndex) + ‘:‘ + lpUnpack.GetCharByIndex(iColIndex))
    #                     StoreUnpack.update({lpUnpack.GetColName(iColIndex): lpUnpack.GetStrByIndex(iColIndex)})
    #                 elif ColType == ‘F‘:
    #                     print(lpUnpack.GetColName(iColIndex) + ‘:‘ + str(lpUnpack.GetDoubleByIndex(iColIndex)))
    #                     StoreUnpack.update({lpUnpack.GetColName(iColIndex): lpUnpack.GetStrByIndex(iColIndex)})
    #                 iColIndex += 1
    #             print(StoreUnpack)
    #             lpUnpack.Next()
    #             RowIndex += 1
    #         index += 1

D:\Anaconda3\python.exe E:/UFXAPIForPython0119/demo/strategy_test.py
init
OnSafeConnect!
OnRegister!
OnConnect
OnSafeConnect
OnRegister
YES
1
OnReceivedBizMsg!
OnReceivedBizMsg
funcid: 333101
init unpacker,591
iDataSetCount: 1
index: 0
iRowCount: 0
Empty DataFrame
Columns: []
Index: []
------------------------------------------------------------

Process finished with exit code 0

这两个是什么原因造成的?

 

python类(4)——自己造第一个轮子

标签:function   pos   通过   register   init   har   +=   pca   demo   

原文地址:https://www.cnblogs.com/bawu/p/8350613.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!