makiolo

Download Forex Prices from Yahoo

Apr 14th, 2021
361
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import pytz
  3. import requests
  4. import pandas as pd
  5. from datetime import datetime, timedelta
  6. # need Stock and Price models ... are very simple
  7. from stocks.models import Price, Stock
  8.  
  9.  
  10. def get_working_dir():
  11.     from django_test.settings import MEDIA_ROOT
  12.     working_dir = os.path.join(MEDIA_ROOT, 'scripts')
  13.     return working_dir
  14.  
  15.  
  16. def download_file(download_path, local_filename):
  17.     with requests.get(download_path, stream=True) as r:
  18.         r.raise_for_status()
  19.         with open(local_filename, 'wb') as f:
  20.             for chunk in r.iter_content(chunk_size=8192):
  21.                 f.write(chunk)
  22.  
  23.  
  24. def import_csv_main():
  25.  
  26.     symbol = 'EUR/USD'  # 'USD/JPY' ... etc ...
  27.     # symbol = 'USD/JPY'
  28.     symbol_replaced = symbol.replace('/', '')
  29.  
  30.     # create dataset folder if not exists
  31.     working_dir = get_working_dir()
  32.     dataset_folder = os.path.join(working_dir, 'dataset')
  33.     if not os.path.isdir(dataset_folder):
  34.         os.makedirs(dataset_folder)
  35.  
  36.     # calculate from / to
  37.     today_0_00 = (datetime.now() + timedelta(days=1)).astimezone(pytz.utc).replace(hour=0, minute=0, second=0, microsecond=0)
  38.     period1 = int(datetime.timestamp(today_0_00 - timedelta(days=365 * 5)))  # 5 years
  39.     period2 = int(datetime.timestamp(today_0_00))
  40.     print('from: {}'.format(datetime.fromtimestamp(period1)))
  41.     print('to: {}'.format(datetime.fromtimestamp(period2)))
  42.  
  43.     # download csv ...
  44.     download_path = 'https://query1.finance.yahoo.com/v7/finance/download/{}=X?period1={}&period2={}&interval=1d&events=history&includeAdjustedClose=true'.format(symbol_replaced, period1, period2)
  45.     local_filename = os.path.join(dataset_folder, '{}.csv'.format(symbol_replaced))
  46.     download_file(download_path, local_filename)
  47.  
  48.     stock, _ = Stock.objects.update_or_create(name=symbol, defaults={'comment': 'yahoo_csv'})
  49.     # clean prices
  50.     stock.prices.all().delete()
  51.     df = pd.read_csv(local_filename, index_col=['Date'], parse_dates=['Date'])
  52.     for index, row in df.iterrows():
  53.         time = index.to_pydatetime().astimezone(pytz.utc)
  54.         print(time)
  55.         close = float(str(row['Close']).replace(',', ''))
  56.         close_adj = float(str(row['Adj Close']).replace(',', ''))
  57.         openp = float(str(row['Open']).replace(',', ''))
  58.         high = float(str(row['High']).replace(',', ''))
  59.         low = float(str(row['Low']).replace(',', ''))
  60.         volume = float(str(row['Volume']).replace(',', ''))
  61.         Price.objects.update_or_create(time=time, stock=stock,
  62.                                        defaults={'close': close, 'close_adj': close_adj,
  63.                                                  'open': openp,
  64.                                                  'high': high, 'low': low,
  65.                                                  'volume': volume})
  66.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×