Python:如何制作标准大小为6的struct.calcsize整数?

时间:2015-09-26 20:52:49

标签: python binary quantitative-finance algorithmic-trading

如果我看这里:https://docs.python.org/2/library/struct.html第7.3.2.2节。格式字符,对于标准大小为6的Python类型整数没有格式字母。我尝试过'6p'甚至'3H',但这不适用于:

import struct
struct.calcsize('!6p')

很抱歉,如果这听起来很愚蠢......我正在学习如何首次将字符串转换为压缩二进制数据,以获得长度为6且整数为值的字符串消息。那么为整数获得标准大小6的正确字母格式是什么?

更新

我的任务是转换NASDAQ TotalVIEW-ITCH 5.0 specification document, here

中包含的二进制消息

例如,如果您查看第4.1节:系统事件消息(页3),我会将类型为“S”的消息转换为:

class SystemEventMessage(Message): 
    type = 'S'
    description = "System Event Message"
    message_size = struct.calcsize("!HH6pc") + 1
def __init__(self, message):
    (self.locate,self.tracking,self.timestamp, self.code) = struct.unpack("!HH6pc", message[1:])

def pack(self):
    return struct.pack("!cHH6pc", self.type,self.locate,self.tracking, self.timestamp, self.code)

我收到此错误:

struct.error: unpack requires a string argument of length 11

所以我假设我的错误与'6p'有关,因为我找不到任何标准大小为6的整数。

更新2

因此,对于文档第4.6节:网络订单不平衡指标(NOII)消息中的以下消息类型“I”,我使用以下建议使用6s而不是6p / strong>,我这样做:

class NoiiMessage(ITCH41MarketMessage):
    type = 'I'
    description = "NOII Message"
    message_size = struct.calcsize("!HH6sQQc8sIIIcc") + 1

    def __init__(self, message):
        (self.locate,self.tracking,self.timestamp, self.pairedShares, self.imbalance,
         self.imbalanceDirection, self.stock, self.farPrice, self.nearPrice,
         self.currentRefPrice, self.crossType, self.priceVariationbsindicator
         ) = struct.unpack("!HH6sQQc8sIIIcc", message[1:])

    def pack(self):
        return struct.pack("!cHH6sQQc8sIIIcc", self.type,self.locate,
                           self.tracking, self.timestamp,
                           self.pairedShares, self.imbalance,
                           self.imbalanceDirection, self.stock,
                           self.farPrice, self.nearPrice,
                           self.currentRefPrice, self.crossType,
                           self.priceVariationbsindicator)

我收到此错误:

struct.error: unpack requires a string argument of length 49

现在这很奇怪,因为!HH6sQQc8sIIIcc长度为49 ......

每个人都感谢你的帮助!

2 个答案:

答案 0 :(得分:3)

struct旨在处理C结构。这就是为什么它只有库存C类型。 “6字节整数”不是库存C类型 - 您不能写struct s { int6 timestamp; }之类的东西来立即获得可用的整数。这就是为什么它在这里不能像这样工作的原因。

那么,您如何解决C中的问题?你可能会

  • unsigned char ts_data[6];
  • 将值复制到其他地方
  • 填充它和
  • 将结果解释为整数

现在,我们所要做的就是在Python中表达同样的事情:

>>> struct.pack('q',1324)
',\x05\x00\x00\x00\x00\x00\x00'    #my arch is big-endian

>>> struct.unpack('q',',\x05\x00\x00\x00\x00')
error: unpack requires a string argument of length 8

>>> struct.unpack('6s',',\x05\x00\x00\x00\x00')
(',\x05\x00\x00\x00\x00',)

>>> s=_[0]
>>> struct.unpack('q',s+'\x00'*2)    #check byte order to find out which side to pad from
(1324,)

答案 1 :(得分:2)

<强> NASDAQ TotalView-ITCH 5.0

[S0099123456Q]________________________________ wireline SEQ _____________
 | | |     ||
 | | |     |+---------------[[ Event Code   ]]
 | | |     +----------------[[ Timestamp ns ]]
 | | +----------------------[[ Tracking NUM ]]
 | +------------------------[[ Stock Locate ]]
 +--------------------------[[ Message Type ]]

Name           | Offset | Length | Value   | Notes
---------------|--------|--------|---------|-------------------------------------------------------------
Message Type   | 0      | 1      | “S”     | System Event Message.
Stock Locate   | 1      | 2      | Integer | == 0 Always
Tracking Number| 3      | 2      | Integer | NASDAQ OMX internal tracking number
Timestamp      | 5      | 6      | Integer | Nanoseconds since midnight.
Event Code     |11      | 1      | Alpha   | == { 0 | S | Q | M | E | C } See System Event Codes below.

