Пример #1
0
 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)
Пример #2
0
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)
Пример #3
0
    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)
Пример #4
0
    if market.cotinue_backtest == False:
        break
    # Handle the events
 
    while True:
        # 获取待处理的事件,如果队列空就结束循环
        if  events.qsize() == 0:
            break
        else:
            event = events.get(False)
        
        # 计算信号    
        if event.type == 'MARKET':
            strategy.calculate_signals(event)
        # 产生信号
        elif event.type =='SIGNAL':
            portfolio.update_signal(event)
        # 执行订单
        elif event.type =='ORDER':   
            excution.execute_order(event)
        # 更新持仓
        elif event.type == 'FILL':
            portfolio.update_fill(event) 

            
    #一天结束了 我们要更新持仓一下
    portfolio.update_after_close()
    
    #看看持仓吧~
    portfolio.ShowPosition()            
Пример #5
0
while bars.continue_backtest == True:

    # print ("Updating Bars.")
    bars.update_bars()

    while True:
        if events.empty():
            break
        else:
            event = events.get(False)
            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)

    time.sleep(0.1)

result_stats = port.output_summary_stats()
print('==========================================')
print(result_stats)
Пример #6
0
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)
Пример #7
0
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]
Пример #8
0
        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':
                    print 'MARKET!'
                    strategy.calculate_signals(event)
                    port.update_timeindex(event)

                elif event.type == 'SIGNAL':
                    print 'SIGNAL!'
                    port.update_signal(event)

                elif event.type == 'ORDER':
                    print 'ORDER!'
                    broker.execute_order(event) #problem

                elif event.type == 'FILL':
                    print 'FILL!'
                    port.update_fill(event)

    #time.sleep(.1)