def post_orders(): session = db_session.create_session() data = request.json validation_error = [] ids = [] for i in data['data']: if not check_keys(i, ('order_id', 'weight', 'region', 'delivery_hours')) or \ not check_all_keys_in_dict(i, ('order_id', 'weight', 'region', 'delivery_hours')): validation_error.append({"id": i['order_id']}) else: order = session.query(Order).filter( Order.order_id == i['order_id']).first() if order: session.delete(order) session.commit() ids.append({"id": i['order_id']}) delivery_hours = [] for j in i['delivery_hours']: delivery_hour = DeliveryHour(order_id=i['order_id']) delivery_hour.set_delivery_hour(j) delivery_hours.append(delivery_hour) order = Order(order_id=i['order_id'], weight=i['weight'], region=i['region'], delivery_hours=delivery_hours) session.add(order) session.commit() if validation_error: return make_resp({'validation_error': { "orders": validation_error }}, 400) else: return make_resp({"orders": ids}, 201)
def sell_strategy(self, *args, **kwargs): """ Here is just a sample sell strategy. # TODO: Sell strategy should be configable. :param data: :return: True or False """ name = kwargs.get('name') price = kwargs.get("price") date = kwargs.get('date') tax = kwargs.get("tax") record = self.stock_asset.get_order(name) if record is not None and record.number > 0: sell_record = Order(name=name, date=date, price=price, number=record.number, tax=0, sell=True) print 'This is the sell record:', sell_record self._fund += record.number * price * 100 * (1 - tax) self.stock_asset.add_order(sell_record) self._sell_record_list.append(sell_record) return True return False
def buy_strategy(self, *argv, **kwargs): """ Here is just a sample buy strategy. :param data: :return: """ # print 'go here' name = kwargs.get('name') price = kwargs.get("price") date = kwargs.get("date") tax = kwargs.get("tax") number = max_buy_number(self._fund, price, tax) if number != 0: self._fund -= price * (1 + tax) * number * 100 cost_tax = price * number * 100 * 0.001 record = Order(name=name, date=date, number=number, price=price, tax=cost_tax, buy=True) print 'This is the buy record: ', record self.stock_asset.add_order(record) self._buy_record_list.append(record) return True return False
def buy(self, stock_name, stock_data): if not self.buy: current_price = stock_data[-1, 4] buy_number = np.floor(self._fund/(current_price*100)) cost_money = buy_number*current_price*100 self._fund -= (cost_money + self.tax*cost_money) return Order(stock_name, buy=True, sell=False, number=buy_number, price=current_price, tax=self.tax, date=stock_data[-1, 0])
def create_buy_order(self, name, price, context, position=1.0): """ This function is used to create buy order by some parameter. :param name: :param price: :param tax_processor: :param position: :return: """ logger.info("create buy order at time {date}".format(date=context.date)) used_cash = self._cash * position buy_price = context.tax_processor.calculate_buy_tax(price) # Here we use 1 based count method. number = np.floor(used_cash/(buy_price*100))*100 # print 'this is the buy number', number, 'cost money', buy_price*number order = Order(name=name, price=buy_price, date=context.date, number=number, current_price=price, buy=True) return order
def create_sell_order(self, name, price, context, position=1.0): """ This function is used to create sell order by some parameter. :param name: :param price: :param date: :param tax_processor: :param position: :return: """ if name not in self._old_order: raise ValueError('Stock not in the order list') order = self._old_order[name] # sell_price = context.tax_processor_calculate_sell_tax(price) sell_price = price sell_number = np.floor(order.number*position) # print 'this is the sell number', sell_number, 'cost money', sell_price*sell_number return Order(name=name, price=sell_price, current_price=price, date=context.date, number=sell_number, sell=True)
def add_order(product, customer, date_placed, date_processed, download): order = Order(product, customer, date_placed, date_processed, download) db.session.add(order) db.session.commit() return order
def get_orders(cls, order_id: int = None, user_id: int = None, user_name:str = None, time_stamp: int = None, time_from: int = None, time_to: int = None) -> List[Order]: raw_orders = OrderDBManager.get_orders(order_id=order_id, time_stamp=time_stamp, user_name = user_name, time_from=time_from, time_to=time_to) orders = [Order(*raw_order) for raw_order in raw_orders] return orders
def get_order_by_id(cls, user_id: int, order_id: int) -> Order: raw_order = OrderDBManager.get_order_by_id(user_id, order_id) return Order(*raw_order)
def get_orders_by_date(cls, user_id: int, time_stamp: int = None, time_from: int = None, time_to: int = None) -> List[Order]: raw_orders = OrderDBManager.get_order_by_date(user_id, time_stamp, time_from, time_to) orders = [Order(*raw_order) for raw_order in raw_orders] return orders
def get_all_orders_by_user(cls, user_id: int) -> List[Order]: raw_orders = OrderDBManager.get_all_orders_by_user(user_id) orders = [Order(*raw_order) for raw_order in raw_orders] return orders
def get_all_orders(cls) -> List[Order]: raw_orders = OrderDBManager.get_all_orders() orders = [Order(*raw_order) for raw_order in raw_orders] return orders