为了得到NASDAQ_Timestamp
使用掩码中的 uchar 6B char[] 6s - s >
只有在消费者方面需要时才将它们后加工成int() 延迟延迟取消阻止目标引擎线缆性能。

+也喜欢Python 7.3.2.1。一旦使用“!”注意BigEndian /网络订购掩模前缀

>>> struct.pack(   ">c2H6sc", "S", 0, 99, "123456", "Q" )
'S\x00\x00\x00c123456Q'

>>> struct.unpack( ">c2H6sc", "S\x00\x00\x00c123456Q" )
('S', 0, 99, '123456', 'Q')
  |   |   |        |    |
  |   |   |        |    +---------------[[ Event Code   ]]
  |   |   |        +--------------------[[ Timestamp ns ]]
  |   |   +-----------------------------[[ Tracking NUM ]]
  |   +---------------------------------[[ Stock Locate ]]
  +-------------------------------------[[ Message Type ]]

广告 UPDATE2

中的另一个问题
"!HH6sQQc8sIIIcc"                              _
 +||-||||-||||||----------------------------1 |_|_ "!"   a "Network"-Byte-order
  +|-||||-||||||----------------------------2 |_|   H as a 2 Byte unsigned short
   | |||| ||||||                            3 |_|_ 
   +-||||-||||||----------------------------4-|_|   H as a 2 Byte unsigned short                                
     |||| ||||||                            5 |_|_ 
     +|||-||||||----------------------------6-|_|  6s as a 6 Byte char[]
      ||| ||||||                            7 |_|  
      ||| ||||||                            8 |_|  
      ||| ||||||                            9 |_|  
      ||| ||||||                           10 |_|  
      ||| ||||||                            1 |_|_ 
      +||-||||||--------------------------- 2-|_|   Q as a 8 Byte unsigned long long
       || ||||||                            3 |_|  
       || ||||||                            4 |_|  
       || ||||||                            5 |_|  
       || ||||||                            6 |_|  
       || ||||||                            7 |_|  
       || ||||||                            8 |_|  
       || ||||||                            9 |_|_ 
       +|-||||||---------------------------20-|_|   Q as a 8 Byte unsigned long long
        | ||||||                            1 |_|  
        | ||||||                            2 |_|  
        | ||||||                            3 |_|  
        | ||||||                            4 |_|  
        | ||||||                            5 |_|  
        | ||||||                            6 |_|  
        | ||||||                            7 |_|_ 
        +-||||||----------------------------8-|_|_  c as a 1 Byte char
          +|||||----------------------------9-|_|  8s as a 8 Byte char[]
           |||||                           30 |_|  
           |||||                            1 |_|  
           |||||                            2 |_|  
           |||||                            3 |_|  
           |||||                            4 |_|  
           |||||                            5 |_|  
           |||||                            6 |_|_ 
           +||||----------------------------7-|_|   I as a 4 Byte unsigned int
            ||||                            8 |_|                                     
            ||||                            9 |_|                                     
            ||||                           40 |_|_ 
            +|||----------------------------1-|_|   I as a 4 Byte unsigned int
             |||                            2 |_|                             
             |||                            3 |_|                             
             |||                            4 |_|_ 
             +||----------------------------5-|_|   I as a 4 Byte unsigned int
              ||                            6 |_|                             
              ||                            7 |_|                             
              ||                            8 |_|_ 
              +|----------------------------9-|_|_  c as a 1 Byte char
               +---------------------------50-|_|_  c as a 1 Byte char

其中:

Format  | C-type               | Python-type        | Standard size
========|======================|====================|===============
     x  | pad byte             | no value           |     
     c  | char                 | string of length 1 | 1 
     b  | signed char          | integer            | 1 
     B  | unsigned char        | integer            | 1 
     ?  | _Bool                | bool               | 1 
     h  | short                | integer            | 2 
     H  | unsigned short       | integer            | 2 
     i  | int                  | integer            | 4 
     I  | unsigned int         | integer            | 4 
     l  | long                 | integer            | 4 
     L  | unsigned long        | integer            | 4 
     q  | long long            | integer            | 8 
     Q  | unsigned long long   | integer            | 8 
     f  | float                | float              | 4 
     d  | double               | float              | 8 
     s  | char[]               | string             | 
     p  | char[]               | string             |  
     P  | void *               | integer            |