Esempio n. 1
0
import os
import glob
import pandas as pd
import numpy as np
import dyplot as dp
import pylab as P
import matplotlib.pyplot as plt

import pandas as pd
from dyplot.dygraphs import Dygraphs
a = pd.Series([1,2,3,4,5,6,7,9,10])
b = pd.Series([1,3,5,9,2,8,5,5,15])
lc= pd.Series([1,3,4,5,6,7,9,3,2])
c = pd.Series([2,4,5,7,8,8,9,4,3])
hc= pd.Series([3,5,7,7,9,11,9,5,8])
dg = Dygraphs(a.index, "index")
dg.plot(series="a", mseries=a)
dg.plot(series="b", mseries=b)
dg.plot(series="c", mseries=c,lseries=lc, hseries=hc)
dg.set_options(title="Test")
dg.set_axis_options(axis="x",axisLabelColor="red")
div = dg.savefig(csv_file=os.getcwd() + "/plot_dynamic/tutorial1.csv", html_file=os.getcwd() + "/plot_dynamic/tutorial1.html")

Esempio n. 2
0
from finpy.financial.portfolio import Portfolio
from dyplot.dygraphs import Dygraphs
if __name__ == '__main__':
    dt_timeofday = dt.timedelta(hours=16)
    dt_start = dt.datetime(2010, 1, 1)
    dt_end = dt.datetime(2010, 12, 31)
    ls_symbols = ['AAPL', '$RUA']
    ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)
    all_data = get_tickdata(ls_symbols=ls_symbols,
                            ldt_timestamps=ldt_timestamps)
    all_stocks = Portfolio(all_data, 0, ldt_timestamps, [])
    dg = Dygraphs(ldt_timestamps, "date")
    dg.plot(series="AAPL", mseries=all_data["AAPL"]['close'], axis='y2')
    dg.plot(series="Russel 3000", mseries=all_data["$RUA"]['close'])
    max_ratio = max(
        all_stocks.normalized("AAPL").max(),
        all_stocks.normalized("$RUA").max())
    min_ratio = min(
        all_stocks.normalized("AAPL").min(),
        all_stocks.normalized("$RUA").min())
    max_ratio *= 1.05
    min_ratio *= 0.95
    dg.set_axis_options(axis='y', valueRange=[all_data["$RUA"]['close'][0]*min_ratio, \
        all_data["$RUA"]['close'][0]*max_ratio])
    dg.set_axis_options(axis='y2', valueRange=[all_data["AAPL"]['close'][0]*min_ratio, \
        all_data["AAPL"]['close'][0]*max_ratio])
    dg.annotate('AAPL', '2010-06-21', "B", "Buy on 2010-06-21")
    dg.annotate('AAPL', '2010-08-13', "S", "Sell on 2010-08-13")
    dg.set_options(title="Tutorial 4", ylabel="Russel 3000", y2label="AAPL")
    div = dg.savefig(csv_file="tutorial4.csv", html_file="tutorial4.html")
Esempio n. 3
0
import os
import glob
import pandas as pd
import numpy as np
import dyplot as dp
import pylab as P
import matplotlib.pyplot as plt

import pandas as pd
from dyplot.dygraphs import Dygraphs
a = pd.Series([1, 2, 3, 4, 5, 6, 7, 9, 10])
b = pd.Series([1, 3, 5, 9, 2, 8, 5, 5, 15])
lc = pd.Series([1, 3, 4, 5, 6, 7, 9, 3, 2])
c = pd.Series([2, 4, 5, 7, 8, 8, 9, 4, 3])
hc = pd.Series([3, 5, 7, 7, 9, 11, 9, 5, 8])
dg = Dygraphs(a.index, "index")
dg.plot(series="a", mseries=a)
dg.plot(series="b", mseries=b)
dg.plot(series="c", mseries=c, lseries=lc, hseries=hc)
dg.set_options(title="Test")
dg.set_axis_options(axis="x", axisLabelColor="red")
div = dg.savefig(csv_file=os.getcwd() + "/plot_dynamic/tutorial1.csv",
                 html_file=os.getcwd() + "/plot_dynamic/tutorial1.html")
Esempio n. 4
0
import datetime as dt
from finpy.financial.equity import get_tickdata
import finpy.utils.fpdateutil as du
from finpy.financial.portfolio import Portfolio
from dyplot.dygraphs import Dygraphs
if __name__ == '__main__':
    dt_timeofday = dt.timedelta(hours=16)
    dt_start = dt.datetime(2010, 1, 1)
    dt_end = dt.datetime(2010, 12, 31)
    ls_symbols = ['AAPL','$RUA']
    ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)
    all_data = get_tickdata(ls_symbols=ls_symbols, ldt_timestamps=ldt_timestamps)
    all_stocks = Portfolio(all_data, 0, ldt_timestamps, [])
    dg = Dygraphs(ldt_timestamps, "date") 
    dg.plot(series="AAPL", mseries=all_data["AAPL"]['close'], axis='y2')
    dg.plot(series="Russel 3000", mseries=all_data["$RUA"]['close'])
    max_ratio = max(all_stocks.normalized("AAPL").max(), all_stocks.normalized("$RUA").max())
    min_ratio = min(all_stocks.normalized("AAPL").min(), all_stocks.normalized("$RUA").min())
    max_ratio *= 1.05
    min_ratio *= 0.95
    dg.set_axis_options(axis='y', valueRange=[all_data["$RUA"]['close'][0]*min_ratio, \
        all_data["$RUA"]['close'][0]*max_ratio])
    dg.set_axis_options(axis='y2', valueRange=[all_data["AAPL"]['close'][0]*min_ratio, \
        all_data["AAPL"]['close'][0]*max_ratio])
    dg.annotate('AAPL', '2010-06-21', "B", "Buy on 2010-06-21")
    dg.annotate('AAPL', '2010-08-13', "S", "Sell on 2010-08-13")
    dg.set_options(title="Tutorial 4", ylabel="Russel 3000", y2label="AAPL")
    div = dg.savefig(csv_file="tutorial4.csv", html_file="tutorial4.html")
