def getOrderById(order_id): conn = connect() cur = conn.cursor() cur.execute("SELECT * FROM orders where id=%s", (order_id, )) res = cur.fetchone() return Order(res[0], res[1], res[2], res[3], res[4], res[5], res[6], res[7], res[8], res[9], res[10])
def send_order(self, pre_order: PreOrder): if self.has_open_position(pre_order.instrument): log(f"Position already open for {pre_order.instrument}. No trade.") return False if self.has_pending_order(pre_order.instrument): log(f"Order pending for {pre_order.instrument}. No trade.") return False # Calculate order if long or short. if pre_order.position_type == trd.LONG_TRADE: open_price, stop_loss, take_profit, position_size = self.calculate_long( pre_order) else: open_price, stop_loss, take_profit, position_size = self.calculate_short( pre_order) order = Order(instrument=pre_order.instrument, position_size=str(position_size), open_price=_converter.round5str(open_price), take_profit=_converter.round5str(take_profit), stop_loss=_converter.round5str(stop_loss), time_in_force=pre_order.time_in_force) # Send order to Oanda server. result = self.oanda_rest.send_limit_order(order) # Process result. if result: message = f"Success. Market order placed for {pre_order.instrument} with stop loss of {stop_loss} and take profit of {take_profit}." self.twilio.send_message(message) else: message = f"Failure. Market order failed for {pre_order.instrument} with stop loss of {stop_loss} and take profit of {take_profit}." self.twilio.send_message(message) return True
def getUserOrders(user): conn = connect() cur = conn.cursor() cur.execute("SELECT * FROM orders WHERE user_id=%s", (user.id, )) allres = cur.fetchall() orders = [] for res in allres: order = Order(res[0], res[1], res[2], res[3], res[4], res[5], res[6], res[7], res[8], res[9], res[10]) orders.append(order) return orders
def place_order(): instrument = "EUR_USD" price = 1.09315 stop_loss = price - 0.0005 take_profit = price + 0.0005 units = 50 gtdTime = str(_time.utc_now()) order = Order(instrument=instrument, position_size=units, open_price=_converter.round5str(price), take_profit=_converter.round5str(take_profit), stop_loss=_converter.round5str(stop_loss), time_in_force="GTC", fill_type="GTC") oar.send_limit_order(order)
# first_name = None # last_name = None # age = None # b = Person() # b.first_name = "Brandon" # b.last_name = "Vicedomini" # b.age = 45 # x = jsonpickle.encode(b) # print(x) orderx = Order(instrument="EUR_USD", position_size=str(-500), open_price=_converter.round4str(1.0011), take_profit=_converter.round5str(1.0012), stop_loss=_converter.round5str(1.0013), fill_type="GTC", time_in_force=(str(_time.utc_now()))) order_dict = orderx.to_dict() print(order_dict) order_dict["position_size"] = -1000 orderx2 = Order.from_dict(order_dict) print(orderx2) # orderd = { # "instrument": "EUR_USD", # "position_size": str(-500), # "open_price": _converter.round4str(1.0011),
def addOrder(cart, total_price, total_discount, form, user): simple_cart = Cart.simplify(cart) tracking_number = makeTrackingNumber() conn = connect() cur = conn.cursor() b_address_id = addAddress(form['b-country'], form['b-city'], form['b-province'], form['b-address'], cur) s_address_id = addAddress(form['s-country'], form['s-city'], form['s-province'], form['s-address'], cur) # we do not store credit card information (typically you would interface with a payment provider now for that) cur.execute( "INSERT INTO orders (cart, total_price, total_discount_price, first_name, last_name, tracking, b_address_id, s_address_id, user_id, order_date) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,'now') RETURNING id, order_date", (Json(simple_cart), str(total_price), str(total_discount), str(form['firstname']), str(form['lastname']), str(tracking_number), str(b_address_id), str(s_address_id), str(user.id))) res = cur.fetchone() order_id = res[0] order_date = res[1] #order_id = cur.fetchone()[0] # now we need to update each items quantity, and pay people accordingly for item_id in cart: order_item = cart[item_id] store_item = order_item['store_item'] owner_share = int( math.floor(order_item['quantity'] * store_item.discount_price * (1 - store_item.revenue_share_percent))) publisher_share = int( math.ceil(order_item['quantity'] * store_item.discount_price * store_item.revenue_share_percent)) #todo pay publisher pay(Constants.OWNER_BANKING_ID, owner_share, cur) if store_item.item_type == ItemType.BOOK: pay(store_item.item.publisher.id, publisher_share, cur) store_item.quantity -= order_item[ 'quantity'] #todo I could detect that we need to order more books here updateItem(store_item, cur) addOrderItemHistory(cur, item_id, order_id, order_item['quantity'], owner_share, publisher_share) #pay(publisher_id, cur) #order complete, commit conn.commit() billing_address = Address(b_address_id, form['b-country'], form['b-city'], form['b-province'], form['b-address']) shipping_address = Address(s_address_id, form['s-country'], form['s-city'], form['s-province'], form['s-address']) order = Order(order_id, total_price, total_discount, simple_cart, form['firstname'], form['lastname'], tracking_number, billing_address, shipping_address, order_date, user.id) cur.close() conn.close() return order
def send_limit_order(self, order: Order): cprintg(order) redis.rpush("order_log", order.to_json()) return True