资源加载中... loading...

Python股票测试代码(注释版)

Author: 扫地僧, Created: 2017-04-05 10:06:54, Updated: 2017-04-05 11:58:28

Python股票测试代码(注释版)

  • 源码

def main():
    while not exchange.IO("status"):                  # 获取与交易所连接状态,如果返回 False 则一直在 while 循环执行
        Sleep(1000)                                   # 暂停1秒
    Sleep(1000)
    Log(_C(exchange.SetContractType, "600577.SH"))    # 获取 600577.SH 股票代码的  详细信息
    while not exchange.IO("status"):                  # 获取 连接状态
        Sleep(1000)
    exchange.SetDirection("buy")                      # 设置 为开多仓
    ticker = exchange.GetTicker()                     # 获取行情
    if ticker:                                        # 如果行情返回值 不为 null 
        exchange.Buy(ticker.Sell+10, 1)               # 下买单
        Sleep(1000)
        Log(exchange.GetPosition())                   # 获取持仓信息,并显示出来。
    exchange.SetDirection("closebuy")                 # 设置 平多仓
    ticker = exchange.GetTicker()                     # 获取行情信息
    if ticker:                                        # 如果获取到行情,即 不为 null
        exchange.Sell(ticker.Buy - 1, 1)              # 下平仓单
    Sleep(1000)
    Log(exchange.GetPosition())                       # 打印持仓信息
    Log(exchange.GetAccount())                        # 打印账户信息
    Log(exchange.GetRawJSON())                        # 打印调用获取账户信息函数时返回的 详细信息
    while True:                                       # while 循环
        ticker = exchange.GetTicker()                 # 获取行情
        if ticker:
            LogStatus(ticker.Last, exchange.GetPosition())  # 在状态栏显示 行情 和 持仓信息

img

  • SetContractType 函数返回的数据, 参数是: 600577.SH

{
    u'LongMarginRatio': 1, 
    u'Area': u'\u5b89\u5fbd',                        # 安徽 
    u'Profit': 72.45, 
    u'TimeToMarket': 20020911, 
    u'Totals': 19.55, 
    u'Pb': 4.12, 
    u'Pe': 53.7, 
    u'Gpr': 11.25, 
    u'InstrumentName': u'\u7cbe\u8fbe\u80a1\u4efd',  # 精达股份
    u'ShortMarginRatio': 1, 
    u'Esp': 0.115, 
    u'Undp': 79225.46, 
    u'Perundp': 0.41, 
    u'VolumeMultiple': 100, 
    u'TotalAssets': 490508.31, 
    u'CreateDate': u'20020911', 
    u'Npr': 2.75, 
    u'MaxLimitOrderVolume': 10000, 
    u'ExchangeID': u'SSE', 
    u'Name': u'\u7cbe\u8fbe\u80a1\u4efd',            # 精达股份
    u'ReservedPerShare': 0.01, 
    u'Rev': 5.14, 
    u'Industry': u'\u7535\u6c14\u8bbe\u5907',        # 电气设备
    u'FixedAssets': 88981.38, 
    u'Bvps': 1.49, 
    u'Holders': 143799, 
    u'LiquidAssets': 325462.25, 
    u'Code': u'600577', 
    u'Reserved': 2283.4, 
    u'MinBuyVolume': 1, 
    u'Outstanding': 14.19, 
    u'PriceTick': 0.01, 
    u'OpenDate': u'20020911'
}
  • GetRawJSON 函数返回的 调用 GetAccount 时的详细信息

{
    "SpecProductFrozenCommission": 0, 
    "Mortgage": 0, 
    "WithdrawQuota": 0, 
    "SpecProductCloseProfit": 0, 
    "SpecProductCommission": 0, 
    "AccountID": "0001", 
    "FundMortgageAvailable": 0, 
    "InterestBase": 0, 
    "FrozenMargin": 0, 
    "PreMargin": 0, 
    "SettlementID": 0, 
    "SpecProductFrozenMargin": 0, 
    "Credit": 0, 
    "BrokerID": "9999", 
    "FrozenCash": 0.0, 
    "Withdraw": 0, 
    "Balance": 999997.921, 
    "ExchangeMargin": 693.0, 
    "ReserveBalance": 0, 
    "SpecProductExchangeMargin": 0, 
    "CurrMargin": 693.0, 
    "FrozenCommission": 0, 
    "CloseProfit": 0.0, 
    "PreFundMortgageOut": 0, 
    "FundMortgageOut": 0, 
    "ExchangeDeliveryMargin": 0, 
    "PreFundMortgageIn": 0, 
    "PositionProfit": -3.767999999999979, 
    "Commission": 2.079, 
    "Interest": 0, 
    "CashIn": 0, 
    "Available": 999304.921, 
    "PreCredit": 0, 
    "PreMortgage": 0, 
    "MortgageableFund": 0, 
    "FundMortgageIn": 0, 
    "DeliveryMargin": 0, 
    "Deposit": 0, 
    "SpecProductMargin": 0, 
    "SpecProductPositionProfitByAlg": 0, 
    "Reserve": 0, 
    "PreDeposit": 0, 
    "TradingDay": "2017-03-09", 
    "CurrencyID": "CNY", 
    "SpecProductPositionProfit": 0, 
    "PreBalance": 1000000.0
}
  • Log 时 字典中 Unicode 中文编码显示 \u7cbe\u8fbe\u80a1\u4efd 的问题

    由于Python 在字典中储存中文为 Unicode 编码,显示出来时都是 \u7cbe\u8fbe\u80a1\u4efd 很不直观!以下是一个小技巧方便显示为中文。

    在代码开头写上:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
...
...
# 在需要显示的地方这样转换
Log(json.dumps(Dict).decode("unicode-escape"))    # Dict 即要显示的包含Unicode中文字符的字典

img

该问题解决的参考帖子:http://wangye.org/blog/archives/629/


更多内容