Esempio n. 5
0
 def algo_wrapper(self, tick):
     """ The wrapper for algo(). Generate various data for backtesting and viewing.
         :param tick: The ticker of the security.
         :return tick: The ticker of the security.
         :return return_ratio: The return ratio of the algorithm.
         :return stock_return: The return ratio of the security in the same preriod.
         :return pf.order: The order of t
     """
     dt_timeofday = dt.timedelta(hours=16)
     equities = get_tickdata(ls_symbols=[tick], ldt_timestamps=self.ldt_timestamps)
     pf, stat = self.algo(equities=equities, tick=tick)
     # Prepare Data for vaiour charts and diagrams
     csvfile = os.path.join(self.args.dir, 'static', 'csv', self.args.subdir,tick + '.csv')
     pf.csvwriter(equity_col=["shares", "close"], csv_file=csvfile, total=True, cash=True, d=',')
     csvfile = os.path.join(self.args.dir, 'static', 'csv', self.args.subdir,tick + '_order.csv')
     pf.write_order_csv(csv_file=csvfile)
     stdev = pf.rolling_normalized_stdev(tick)
     market_nml = self.benchmark.normalized(self.benchmark_tick)
     market_dygraph = ";" + market_nml.map(str) + ";" 
     tick_nml = pf.normalized(tick) 
     total_nml = pf.total/pf.total[0]
     algo = tick + ' ALGO'
     ba = pf.bollinger_band(tick)
     ba_hi_nml = ba['hi']/pf.equities[tick]['close'][0]
     ba_lo_nml = ba['lo']/pf.equities[tick]['close'][0]
     col = ['b', 'r', 'g', 'c', 'y', 'm']
     buy_list = [x.date for x in pf.order if x.action == "buy"]
     buy_price = pf.normalized(tick)[buy_list]
     sell_list = [x.date for x in pf.order if x.action == "sell"]
     sell_price = pf.normalized(tick)[sell_list]
     dg = Dygraphs(tick_nml.index, "date")
     dg.plot(series=tick, mseries = tick_nml, lseries = ba_lo_nml, hseries = ba_hi_nml)
     dg.plot(series=algo, mseries = total_nml)
     dg.plot(series=self.benchmark_tick, mseries = market_nml)
     for o in pf.order:
         text = tick + '@' + str(pf.equities[tick]['close'][o.date]) + ' on ' \
             + o.date.strftime("%Y-%m-%d")
         dg.annotate(tick, o.date.strftime("%Y-%m-%d"), o.action[0].upper(), text)
     csv_file = os.path.join(self.args.dir, 'static', 'csv', self.args.subdir, tick + "_dygraph.csv")
     csv_url = "/static/csv/" + self.args.subdir + "/" + tick + "_dygraph.csv"
     div_id = "id0"
     js_vid = 'dyg' 
     dg.set_options(title=tick, ylabel="Ratio")
     dg.set_axis_options("x", drawAxis = False)
     div = dg.savefig(csv_file=csv_file, div_id=div_id, js_vid=js_vid, dt_fmt="%Y-%m-%d", csv_url=csv_url)
     gstd = Dygraphs(self.ldt_timestamps, "date")
     gstd.plot(series=tick, mseries= stdev)
     csv_file = os.path.join(self.args.dir, 'static', 'csv', self.args.subdir, tick + "_std.csv")
     csv_url = "/static/csv/" + self.args.subdir + "/" + tick + "_std.csv"
     div_id = "id1"
     js_vid = 'std' 
     gstd.set_options(ylabel="Volatility", digitsAfterDecimal="4", height=120)
     divstd = gstd.savefig(csv_file=csv_file, div_id=div_id, js_vid=js_vid, dt_fmt="%Y-%m-%d", height="100ptx", csv_url=csv_url)
     fail = 0
     succ = 0
     pie = ""
     for x in stat:
         if x.sell_date != None:
             if x.sell_price > x.buy_price:
                 succ += 1
             else:
                 fail += 1
     if succ+fail != 0:
         succ_frac = succ*100/(succ+fail)
         fail_frac = fail*100/(succ+fail)
         labels = 'Success', 'Fail'
         gpie = Pie([succ_frac, fail_frac], labels=labels)
         div_id = tick + "_pie"
         js_vid = 'pie' + tick
         pie = gpie.savefig(div_id=div_id, js_vid=js_vid)
     return_ratio = pf.return_ratio()
     stock_return = pf.equities[tick]['close'][-1]/pf.equities[tick]['close'][0]
     return (tick, return_ratio, stock_return, pf.order, equities[tick], stat, div, divstd, pie)