Can not execute options trade
spawnaga at gmail.com
spawnaga at gmail.com
Thu Jul 25 21:26:16 EDT 2019
The placeOrder command does not send the order for options trades.
I tried to submit the order for stocks and it went through but not options
# -*- 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()
I am loading trading suggestions from an online excel sheet and submit the order to TWS. I have successfully loaded the information from my google sheet and put the code to order the security but the execution part is not going through. I think my makeOptContract() is the problem that is holding me back from executing my order. Please provide some suggestions
More information about the Python-list
mailing list