def __init__(self, csv_file, strategy, strategy_params={}, plot_date=None, balance=10000.0, target_date=None): # init assets print('\nInitializing backtest.') self.balance_manager = BalanceManager(balance) self.order_manager = OrderManager(self.balance_manager) self.strategy = strategy(self.order_manager, **strategy_params) # load data print('Loading data.') self.data = pd.read_csv( csv_file, names=['datetime', 'volume', 'open', 'high', 'low', 'close']).to_numpy() # add target data self.plot_date = plot_date self.target_date = target_date if target_date is not None: self.plot_date = target_date else: self.plot_date = plot_date # add plot data self.metrics = {} self.sub_metrics = {} self.long_markers = [] self.short_markers = [] print('Backtest ready.')
class OrderScanner(object): def __init__(self, config, *args, **kw): key_config = config['apikey'] self.order_manager = OrderManager(*args, **kw) self.strategy_manager = StrategyManager(*args, **kw) self.exchanges = { 'zb': Zb(key_config['zb']['key'], key_config['zb']['secret']), 'okex': Okex(key_config['okex']['key'], key_config['okex']['secret']) } def scan(self): try: while True: placed_orders = self.order_manager.list_by_status(ORDER_STATUS.PLACED) for order in placed_orders: api = self.exchanges[order.exchange] symbol = order.coin order_info = api.order_info(symbol, order.ex_id) if order_info.status == ORDER_STATUS.SUCCESS: logging.info("update order success %s, %s, %s" % (order.exchange, order.id, order.ex_id)) self.order_manager.success(order['id']) elif order_info.status == ORDER_STATUS.CANCELLED: logging.info("update order cancel %s, %s, %s" % (order.exchange, order.id, order.ex_id)) self.order_manager.update_status(order['id'], ORDER_STATUS.CANCELLED) except: logging.exception("") time.sleep(2)
def __init__(self, config, *args, **kw): key_config = config['apikey'] self.order_manager = OrderManager(*args, **kw) self.strategy_manager = StrategyManager(*args, **kw) self.exchanges = { 'zb': Zb(key_config['zb']['key'], key_config['zb']['secret']), 'okex': Okex(key_config['okex']['key'], key_config['okex']['secret']) }
def __init__(self, session): self.session = session self.logged_in = False self.user = None self.is_admin = False self.cart_items = None # self.login() self.category_manager = CategoryManager(session=session) self.cart_manager = CartManager(session=session) self.product_manager = ProductManager(session=session) self.user_manager = UserManager(session=session) self.order_manager = OrderManager(session=session)
def main(self): # Initialize objects self.ing_map = Ingredient_map() self.order_m = OrderManager() self.kitchen_obj = Kitchen(self.WORKERS, self) # Bind self.soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.soc.bind((self.HOST, self.PORT)) while True: try: self.listen() except: pass # end while soc.close()
class Driver: def __init__(self, session): self.session = session self.logged_in = False self.user = None self.is_admin = False self.cart_items = None # self.login() self.category_manager = CategoryManager(session=session) self.cart_manager = CartManager(session=session) self.product_manager = ProductManager(session=session) self.user_manager = UserManager(session=session) self.order_manager = OrderManager(session=session) def login(self): email = input('Enter E-mail: ') pwd = input('Enter Password: '******'{user} logged in successfully') except exc.NoResultFound: raise Exception('wrong credentials. please try again.') def collect_category_details(self): category_detail = {} for key, value in CATEGORY_DETAILS.items(): category_detail[key] = input(f'Enter {value}: ') return category_detail def collect_product_details(self): product_detail = {} for key, value in PRODUCT_DETAILS.items(): product_detail[key] = input(f'Enter {value}: ') return product_detail def select_user(self): users = self.user_manager.list_users() for index, value in enumerate(users, 1): print(f'{index}. {value}') user_index = int(input('Select User: '******'Products added to the cart by user {user}') for item in cart_items: print(f'Product: {item.product}, Quantity: {item.quantity}') print() def select_category(self): categories = self.category_manager.list_categories() for index, category in enumerate(categories, 1): print(f'{index}. {category}') category_index = int(input('Select Category: ')) return categories[category_index-1] def select_product(self): products = self.product_manager.list_products() for index, product in enumerate(products, 1): print(f'{index}. {product}') product_index = int(input('Select Product: ')) return products[product_index-1] def display_cart(self, action=None): cart_items = self.user.cart_items print('User\'s Cart: ') for index, item in enumerate(cart_items, 1): print(f'{index}. Product: {item.product}, Quantity: {item.quantity}') if action == 'select': item_index = int(input('Select Product: ')) return cart_items[item_index-1] def place_order(self): items = [] self.order_manager.create_order(self.user) while True: item = self.display_cart(action='select') self.order_manager.add_item(item=item.product, quantity=item.quantity) items.append(item) self.cart_manager.remove_item(product=item, silent=True, commit=False) add_more = input('Want to add more items from cart? (y/n)') if add_more not in ['y', 'Y', 'Yes', 'YES', 'yes']: break print('Order Placed.') res = self.order_manager.is_discount() if res[1]: print('Voila.. You got 500 discount on this order') print(f'Order Amount: {res[0]}, Discount: 500, Net Payable: {res[0]-500}') else: print(f'Order Amount: {res[0]}, Net Payable: {res[0]}') self.cart_manager.commit_changes() self.order_manager.complete_order() def is_already_added_to_cart(self, product): for cart_item in self.user.cart_items: if product.id == cart_item.product_id: return cart_item else: return False def remove_item_from_cart(self): item = self.display_cart(action='select') self.cart_manager.remove_item(product=item) def add_product_to_user_cart(self): product = self.select_product() quantity = int(input('Enter Quantity: ')) cart_item = self.is_already_added_to_cart(product) if cart_item: self.cart_manager.update_item(cart_item, quantity) else: self.cart_manager.add_item(user=self.user, product=product, quantity=quantity) def view_product_details(self): product = self.select_product() details = self.product_manager.view_details(product_id=product.id) for k, v in details.items(): print(f'{k}: {v}') def list_products_by_category(self): category = self.select_category() products = self.category_manager.list_products_by_category(category_name=category.name) for index, product in enumerate(products, 1): print(f'{index}. {product}') def list_product_categories(self): categories = self.category_manager.list_categories() for index, category in enumerate(categories, 1): print(f'{index}. {category}') def create_user(self): user_detail = {} for key, value in USER_DETAILS.items(): user_detail[key] = input(f'Enter {value}: ') self.user_manager.add_user(**user_detail) def start_admin_flow(self): while True: print('Select option: ') for index, value in enumerate(ACTIONS.get('admin', []), 1): print(f'{index}. {value}') admin_choice = int(input()) if admin_choice == 1: # Add Category category_detail = self.collect_category_details() self.category_manager.add_category(**category_detail) input('Press Enter to Continue..') elif admin_choice == 2: # Add Product product_details = self.collect_product_details() self.product_manager.add_product(**product_details) input('Press Enter to Continue..') elif admin_choice == 3: # View Cart Details for_user = self.select_user() self.display_user_cart(for_user) input('Press Enter to Continue..') elif admin_choice == 4: # View Bills user = self.select_user() bill = self.user_manager.list_bills(user=user) # print('Function Under Construction') print(json.dumps(bill, indent=4, default=str)) input('Press Enter to Continue..') elif admin_choice == 5: return else: print('Enter Valid Input') input('Press Enter to Continue..') def start_user_flow(self): while True: print('Select option: ') for index, value in enumerate(ACTIONS.get('user', []), 1): print(f'{index}. {value}') user_choice = int(input()) if user_choice == 1: # List Categories self.list_product_categories() input('Press Enter to Continue..') elif user_choice == 2: # List Products by Category self.list_products_by_category() input('Press Enter to Continue..') elif user_choice == 3: # View Product Detail self.view_product_details() input('Press Enter to Continue..') elif user_choice == 4: # View Cart self.display_cart() input('Press Enter to Continue..') elif user_choice == 5: # Add Product to Cart self.add_product_to_user_cart() input('Press Enter to Continue..') elif user_choice == 6: # Remove Product from Cart self.remove_item_from_cart() input('Press Enter to Continue..') elif user_choice == 7: # Place Order self.place_order() input('Press Enter to Continue..') elif user_choice == 8: return else: print('Enter Valid Input')
from celery import Celery app = Celery('orders', backend='redis://localhost:6379/4', broker='redis://localhost:6379/4') from order import OrderManager handler = OrderManager("order") #@app.task #def patch(message): # print(message) # return handler.login_check(message) @app.task def post(message): print(message) return handler.generate_order(message) @app.task def get(message): print(message) return handler.get_order_details(message) @app.task def put(message): print(message)
class BacktestManager: def __init__(self, csv_file, strategy, strategy_params={}, plot_date=None, balance=10000.0, target_date=None): # init assets print('\nInitializing backtest.') self.balance_manager = BalanceManager(balance) self.order_manager = OrderManager(self.balance_manager) self.strategy = strategy(self.order_manager, **strategy_params) # load data print('Loading data.') self.data = pd.read_csv( csv_file, names=['datetime', 'volume', 'open', 'high', 'low', 'close']).to_numpy() # add target data self.plot_date = plot_date self.target_date = target_date if target_date is not None: self.plot_date = target_date else: self.plot_date = plot_date # add plot data self.metrics = {} self.sub_metrics = {} self.long_markers = [] self.short_markers = [] print('Backtest ready.') def run(self): total_days = self.data.shape[0] // BARS_PER_DAY print('\nRunning backtest.') deltas = 0 for date_index in range(total_days): true_date_index = date_index * BARS_PER_DAY # get backtest datetime datetime = self.data[true_date_index, ROW_INDICES.DATETIME] date_str = datetime.split(' ')[0] print('Backtesting on: {}'.format(date_str), end='\r') # prepare plot data record_metrics = False if self.plot_date == date_str: self.plot_date_index = true_date_index record_metrics = True elif self.target_date: continue for bar_index in range(BARS_PER_DAY): true_bar_index = true_date_index + bar_index date_data = self.data[true_date_index:true_bar_index + 1] bar_data = date_data[-1] # update assets self.order_manager.update(bar_data) update = self.strategy.update(date_data, bar_index) # record data if record_metrics: # update main metrics if update.metrics is not None: for m_name, metric in update.metrics.items(): if m_name in self.metrics: self.metrics[m_name].append(metric) else: self.metrics[m_name] = [metric] # update sub metrics if update.sub_metrics is not None: for m_name, metric in update.sub_metrics.items(): if m_name in self.sub_metrics: self.sub_metrics[m_name].append(metric) else: self.sub_metrics[m_name] = [metric] # update positions if update.direction == ORDER_DIRS.LONG: self.long_markers.append(bar_data[ROW_INDICES.CLOSE]) self.short_markers.append(np.nan) elif update.direction == ORDER_DIRS.SHORT: self.long_markers.append(np.nan) self.short_markers.append(bar_data[ROW_INDICES.CLOSE]) else: self.long_markers.append(np.nan) self.short_markers.append(np.nan) # quit after target if self.target_date: break print(deltas) # wrap-up backtest print('\n\n--- Results for {} ---\n'.format( self.strategy.__class__.__name__)) for k, v in self.balance_manager.get_report().items(): print('{}: {}'.format(k, round(v, 3))) print('\n--------------------{}'.format( '-' * len(self.strategy.__class__.__name__))) def plot(self): if self.plot_date is not None: print('\nPlotting results.') # configure datetime axis date_fmt = mdates.DateFormatter('%H:%M') datetime_data = self.data[ self.plot_date_index:self.plot_date_index + BARS_PER_DAY, ROW_INDICES.DATETIME] datetime_data = datetime_data.astype(np.datetime64) # configure close price axis close_data = self.data[self.plot_date_index:self.plot_date_index + BARS_PER_DAY, ROW_INDICES.CLOSE] # plot close data fig, ax = plt.subplots(1 + int(len(self.sub_metrics) > 0), figsize=(12, 8)) if len(self.sub_metrics) == 0: ax = [ax] ax[0].plot(datetime_data, close_data) ax[0].xaxis.set_major_formatter(date_fmt) metric_labels = ['close'] # plot metric data for m_name, metric in self.metrics.items(): metric_arr = np.array(metric) ax[0].plot(datetime_data, metric_arr) metric_labels.append(m_name) # plot sub-metric data sub_metric_labels = [] for m_name, metric in self.sub_metrics.items(): metric_arr = np.array(metric) if math.isnan(metric_arr[0]): metric_arr[0] = 0.0 metric_arr[1] = np.nan if math.isnan(metric_arr[-1]): metric_arr[-1] = 0.0 metric_arr[-2] = np.nan ax[1].plot(datetime_data, metric_arr) sub_metric_labels.append(m_name) # plot signal data long_markers = np.array(self.long_markers) - 1 short_markers = np.array(self.short_markers) + 1 ax[0].plot(datetime_data, long_markers, marker='^', color='g', markersize=10, linestyle='None') ax[0].plot(datetime_data, short_markers, marker='v', color='r', markersize=10, linestyle='None') metric_labels.extend(['l_signal', 's_signal']) # finalize plots ax[0].legend(metric_labels) if len(sub_metric_labels) > 0: ax[1].legend(sub_metric_labels) ax[1].xaxis.set_major_formatter(date_fmt) plt.show()
def order_manager(session): return OrderManager(session)
# making sure to provide authentication commands dropcopy = OANDAStreamingDropcopy(settings.STREAM_DOMAIN, settings.ACCESS_TOKEN, settings.ACCOUNT_ID, pairs, events) # Create the strategy/signal generator, passing the # instrument and the events queue strategy = Mustang(pairs, events) # Create the portfolio object that will be used to # compare the OANDA positions with the local, to # ensure backtesting integrity. book = Book(prices, events, equity=equity, backtest=False) # Create an order manager order = OrderManager(events, book) # Create an fill manager fill = FillManager(events, book) # Create the execution handler making sure to # provide authentication commands execution = OANDAExecutionHandler(settings.API_DOMAIN, settings.ACCESS_TOKEN, settings.ACCOUNT_ID) # Create two separate threads: One for the trading loop # and another for the market price streaming class trade_thread = threading.Thread(target=trade, args=(events, strategy, book, order, execution, fill, heartbeat))