I have attached what I believe to be an improved version of my main snippet for
testing.
--V :-)
'''
Purpose: get current bid, ask and rate for currency exchanges (FOREX trading)
Note:
1. yahoo seems to give the best estimates for the currency exchange rates
2. Not sure where the "bid" and "ask" values come from.
3. Not sure how often updates to currencies occur during weekdays; but very fast (real-time?) during weekdays
Author: v...@it.uu.se
Vesion: 2015.05.05.2
'''
import pytz
from yahoo_finance import Currency
from datetime import datetime
import time
#from time import clock # clock gives good resolution in MS Windows
NUM_TICS = 500 # number of values to be downloaded
CURRENCY_PAIR = 'EUR/SEK'
MY_TIME = "Europe/Stockholm"
OTHER_TIME = "America/New_York"
FILE_OT = 'CurrencyInfo.txt'
PRINT_TIMEZONES = True
PRINT_TIMEZONES = False
if PRINT_TIMEZONES:
for tz in pytz.all_timezones:
time.sleep(0.5)
print (tz)
def updateMeanVar(x,k,mu,vr):
'''
Purpose: Update the estimates for the mean and variance (recursive mean,variance)
Inputs:
x -- new value (x_k)
k -- counter (index) for new value (1,2,...)
mu -- previously estimated mean (x_k not included)
vr -- previously estimated variance (x_k not included)
Otputs:
mu -- updated mean (with x_k included)
vr -- updated variance (with x_k included)
'''
delta = x - mu
mu += delta/k
vr += delta*(x - mu)
return mu,vr
def get_Times(myTimeZone=MY_TIME,otherTimeZone=OTHER_TIME):
fmt = '%Y-%m-%d %H:%M:%S %Z%z'
Mine = pytz.timezone(myTimeZone)
Other = pytz.timezone(otherTimeZone)
nowMine = datetime.now(Mine)
nowOther = datetime.now(Other)
return nowMine.strftime(fmt) + ' ('+nowOther.strftime(fmt)+')'
DateTimeStr = get_Times()
def InitFile(file=FILE_OT,currencyPair=CURRENCY_PAIR,dateTimeStr=DateTimeStr):
f = open(file,'a')
f.write('Currency Pair: %s, TimeStamp; %s\n'%(currencyPair,dateTimeStr))
f.write(' bid ask rate datetime\n')
return f
def SaveToFile(f,Bid,Ask,Rate,dateTime):
f.write('%s %s %s %s\n'%(Bid,Ask,Rate,dateTime))
pass
currency_Ex = Currency(CURRENCY_PAIR.replace('/',''))
fOt = InitFile()
print ('Currency pair: %s'%CURRENCY_PAIR)
print (' bid ask rate datetime')
prev_dateTime = currency_Ex.get_trade_datetime()
NMax = NUM_TICS
mu = 0.0; va = 0.0
for k in range(1,NUM_TICS+1):
t0 = time.clock()
currency_Ex.refresh()
Bid = currency_Ex.get_bid()
if Bid == None:
continue
Ask = currency_Ex.get_ask()
if Ask == None:
continue
Rate = currency_Ex.get_rate()
if Rate == None:
continue
dateTime = currency_Ex.get_trade_datetime()
if dateTime == None:
continue
print ('%s %s %s %s'%(Bid,Ask,Rate,dateTime))
if dateTime[:16] != prev_dateTime[:16]:
# Save currency exchange info when minute changes
SaveToFile(fOt,Bid,Ask,Rate,dateTime)
prev_dateTime = dateTime
# Estimate Time To Completion (ETTC) with recursive mean of 'loop' time
dt = time.clock() - t0
mu,va = updateMeanVar(dt,k,mu,va)
ETTC = mu*(NMax-k)
m,s = divmod(ETTC,60)
h,m = divmod(m,60)
print ('ETTC = %d:%02d:%03d'%(h,m,s))
fOt.close()
--
https://mail.python.org/mailman/listinfo/python-list