Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'author:': 0.03; 'currency': 0.07; 'filename:fname piece:py': 0.07; 'none:': 0.07; 'skip:% 20': 0.09; 'snippet': 0.09; 'subject:version': 0.09; 'variance': 0.09; 'def': 0.12; 'changes': 0.15; 'windows': 0.15; '%s,': 0.16; "'''": 0.16; 'currencies': 0.16; 'exchanges': 0.16; 'skip:g 50': 0.16; ':-)': 0.16; 'seems': 0.21; 'import': 0.22; 'previously': 0.22; 'to:name:python-list@python.org': 0.22; 'print': 0.22; 'delta': 0.24; 'skip:% 10': 0.24; 'pass': 0.26; 'downloaded': 0.26; 'values': 0.27; 'resolution': 0.29; 'skip:( 20': 0.30; 'gives': 0.31; "skip:' 10": 0.31; 'clock': 0.31; 'testing.': 0.31; 'skip:c 30': 0.32; 'updated': 0.34; 'subject:the': 0.34; 'info': 0.35; 'skip:s 30': 0.35; 'skip:u 20': 0.35; 'but': 0.35; 'version': 0.36; 'false': 0.36; 'mine': 0.38; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'improved': 0.60; 'new': 0.61; 'rates': 0.61; 'save': 0.62; 'exchange': 0.63; 'occur': 0.65; 'note:': 0.66; 'believe': 0.68; '500': 0.70; 'completion': 0.78; 'bid': 0.84; 'header:mime-version:1': 0.84; 'from.': 0.93 X-SENDER-IP: [213.112.60.117] X-LISTENER: [smtp.bredband.net] X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A2CWAQCUAklV/3U8cNUNT4Q7gx22To1niA4QAQEBAQEBAYVUVT0WCwILAwIBAgFLDQYCAQGILbAJcJNqAQsgky2BRQWFPAqOVIE6iEONUIc7giocgVNtgkUBAQE X-IPAS-Result: A2CWAQCUAklV/3U8cNUNT4Q7gx22To1niA4QAQEBAQEBAYVUVT0WCwILAwIBAgFLDQYCAQGILbAJcJNqAQsgky2BRQWFPAqOVIE6iEONUIc7giocgVNtgkUBAQE X-IronPort-AV: E=Sophos;i="5.13,373,1427752800"; d="py'?scan'208";a="234589823" To: "python-list@python.org" From: Virgil Stokes Subject: An improved version of the main snippet Date: Tue, 5 May 2015 19:54:06 +0200 user-agent: Mozilla/5.0 (Windows NT 6.0; rv:38.0) Gecko/20100101 Thunderbird/38.0 mime-version: 1.0 Content-Type: multipart/mixed; boundary="------------010408070704070909020101" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 133 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430850052 news.xs4all.nl 2826 [2001:888:2000:d::a6]:52982 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89987 This is a multi-part message in MIME format. --------------010408070704070909020101 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit I have attached what I believe to be an improved version of my main snippet for testing. --V :-) --------------010408070704070909020101 Content-Type: text/x-java; name="YahooForexData_A3.py" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="YahooForexData_A3.py" ''' 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: vs@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() --------------010408070704070909020101--