import datetime as dt import numpy as np import pandas as pd from dyplot.dygraphs import Dygraphs if __name__ == '__main__': foo = np.random.rand(100) boo = pd.Series(foo, index=range(0,100)) dg = Dygraphs(boo.index, "date") dg.plot(series="Random", mseries=boo) dg.set_options(title="Tutorial 0") div = dg.savefig(csv_file="tutorial0.csv", div_id="demodiv", js_vid="jid", html_file="tutorial0.html") print(div)
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")
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")
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")
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")
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(2014, 9, 1) dt_end = dt.datetime(2014, 12, 31) ls_symbols = ['AAPL'] ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday) all_stocks = get_tickdata(ls_symbols=ls_symbols, ldt_timestamps=ldt_timestamps) p = Portfolio(all_stocks, 0, ldt_timestamps) p.normalized_price(tick="AAPL") dg = Dygraphs(ldt_timestamps, "date") dg.candleplot(open=p.equities["AAPL"]['open'], high=p.equities["AAPL"]['high'], low=p.equities["AAPL"]['low'], close=p.equities["AAPL"]['close']) dg.set_options(title="Tutorial 7") div = dg.savefig(csv_file="tutorial7.csv", html_file="tutorial7.html")
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(2014, 9, 1) dt_end = dt.datetime(2014, 12, 31) ls_symbols = ['AAPL'] ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday) all_stocks = get_tickdata(ls_symbols=ls_symbols, ldt_timestamps=ldt_timestamps, source="Google") p = Portfolio(all_stocks, 0, ldt_timestamps) dg = Dygraphs(ldt_timestamps, "date") dg.candleplot(open=p.equities["AAPL"]['open'], high=p.equities["AAPL"]['high'], low=p.equities["AAPL"]['low'], close=p.equities["AAPL"]['close']) dg.plot(series="10D MA", mseries=p.moving_average(window=20, tick="AAPL")) dg.set_options(title="Tutorial 8") div = dg.savefig(csv_file="tutorial8.csv", html_file="tutorial8.html")
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)
def backtesting(self): market = self.benchmark buy_price = {} output = '<table class="table table-bordered">\n' output += "<th>Date</th><th>Action</th><th>Ticker</th><th>Shares</th>" output += "<th>Price</th><th>Cash Before</th><th>Cash After</th><th>Gain</th>" for o in self.all_order: self.pf.cal_total(o.date) sdate = o.date.strftime("%Y-%m-%d") if o.action == "buy": output += "<tr>\n" if self.pf.cash[o.date] >= 200000.0: buy_size = 20000.0 elif self.pf.cash[o.date] >= 100000.0: buy_size = 10000.0 else: buy_size = 5000.0 if self.pf.cash[o.date] >= buy_size: shares = np.floor(buy_size/o.price) print("buy", shares, "of", o.tick, "at", o.price, o.date) ticka = '<a href=\"%s.html\">%s</a>' %(o.tick, o.tick) output += "<td>%s</td><td>buy</td><td>%s</td><td>%d</td><td>%f</td>" %(sdate,ticka,shares,o.price) output += "<td>%d</td>" %(self.pf.cash[o.date]) self.pf.buy(shares=shares, tick=o.tick, price=o.price, date=o.date, update_ol=True) output += "<td>%d</td>" %(self.pf.cash[o.date]) output += "<td></td>\n" buy_price[o.tick] = o.price else: shares = np.floor(buy_size/o.price) print("Not enough cash. Skip buying", shares, "of", o.tick, "at", o.price, "on", o.date) ticka = '<a href=\"%s.html\">%s</a>' %(o.tick, o.tick) output += "<td>%s</td><td>skip buy</td><td>%s</td><td>%d</td><td>%f</td>\n" %(sdate,ticka,shares,o.price) output += "<td></td><td></td><td></td>\n" else: shares = self.pf.equities[o.tick]['shares'][o.date] price = self.pf.equities[o.tick]['close'][o.date] if shares > 0: print("sell", shares, " of", o.tick, " at", o.price, o.date, ". A gain of", o.price/buy_price[o.tick]) gain = o.price/buy_price[o.tick] if gain > 1: output += "<tr class=\"success\">\n" else: output += "<tr class=\"danger\">\n" ticka = '<a href=\"%s.html\">%s</a>' %(o.tick, o.tick) output += "<td>%s</td><td>sell</td><td>%s</td><td>%d</td><td>%f</td>" %(sdate,ticka,shares,o.price) output += "<td>%d</td>" %(self.pf.cash[o.date]) self.pf.sell(date=o.date, tick=o.tick, shares=shares, price=price, update_ol=True) output += "<td>%d</td>\n" %(self.pf.cash[o.date]) output += "<td>%f</td>\n" %(gain) output += "</tr>\n" output += "</table>\n" fund_csv = os.path.join(self.args.dir, 'static', 'csv', self.args.subdir,'fund.csv') self.pf.csvwriter(equity_col=["shares"], csv_file=fund_csv, total=True, cash=True, d=',') fund_order_csv = os.path.join(self.args.dir, 'static', 'csv', self.args.subdir,'fund_order.csv') self.pf.write_order_csv(csv_file=fund_order_csv) ldt_timestamps = self.pf.ldt_timestamps() self.pf.cal_total(ldt_timestamps[-1]) dg = Dygraphs(ldt_timestamps, "date") dg.plot(series='Market', mseries = market.normalized('$RUA')) dg.plot(series="Portfolio", mseries = self.pf.total/self.pf.total[0]) csv_file = os.path.join(self.args.dir, 'static', 'csv', self.args.subdir,'fund_dygraph.csv') div_id = "fund_graphdiv" js_vid = 'gfund' dg.set_options(title="Fund Performance") csv_url = "/static/csv/" + self.args.subdir + "/" + "fund_dygraph.csv" fund_graph = dg.savefig(csv_file=csv_file, div_id=div_id, js_vid=js_vid, dt_fmt="%Y-%m-%d", csv_url=csv_url) return output, fund_graph
date_list = [x.date for x in order_list] date_list.sort() dt_start = date_list[0] dt_end = date_list[-1] tick_set = set([x.tick for x in order_list]) ls_symbols = ['$SPX'] while(tick_set): ls_symbols.append(tick_set.pop()) ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday) all_stocks = get_tickdata(ls_symbols=ls_symbols, ldt_timestamps=ldt_timestamps) pf = Portfolio(equities=all_stocks, cash=cash, dates=ldt_timestamps, order_list=order_list) pf.sim() equity_col = ['buy', 'sell', 'close'] pf.csvwriter(csv_file=value_file, d=',', cash=False) print("Details of the Performance of the portfolio :") print("Data Range :", ldt_timestamps[0], "to", ldt_timestamps[-1]) print("Sharpe Ratio of Fund :", pf.sharpe_ratio()) print("Sharpe Ratio of $SPX :", pf.sharpe_ratio('$SPX')) print("Total Return of Fund :", pf.return_ratio()) print(" Total Return of $SPX :", pf.return_ratio('$SPX')) print("Standard Deviation of Fund :", pf.std()) print(" Standard Deviation of $SPX :", pf.std('$SPX')) print("Average Daily Return of Fund :", pf.avg_daily_return()) print("Average Daily Return of $SPX :", pf.avg_daily_return('$SPX')) dg = Dygraphs(ldt_timestamps, "date") dg.plot(series='S&P 500', mseries=pf.normalized('$SPX')) dg.plot(series='Return', mseries=pf.normalized()) dg.set_options(title="Market Sim") dg.savefig(csv_file="marketsim.csv", html_file="marketsim.html")
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','XOM', 'MSFT', 'WMT'] 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") for tick in ls_symbols: dg.plot(series=tick, mseries=all_stocks.normalized(tick)) dg.set_options(title="Tutorial 2") div = dg.savefig(csv_file="tutorial2.csv", html_file="tutorial2.html")
import datetime as dt import numpy as np import pandas as pd from dyplot.dygraphs import Dygraphs if __name__ == '__main__': foo = np.random.rand(100) boo = pd.Series(foo, index=range(0, 100)) dg = Dygraphs(boo.index, "date") dg.plot(series="Random", mseries=boo) dg.set_options(title="Tutorial 0") div = dg.savefig(csv_file="tutorial0.csv", div_id="demodiv", js_vid="jid", html_file="tutorial0.html") print(div)
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', 'XOM', 'MSFT', 'WMT'] 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") for tick in ls_symbols: dg.plot(series=tick, mseries=all_stocks.normalized(tick)) dg.set_options(title="Tutorial 2") div = dg.savefig(csv_file="tutorial2.csv", html_file="tutorial2.html")
import datetime as dt from finpy.financial.equity import get_tickdata import finpy.utils.fpdateutil as du 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_stocks = get_tickdata(ls_symbols=ls_symbols, ldt_timestamps=ldt_timestamps) dg = Dygraphs(ldt_timestamps, "date") dg.plot(series="AAPL", mseries=all_stocks["AAPL"]['close'], axis='y2') dg.plot(series="$RUA", mseries=all_stocks["$RUA"]['close']) dg.set_options(title="Tutorial 3") div = dg.savefig(csv_file="tutorial3.csv", html_file="tutorial3.html")