无法执行期权交易

2024-10-03 06:31:35 发布

您现在位置:Python中文网/ 问答频道 /正文

placeOrder命令不发送期权交易的订单

我试图提交股票订单,但它通过了,但没有选择

# -*- coding: utf-8 -*-
"""
Created on Wed Jul 24 21:58:32 2019

@author: Alex Oraibi
"""

import time
from time import localtime, strftime
from ib.ext.Contract import Contract
from ib.ext.Order import Order
from ib.opt import ibConnection, message
import gspread
from oauth2client.service_account import ServiceAccountCredentials
def Google() :   
    json_key = 'G:\IBridgeby\spreadsheet.json'
    scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']

    credentials = ServiceAccountCredentials.from_json_keyfile_name(json_key, scope)

    gc = gspread.authorize(credentials)
    wks = gc.open("Test").sheet1
    return (wks)
def error_handler(msg):
    """Handles the capturing of error messages"""
    print("Server Error: %s" % msg)

def reply_handler(msg):
    """Handles of server replies"""
    print("Server Response: %s, %s" % (msg.typeName, msg))
def makeStkContract(a,c):
    newStkContract = Contract()
    newStkContract.m_symbol = a
    newStkContract.m_secType = "OPT"
    newStkContract.m_strike = c
    newStkContract.m_exchange = "SMART"
    newStkContract.m_currency = "USD"
    return newStkContract

def makeOptContract(a, b, e, c):
    newOptContract = Contract()
    newOptContract.m_symbol = a
    newOptContract.m_secType = "OPT"
    newOptContract.m_right = e
    newOptContract.m_expiry = b
    newOptContract.m_strike = float(c)
    newOptContract.m_exchange = "SMART"
    newOptContract.m_currency = "USD"
    # newOptContract.m_localSymbol = ''
    # newOptContract.m_primaryExch = ''
    return newOptContract
def makeOptOrder(action, orderID, f):
    newOptOrder = Order()
    newOptOrder.m_orderId = orderID
    newOptOrder.m_permid = 0
    newOptOrder.m_action = 'BUY'
    newOptOrder.m_lmtPrice = f
    newOptOrder.m_auxPrice = 0
    newOptOrder.m_tif = 'DAY'
    newOptOrder.m_transmit = False
    newOptOrder.m_orderType = 'LMT'
    newOptOrder.m_totalQuantity = 1
    newOptOrder.m_multiplier = "100"
    return newOptOrder
if __name__ == "__main__":
    tws_conn = ibConnection("127.0.0.1", port=749)
    tws_conn.connect()

    order_id = 1000
    Exec_time = strftime("%H", localtime())
    tickID = 36
    while Exec_time != '13': 
        Exec_time = strftime("%H", localtime())
        if Exec_time == '13':
            break
        else:
            hup = Google()
            tic = hup.cell(2,15).value
            stri = hup.cell(2,4).value
            exp = hup.cell(2,3).value
            rig = hup.cell(2,13).value
            lim = hup.cell(2,6).value
            if tic == '#VALUE!' :
                tic =''
            stkContract = makeStkContract(tic,stri)
            optContract = makeOptContract(tic, exp, rig, stri)
            print(stkContract, optContract)
            tws_conn.reqMktData(1, stkContract, "", "")
            tws_conn.reqMktData(tickID, optContract, "", "")
            optOrder = makeOptOrder("BUY", order_id, lim)
            print(tws_conn.reqMktData(tic, optContract, "", True))
            tws_conn.placeOrder(order_id, optContract, optOrder)
            order_id += 1
            time.sleep(10)
            continue
            tws_conn.disconnect()

我正在从在线excel表格中加载交易建议,并将订单提交给TWS。我已经成功地从我的谷歌工作表中加载了信息,并将代码用于订购安全性,但执行部分没有通过。我认为我的makeOptContract()是阻碍我执行订单的问题。请提供一些建议


Tags: fromimporttimevaluedefcellmsgtic