def get_rate(args_dict): amt = args_dict['amt'] b_curr = args_dict['b_curr'] d_curr = args_dict['d_curr'] c = args_dict['currency'] n = args_dict['name'] # check whether we need to find ISO 4217 codes if c or n: currency_options = (c, n) names = b_curr, d_curr b_curr, d_curr = parse_args(currency_options, names) ISO_str = b_curr + d_curr # Yahoo Finance API call currency = Currency(ISO_str) trade_time = currency.get_trade_datetime() current_rate = float(currency.get_rate()) value = amt * current_rate # modify args dictionary with data needed for display args_dict['b_curr_updated'] = b_curr args_dict['d_curr_updated'] = d_curr args_dict['value'] = value args_dict['current_rate'] = current_rate args_dict['trade_time'] = trade_time return args_dict
def calculate_premium_fiat(): usdkrw = Currency('USDKRW') curr = float(usdkrw.get_ask()) btcusd = self.polo_bot.btcusd print("\tBTC premeium KRW/USD : ", str((self.krx_bot.btckrw_buy_price / (curr * btcusd))), 'with btcusd =', btcusd)
def __init__(self, name): self.name = name if Currency(self.name).get_rate() is None: raise ValueError(name + " is not a valid currency.") self.bid = Currency(self.name).get_bid() self.ask = Currency(self.name).get_ask() self.rate = Currency(self.name).get_rate() self.datetime = Currency(self.name).get_trade_datetime()
def _getValue(symbol): ret = {} cur = Currency(symbol) ret['rate'] = cur.get_rate() if ret['rate']: time = datetime.datetime.strptime(cur.get_trade_datetime(), '%Y-%m-%d %H:%M:%S %Z%z') ret['tradeDateTime'] = int(time.timestamp()) return ret return None
def get_curr_prices(currs): data = [] for c in currs: curr_data = {} curr = Currency(c) curr_data['symbol'] = c curr_data['price'] = round(float(curr.get_rate()), 2) data.append(curr_data) return data
def get_rate(ticker): rate = 0 while rate == 0: try: ccy_pair = Currency(ticker) ccy_pair.refresh() rate = ccy_pair.get_rate() except Exception, e: pass
def test_eurpln_date(self): eur_pln = Currency('EURPLN') try: datetime.datetime.strptime(eur_pln.get_trade_datetime(), "%Y-%m-%d %H:%M:%S %Z%z") except ValueError as v: if "bad directive" in str(v): raise SkipTest("datetime format checking requires the %z directive.") else: raise
def test_eurpln_date(self): eur_pln = Currency('EURPLN') try: datetime.datetime.strptime(eur_pln.get_trade_datetime(), "%Y-%m-%d %H:%M:%S %Z%z") except ValueError as v: if "bad directive" in str(v): raise SkipTest( "datetime format checking requires the %z directive.") else: raise
def main(): """ Reads in a CSV file specifying the portfolio of stocks and their respective ratios within the portfolio.""" stocks = {} with open(STOCK_RATIO_FILE) as csvfile: reader = csv.reader(csvfile, delimiter=',') for row in reader: ticker, fraction = row[0].strip(), float(row[1].strip()) stocks[ticker] = {} share = Share(ticker) scurr = share.get_currency() if scurr != CURRENCY: xchrate = float(Currency(scurr + CURRENCY).get_rate()) else: xchrate = 1.0 price = float(share.get_price()) * xchrate stocks[ticker]['price'] = price stocks[ticker]['in_currency'] = CURRENCY stocks[ticker]['target_ratio'] = fraction stocks[ticker]['name'] = share.get_name() stocks[ticker]['target_number'] = np.round(fraction * TOTAL_CASH / price) print(json.dumps(stocks, indent=2)) total_value = 0 for stock in stocks: s = stocks[stock] total_value += s['price'] * s['target_number'] print('Total Portfolio Value:') print(total_value)
def jpy(): global chf_jpy, jpy_chf, cad_jpy, jpy_cad, aud_jpy, jpy_aud, hkd_jpy, jpy_hkd, inr_jpy, jpy_inr chf_jpy = float(Currency('CHFJPY').get_rate()) jpy_chf = 1 / chf_jpy cad_jpy = float(Currency('CADJPY').get_rate()) jpy_cad = 1 / cad_jpy aud_jpy = float(Currency('AUDJPY').get_rate()) jpy_aud = 1 / aud_jpy hkd_jpy = float(Currency('HKDJPY').get_rate()) jpy_hkd = 1 / hkd_jpy inr_jpy = float(Currency('INRJPY').get_rate()) jpy_inr = 1 / inr_jpy
def data_extraction(interval, length): x = [] y = [] eur_sek = Currency('EURSEK') for i in range (length): eur_sek.refresh() timestamp = datetime.datetime.now() rate = eur_sek.get_bid() print('Time: ',timestamp) print ('EUR-SEK: ',rate) print ('') x.append(timestamp) y.append(rate) time.sleep(interval) data = pd.DataFrame(x, columns=['Date']) data['Rate'] = y return data
def gbp(): global jpy_gbp, gbp_jpy, chf_gbp, gbp_chf, cad_gbp, gbp_cad, aud_gbp, gbp_aud, hkd_gbp, gbp_hkd jpy_gbp = float(Currency('JPYGBP').get_rate()) gbp_jpy = 1 / jpy_gbp chf_gbp = float(Currency('CHFGBP').get_rate()) gbp_chf = 1 / chf_gbp cad_gbp = float(Currency('CADGBP').get_rate()) gbp_cad = 1 / cad_gbp aud_gbp = float(Currency('AUDGBP').get_rate()) gbp_aud = 1 / aud_gbp hkd_gbp = float(Currency('HKDGBP').get_rate()) gbp_hkd = 1 / hkd_gbp
def chf(): global cad_chf, chf_cad, aud_chf, chf_aud, hkd_chf, chf_hkd, inr_chf, chf_inr, sgd_chf, chf_sgd cad_chf = float(Currency('CADCHF').get_rate()) chf_cad = 1 / cad_chf aud_chf = float(Currency('AUDCHF').get_rate()) chf_aud = 1 / aud_chf hkd_chf = float(Currency('HKDCHF').get_rate()) chf_hkd = 1 / hkd_chf inr_chf = float(Currency('INRCHF').get_rate()) chf_inr = 1 / inr_chf sgd_chf = float(Currency('SGDCHF').get_rate()) chf_sgd = 1 / sgd_chf
def eur(): global jpy_eur, eur_jpy, gbp_eur, eur_gbp, chf_eur, eur_chf, cad_eur, eur_cad, aud_eur, eur_aud jpy_eur = float(Currency('JPYEUR').get_rate()) eur_jpy = 1 / jpy_eur gbp_eur = float(Currency('GBPEUR').get_rate()) eur_gbp = 1 / gbp_eur chf_eur = float(Currency('CHFEUR').get_rate()) eur_chf = 1 / chf_eur cad_eur = float(Currency('CADEUR').get_rate()) eur_cad = 1 / cad_eur aud_eur = float(Currency('AUDEUR').get_rate()) eur_aud = 1 / aud_eur
def aud(): global hkd_aud, aud_hkd, inr_aud, aud_inr, sgd_aud, aud_sgd, inr_gbp, gbp_inr, sgd_gbp, gbp_sgd hkd_aud = float(Currency('HKDAUD').get_rate()) aud_hkd = 1 / hkd_aud inr_aud = float(Currency('INRAUD').get_rate()) aud_inr = 1 / inr_aud sgd_aud = float(Currency('SGDAUD').get_rate()) aud_sgd = 1 / sgd_aud inr_gbp = float(Currency('INRGBP').get_rate()) gbp_inr = 1 / inr_gbp sgd_gbp = float(Currency('SGDGBP').get_rate()) gbp_sgd = 1 / sgd_gbp
def cad(): global aud_cad, cad_aud, hkd_cad, cad_hkd, inr_cad, cad_inr, sgd_cad, cad_sgd, sgd_jpy, jpy_sgd aud_cad = float(Currency('AUDCAD').get_rate()) cad_aud = 1 / aud_cad hkd_cad = float(Currency('HKDCAD').get_rate()) cad_hkd = 1 / hkd_cad inr_cad = float(Currency('INRCAD').get_rate()) cad_inr = 1 / inr_cad sgd_cad = float(Currency('SGDCAD').get_rate()) cad_sgd = 1 / sgd_cad sgd_jpy = float(Currency('SGDJPY').get_rate()) jpy_sgd = 1 / sgd_jpy
def hkd(): global inr_hkd, hkd_inr, sgd_hkd, hkd_sgd, hkd_eur, eur_hkd, inr_eur, eur_inr, sgd_eur, eur_sgd inr_hkd = float(Currency('INRHKD').get_rate()) hkd_inr = 1 / inr_hkd sgd_hkd = float(Currency('SGDHKD').get_rate()) hkd_sgd = 1 / sgd_hkd hkd_eur = float(Currency('HKDEUR').get_rate()) eur_hkd = 1 / hkd_eur inr_eur = float(Currency('INREUR').get_rate()) eur_inr = 1 / inr_eur sgd_eur = float(Currency('SGDEUR').get_rate()) eur_sgd = 1 / sgd_eur
def inr(): global sgd_inr, inr_sgd, aud_usd, usd_aud, hkd_usd, usd_hkd, inr_usd, usd_inr, sgd_usd, usd_sgd sgd_inr = float(Currency('SGDINR').get_rate()) inr_sgd = 1 / sgd_inr aud_usd = float(Currency('AUDUSD').get_rate()) usd_aud = 1 / aud_usd hkd_usd = float(Currency('HKDUSD').get_rate()) usd_hkd = 1 / hkd_usd inr_usd = float(Currency('INRUSD').get_rate()) usd_inr = 1 / inr_usd sgd_usd = float(Currency('SGDUSD').get_rate()) usd_sgd = 1 / sgd_usd
def usd(): global usd_eur, eur_usd, usd_jpy, jpy_usd, chf_usd, usd_chf, cad_usd, usd_cad, gbp_usd, usd_gbp eur_usd = float(Currency('EURUSD').get_rate()) usd_eur = 1 / eur_usd jpy_usd = float(Currency('JPYUSD').get_rate()) usd_jpy = 1 / jpy_usd gbp_usd = float(Currency('GBPUSD').get_rate()) usd_gbp = 1 / gbp_usd chf_usd = float(Currency('CHFUSD').get_rate()) usd_chf = 1 / chf_usd cad_usd = float(Currency('CADUSD').get_rate()) usd_cad = 1 / cad_usd
def Run(): currencies = [['EURCHF',1.05,1.20], ['EURUSD',1.05,1.25], ['CHFUSD',0.98,1.08], ] for curr in currencies: print curr eur_pln = Currency(curr[0]) #print eur_pln.get_bid() #print eur_pln.get_ask() conv_rate = float(eur_pln.get_rate()) print conv_rate line ='' if conv_rate<curr[1]: line ='Should consider switching currency: %0.2f for %s' %(conv_rate,curr[0]) if conv_rate>curr[2]: line ='Should consider switching currency: %0.2f for %s' %(conv_rate,curr[0])
def get_rate(ticker): logging.debug('get_rate %s' % ticker) rate = 0 trade_time = 0 while rate == 0: try: ccy_pair = Currency(ticker) ccy_pair.refresh() rate = ccy_pair.get_rate() #trade_time = str(ccy_pair.get_trade_datetime()) trade_time = datetime.datetime.now().time() except HTTPError as e: logging.debug('HTTP Error') except Exception as e: logging.exception(e) raise e return rate, trade_time
class TestCurrency(TestCase): def setUp(self): self.eur_pln = Currency('EURPLN') def test_eurpln(self): # assert these are float-like float(self.eur_pln.get_bid()) float(self.eur_pln.get_ask()) float(self.eur_pln.get_rate()) def test_eurpln_date(self): eur_pln = Currency('EURPLN') try: datetime.datetime.strptime(eur_pln.get_trade_datetime(), "%Y-%m-%d %H:%M:%S %Z%z") except ValueError as v: if "bad directive" in str(v): raise SkipTest("datetime format checking requires the %z directive.") else: raise
class TestCurrency(TestCase): def setUp(self): self.eur_pln = Currency('EURPLN') def test_eurpln(self): # assert these are float-like float(self.eur_pln.get_bid()) float(self.eur_pln.get_ask()) float(self.eur_pln.get_rate()) def test_eurpln_date(self): eur_pln = Currency('EURPLN') try: datetime.datetime.strptime(eur_pln.get_trade_datetime(), "%Y-%m-%d %H:%M:%S %Z%z") except ValueError as v: if "bad directive" in str(v): raise SkipTest( "datetime format checking requires the %z directive.") else: raise
def get_currency_rate(t0, t1=None): ''' ''' t0 = t0.upper() if not t1: ticker = Currency(t0) return ticker.get_rate() else: t1 = t1.upper() ticker = Currency(t0 + t1) return ticker.get_rate()
def logPrice(self): prices = {} try: price = Share(name) type = "sh" except: price = Currency(name) type = "cu" outfile = open(str(name + '_pricelog.json'), 'a') starttime = time() if (type == 'sh'): price.refresh() dumps({name: {int(time() - starttime): price.get_price()}}) if (type == 'cu'): price.refresh() dumps({name: {int(time() - starttime): price.get_price()}})
def _get_dollar_exchange_rate(self): usd_brl = Currency('USDBRL') return usd_brl.get_bid()
from yahoo_finance import Currency eur_pln = Currency('EURPLN') print (eur_pln.get_bid())
# Fix bug: https://github.com/ranaroussi/fix-yahoo-finance (The future, I think that the bug is solved) # pip install fix_yahoo_finance --upgrade --no-cache-dir print("+++++pandas_datareader+++++") from pandas_datareader import data as pdr import fix_yahoo_finance as yf # <== that's all it takes :-) # For Stock of Thailand, the symbol must be followed with '.BK' # download dataframe ptt = pdr.get_data_yahoo("PTT.BK", start="2017-01-01", end="2017-04-30") print(ptt.tail()) print() # Forth example # How to install yahoo_finance package # Reference: https://pypi.python.org/pypi/yahoo-finance # pip install yahoo_finance from yahoo_finance import Share yahoo = Share("YHOO") print("\n++++YHOO stock++++") print (yahoo.get_open()) print (yahoo.get_price()) print (yahoo.get_trade_datetime()) from yahoo_finance import Currency eur_pln = Currency('EURPLN') print("\n++++Currency of EURPLN++++") print (eur_pln.get_bid()) print (eur_pln.get_ask()) print (eur_pln.get_rate()) print (eur_pln.get_trade_datetime())
def get_data(symbol): return Currency(symbol).get_rate()
data = data = quandl.get(tickers,start_date=start, end_date=end) data.tail(1).T s=data[["CHRIS/SHFE_AU1 - Close","YAHOO/INDEX_HUI - Adjusted Close","CHRIS/CME_GC1 - Last"]] s.apply(lambda x: (x - np.mean(x)) / (np.max(x) - np.min(x)), axis=0).ix['2014':].plot(color=tableau20) #============================================================================== # #============================================================================== import pandas_datareader.data as web df = web.DataReader("EURUSD=x", 'yahoo', start, end) from yahoo_finance import Currency fx = Currency('EURUSD') fx.get_bid() fx.get_ask() fx.get_rate() fx.get_trade_datetime() fx.get_historical('2014-04-25', '2014-04-29') fx.data_set
def setUp(self): self.eur_pln = Currency('EURPLN')
from yahoo_finance import Currency file = open("Currency_update.txt", 'r') x = file.readlines() for y in x: L = map(str, y.split()) res = '' for z in L[:-1]: res += z + ' ' print '%30s %s' % (res, L[-1]) first_currency = raw_input("enter first currency: ") second_currency = raw_input("enter second currency: ") conversion = first_currency + second_currency eur_pln = Currency(conversion) print eur_pln.get_bid()
# get_year_high() # get_year_low() # get_50day_moving_avg() # get_200day_moving_avg() # get_price_earnings_ratio() # get_price_earnings_growth_ratio() # get_price_sales() # get_price_book() # get_short_ratio() # get_trade_datetime() # get_historical(start_date, end_date) # get_info() # refresh() from yahoo_finance import Currency eur_pln = Currency('EURPLN') print eur_pln.get_bid() print eur_pln.get_ask() print eur_pln.get_rate() print eur_pln.get_trade_datetime() eur_pln.refresh() print eur_pln.get_rate() print eur_pln.get_trade_datetime() # get_bid() # get_ask() # get_rate() # get_trade_datetime() # refresh()
def get_rate(self, source_code, dest_code): previous_locale = locale.getlocale() locale.setlocale(locale.LC_ALL, 'C') currency = Currency(source_code + dest_code) locale.setlocale(locale.LC_ALL, previous_locale) return currency.get_rate()
#This program is distributed in the hope that it will be useful, but #WITHOUT ANY WARRANTY; without even the implied warranty of #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #General Public License for more details. import matplotlib.pyplot as plt import matplotlib.animation as animation import matplotlib.dates as mdates import datetime from yahoo_finance import Currency fig = plt.figure() ax1 = fig.add_subplot(1, 1, 1) currencies = Currency('usdeur') spot = 1 xar=[] yar=[] ymin = float(currencies.get_rate())-0.0005 ymax = ymin + 0.0010 def animate(i): global xar, yar, ymin, ymax #gets the course currencies.refresh() y = currencies.get_rate() x = datetime.datetime.now()
def get(self): return float(Currency('USDRUB').get_bid())
#from pandas_datareader import data as web from yahoo_finance import Currency #import pandas.io.data as web from tabulate import tabulate from datetime import datetime as dt start_dt = '2011-01-01' end_dt = dt.today().strftime("%Y-%m-%d") jpy = Currency('EURUSD') print jpy.get_rate()
def _get_euro_exchange_rate(self): eur_brl = Currency('EURBRL') return eur_brl.get_bid()
from yahoo_finance import Currency import csv, time, os MyCurrencies = [] usd_eur = Currency('USDEUR') usd_eur.cur_name = 'USDEUR' MyCurrencies.append(usd_eur) eur_usd = Currency('EURUSD') eur_usd.cur_name = 'EURUSD' MyCurrencies.append(eur_usd) gbp_usd = Currency('GBPUSD') gbp_usd.cur_name = 'GBPUSD' MyCurrencies.append(gbp_usd) usd_gbp = Currency('USDGBP') usd_gbp.cur_name = 'USDGBP' MyCurrencies.append(usd_gbp) cny_usd = Currency('CNYUSD') cny_usd.cur_name = 'CNYUSD' MyCurrencies.append(cny_usd) usd_cny = Currency('USDCNY') usd_cny.cur_name = 'USDCNY' MyCurrencies.append(usd_cny) jpy_usd = Currency('JPYUSD') jpy_usd.cur_name = 'JPYUSD' MyCurrencies.append(jpy_usd) usd_jpy = Currency('USDJPY') usd_jpy.cur_name = 'JPYUSD' MyCurrencies.append(usd_jpy) def __checkfileexist(currency_name, directory='/home/jedwards/cur_data'): if os.path.isfile(directory + '/' + currency_name + '.csv') == False: