Skip to content
This repository has been archived by the owner on Jun 8, 2023. It is now read-only.

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lsbardel committed Nov 1, 2016
1 parent bdb3192 commit 9b83fab
Show file tree
Hide file tree
Showing 34 changed files with 278 additions and 523 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ _dep.py

# IDE
.idea
.project
.pydevproject
.DS_Store

.coverage
.coveralls-repo-token
Expand Down
21 changes: 0 additions & 21 deletions AUTHORS

This file was deleted.

File renamed without changes.
112 changes: 2 additions & 110 deletions dynts/__init__.py
Original file line number Diff line number Diff line change
@@ -1,114 +1,6 @@
'''Quantitative financial timeseries analysis'''
"""Quantitative financial timeseries analysis"""
from dynts.utils.version import get_version

VERSION = (0, 5, 0, 'alpha', 1)

from dynts.exceptions import *
from dynts.utils.version import get_version

__version__ = version = get_version(VERSION)
__license__ = "BSD"
__author__ = "Luca Sbardella"
__contact__ = "luca@quantmind.com"
__homepage__ = "http://github.com/quantmind/dynts/"
CLASSIFIERS = [
'Development Status :: 4 - Beta',
'Environment :: Plugins',
'Intended Audience :: Developers',
'Intended Audience :: Financial and Insurance Industry',
'Intended Audience :: Science/Research',
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: JavaScript',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.1',
'Programming Language :: Python :: 3.2',
'Topic :: Scientific/Engineering',
'Topic :: Scientific/Engineering :: Mathematics',
'Topic :: Office/Business :: Financial'
]
from functools import reduce

from .backends import *
from .dsl import parse, evaluate, merge, dslresult, function_registry, functions
from .data import providers
from . import formatters
Formatters['flot'] = formatters.ToFlot()
Formatters['jsonvba'] = formatters.ToJsonVba()
Formatters['csv'] = formatters.ToCsv()
Formatters['excel'] = formatters.ToExcel()
Formatters['xls'] = formatters.ToXls()
Formatters['plot'] = formatters.ToPlot()


def statistics(expression,
start = None,
end = None,
functions = None,
multivariate = False, **kwargs):
tseries = evaluate(expression, start = start, end = end, **kwargs).ts()
if not multivariate:
return BasicStatistics(tseries, functions = functions)
else:
raise NotImplementedError


def tsname(*names):
from dynts.conf import settings
sp = settings.splittingnames
return reduce(lambda x,y: '%s%s%s' % (x,sp,y), names)


def composename(name, *names, **kwargs):
from dynts.conf import settings
sp = settings.splittingnames
kw = ','.join(('{0}={1}'.format(*v) for v in kwargs.items()))
if kw:
kw = ','+kw
return sp.join(('{0}({1}{2})'.format(name, x, kw) for x in names))


def hasextensions():
'''True if cython extensions are available'''
from .lib import hasextensions
return hasextensions


def function_title_and_body(name,with_body=True):
'''Given a function *name* return a tuple containing
the function title and the restructured text used to
create the function ducumentation.'''
link = '.. _functions-{0}:'.format(name)
func = function_registry[name]
if func.description:
title = '{0} - {1}'.format(name,func.description)
else:
title = name
if with_body:
under = (2+len(title))*'='
fdoc = func.__doc__
if not fdoc:
raise FunctionError('Function %s has no documentation.' % name)
body = '\n'.join((link, '', title, under, '', fdoc,'\n'))
return title, body
else:
return title


def function_doc(name):
'''Given a function *name* return the restructured text used to
create the function ducumentation.'''
return function_title_and_body(name)[1]


def functions_docs():
names = sorted(function_registry)
return '\n'.join((function_doc(name) for name in names))


def dump_docs(filename = 'dyntslist.rst'):
docs = functions_docs()
f = open(filename,'w')
f.write(docs)
f.close()
print(('Saved function documentations in {0}'.format(filename)))
File renamed without changes.
File renamed without changes.
41 changes: 21 additions & 20 deletions dynts/conf.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,62 @@
from math import isnan

toupper = lambda x : str(x).upper()


