-
Notifications
You must be signed in to change notification settings - Fork 1
/
Contracts.py
120 lines (105 loc) · 3.74 KB
/
Contracts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import sys
import datetime as dt
import numpy as np
import pandas as pd
import pdb
import csv
import logging
# types
from ibapi.common import *
from ibapi.order_condition import *
from ibapi.contract import *
from ibapi.order import *
from ibapi.order_state import *
from ibapi.execution import Execution
from ibapi.execution import ExecutionFilter
from ibapi.commission_report import CommissionReport
from ibapi.scanner import ScannerSubscription
from ibapi.ticktype import *
from App import TestApp
class Request:
def __init__(self, symbol, callback):
self.symbol = symbol
self.callback = callback
global nextReqId, requestDict, fileName, contractDict
nextReqId = 1000
requestDict = {}
fileName = "contracts.csv"
contractDict = {}
logger = logging.getLogger('Contracts')
logger.setLevel(logging.DEBUG)
def loadDict():
if len(contractDict) == 0:
with open(fileName, mode='r') as fin:
reader = csv.reader(fin)
for row in reader:
contract = Contract()
contract.symbol = row[0]
contract.secType = row[1]
contract.currency = row[2]
contract.primaryExchange = row[3]
contract.exchange = row[4]
contractDict[contract.symbol] = contract
def writeToDict(contract: Contract):
contractDict[contract.symbol] = contract
logger.debug("Saving contract: symbol:%s, secType:%s, currency:%s, primaryExchange:%s, exchange:%s",
contract.symbol,
contract.secType,
contract.currency,
contract.primaryExchange,
contract.exchange
)
with open(fileName, mode='a') as fout:
writer = csv.writer(fout)
writer.writerow([
contract.symbol,
contract.secType,
contract.currency,
contract.primaryExchange,
contract.exchange
])
def request(symbol, callback = None):
loadDict()
contract = contractDict.get(symbol)
if contract != None:
if callback != None:
callback(contract)
return
logger.debug("Contract not found in local cache. Requesting contract for %s", symbol)
nextReqId = nextReqId + 1
req = Request(symbol, callback)
requestDict[nextReqId] = req
TestApp.Instance().reqMatchingSymbols(nextReqId, symbol)
def resolve(reqId, contractDescriptions: ListOfContractDescription):
req = requestDict[reqId]
if req != None:
count = len(contractDescriptions)
if count == 0:
logger.debug("Did not find contracts for %s", req.symbol)
elif count == 1:
logger.debug("Exactly one contract found for %s", req.symbol)
contract = contractDescriptions[0].contract
requestDetail(reqId, contract)
else:
logger.debug("%d contracts found for %s", count, req.symbol)
for cd in contractDescriptions:
contract = cd.contract
if contract.currency == 'USD':
logger.debug("Using first USD contract")
logger.debug("Contract: conId:%s, symbol:%s, secType:%s, currency:%s, primaryExchange:%s",
contract.conId,
contract.symbol,
contract.secType,
contract.currency,
contract.primaryExchange
)
requestDetail(reqId, contract)
break
def requestDetail(reqId, contract:Contract):
TestApp.Instance().reqContractDetails(reqId, contract)
def resolveDetail(reqId, contract:ContractDetails):
req = requestDict[reqId]
if req != None:
writeToDict(contract.summary)
if req.callback != None:
req.callback(contract.summary)