def main(): try: with open("DEFAULT_STOCK.txt") as infile: venue, symbol = infile.readline().split() except: venue, symbol = "TESTEX", "FOOBAR" account = "NZ" + str(random.randint(0, 999999999)) # account = "NOISEBOTS" sf.change_api_key("unused") orderType = "limit" all_orders = [] while 1: try: price = sf.quote(venue, symbol)["last"] if price == 0: price = 5000 except: price = 5000 price += random.randint(-100, 100) qty = 100 qty += random.randint(-50, 50) direction = random.choice(["buy", "sell"]) r = sf.execute_d( { "price": price, "qty": qty, "direction": direction, "orderType": orderType, "account": account, "venue": venue, "stock": symbol, }, verbose=True, ) try: id = r["id"] all_orders.append(id) except: print("Trouble getting ID.") time.sleep(0.5) if len(all_orders) > 10: id = all_orders.pop(0) sf.cancel(venue, symbol, id, verbose=True)
def main(): try: with open("DEFAULT_STOCK.txt") as infile: venue, symbol = infile.readline().split() except: venue, symbol = "BUYEX", "DOGS" account = "NZ" + str(random.randint(0, 999999999)) orderType = "limit" all_orders = [] while 1: try: price = sf.quote(venue, symbol)["last"] if price == 0: price = 5000 except: price = 5000 price += random.randint(-100, 100) qty = 100 qty += random.randint(-50, 50) direction = random.choice(["buy", "sell"]) r = sf.execute_d( { "price": price, "qty": qty, "direction": direction, "orderType": orderType, "account": account, "venue": venue, "stock": symbol }, verbose=True) try: id = r["id"] all_orders.append(id) except: print("Trouble getting ID.") time.sleep(0.5) if len(all_orders) > 10: id = all_orders.pop(0) sf.cancel(venue, symbol, id, verbose=True)
def main(): global account global venue global symbol orderType = "limit" all_orders = [] while 1: try: price = sf.quote(venue, symbol)["last"] if price == 0: price = 5000 except: price = 5000 price += random.randint(-100, 100) qty = 100 qty += random.randint(-50, 50) direction = random.choice(["buy", "sell"]) r = sf.execute_d( { "price": price, "qty": qty, "direction": direction, "orderType": orderType, "account": account, "venue": venue, "stock": symbol, }, verbose=True, ) try: id = r["id"] all_orders.append(id) except: print("Trouble getting ID.") time.sleep(random.uniform(0.3, 0.7)) if len(all_orders) > 10: id = all_orders.pop(0) sf.cancel(venue, symbol, id, verbose=True)
def main(): try: with open("DEFAULT_STOCK.txt") as infile: venue, symbol = infile.readline().split() except: venue, symbol = "TESTEX", "FOOBAR" account = "LH" + str(random.randint(0,999999999)) # account = "BLSHBOTS" sf.change_api_key("unused") orderType = "limit" last_id = None last_price = None active_ids = [] myshares, mycents = 0, 0 print("Waiting to see some prices before placing orders...") while 1: try: last_price = sf.quote(venue, symbol)["last"] except: time.sleep(5) continue if len(active_ids) > 10: r = sf.cancel(venue, symbol, active_ids[0], verbose = True) if r: deltas = sf.parse_fills_from_response(r) myshares += deltas["shares"] mycents += deltas["cents"] print("\nShares: {}, Cents: {}, NAV: {} (current price: {})\n".format(myshares, mycents, myshares * last_price + mycents, last_price)) active_ids.pop(0) qty = 100 qty += random.randint(-25, 0) if myshares < 0: direction = "buy" elif myshares > 0: direction = "sell" else: direction = random.choice(["buy", "sell"]) if direction == "buy": price = last_price - 50 else: price = last_price + 50 print("Placing order...") r = sf.execute_d( { "price" : price, "qty" : qty, "direction" : direction, "orderType" : orderType, "account" : account, "venue" : venue, "stock" : symbol }, verbose = False) try: active_ids.append(r["id"]) except: print("Trouble getting ID.") time.sleep(0.5)
for n in range(TEST_SIZE): INFO.price = random.randint(1, 20) INFO.qty = random.randint(1, 20) INFO.direction = random.choice(["buy", "sell"]) INFO.orderType = random.choice(["limit", "limit", "limit", "limit", "market", "immediate-or-cancel", "fill-or-kill"]) set_from_account_1(INFO) res1 = sf.execute(INFO) id1 = res1["id"] if n == 0: first_id_1 = id1 if n == TEST_SIZE - 1: last_id_1 = id1 q1 = sf.quote(INFO.venue, INFO.symbol) o1 = sf.orderbook(INFO.venue, INFO.symbol) set_from_account_2(INFO) res2 = sf.execute(INFO) id2 = res2["id"] if n == 0: first_id_2 = id2 if n == TEST_SIZE - 1: last_id_2 = id2 q2 = sf.quote(INFO.venue, INFO.symbol) o2 = sf.orderbook(INFO.venue, INFO.symbol) print("IDs (adjusted, should match): {}, {} ----- {} {} @ {} ({})".format(id1 - first_id_1, id2 - first_id_2, INFO.direction, INFO.qty, INFO.price, INFO.orderType)) bids_match = False
def main(): global account global venue global symbol orderType = "limit" last_id = None last_price = None active_ids = [] myshares, mycents = 0, 0 print("Waiting to see some prices before placing orders...") # The strategy of this stupid bot is: # # If we have positive shares, try to sell above current price # If we have negative shares, try to buy below current price while 1: try: last_price = sf.quote(venue, symbol)["last"] except: time.sleep(5) continue if len(active_ids) > 10: r = sf.cancel(venue, symbol, active_ids[0], verbose = True) if r: deltas = sf.parse_fills_from_response(r) myshares += deltas["shares"] mycents += deltas["cents"] print("\nShares: {}, Cents: {}, NAV: {} (current price: {})\n".format(myshares, mycents, myshares * last_price + mycents, last_price)) active_ids.pop(0) qty = 100 qty += random.randint(-25, 0) if myshares < 0: direction = "buy" elif myshares > 0: direction = "sell" else: direction = random.choice(["buy", "sell"]) if direction == "buy": price = last_price - 50 else: price = last_price + 50 print("Placing order...") r = sf.execute_d( { "price" : price, "qty" : qty, "direction" : direction, "orderType" : orderType, "account" : account, "venue" : venue, "stock" : symbol }, verbose = False) try: active_ids.append(r["id"]) except: print("Trouble getting ID.") time.sleep(0.5)
order.direction = "buy" order.qty = 999999 order.price = 1 sf.execute(order) # ------------------------------------- order.orderType = "limit" order.direction = "sell" order.qty = 5 order.price = 237 sf.execute(order) order.direction = "sell" order.qty = 96 order.price = 360 sf.execute(order) order.direction = "buy" order.qty = 96 order.price = 360 q = sf.quote(order.venue, order.symbol) print(q["lastSize"], "@", q["last"])
def main(): try: with open("DEFAULT_STOCK.txt") as infile: venue, symbol = infile.readline().split() except: venue, symbol = "BUYEX", "DOGS" account = "LH" + str(random.randint(0,999999999)) orderType = "limit" last_id = None last_price = None active_ids = [] myshares, mycents = 0, 0 while 1: try: last_price = sf.quote(venue, symbol)["last"] except: time.sleep(5) continue if len(active_ids) > 10: r = sf.cancel(venue, symbol, active_ids[0], verbose = True) if r: deltas = sf.parse_fills_from_response(r) myshares += deltas["shares"] mycents += deltas["cents"] print("\nShares: {}, Cents: {}, NAV: {} (current price: {})\n".format(myshares, mycents, myshares * last_price + mycents, last_price)) active_ids.pop(0) qty = 100 qty += random.randint(-25, 0) if myshares < 0: direction = "buy" elif myshares > 0: direction = "sell" else: direction = random.choice(["buy", "sell"]) if direction == "buy": price = last_price - 50 else: price = last_price + 50 print("Placing order...") r = sf.execute_d( { "price" : price, "qty" : qty, "direction" : direction, "orderType" : orderType, "account" : account, "venue" : venue, "stock" : symbol }, verbose = False) try: active_ids.append(r["id"]) except: print("Trouble getting ID.") time.sleep(0.5)
o = {"account": account, "venue": venue, "stock": symbol, "qty": 50, "orderType": "limit", "price": 5000, "direction": "sell"} r = sf.execute_d(o) print("Last ID was {}".format(r["id"])) print("Buying 40 of it back...", end = "") o = {"account": account, "venue": venue, "stock": symbol, "qty": 40, "orderType": "limit", "price": 5000, "direction": "buy"} r = sf.execute_d(o) print(r["fills"]) print("Last ID was {}".format(r["id"])) print("Buying final 10 with market...", end = "") o = {"account": account, "venue": venue, "stock": symbol, "qty": 10, "orderType": "market", "price": 5000, "direction": "buy"} r = sf.execute_d(o) print(r["fills"]) print("Last ID was {}".format(r["id"])) q = sf.quote(venue, symbol) ls = q["lastSize"] print("Quote: lastSize was {}".format(ls), end = "") if ls != 10: print(" <---------------------------------------------------------") time.sleep(5) q = sf.quote(venue, symbol) print("After 5 seconds: Last size was {}".format(q["lastSize"])) else: print()