class Settings(object):
'''\
The setting class contains configuration parameters used in dynts.
.. attribute:: backend
Default :class:`dynts.TimeSeries` backend class.
Default ``"numpy"``.
.. attribute:: desc
Default sorting order for dates. If ``False`` timeseries are ordered in
Default sorting order for dates. If ``False`` timeseries are ordered in
ascending date order, if ``True`` in a descending order.
Default ``False``.
.. attribute:: concat_operator
Operator for concatenating expressions.
Default ``,``.
.. attribute:: default_loader
Default :class:`dynts.data.TimeSerieLoader` class.
If this is ``None`` the :class:`dynts.data.TimeSerieLoader` class will be used as loder.
Default ``None``.
.. attribute:: default_provider
Default :class:`dynts.data.DataProvider` code. Default ``"YAHOO"``.
.. attribute:: field_separator
Character used to separate tickers from fields and providers. Default ``:``.
.. attribute:: months_history
the default number of months of history. Default: ``12``.
.. attribute:: proxies
dictionary of proxy servers. Default ``{}``.
If you need to use a proxy server to access the web::
from dynts.conf import settings
settings.proxies['http'] = 'http://yourproxy.com:80'
To change settings::
from dynts.conf import settings
settings.default_provider = 'GOOGLE'
'''
def __init__(self):
Expand All @@ -72,18 +74,17 @@ def __init__(self):
self.symboltransform = toupper
self.default_daycounter = 'ACT/365'
self.missing_value = float('nan')

@property
def special_operators(self):
return [self.concat_operator,self.separator_operator]

def getdc(self):
import ccy
return ccy.getdc(self.default_daycounter)

def ismissing(self, value):
return isnan(value)





settings = Settings()
43 changes: 19 additions & 24 deletions dynts/data/gy.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import os
import csv
from io import StringIO

from dynts.utils.http import http_client

from .base import DataProvider


Expand All @@ -14,34 +11,32 @@
class WebCsv(DataProvider):

def __init__(self, http=None):
self._http = http

def http(self):
if self._http == None:
self._http = http_client()
return self._http

if http is None:
import requests
http = requests.Session()
self.http = http

def string_to_date(self, sdte):
from ccy import dateFromString
return dateFromString(sdte)

def request(self, url):
response = self.http().get(url)
if response.status_code == 200:
return StringIO(response.content_string('ascii', 'ignore'))

def rowdata(self, ticker, startdate, enddate):
url = self.hystory_url(str(ticker), startdate, enddate)
res = self.request(url)
if res:
return csv.DictReader(res)

def hystory_url(self, ticker, startdate, enddate, field = None):
raise NotImplementedError

def allfields(self, ticker = None):
return ['Close','Open','Low','High','Volume']

def load(self, symbol, startdate, enddate, logger, backend, **kwargs):
from ccy import dateFromString
ticker = symbol.ticker
Expand All @@ -65,7 +60,7 @@ def load(self, symbol, startdate, enddate, logger, backend, **kwargs):
datestr = k
continue
fields[str(k).upper()] = []

dt = dateFromString(r[datestr])
dates.append(dt)
for k,v in r.items():
Expand All @@ -74,42 +69,42 @@ def load(self, symbol, startdate, enddate, logger, backend, **kwargs):
fields[k].append(float(v))
except:
continue

field = field or 'CLOSE'
return {'date': dates,
'value': fields.get(str(field).upper(),None)}


class google(WebCsv):
baseurl = 'http://finance.google.com/finance'

def getdate(self, st, dte):
m = short_month[dte.month-1]
return '%s=%s+%s,+%s' % (st,m,dte.day,dte.year)

def hystory_url(self, ticker, startdate, enddate, field = None):
b = self.baseurl
st = self.getdate('startdate', startdate)
et = self.getdate('enddate', enddate)
return '%s/historical?q=%s&%s&%s&output=csv' % (b,ticker,st,et)

def weblink(self, ticker):
return '%s?q=%s' % (self.baseurl,ticker)


class yahoo(WebCsv):
baseurl = 'http://ichart.yahoo.com'

def getdate(self, st, dte):
return '%s=%s&%s=%s&%s=%s' % (st[0],dte.month-1,st[1],
dte.day,st[2],dte.year)

def hystory_url(self, ticker, startdate, enddate):
b = self.baseurl
st = self.getdate(('a','b','c'), startdate)
et = self.getdate(('d','e','f'), enddate)
return '%s/table.csv?s=%s&%s&%s&g=d&ignore=.csv' % (b,ticker,st,et)

def weblink(self, ticker):
return 'http://finance.yahoo.com/q?s=%s' % ticker

Expand Down
Loading

0 comments on commit 9b83fab

Please sign in to comment.