def test_execution(self): events_queue = queue.Queue(100) oe = OrderEvent('BTC_ETC', 'MKT', 1.0, 'BUY') e = SimulatedExecutionHandler(events_queue, portfolio=None) e.execute_order(oe) ee = events_queue.get(False) self.assertEqual(ee.type, 'FILL') self.assertEqual(ee.direction, 'BUY') self.assertEqual(ee.exchange, 'BACKTEST') self.assertEqual(ee.quantity, 1.0)
def setUp(self): self.events = queue.Queue() self.status = dict() self.portfolio = PortfolioLocal(self.status) self.execution = SimulatedExecutionHandler(self.status) self.manager = Manager(self.status, self.events, self.execution, self.portfolio, None) self.event_src = DukascopyCSVPriceHandler("EUR_USD", self.events)
def __init__(self, data, strategy, portfolio): self.events = events self.Feed = data self.strategy = strategy self.portfolio = portfolio self.broker = SimulatedExecutionHandler(commission=None) self.cur_holdings = self.portfolio.current_holdings self.cur_positions = self.portfolio.current_positions self.all_holdings = self.portfolio.all_holdings self.all_positions = self.portfolio.all_positions self.initial_capital = self.portfolio.initial_capital self._activate = {} self._activate['print_order'] = False self._activate['print_stats'] = False self._activate['full_stats'] = False
def __init__(self, bars=None, strategy=None, port=None, broker=None, start_date=None, end_date=None): if bars is None: bars = CoinDataHandler(self, ['okcoinUSD']) if strategy is None: strategy = BuyAndHoldStrategy(bars, self) if port is None: port = NaivePortfolio(bars, self, '2017-1-1') if broker is None: broker = SimulatedExecutionHandler(self) self.bars = bars self.strategy = strategy self.port = port self.broker = broker self.__event_queue = Queue() self.__thread = Thread(target=self.__run) self.__active = False self.__handlers = { 'MARKET': [self.__filte_market_event], 'SIGNAL': [port.update_signal], 'ORDER': [broker.execute_order], 'FILL': [port.update_fill] } if start_date is not None: try: sd = datetime.datetime.strptime(start_date + " 00:00:00", "%Y-%m-%d %H:%M:%S") except ValueError: print( "Parameter start_date can't be parsed by datetime.strptime," "start_date will equal to None.") sd = None if end_date is not None: try: ed = datetime.datetime.strptime(end_date + " 00:00:00", "%Y-%m-%d %H:%M:%S") except ValueError: print( "Parameter end_date can't be parsed by datetime.strptime," "end_date will equal to None.") ed = None self.__start_date = sd self.__end_date = ed
def test(): events=Queue() csv_dir='/Users/weileizhang/Downloads/' symbol_list=['XOM'] #start_date=datetime.datetime(2013,1,1) #end_date=datetime.datetime(2015,1,5) #bars=HistoricCSVDataHandler(events,csv_dir,symbol_list,start_date,end_date) start_date='2010-1-1' end_date='2015-1-1' bars=DataBaseDataHandler(events,symbol_list,start_date,end_date) # strategy=BuyAndHoldStrategy(bars,events) strategy=MovingAverageCrossStrategy(bars,events,short_window=100,long_window=400) port=NaivePortfolio(bars,events,start_date,initial_capital=100000.0) broker=SimulatedExecutionHandler(events) while True: if bars.continue_backtest==True: bars.update_bars() else: break while True: try: event=events.get(False) except: break else: if event is not None: if event.type=='MARKET': strategy.calculate_signals(event) port.update_timeindex(event) elif event.type=='SIGNAL': port.update_signal(event) elif event.type=='ORDER': broker.execute_order(event) elif event.type=='FILL': port.update_fill(event) port.create_equity_curve_dataframe() stats=port.output_summary_stats() print(stats)
def __init__(self, strategy): self.events = events self.strategy = strategy self.Feed = self.strategy.portfolio.bars self.portfolio = self.strategy.portfolio self.broker = SimulatedExecutionHandler(commission=0) self.commission = 0 self.cur_holdings = self.portfolio.cur_holdings self.cur_positions = self.portfolio.cur_posit self.all_holdings = self.portfolio.all_holdings self.all_positions = self.portfolio.all_positions self.initial_capital = self.portfolio.initial_capital self.leverage = self.portfolio.leverage self._activate = {} self._activate['print_order'] = False self._activate['print_stats'] = False self._activate['full_stats'] = False
def test_execution_stop_order(self): csv_dir = './tests/datasets/' symbol_list = [ 'BTC_ETC', ] events_queue = queue.Queue(100) bars = HistoricCSVDataHandler(events_queue, csv_dir, symbol_list, ['open', 'high', 'low', 'close']) p = NaivePortfolio(bars, events_queue, datetime(2017, 4, 1, 0, 0, 0), 1000.0) e = SimulatedExecutionHandler(events_queue, portfolio=p) bars.update_bars() oe = OrderEvent('BTC_ETC', 'STP', 300000.0, 'BUY', 0.00259) e.execute_order(oe) self.assertTrue(len(e.stop_orders) > 0) bars.update_bars() ee = events_queue.get(False) e.check_stop_orders(ee) p.update_timeindex(ee) p.create_equity_curve_dataframe() self.assertEqual(p.equity_curve['equity_curve'][-1], 1.0001020000000001)
# 管理市场信息 market = BackTestMarket(backtestlogger,timemodule,events) market.initdata() # 策略模块 imp_module = __import__(StrategyFile) classname = StrategyName StrategyFunc = getattr(imp_module, classname) strategy = StrategyFunc(backtestlogger,market,events) # 持仓模块 portfolio = NaivePortfolio(backtestlogger,tradelogger,market,events) # 执行模块 excution = SimulatedExecutionHandler(backtestlogger,events) while True: # Update the bars (specific backtest code, as opposed to live trading) market.update() if market.cotinue_backtest == False: break # Handle the events while True: # 获取待处理的事件,如果队列空就结束循环 if events.qsize() == 0: break else: event = events.get(False)
import time from queue import Queue from data import HistoricPDDataHandler from strategy import BuyAndHoldStrategy, MeanReversionStrategy from execution import SimulatedExecutionHandler from portfolio import NaivePortfolio events = Queue() bars = HistoricPDDataHandler(events, "../data", ["fb"]) # strategy = BuyAndHoldStrategy(bars, events) strategy = MeanReversionStrategy(bars, events, short_window=5) # def __init__(self, bars, events, start_date, initial_capital=100000.0): port = NaivePortfolio(bars, events, "2018-10-3", 100000) broker = SimulatedExecutionHandler(events) print("Days: 50, Strategy: Mean-Reversion, Stock: FB") print("Press Enter to Start...") input() while bars.continue_backtest == True: # print ("Updating Bars.") bars.update_bars() while True: if events.empty(): break else: event = events.get(False)
class OnePiece(): def __init__(self, strategy): self.events = events self.strategy = strategy self.Feed = self.strategy.portfolio.bars self.portfolio = self.strategy.portfolio self.broker = SimulatedExecutionHandler(commission=0) self.commission = 0 self.cur_holdings = self.portfolio.cur_holdings self.cur_positions = self.portfolio.cur_posit self.all_holdings = self.portfolio.all_holdings self.all_positions = self.portfolio.all_positions self.initial_capital = self.portfolio.initial_capital self.leverage = self.portfolio.leverage self._activate = {} self._activate['print_order'] = False self._activate['print_stats'] = False self._activate['full_stats'] = False def sunny(self): while True: try: event = self.events.get(False) except Queue.Empty: self.Feed.update_bars() self.portfolio._update_timeindex() else: if event is not None: if event.type == 'Market': self.strategy._context() self.strategy.Execute_list() self.strategy.luffy() if event.type == 'Signal': self.portfolio.update_signal(event) if event.type == 'Order': One_lot_depo = 100000.0 / self.leverage * marginRate[ event.symbol] + self.commission if 'EXIT' in event.signal_type: self.broker.execute_order(event) elif (self.all_holdings[-1]['cash'] > event.quantity_l * One_lot_depo and self.all_holdings[-1]['cash'] > event.quantity_s * One_lot_depo): self.broker.execute_order(event) # print order if self._activate['print_order']: event.print_order() else: if self._activate['print_order']: print "Cash is not enough!!!!!!!!!!!!!!!!" event.cancel_order() if event.type == 'Fill': self.portfolio.update_fill(event) if self._activate['print_order']: event.print_executed() if self.Feed.continue_backtest == False: print 'Final Portfolio Value: ' + str( self.all_holdings[-1]['total']) if self._activate['print_stats']: self.portfolio.create_equity_curve_df() print self.portfolio.output_summary_stats() if self._activate['full_stats']: self.get_analysis() break def print_trade(self): self._activate['print_order'] = True def print_stats(self, full=False): self._activate['print_stats'] = True if full: self._activate['full_stats'] = True def get_equity_curve(self): df = self.portfolio.create_equity_curve_df() df = df.join(self.get_all_holdings()['total']) df.index = pd.DatetimeIndex(df.index) df.drop('1993-08-07', inplace=True) return df def get_analysis(self): ori_tlog = self.get_log(True) tlog = self.get_log() dbal = self.get_equity_curve() start = self.get_equity_curve().index[0] end = self.get_equity_curve().index[-1] capital = self.initial_capital print stats(tlog, ori_tlog, dbal, start, end, capital) def get_log(self, exit=False): # Original log, include exit_order ori_tlog = pd.DataFrame(self.portfolio.trade_log) ori_tlog.set_index('datetime', inplace=True) ori_tlog.index = pd.DatetimeIndex(ori_tlog.index) # generate PnL, perfect log log, n = generate_perfect_log( tlog=ori_tlog, latest_bar_dict=self.Feed.latest_bar_dict) df = pd.DataFrame(log) df.set_index('datetime', inplace=True) df.index = pd.DatetimeIndex(df.index) df.sort_index(inplace=True) if exit: print 'Still_Opening_trades: ' + str(n) return ori_tlog[[ 'symbol', 's_type', 'price', 'qty', 'cur_positions', 'cash', 'total' ]] else: return df[[ 'symbol', 's_type', 'price', 'qty', 'cur_positions', 'exit_date', 'period', 'cash', 'PnL', 'total' ]] def set_commission(self, commiss): self.broker = SimulatedExecutionHandler(commission=commiss) self.commission = commiss ####################### from portfolio ############################### def get_cur_holdings(self): return pd.DataFrame(self.cur_holdings) def get_cur_posit(self): return pd.DataFrame(self.cur_positions) def get_all_holdings(self): df = pd.DataFrame(self.all_holdings) df.set_index('datetime', inplace=True) df.drop('1993-08-07', inplace=True) df.index = pd.DatetimeIndex(df.index) return df def get_all_positions(self): df = pd.DataFrame(self.all_positions) df.set_index('datetime', inplace=True) df.drop('1993-08-07', inplace=True) df.index = pd.DatetimeIndex(df.index) return df def get_symbol_list(self): return self.portfolio.symbol_list def get_initial_capital(self): return self.initial_capital def plot(self, symbol, engine='plotly', notebook=False): data = plotter(latest_bar_dict=self.Feed.latest_bar_dict, enquity_curve=self.get_equity_curve(), tlog=self.get_log(), positions=self.get_all_positions(), holdings=self.get_all_holdings()) data.plot(symbol=symbol, engine=engine, notebook=notebook) def plot_log(self, symbol, engine='plotly', notebook=False): data = plotter(latest_bar_dict=self.Feed.latest_bar_dict, enquity_curve=self.get_equity_curve(), tlog=self.get_log(), positions=self.get_all_positions(), holdings=self.get_all_holdings()) data.plot_log(symbol=symbol, engine=engine, notebook=notebook)
def set_commission(self, commiss): self.broker = SimulatedExecutionHandler(commission=commiss) self.commission = commiss
import time import queue from data import HistoricCSVDataHandler from portfolio import NaivePortfolio from strategy import BuyAndHoldStrategy, StatArbitrageStrategy from execution import SimulatedExecutionHandler events_queue = queue.Queue(100) broker = SimulatedExecutionHandler(events_queue) bars = HistoricCSVDataHandler(events_queue, './datasets/', ['BTC_ETC', 'BTC_LTC'], fields=[ 'open', 'high', 'low', 'close', ]) strategy = StatArbitrageStrategy(bars, events_queue) port = NaivePortfolio(bars, events_queue, None, 1000.0) def run(heartbeat=3): while True: # Update the bars (specific backtest code, as opposed to live trading) if bars.continue_backtest: bars.update_bars() else: break # Handle the events while True:
portfolio.rpnl.plot() plt.grid(True) plt.show() if __name__ == "__main__": events = queue.Queue() # 同期キュー status = dict() # tick をまたいで記憶しておきたい情報 status["is_sim"] = True portfolio = PortfolioLocal(status) execution = SimulatedExecutionHandler(status) timeseries = TimeSeries(status) strategy = OLSTIME(status) # strategy = SMAKNN(status) # strategy = MARTINRSI(status) # strategy = SMABOLPIPRSI(status) # strategy = SMABOLPIP(status) # strategy = SMAPIP(status) # strategy = SMAPIPRSI(status) # strategy = SMAOLSPIP(status) # strategy = SMABOL(status) # strategy = SMARSIOLS(status) # strategy = WMA(status) # strategy = SMAOLS(status)
def simulate(): events = queue.Queue() ##Set this directory as the one where you will store the .csv files by ticker symbol ex. FB.csv etc it will be named from root ##so a directory in your home folder might be /home/data where /data is the folder with the files directory = '/twoterradata' ab_path = os.path.abspath(directory) ##Below symbol list for stocks ##Could be modified for Futures symbol_list = ['FB', 'AAPL', 'GOOG'] ##list of thresholds to be set initially for each stock/futures symbols and passed in to the strategy class ##May need to think through whether these can work better with Order types of LMT, Trailing Orders etc for better execution # g_sell_gain_thresh = 0 # g_sell_loss_thresh = 0 # g_buy_thresh = 0 # g_buy_again_thresh = 0 # g_incr is the global_thresholds = { 'g_sell_gain_thresh': 0, 'g_sell_loss_thresh': 0, 'g_buy_thresh': 0, 'g_buy_again_thresh': 0, 'g_incr': 0 } for s in symbol_list: global_symbol_thresholds[s] = global_thresholds ##Futures_list --would have to update code or use the current symbol_list variable modified for Futures ##Define these global thresholds for each value in the symbol ##Ensures person executes this tester on a Linux or Mac or uses a VM if platform.system() not in ['Linux', 'Darwin']: print "Program must run on Linux/Unix system or on Virtual Machine running such a system, please run again" quit() ##ensure you are logged into session at quandl or set the api key, but for WIKI dataset not necessary ##Below URL will be modifed to obtain futures dataset and most likely will be modified with database queries quandl_url = "\'https://www.quandl.com/api/v3/datasets/WIKI/" for s in symbol_list: if os.path.exists(ab_path + '/' + s + '.csv'): days_modified = (calendar.timegm(time.gmtime()) - os.path.getmtime(ab_path + '/' + s + '.csv')) if days_modified > 86400: cmd = "curl " + quandl_url + s + "/data.csv\'" + "> \'" + ab_path + '/' + s + ".csv\'" os.system(cmd) else: cmd = "curl " + quandl_url + s + "/data.csv\'" + "> \'" + ab_path + '/' + s + ".csv\'" os.system(cmd) print global_symbol_thresholds bars = HistoricCSVDataHandler(events, ab_path + '/', symbol_list) ##strategy = BuyAndHoldStrategy(bars, events) ##strategy for simple trends, this variable must be set as a list to test multiple strategies etc. ## Set strategy by modifying here strategy = SimpleTrendsStrategy(bars, events) port = NaivePortfolio(bars, events, "2015-11-18") broker = SimulatedExecutionHandler(events) while True: # Update the bars (specific backtest code, as opposed to live trading) if bars.continue_backtest == True: bars.update_bars() else: break # Handle the events while True: try: event = events.get(False) except queue.Empty: break else: if event is not None: if event.type == 'MARKET': strategy.calculate_signals(event) port.update_timeindex(event) elif event.type == 'SIGNAL': port.update_signal(event) elif event.type == 'ORDER': #event.print_order() broker.execute_order(event) elif event.type == 'FILL': port.update_fill(event) print port.output_summary_stats() print port.all_holdings[-1]
os.chdir("/home/taylor/backtester/") import Queue import time from data import HistoricCSVDataHandler from strategy import BuyAndHoldStrategy from portfolio import NaivePortfolio from execution import SimulatedExecutionHandler start_date = '2015-03-13' #figure out better solution for this events = Queue.Queue(maxsize=100) bars = HistoricCSVDataHandler(events, "/home/taylor/backtester/csv/", ['yhoo']) strategy = BuyAndHoldStrategy(bars, events) port = NaivePortfolio(bars, events, start_date, initial_capital=100000.) broker = SimulatedExecutionHandler(events) while True: # Update the bars (specific backtest code, as opposed to live trading) if bars.continue_backtest == True: bars.update_bars() else: break # Handle the events while True: try: event = events.get(False) except Queue.Empty: break else:
import event import Queue from datasource import HistoricTDXDataHandler from execution import SimulatedExecutionHandler from Tradingstrategy import BollLineMinutesStrategy import portfolio events = Queue.Queue() # first with a signal stock to go through testing symbol_list = ["300024", "002594", "002415"] datadir = "D:\\tools\\new_tdx\\vipdoc\\sz\\minline" bars = HistoricTDXDataHandler(events, datadir, symbol_list) bars.load_bars(480) # load trading datum of two days minutes strategy = BollLineMinutesStrategy(bars, events) port = portfolio.TrendPortfolio(bars, events, "2015-02-06") broker = SimulatedExecutionHandler( events) # reinforce the Order execution,polish # when deal with the real time transactions while True: # Update the bars (specific backtest code, as opposed to live trading) if bars.continue_backtest == True: bars.update_bars() else: break # Handle the events while True: try: event = events.get(False) except Queue.Empty: break