def plot_contrast(self, moneyrecord1, moneyrecord2, date_ls, startdate, enddate): startdate = self.datetransfer(startdate) date_ls_change = [] for i in date_ls: if int(i) >= int(startdate) and int(i) <= int(enddate): date_ls_change.append(i) mondays = WeekdayLocator(MONDAY) months = MonthLocator(range(1, 13), bymonthday=1, interval=3) monthsFmt = DateFormatter("%b '%y") print(date_ls_change) #print(date_ls) date = [str(e) for e in date_ls_change] date_ls = [ datetime.datetime.strptime(d, '%Y%m%d').date() for d in date ] algo_pv = moneyrecord1['money'].tolist() print(algo_pv) bench_pv = moneyrecord2['moneybenchmark'].tolist() fig, ax = plt.subplots() plt.gca().set_color_cycle(['blue', 'red']) ax.plot_date(date_ls, algo_pv, '-') ax.plot_date(date_ls, bench_pv, '-') months.MAXTICKS = 5000 mondays.MAXTICKS = 5000 ax.xaxis.set_major_locator(months) ax.xaxis.set_major_formatter(monthsFmt) ax.xaxis.set_minor_locator(mondays) ax.autoscale_view() ax.grid(True) fig.autofmt_xdate() plt.title('P&L') plt.legend(['strategy', 'benchmark'], loc='upper left') plt.show()
def plot(outright_index, fly_index, hedged_index): date = m["DateTime"].tolist() outright_prices = m[outright_index].values.tolist() fly_prices = m[fly_index].values.tolist() hedged_prices = m[hedged_index].values.tolist() upper_band = (m[hedged_index+"_mean"]+EntryStd*m[hedged_index+"_std"]).values.tolist() lower_band = (m[hedged_index+"_mean"]-EntryStd*m[hedged_index+"_std"]).values.tolist() buy_dates = [x[0] for x in trade_recorder[hedged_index].trades if x[2]>0] buy_prices = [x[1] for x in trade_recorder[hedged_index].trades if x[2]>0] sell_dates = [x[0] for x in trade_recorder[hedged_index].trades if x[2]<0] sell_prices = [x[1] for x in trade_recorder[hedged_index].trades if x[2]<0] fig, (ax1, ax2, ax3) = plt.subplots(3, sharex = True) mondays = WeekdayLocator(MONDAY) mondays.MAXTICKS = 2000 alldays = DayLocator() # minor ticks on the days alldays.MAXTICKS = 2000 weekFormatter = DateFormatter('%b %d') # e.g., Jan 12 dayFormatter = DateFormatter('%d') # e.g., 12 ax1.xaxis.set_major_locator(mondays) ax1.xaxis.set_minor_locator(alldays) ax1.xaxis.set_major_formatter(weekFormatter) ax1.plot(date, outright_prices) ax2.plot(date, fly_prices) ax3.plot(date, hedged_prices) ax3.plot(date, upper_band, color='k') ax3.plot(date, lower_band, color='k') ax3.plot(buy_dates, buy_prices, "^", markersize = 5, color='m') ax3.plot(sell_dates, sell_prices, "v", markersize = 5, color='k') ax1.xaxis_date() ax1.autoscale_view()
def plot_contrast(self,moneyrecord1,moneyrecord2,date_ls,startdate,enddate): startdate=self.datetransfer(startdate) date_ls_change=[] for i in date_ls: if int(i)>=int(startdate) and int(i)<=int(enddate): date_ls_change.append(i) mondays = WeekdayLocator(MONDAY) months = MonthLocator(range(1, 13), bymonthday=1, interval=3) monthsFmt = DateFormatter("%b '%y") print(date_ls_change) #print(date_ls) date = [str(e) for e in date_ls_change] date_ls = [datetime.datetime.strptime(d,'%Y%m%d').date() for d in date] algo_pv=moneyrecord1['money'].tolist() print(algo_pv) bench_pv=moneyrecord2['moneybenchmark'].tolist() fig, ax = plt.subplots() plt.gca().set_color_cycle(['blue', 'red']) ax.plot_date(date_ls, algo_pv,'-') ax.plot_date(date_ls, bench_pv,'-') months.MAXTICKS=5000 mondays.MAXTICKS=5000 ax.xaxis.set_major_locator(months) ax.xaxis.set_major_formatter(monthsFmt) ax.xaxis.set_minor_locator(mondays) ax.autoscale_view() ax.grid(True) fig.autofmt_xdate() plt.title('P&L') plt.legend(['strategy', 'benchmark'], loc='upper left') plt.show()
def plot_trade(self, symbol, init_index = None, end_index=None, PnL=False, ATR=False): if init_index == None: init_index = 0 init_date = b.bars[symbol][init_index]["D"] if end_index == None: end_index = len(b.bars[symbol]) end_date = b.bars[symbol][end_index-1]["D"] mondays = WeekdayLocator(MONDAY) mondays.MAXTICKS = 2000 alldays = DayLocator() # minor ticks on the days alldays.MAXTICKS = 2000 weekFormatter = DateFormatter('%b %d') # e.g., Jan 12 dayFormatter = DateFormatter('%d') # e.g., 12 history = [(date2num(x["D"]), x["O"], x["H"], x["L"], x["C"]) for x in b.bars[symbol][init_index:end_index]] if PnL or ATR: fig, (ax, ax2) = plt.subplots(2, sharex = True) else: fig, ax = plt.subplots() ax.xaxis.set_major_locator(mondays) ax.xaxis.set_minor_locator(alldays) ax.xaxis.set_major_formatter(weekFormatter) candlestick_ohlc(ax, history) buys = [(x["Date"], x["Price"]) for x in self.trades[symbol] if (x["Lots"] > 0 and x["Date"] > init_date and x["Date"]<end_date) ] buy_dates, buy_values = zip(*buys) ax.plot(buy_dates, buy_values, "^", markersize = 5, color='m') sells = [(x["Date"], x["Price"]) for x in self.trades[symbol] if (x["Lots"] < 0 and x["Date"] > init_date and x["Date"]<end_date)] sell_dates, sell_values = zip(*sells) ax.plot(sell_dates, sell_values, "v", markersize = 5, color='k') ax.xaxis_date() ax.autoscale_view() if PnL: equity_history = [(date, record["equity"], record["drawdown"]) for date, record in self.history.items()] dates, values, drawdowns = zip(*equity_history) ax2 = fig.add_subplot(212) ax2.plot(dates, values, 'b-') elif ATR: equity_history = [(x["D"], x["ATR"]) for x in self.bars[symbol][init_index:end_index]] dates, values = zip(*equity_history) ax2 = fig.add_subplot(212) ax2.plot(dates, values, 'b-') ## ax3 = ax2.twinx() ## ax3.plot(dates, drawdowns, 'r-') plt.show()
def plot_alpha(self, alpha, date_ls, startdate, enddate): startdate = self.datetransfer(startdate) date_ls_change = [] for i in date_ls: if int(i) >= int(startdate) and int(i) <= int(enddate): date_ls_change.append(i) mondays = WeekdayLocator(MONDAY) months = MonthLocator(range(1, 13), bymonthday=1, interval=3) monthsFmt = DateFormatter("%b '%y") #print(date_ls) date = [str(e) for e in date_ls_change] date_ls = [ datetime.datetime.strptime(d, '%Y%m%d').date() for d in date ] algo_pv = alpha['alpha'].tolist() #print(algo_pv) fig, ax = plt.subplots() plt.gca().set_color_cycle(['blue', 'red']) ax.plot_date(date_ls, algo_pv, '-') months.MAXTICKS = 5000 mondays.MAXTICKS = 5000 ax.xaxis.set_major_locator(months) ax.xaxis.set_major_formatter(monthsFmt) ax.xaxis.set_minor_locator(mondays) ax.autoscale_view() ax.grid(True) fig.autofmt_xdate() plt.title('Alpha') plt.legend(['alpha'], loc='upper left') plt.show() #KJ_profit_df=pd.DataFrame(columns=[i/10 for i in range(1,121)]) #for K in list_K: #for J in list_J: #KJ=B(cash,account,startdate,enddate,K,J) #KJ_total_portfolio=KJ.test(startdate,enddate,K,J,cash) #KJ_profit_df.loc[10*J-1,K]=KJ_total_portfolio #KJ_profit_df.to_csv('KJ_finalprofit.csv')
def plot_alpha(self,alpha,date_ls,startdate,enddate): startdate=self.datetransfer(startdate) date_ls_change=[] for i in date_ls: if int(i)>=int(startdate) and int(i)<=int(enddate): date_ls_change.append(i) mondays = WeekdayLocator(MONDAY) months = MonthLocator(range(1, 13), bymonthday=1, interval=3) monthsFmt = DateFormatter("%b '%y") #print(date_ls) date = [str(e) for e in date_ls_change] date_ls = [datetime.datetime.strptime(d,'%Y%m%d').date() for d in date] algo_pv=alpha['alpha'].tolist() #print(algo_pv) fig, ax = plt.subplots() plt.gca().set_color_cycle(['blue', 'red']) ax.plot_date(date_ls, algo_pv,'-') months.MAXTICKS=5000 mondays.MAXTICKS=5000 ax.xaxis.set_major_locator(months) ax.xaxis.set_major_formatter(monthsFmt) ax.xaxis.set_minor_locator(mondays) ax.autoscale_view() ax.grid(True) fig.autofmt_xdate() plt.title('Alpha') plt.legend(['alpha'], loc='upper left') plt.show() #KJ_profit_df=pd.DataFrame(columns=[i/10 for i in range(1,121)]) #for K in list_K: #for J in list_J: #KJ=B(cash,account,startdate,enddate,K,J) #KJ_total_portfolio=KJ.test(startdate,enddate,K,J,cash) #KJ_profit_df.loc[10*J-1,K]=KJ_total_portfolio #KJ_profit_df.to_csv('KJ_finalprofit.csv')