-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContractor.py
61 lines (48 loc) · 1.96 KB
/
Contractor.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
from ibapi.client import EClient, Contract
from ibapi.contract import ContractDetails
from ibapi.wrapper import EWrapper
from ibapi.common import TickerId
from Database import DbManager
import time
class Contractor(EClient, EWrapper):
def __init__(self):
EClient.__init__(self, self)
self.ticker = None
self.ids = []
def contractDetails(self, reqId: int, contractDetails: ContractDetails) -> None:
self.ids.append([self.ticker, contractDetails.contract.conId])
def contractDetailsEnd(self, reqId: int) -> None:
self.disconnect()
def create_id(self, ticker) -> None:
self.ticker = None
self.ticker = ticker
new_contract = Contract()
new_contract.symbol = self.ticker
new_contract.secType = "STK"
new_contract.exchange = "SMART"
new_contract.currency = "USD"
new_contract.primaryExchange = "ISLAND"
time.sleep(1)
self.reqContractDetails(1, new_contract)
def error(self, reqId: TickerId, errorCode: int, errorString: str, advancedOrderRejectJson="") -> None:
super().error(reqId, errorCode, errorString)
if errorCode == 200:
print(f"Unable to retrieve contract information for symbol {self.ticker}, you will have to add it manually."
f" Continuing...")
self.disconnect()
def req_ids(db: DbManager) -> None:
app = Contractor()
while True:
symbols = (input("Enter symbols or -1 for help: ")).upper()
if symbols == "-1":
print("Enter space separated tickers for the stocks you wish to add")
else:
symbols = list(symbols.split(" "))
for symbol in symbols:
app.connect("127.0.0.1", 7497, 1000)
app.create_id(symbol)
app.run()
for new_id in app.ids:
db.insert_ticker(new_id[0])
db.update_contract_id(new_id[0], new_id[1])
break