I'm using python to connect to TWS API and pull values.
My first script:
#use option chain csv to get prics
from datetime import datetime
from threading import Thread
import time
from ibapi.client import EClient, Contract
from ibapi.wrapper import EWrapper
from ibapi.utils import iswrapper
import math
import pandas as pd
import numpy as np
#import ticker and option expiry from option_chain
from option_chain_06 import ticker, option_dates
global q_up, q_down, vt
q_down = 0
q_up = 0
vt = 0
#set ticker variable
#ticker = 'AAPL'
#df = pd.read_csv('C:/Users/Mischa/Documents/Options/'+ticker+'option_chain.csv')
class TestApp(EWrapper, EClient):
def __init__(self, addr, port, client_id):
EWrapper.__init__(self)
EClient. __init__(self, self)
self.current_price = 0
self.current_IV = 0
self.opt_bid_price = 0
self.opt_ask_price = 0
self.opt_last_price = 0
self.opt_close_price = 0
self.opt_mid_price = 0
# Connect to TWS
self.connect(addr, port, client_id)
print('connect')
# Launch the client thread
thread = Thread(target=self.run)
thread.start()
def error(self, reqId, errorCode, errorString):
print('Error: ', reqId, " ", errorCode, " ", errorString)
@iswrapper
def tickByTickMidPoint(self, reqId, time, midpoint):
self.current_price = midpoint
@iswrapper
def tickGeneric(self, reqId, tickType, value):
if tickType == 24:
def get_option_price(client, ticker, option_dates, q_up, q_down, vt):
df = pd.read_csv('C:/Users/Mischa/Documents/Options/'+ticker+'option_chain.csv')
a = 1.5
date = option_dates[0]
current_date = datetime.today().date()
exp_date = datetime.strptime(date, '%Y%m%d').date()
interval = (exp_date - current_date).days
#define contract
contract = Contract()
contract.symbol = ticker
contract.secType = 'STK'
contract.multiplier = '100'
contract.exchange = 'SMART'
contract.currency = 'USD'
client.reqTickByTickData(0, contract, "MidPoint",1 , True)
time.sleep(4)
print(client.current_price)
#get IV
client.reqMktData(1, contract, '106', False, False, [])
time.sleep(1)
#get q up and q down
vt = client.current_IV * math.sqrt(interval/365)
q_up = client.current_price * math.exp(a*vt)
q_down = client.current_price * math.exp(-a*vt)
#round q_down down, round q_up up
q_down = math.floor(q_down)
q_up = math.ceil(q_up)
return q_down, q_up, vt
def main(ticker, option_dates, q_up, q_down, vt):
client = TestApp('127.0.0.1', 7497, 129)
q_down, q_up, vt = get_option_price(client, ticker, option_dates, q_up, q_down, vt)
print(q_down, q_up, vt)
return q_down, q_up, vt
client.done=True
client.disconnect()
if __name__ == '__main__':
main(ticker, option_dates, q_up, q_down, vt)
It prints out the values for the 3 variables I want in def(main)
Then I try importing those 3 vairables (q_up, q_down, vt) into another script:
#use option chain csv to get prics
from datetime import datetime
from threading import Thread
import time
from ibapi.client import EClient, Contract
from ibapi.wrapper import EWrapper
from ibapi.utils import iswrapper
import math
import pandas as pd
import numpy as np
#import q's, IV from other file
from option_prices_03 import q_up, q_down, vt, ticker
print(ticker, q_down, q_up, vt)
And I get error:
ImportError: cannot import name 'q_up' from 'option_prices_03' (C:\TWS API\source\pythonclient\tests\option_prices_03.py)
I've tested just setting the 3 variables at the beginning of the first script (option_prices_03.py) and importing them and that works. But it won't import the variables as they are being assigned.
EDIT: tried making the variables global:
def get_option_price(client, ticker, option_dates):
df = pd.read_csv('C:/Users/Mischa/Documents/Options/'+ticker+'option_chain.csv')
global q_up, q_down, vt
q_down = 0
q_up = 0
vt = 0
a = 1.5
date = option_dates[0]
current_date = datetime.today().date()
exp_date = datetime.strptime(date, '%Y%m%d').date()
interval = (exp_date - current_date).days
Still get the same error.
Tried adding a "return" to if __name__ == '__main__':
and got a Syntax error:
if __name__ == '__main__':
main(ticker, option_dates, q_up, q_down, vt)
return q_up
> SyntaxError: 'return' outside function