reqFundamentalData - need market cap

67 views
Skip to first unread message

jake.p.z...@gmail.com

unread,
Oct 18, 2017, 2:42:28 PM10/18/17
to IbPy Discuss
Not sure if something i posted before actually posted, but i forgot some code anyway. I just need to print a stock's market cap. I see there is a list of fundamental ratio tags, but having difficulty figuring out how to implement correctly

from ib.opt import ibConnection, message 
from ib.ext.Contract import Contract
from ib.ext.TickType import TickType as tt


def fundamental_data_handler(msg):
    ##retireve market caps for tickers and store in dictionary
    
   if (msg.data == "MKTCAP"):  -> obviously this is wrong. Trying to figure out correct implementation
         print(msg.data)
    


tws = ibConnection(port=7497, clientId=100)
tws.register(fundamental_data_handler, message.fundamentalData) 
tws.connect()

x=1
for cell in all_cells: -> im reading in stocks from a google sheet. dont worry about that part (didnt include the api code here as it isnt relevant)
        c = Contract()
        c.m_symbol = cell.value
        c.m_exchange = 'SMART'
        c.m_currency = 'USD'
        c.m_secType = 'STK'
        tws.reqFundamentalData(x, c, "ReportSnapshot") . -> wasn't sure which report type is appropriate so I just chose that
        x+=1

jake.p.z...@gmail.com

unread,
Oct 18, 2017, 4:47:19 PM10/18/17
to IbPy Discuss
*fixed this with below script* - this doesnt work with all tickers though, and call back is always inconsistent. It's supposed to return a giant string containing the fundamental ratios - in this case for Wayfair, it does. If i change that symbol to FB or NVDA or some others, the call back is not that string, and the response. Console output testing these tickers is below the script

from ib.opt import ibConnection, message
from ib.ext.Contract import Contract
from time import sleep
from ib.ext.TickType import TickType as tt



def my_callback_handler(msg):
    print("string value: " + msg.value)
    
 
tws = ibConnection(port=7497, clientId=100)
tws.register(my_callback_handler, message.tickString)
tws.connect()

c = Contract()
c.m_symbol = "W"
c.m_secType = "STK"
c.m_exchange = "SMART"
c.m_currency = "USD"

tws.reqMarketDataType(1)
tws.reqMktData(1,c,"258", False)


print(c.m_symbol + '\n')
print 'All done'

tws.disconnect()


**so again, W was fine. In some cases when I run, say, FB (below W), the output is usually a random string of letters - in this case its consistently the same random letters, but there have been some cases where it just prints a different random set of letters each time I run without making any changes.

W

string value: TTMNPMGN=-5.09725;NLOW=27.6;ACFSHR=-1.633419;ALTCL=-99999.99;TTMPRCFPS=-47.36392;TTMCFSHR=-1.492368;ASFCF=-21.092;AEPSNORM=-2.28738;TTMRECTURN=206.5417;AATCA=477.091;QCSHPS=2.93999;TTMFCF=-61.467;LATESTADATE=2017-06-30;APTMGNPCT=-5.73214;TTMNIAC=-200.31;EV_Cur=3796.346;QATCA=417.246;PR2TANBK=734.8508;TTMFCFSHR=-0.71593;NPRICE=69.73;ASICF=32.206;REVTRENDGR=-99999.99;QSCEX=-45.326;PRICE2BK=508.2617;ALSTD=-99999.99;AOTLO=62.814;TTMPAYRAT=-99999.99;QPR2REV=5.404652;;TTMREVCHG=37.01928;TTMROAPCT=-28.30345;QTOTCE=11.94;APENORM=-30.48466;QLTCL=598.547;QSFCF=-0.66;TTMROIPCT=-102.2621;DIVGRPCT=-99999.99;QOTLO=18.101;TTMEPSCHG=-63.77669;YIELD=0;TTMREVPS=45.77132;TTMEBT=-199.906;ADIV5YAVG=-99999.99;Frac52Wk=0.72901;NHIG=84.19;ASCEX=-128.086;QTA=751.478;TTMGROSMGN=20.40677;QTL=819.361;AFPRD=-99999.99;QCURRATIO=0.6971;TTMREV=3929.765;TTMINVTURN=195.4403;QCASH=255.869;QLSTD=0;TTMOPMGN=-5.06738;TTMPR2REV=1.54683;QSICF=18.651;TTMNIPEREM=-35534.86;EPSCHNGYR=21.26071;TTMPRFCFPS=-99999.99;TTMPTMGN=-5.08697;AREVPS=39.77971;AEBTNORM=-193.767;ASOPI=-196.217;NetDebt_I=-173.144;PRYTDPCTR=74.14594;TTMEBITD=-126.954;AFEEPSNTM=-1.2345;EPSTRENDGR=-99999.99;QTOTD2EQ=692.8392;QSOPI=-37.552;QBVPS=0.13719;YLD5YAVG=-99999.99;PR13WKPCT=-7.088607;PR52WKPCT=95.15814;AROAPCT=-26.69502;QTOTLTD=-99999.99;TTMEPSXCLX=-2.33521;QPRCFPS=-99999.99;QTANBVPS=0.09505;AROIPCT=-77.43746;QEBIT=-37.101;QEBITDA=-18.22906;MKTCAP=6078.686;TTMINTCOV=-88.97945;TTMROEPCT=-230.2096;TTMREVPERE=697137.7;AEPSXCLXOR=-2.287384;QFPRD=-99999.99;REVCHNGYR=42.68853;AFPSS=0.209;CURRENCY=USD;EV2EBITDA_Cur=-208.2579;PEEXCLXOR=-29.86002;QQUICKRATI=0.67269;ASINN=-99999.99;QFPSS=0.108;BETA=-99999.99;ANIACNORM=-194.375;PR1WKPCT=2.123616;QLTD2EQ=692.8392;QSINN=-99999.99;PR4WKPCT=-13.15232;AEBIT=-194.461All done

string value: 1508345976
>>> 

==== RESTART: /Users/jakezimmerman/Documents/PythonIBZip/test_mkt_data.py ====
Server Version: 76
TWS Time at connection:20171018 16:44:40 EST
FB
string value: M

All donestring value: Q

>>> 
==== RESTART: /Users/jakezimmerman/Documents/PythonIBZip/test_mkt_data.py ====
Server Version: 76
TWS Time at connection:20171018 16:44:45 EST
FB
string value: M

All donestring value: Q

>>> 
==== RESTART: /Users/jakezimmerman/Documents/PythonIBZip/test_mkt_data.py ====
Server Version: 76
TWS Time at connection:20171018 16:44:49 EST
FB
string value: M

All donestring value: Q

>>> 
==== RESTART: /Users/jakezimmerman/Documents/PythonIBZip/test_mkt_data.py ====
Server Version: 76
TWS Time at connection:20171018 16:44:53 EST
FB
string value: M

All donestring value: Q

>>> 
==== RESTART: /Users/jakezimmerman/Documents/PythonIBZip/test_mkt_data.py ====
Server Version: 76
TWS Time at connection:20171018 16:45:05 EST
NVDA
string value: C

All donestring value: WT

>>> 
==== RESTART: /Users/jakezimmerman/Documents/PythonIBZip/test_mkt_data.py ====
Server Version: 76
TWS Time at connection:20171018 16:45:10 EST
NVDA
string value: C

All donestring value: WT
Reply all
Reply to author
Forward
0 new messages