def test_eia_release_analysis(self): eikonkey = os.environ['EIKON_KEY'] ek.set_app_key(eikonkey) d = stocks.eia_release_analysis() self.assertTrue(isinstance(d, pd.DataFrame))
def __init__(self, eikon_app_key, data, outputdir, iscontinue=False, pathtodone=None): """ data :: [Str] List with company rics outputdir :: Str Path to outputdirname iscontinue :: Bool If true, won't get headlines for dates which already exist pathtodone :: Str Path to textfile with done rics dateto: :: Str | datetime Ending date, if Str form YYYY-MM-DD datefrom :: Str | datetime Starting date, if Str form YYYY-MM-DD """ ek.set_app_key(eikon_app_key) print('Eikon connected') eventlet.monkey_patch() self.ordered = ['A', 'O', 'B', 'K', 'PK'] #exchange types self.rics = data self.outputdir = outputdir self.iscontinue = iscontinue os.makedirs(self.outputdir, exist_ok=True) self.done = pathtodone if self.done is not None: #READ ALL COMPS ALREADY DONE with open(self.done, 'r') as f: done_rics = set(f.read().strip().split('\n')) self.rics -= done_rics print('Updating %d companies' % len(self.rics))
def rtConn(): try: TR_ID = '70650e2c881040408f6f95dea2bf3fa13e9f66fe' ek.set_app_key(TR_ID) except ek.eikonError.EikonError as e: print(e) return None
def reuters_eikon_data_scraper(instruments: list, fields: list, properties:dict, api_key:str): df, err = None, None ek.set_app_key(api_key) try: df, err = ek.get_data(instruments, fields, properties, field_name=True, raw_output=False) if err is not None: err = {'error_type': 'REUTERS', 'error_message': str(err), 'req_instruments': instruments, 'req_fields': fields, 'req_properties': properties} else: df.columns = [i.replace('.', '_') for i in df.columns.tolist()] except Exception as error: err = {'error_type': 'PyREUTERS', 'error_message': str(error).replace('\n', ';').encode(), 'req_instruments': instruments, 'req_fields': fields, 'req_properties':properties} finally: return df, err
def retrieve_monthly_assets_total_returns(date_from, date_to, assets_ric_list): if date_from >= date_to: raise Exception("Dates are wrong") if len(assets_ric_list) == 0: raise Exception("At least one RIC needed in the RICs list") ek.set_app_key("94baceedc0c24ae8961456ae2b67bc72532e997f") start_date = date_from end_date = date_to timeline = [] next_from_date = start_date.replace(day=1) next_to_date = (start_date + rd.relativedelta(months=1)) + rd.relativedelta(days=-1) next_time_slot = (next_from_date, next_to_date) while next_time_slot[1] < end_date: timeline.append(next_time_slot) next_from_date = timeline[-1][1] + rd.relativedelta(days=1) next_to_date = (next_from_date + rd.relativedelta(months=1)) + rd.relativedelta(days=-1) next_time_slot = (next_from_date, next_to_date) total_return_percentage_all = {} for target_asset in assets_ric_list: total_return_percentage_all[target_asset] = {} total_return_percentage_all[target_asset]["timeline"] = [] for timeslot in timeline: start_time = time.time() i = timeline.index(timeslot) tl = len(timeline) f = dt.datetime.strftime(timeslot[0], "%Y-%m-%d") t = dt.datetime.strftime(timeslot[1], "%Y-%m-%d") o = ek.get_data(assets_ric_list, ['TR.TotalReturn'], {'SDate': f, 'EDate': t}) o_assets_list = o[0]["Instrument"].to_list() for target_asset in assets_ric_list: index_of_target_asset = o_assets_list.index(target_asset) try: to_store = ((f, t), float(o[0]["Total Return"][index_of_target_asset]) / 100) except ValueError: to_store = ((f, t), np.nan) total_return_percentage_all[target_asset]["timeline"].append(to_store) end_time = time.time() msg = "Loading from Eikon ({:d} of {:d}, {:.2f}%) (remaining time: {:.2f} seconds)" print(msg.format(i, tl, 100 * i / tl, (end_time - start_time)*(tl-i))) return total_return_percentage_all
def init( app ): cfg = json.load( fp = open( os.path.join( os.getenv( 'FLASKEIKON' ), 'resource', 'config' ) ) ) eikon.set_app_key( cfg[ 'appid' ] ) handler = TimedRotatingFileHandler( filename = cfg[ 'log' ], when = 'MIDNIGHT', encoding = 'utf-8' ) handler.setFormatter( logging.Formatter( '[%(processName)s[%(process)d]|%(threadName)s[%(thread)d]|%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s > %(message)s' ) ) app.logger.addHandler( handler ) app.logger.setLevel( logging.DEBUG ) app.config[ 'IP' ] = cfg[ 'ip' ] app.config[ 'PORT' ] = cfg[ 'port' ] from app.api import api as api_blueprint app.register_blueprint( blueprint = api_blueprint, url_prefix = '/api/' )
def makeResponse(req): if req.get("result").get("action") != "fetchNews": return {} result = req.get("result") parameters = result.get("parameters") ticker = parameters.get("ticker") if ticker is None: return None tr.set_app_key('229dfa317f614c3c9cdfa2908c1ff66af4fdf1c6') r = tr.get_news_headlines('ticker', date_from='2020-03-09', date_to='2020-03-10', count=5) r.head() news = r("text") print(news) result = "The news on" + ticker + "are" + news return { "text": result, }
def update_daily_data(stocks): ''' Update the daily data for the stocks specified. ''' s = sched.scheduler(time.time, time.sleep) ek.set_app_key('66063f6d35e4453ebba0696f40307bc61e7172f2') config = util.import_config() stock_data_folder = config['STOCK_DATA_FOLDER'] # Get the current moment date in US end_date = util.get_us_time() for stock_code in stocks: fname = os.path.join(stock_data_folder, stock_code + '.csv') if os.path.isfile(fname): stock_df = pd.read_csv(fname) start_date = max(stock_df["Date"]) else: stock_df = pd.DataFrame() start_date = end_date try: new_df = ek.get_timeseries(stock_code, start_date=start_date, end_date=end_date).reset_index() new_df["Date"] = new_df["Date"][0:10] stock_df = stock_df.append(new_df) stock_df = stock_df.reindex(columns=[ "Date", "HIGH", "CLOSE", "LOW", "OPEN", "COUNT", "VOLUME" ]) stock_df.drop_duplicates(["Date"], inplace=True) stock_df.to_csv(fname, index=False) #time.sleep(5.0 - ((time.time() - start_time) % 5.0)) print("Successfully updated daily date for", stock_code) except Exception as e: print("Error in acquiring ", stock_code, ":") print(str(e))
def thomson_timeseries( tickers, fields, corax, start_date=datetime.today().strftime("%Y%m%d"), end_date=datetime.today().strftime("%Y%m%d"), ): """ 톰슨 로이터 시계열 데이터 소싱 """ ek.set_app_key("9c4370471d3c41ea9a2a8911d842812e941e06df") try: data = ek.get_timeseries( tickers, fields=fields, start_date=start_date, end_date=end_date, interval="daily", corax=corax, ) print(data) except: raise ValueError("thomson data sourcing ERROR") return data
def main(): # Excel sheet = xw.Book.caller().sheets[0] rng = sheet['A2'].expand() pid_file = Path(__file__).resolve().parent / "pid" if pid_file.exists(): # Stop streaming with open(pid_file, 'r') as f: pid = f.read() try: os.kill(int(pid), signal.SIGTERM) pid_file.unlink() except ProcessLookupError as e: os.remove(pid_file) finally: rng[1:, 1:].clear_contents() rng[0, 0].offset(row_offset=-1, column_offset=1).clear_contents() else: # Start streaming with open(pid_file, 'w') as f: f.write(str(os.getpid())) # Connect to Refinitiv Workspace conf = ConfigParser() conf.read(os.path.join(os.path.dirname(__file__), '..', 'eikon.conf')) ek.set_app_key(conf['eikon']['APP_KEY']) instruments, fields = rng[1:, 0].value, rng[0, 1:].value streaming_prices = ek.StreamingPrices(instruments=instruments, fields=fields) streaming_prices.open() while True: rng[1, 1].value = streaming_prices.get_snapshot().to_numpy()[:, 1:] rng[0, 0].offset(row_offset=-1, column_offset=1).value = dt.datetime.now()
def thomson_sourcing(self): ek.set_app_key("9c4370471d3c41ea9a2a8911d842812e941e06df") tr_fields = [ "TR.CLOSEPRICE", "TR.OPENPRICE(Adjusted=1)", "TR.CLOSEPRICE(Adjusted=1)", "TR.HIGHPRICE(Adjusted=1)", "TR.LOWPRICE(Adjusted=1)", "TR.IssueMarketCap", ] sec = 4 interval = int(np.floor(len(self.tsTickers) / sec)) tot_data = pd.DataFrame() for i in range(0, len(self.tsTickers), interval): tsData = self.thomson( self.tsTickers[i : i + interval], tr_fields ) # , parameters={'SDate':self.start, 'EDate':self.end}) tot_data = pd.concat([tot_data, tsData], axis=0) time.sleep(3) tot_data.columns = [ "Instrument", "종가", "수정시가", "수정주가", "수정고가", "수정저가", "시가총액", "date", ] tot_data.to_csv("{}/thomson.csv".format(self.today), encoding="cp949") self.organize_data("thomson", tot_data)
import eikon as ek import xlwings as xw # Please read your Eikon APP_KEY from a config file or environment variable ek.set_app_key('YOUR_APP_KEY') def main(): # These parameters could also come from a config sheet in the template instrument = '.DJI' start_date = '2020-01-01' end_date = '2020-01-31' # Eikon queries df = ek.get_timeseries(instrument, fields='*', start_date=start_date, end_date=end_date) summary, err = ek.get_data(instrument, ['TR.IndexName', 'TR.IndexCalculationCurrency']) # Populate the Excel template with the data wb = xw.Book.caller() wb.sheets[0][ 'A4'].value = f"{summary.loc[0, 'Index Name']} ({summary.loc[0, 'Calculation Currency']})" wb.sheets[0]['A6'].value = df if __name__ == '__main__': xw.Book('sample2.xlsx').set_mock_caller()
import numpy import pandas import eikon eikon.set_app_key('638fa3bbb90349d5a97dc60c1c0cc4b0b5646846') eikon.get_news_headlines('R:LHAG.DE', date_from='2018-01-01T00:00:00', date_to='2018-12-13T18:00:00') #df = eikon.get_timeseries("SPY", # ['TR.IndexConstituentRIC', # 'TR.IndexConstituentName', # 'TR.IndexConstituentWeightPercent'], data_grid, err = eikon.get_data("SPY", ['TR.IndexConstituentRIC', {'TR.IndexConstituentWeightPercent':{'sort_dir':'desc'}}], {'SDate':'2018-12-14'} ) print(data_grid) weight = data_grid.loc[:, "Weight percent"] weight import matplotlib.pyplot as plt fig1, ax1 = plt.subplots() ax1.pie(weight, shadow = True) ax1.axis('equal') plt.show() NESN = eikon.get_timeseries(["NESN.S"], start_date="2014-01-06", end_date="2018-12-20", interval="daily") plt.plot(NESN.loc[:, "CLOSE"])
import eikon as ek # the Eikon Python wrapper package import re import time import datetime import os import pandas as pd import eventlet eventlet.monkey_patch() #set_app_id function being deprecated eikon_app_id = 'fb1f0ff6cf794376af6ff9f29926d3fb17997843' #paste real app id ek.set_app_key(eikon_app_id) print('Eikon connected') #range of dates start = datetime.datetime.strptime("2018-02-28", "%Y-%m-%d") end = datetime.datetime.strptime("2019-06-1", "%Y-%m-%d") date_generated = [start + datetime.timedelta(days=x) for x in range((end-start).days)][::-1] rics = set(pd.read_csv('constituents_matched.csv', sep=',')['reuters']) # #find not done yet with open('done.txt', 'r') as f: notdone = set(f.read().strip().split('\n')[:-100]) rics -= notdone print('Updating {} companies'.format(str(len(rics)))) def _handle_time(e, event): if hasattr(e, 'message'):
import eikon as ek import pandas as pd ek.set_app_key('02251fexxxxxxxxxxxxxxxxxxxxxxxxxxxxxfd2c') constituents, err = ek.get_data(".FTSE", "TR.IndexConstituentRIC") for company in constituents['Constituent RIC']: data, err = ek.get_data(company, ["TR.CompanyName", "TR.OfficerName(RNK=R1:R100)", "TR.OfficerTitle(RNK=R1:R100)"]) data.to_csv("data/"+company+".csv")
# -*- coding: utf-8 -*- """ Created on Wed Oct 16 12:20:19 2019 @author: Admin """ import eikon as ek ek.set_app_key('88728d9ceb094a57aaa16f1bdceb8d899dafab9a') p1 = ek.get_timeseries(["JNJ.N"], start_date="2010-02-01T15:04:05", end_date="2017-02-05T15:04:05", interval="tick")
import math import eikon as ek # the Eikon Python wrapper package ek.set_app_key('0ed6a35e0937415eab446d3375bca7cf671d6b4c') import numpy as np # NumPy import pandas as pd # pandas import cufflinks as cf # Cufflinks import configparser as cp # import scipy.optimize as sco # optimization routines cf.set_config_file(offline=True, theme="white") #Font Setting from matplotlib.font_manager import FontProperties import sys if sys.platform.startswith('win'): FontPath = 'C:\\Windows\\Fonts\\meiryo.ttc' elif sys.platform.startswith('darwin'): FontPath = '/System/Library/Fonts/ヒラノギ角ゴシック W4.ttc' elif sys.platform.startswith('linux'): FontPath = '/usr/share/fonts/truetype/takao-gothic/TakaoExGothic.ttc' jpfont = FontProperties(fname=FontPath) import matplotlib.pyplot as plt import matplotlib as mpl import os mpl_dirpath = os.path.dirname(mpl.__file__) # デフォルトの設定ファイルのパス default_config_path = os.path.join(mpl_dirpath, 'mpl-data', 'matplotlibrc') # カスタム設定ファイルのパス custom_config_path = os.path.join(mpl.get_configdir(), 'matplotlibrc') import plotly.graph_objects as go import chart_studio.plotly as py
import eikon as ek ek.set_app_key('ba4e52456ba64a87be3b82001782535f159b564e') print( ek.get_news_headlines('R:IBM.N', date_from='2019-09-27T09:00:00', date_to='2019-09-27T10:00:00'))
import eikon as ek import configparser as cp import pandas as pd # Set global variables. cfg = cp.ConfigParser() cfg.read('eikon_cfg.cfg') ek.set_app_key(cfg['eikon']['app_id']) def get_time_series(rics: list, fields: list, start_date, end_date) -> pd.DataFrame: """ Retrieve time series data as Pandas DataFrame. :param rics: List of RIC:s to get. :param fields: List fields to get. :param start_date: Start date of time series. :param end_date: End date of time series. :return: Pandas DataFrame. """ data = ek.get_timeseries(rics, fields, start_date=start_date, calendar='native', end_date=end_date, interval='daily') return data
import warnings warnings.simplefilter(action='ignore', category=FutureWarning) from datetime import datetime from dateutil.relativedelta import relativedelta import numpy as np import pandas as pd import eikon as ek ek.set_app_key('bf38826c5e014c1cadf21425ee6e417d2b72fc9a') Benchmark = '.FTSETWMC' DATE = datetime.strptime('2020-12-31', r'%Y-%m-%d') time_delta = relativedelta(months=-1) RICs = list(pd.read_excel('data/TWMC100_RICs.xlsx', usecols='A')['RIC']) df = pd.read_csv('data/TWMC100_20yr.csv', index_col='Date') df2 = ek.get_timeseries(RICs + ['.TWII', Benchmark], fields='CLOSE', start_date='2020-10-31', end_date=DATE, interval='monthly', corax='adjusted') df2['Date'] = [DATE + 2 * time_delta, DATE + time_delta, DATE] df2.set_index('Date', inplace=True) df = df.append(df2) df.to_csv('data/TWMC100_20yr.csv') # check if all data are gotten!
elif contextAction[sender]['actionName'] == "gettimeseries": resp_msg = '@' + sender + ' ' + gettimeseries(actionValue) elif contextAction[sender]['actionName'] == "changeconfidencelevel": confidence_level = float(actionValue) resp_msg = 'Confidence Level is now adjusted to ' + actionValue contextAction.pop(sender) return resp_msg # =============================== Main Process ======================================== # Running the tutorial if __name__ == '__main__': if use_data_from_eikon: try: # Set Eikon AppKey to reqeuest data from Eikon Desktop via Eikon Data API ek.set_app_key(eikon_data_api_app_key) except ek.EikonError as ex: print("Eikon Initialize Error:{}".format(ex.message)) print('Load data and model for AHS Chatbot...') loadmodel() # Setting Python Logging logging.basicConfig(format='%(levelname)s:%(name)s :%(message)s', level=log_level) print("ChatRoom is starting now. Type 'bye' to exit") while is_running: message = input("{} input:".format(userId)) print("Bot:{}\n".format(process_message(message, userId)))
def main(): # Files template = xw.Book.caller() report_path = os.path.join(os.path.dirname(__file__), 'report.xlsx') # Eikon setup conf = ConfigParser() conf.read(os.path.join(os.path.dirname(__file__), '..', 'eikon.conf')) ek.set_app_key(conf['eikon']['APP_KEY']) # Configuration date_format = template.sheets['Config']['date_format'].value if date_format == 'UK': fmt = '%e %b %Y' elif date_format == 'US': fmt = '%b %e, %Y' else: fmt = '%e %b %Y' instrument = template.sheets['Config']['instrument'].value start_date = dt.datetime(dt.datetime.now().year - 4, 1, 1) # Prices prices = ek.get_timeseries(instrument, fields=['close'], start_date=start_date, end_date=dt.datetime.now(), interval='weekly') end_date = prices.index[-1] # Summary summary, err = ek.get_data(instrument, [ 'TR.PriceClose', 'TR.Volume', 'TR.PriceLow', 'TR.PriceHigh', 'TR.IndexName', 'TR.IndexCalculationCurrency' ]) # Constituents constituents, err = ek.get_data( f'0#{instrument}', fields=['TR.CommonName', 'TR.PriceClose', 'TR.TotalReturnYTD']) constituents = constituents.set_index('Company Common Name') # Add empty columns so it goes into the desired Excel cells for i in range(0, 6): constituents.insert(loc=i, column='merged' + str(i), value=np.nan) constituents = constituents.drop(['Instrument'], axis=1) constituents = constituents.rename(columns={"YTD Total Return": "YTD %"}) # Collect data data = dict(perf_start_date=start_date.strftime(fmt), perf_end_date=end_date.strftime(fmt), index_name=summary.loc[0, 'Index Name'], currency=summary.loc[0, 'Calculation Currency'], reference_date=dt.date.today().strftime(fmt), historical_perf=prices, constituents=constituents, price_close=float(summary['Price Close']), volume=float(summary['Volume']), price_low=float(summary['Price Low']), price_high=float(summary['Price High'])) # Create the Excel report wb = create_report(template.fullname, report_path, **data)
import os.path import numpy as np import pandas as pd import eikon as ek ek.set_app_key('48f17fdf21184b0ca9c4ea8913a840a92b338918') from .future_root_factory import FutureRootFactory from pandas import read_hdf from pandas import HDFStore,DataFrame import os dirname = os.path.dirname(__file__) #run through loop to get data for all, keeping in mind 5 year limit to history def put_to_hdf(df): hdf = HDFStore(dirname + "\_InstrumentData.5") hdf.put('InstrumentData', df, format='table', data_columns=True) hdf.close() # closes the file columns =[ 'exchange_symbol', 'root_symbol', 'instrument_name', 'underlying_name', 'underlying_asset_class_id', 'settle_start', 'settle_end', 'settle_method', 'settle_timezone', 'final_settle_start', 'final_settle_end',
def get_news_and_sentiments(key_id, ticker_and_params, ticker, start_date, end_date): ek.set_app_key(key_id) Date_range = pd.date_range(start=start_date, end=end_date, freq='D') date_range = [] for i in range(len(Date_range) - 1): start_date = pd.to_datetime(Date_range)[i].strftime( "%Y-%m-%dT%H:%M:%S") end_date = pd.to_datetime(Date_range)[i + 1].strftime("%Y-%m-%dT%H:%M:%S") date_range.append([]) for j in range(1): date_range[i].append(start_date) date_range[i].append(end_date) example_data = pd.DataFrame() for i in range(len(date_range)): example_data = example_data.append(ek.get_news_headlines( f'{ticker_and_params}', count=10, date_from=date_range[i][0], date_to=date_range[i][1]), ignore_index=False) news = example_data stories = [] for i, storyId in enumerate(news['storyId']): try: html = ek.get_news_story(storyId) story = BeautifulSoup(html, 'html5lib').get_text() stories.append(story) except: stories.append('') news['story'] = stories ## Sentiment Over Time sentiment = pd.DataFrame() sid = SentimentIntensityAnalyzer() for storyId in news['storyId']: row = news[news['storyId'] == storyId] scores = sid.polarity_scores(row['story'][0]) sentiment = sentiment.append( pd.DataFrame(scores, index=[row['versionCreated'][0]])) sentiment.index = pd.DatetimeIndex(sentiment.index) sentiment.sort_index(inplace=True) sentiment_list = list(sentiment['compound']) news['sentiment'] = sentiment_list #group by day dates_normal = [] for i in range(len(news['versionCreated'])): dates_normal.append(news['versionCreated'][i].strftime("%Y-%m-%d")) dates_normal_2 = pd.to_datetime(dates_normal) news['dates_normal'] = dates_normal_2 #save to csv news.to_csv(f"{ticker}_news.csv") daily_sentiments_listed = news.groupby( pd.Grouper(key="dates_normal", freq="D"))['sentiment'].apply(list).reset_index() #Daily mood index DMI = [] for i in range(len(daily_sentiments_listed['sentiment'])): pos = 0 neg = 0 for j in range(len(daily_sentiments_listed['sentiment'][i])): try: if daily_sentiments_listed['sentiment'][i][j] > 0: pos += 1 elif daily_sentiments_listed['sentiment'][i][j] < 0: neg += 1 except: pass DMI.append(np.log((1 + pos) / (1 + neg))) daily_sentiments_listed['DMI'] = DMI #Average Sentiment Average_S = [] for i in range(len(daily_sentiments_listed['sentiment'])): Average_S.append(np.mean(daily_sentiments_listed['sentiment'][i])) daily_sentiments_listed['Average_Sentiment'] = pd.DataFrame( Average_S).fillna(0) #save sentiments to csv daily_sentiments_listed.to_csv(f"{ticker}_sentiment.csv", index=False) return daily_sentiments_listed, news
import quandl import numpy as np from sklearn.linear_model import LinearRegression from sklearn.svm import SVR from sklearn.model_selection import train_test_split import eikon as ek import pandas as pd import matplotlib.pyplot as plt ek.set_app_key('b47a7929747c40149d93089db6b3e895a3f8bca5') df = ek.get_timeseries(["CHRS.O"], start_date = "2001-10-10", end_date = "2019-02-05") print(df) df = df[['CLOSE']] print(df) forecast_out = 30 df['Prediction'] = df[['CLOSE']].shift(-forecast_out) print(df) X = np.array(df.drop(['Prediction'],1)) X = X[:-forecast_out] print(X) y = np.array(df['Prediction']) y = y[:-forecast_out] print(y) x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2) svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1) svr_rbf.fit(x_train, y_train) svm_confidence = svr_rbf.score(x_test, y_test) print("svm confidence: ", svm_confidence) lr = LinearRegression() lr.fit(x_train, y_train) lr_confidence = lr.score(x_test, y_test) print("lr confidence: ", lr_confidence)
def logkey(n_clicks, value): if n_clicks != None: ek.set_app_key(value) ek.set_timeout(60 * 5) return 'Confirm: "{}"'.format(value)
def main(): # Files template = xw.Book.caller() report_path = Path(template.fullname).resolve().parent / 'report.xlsx' # Eikon setup ek.set_app_key(os.getenv('EIKON_APP_KEY')) # Configuration date_format = template.sheets['Config']['date_format'].value if date_format == 'UK': fmt = '%e %b %Y' elif date_format == 'US': fmt = '%b %e, %Y' else: fmt = '%e %b %Y' instrument = template.sheets['Config']['instrument'].value start_date = dt.datetime(dt.datetime.now().year - 4, 1, 1) # Prices prices = ek.get_timeseries(instrument, fields=['close'], start_date=start_date, end_date=dt.datetime.now(), interval='weekly') end_date = prices.index[-1] # Summary summary, err = ek.get_data(instrument, ['TR.PriceClose', 'TR.Volume', 'TR.PriceLow', 'TR.PriceHigh', 'TR.IndexName', 'TR.IndexCalculationCurrency']) # Constituents constituents, err = ek.get_data(f'0#{instrument}', fields=['TR.CommonName', 'TR.PriceClose', 'TR.TotalReturnYTD']) constituents = constituents.set_index('Company Common Name') # Add empty columns so it goes into the desired Excel cells for i in range(6): constituents.insert(loc=i, column='merged' + str(i), value=np.nan) constituents = constituents.drop(['Instrument'], axis=1) constituents = constituents.rename(columns={"YTD Total Return": "YTD %"}) constituents = constituents.sort_values("YTD %", ascending=False) # Collect data data = dict( perf_start_date=start_date.strftime(fmt), perf_end_date=end_date.strftime(fmt), index_name=summary.loc[0, 'Index Name'], currency=summary.loc[0, 'Calculation Currency'], reference_date=dt.date.today().strftime(fmt), historical_perf=prices, constituents=constituents, price_close=float(summary['Price Close']), volume=float(summary['Volume']), price_low=float(summary['Price Low']), price_high=float(summary['Price High']) ) # Create the Excel report app = template.app app.screen_updating = False wb = create_report(template.fullname, report_path, app=app, **data) app.screen_updating = True wb.sheets.active["A1"].select()
def CalcNetChart(row_ids, data, children, value): if (row_ids != None) & (data != None): if (len(row_ids) > 0) & (len(data) > 0): ek.set_app_key(value) ek.set_timeout(60 * 5) data = pd.DataFrame(data) data = data[data.index.isin(row_ids)] data['Amount'] = data['Amount'].astype(float) data['Price'] = data['Price'].astype(float) data['Strike'] = data['Strike'].astype(float) data["Day"] = pd.to_datetime(data["Day"]) data["Maturity"] = pd.to_datetime(data["Maturity"]) data["Financial"] = data['Amount'] * data['Price'] data["Financial"] = data["Financial"].round(2) spot = pd.DataFrame(json.loads(json.loads(str(children))['spot'])) spot['TRDPRC_1'] = spot['TRDPRC_1'].astype(float) spot["TRADE_DATE"] = pd.to_datetime(spot["TRADE_DATE"]) ssp = spot[ spot["Instrument"] != "BRSELICD=CBBR"].TRDPRC_1.values[0] days = (data.apply(lambda row: businessDuration( startdate=row['Day'], enddate=row['Maturity'], unit='day'), axis=1)).values days = list(set(days)) vola, err = ek.get_data([ spot[ spot["Instrument"] != "BRSELICD=CBBR"].Instrument.values[0] ], [ 'TR.Volatility5D', 'TR.Volatility10D', "TR.Volatility20D", "TR.Volatility30D", "TR.Volatility60D", "TR.Volatility90D" ]) volaV = (sum(( vola.T[0].values[1:] * (abs(np.array([5, 10, 20, 30, 60, 90]) - 15) + 1) / (sum(abs(np.array([5, 10, 20, 30, 60, 90]) - 15) + 1))))) / 100 vola.columns = ['Instrument', 5, 10, 20, 30, 60, 90] vola[[5, 10, 20, 30, 60, 90]] = vola[[5, 10, 20, 30, 60, 90]] / 100 days = list(np.unique(days)) dists = [] vollist = [] for d in days: y_interp = scipy.interpolate.interp1d( vola.columns[1:].astype(float), vola.T[0].values[1:].astype(float)) volaV = y_interp(d) Distrib = 1 - np.random.normal(0, (volaV / (252**0.5) * (d**0.5)), size=5000) Distrib = Distrib * ssp vollist.append(volaV) dists.append(Distrib) Price = dists[pd.DataFrame(dists).max(1).argmax()] Price.sort() net = [] for k in range(len(data)): if data['Amount'].iloc[k] >= 0: if "C" in data['type'].iloc[k]: Rt = -data['Strike'].iloc[k] - data['Price'].iloc[ k] + Price Rt[Rt <= -data['Price'].iloc[k]] = -data['Price'].iloc[k] Rt = Rt * abs(data['Amount'].iloc[k]) net.append(list(Rt)) else: Rt = data['Strike'].iloc[k] - data['Price'].iloc[ k] - Price Rt[Rt <= -data['Price'].iloc[k]] = -data['Price'].iloc[k] Rt = Rt * abs(data['Amount'].iloc[k]) net.append(list(Rt)) else: if "C" in data['type'].iloc[k]: Rt = data['Strike'].iloc[k] + data['Price'].iloc[ k] - Price Rt[Rt >= data['Price'].iloc[k]] = data['Price'].iloc[k] Rt = Rt * abs(data['Amount'].iloc[k]) net.append(list(Rt)) else: Rt = -data['Strike'].iloc[k] + data['Price'].iloc[ k] + Price Rt[Rt >= data['Price'].iloc[k]] = data['Price'].iloc[k] Rt = Rt * abs(data['Amount'].iloc[k]) net.append(list(Rt)) pay = pd.DataFrame(net) pay.columns = Price pays = pay # print(pays.values) pay = pay.sum() tempTT = [] for j in range(len(set(days))): tempT = [] for s in range(len(data["Strike"])): temp = Func.OpitionsPrice( Price, data["Strike"].values.astype(float)[s], spot[spot["Instrument"] == "BRSELICD=CBBR"].TRDPRC_1. values[0].astype(float) / 100, np.array(data['ImpVol'].values[j]) / 100, days[j]) if "C" in data["type"].values[s]: temp = list( (temp.ValorC.values - data['Price'].values[s]) * data['Amount'].values[s]) else: temp = list( (temp.ValorP.values - data['Price'].values[s]) * data['Amount'].values[s]) tempT.append(temp) tempTT.append(tempT) fig = go.FigureWidget( make_subplots(shared_xaxes=True, specs=[[{ "secondary_y": True }]], print_grid=False)) trace3 = go.Scatter(name="0 line", x=pay.index, y=np.array([0 for i in Distrib]), xaxis='x1', yaxis='y2', line=dict(color='black', width=2, dash='dash')) fig.add_trace(trace3, secondary_y=False) trace1 = go.Scatter(name="Payoff", x=pay.index, y=pay.values, xaxis='x1', yaxis='y2', mode='lines', fill='tozeroy') fig.add_trace(trace1, secondary_y=False) for i in range(len(tempTT)): trace5 = go.Scatter(name="Price - " + str(days[i]) + ' Days', x=pay.index, y=pd.DataFrame(tempTT[i]).sum().values, xaxis='x1', yaxis='y2', mode='lines') fig.add_trace(trace5, secondary_y=False) for lin, i in zip(pays.values, pays.index): trace4 = go.Scatter(name=data.Instrument.values[i], x=pay.index, y=lin, xaxis='x1', yaxis='y2', line=dict(width=2, dash='dash')) fig.add_trace(trace4, secondary_y=False) for i, j in zip(days, dists): trace2 = ff.create_distplot([j], [str(i) + " Days - Probabilidade"], bin_size=.5, curve_type='normal', show_hist=False, show_rug=False) fig.add_trace(trace2['data'][0], secondary_y=True) prob = round(sum(pay.values > 0) / len(Distrib) * 100, 2) return [fig, html.H6("Profit probability: " + str(prob) + "%")] else: fig = go.FigureWidget( make_subplots(shared_xaxes=True, specs=[[{ "secondary_y": True }]], print_grid=False)) trace3 = go.Scatter(name="0 line", x=[0], y=[0], xaxis='x1', yaxis='y2', line=dict(color='black', width=2, dash='dash')) fig.add_trace(trace3, secondary_y=False) return [fig, html.H6(str(0) + "%")] else: fig = go.FigureWidget( make_subplots(shared_xaxes=True, specs=[[{ "secondary_y": True }]], print_grid=False)) trace3 = go.Scatter(name="0 line", x=[0], y=[0], xaxis='x1', yaxis='y2', line=dict(color='black', width=2, dash='dash')) fig.add_trace(trace3, secondary_y=False) return [fig, html.H6(str(0) + "%")]
# -*- coding: utf-8 -*- """ Created on Fri Nov 29 15:14:04 2019 @author: Administrator """ import eikon as ek ek.set_app_key('9559d6b91c084b348f50f9bcc0ef6a010b997405') import pandas as pd import eikon as ek import numpy as np ek.set_app_key('9559d6b91c084b348f50f9bcc0ef6a010b997405') from datetime import date start = '2004-1-1' today = date.today().strftime('%Y-%m-%d') firstday = '1999-12-30' #reuters start = '2004-1-1' ### TURKEY turkey_yield_tickers = [
# Thomson Reuters Eikon API Implementation to track news import os from dotenv import load_dotenv load_dotenv() import eikon as ek ek.set_app_key(os.getenv('EIKON_API_KEY')) import pandas as pd def get_news(topic, source, start_date, end_date, n_articles): ''' To search a company, Append 'R:' in front of RIC (ticker followed by a dot and Echange ID (eg: TSLA.O)) The following is a list of news sources and their respective code from Mexico. mexican_sources = { 'Mexico Ledger': 'MEXLED', 'El Financiero': 'ELMEX', 'El Economista': 'ELECOP', 'La Jornada': 'LAMEX', 'Mega News': 'MEGNEW', 'Milenio': 'MILMEX', 'El Nacional': 'ELNACI', 'Expansion': 'EXPSPB', 'Excelsior': 'EXCMEX', 'Mural': 'MURMEX', 'El Norte': 'ELNORT', 'Estrategia': 'ESTMEX', 'La I': 'LAIMEX', 'Al Chile': 'ALCHIL',