Example #1
0
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 = "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)
def set_from_account_2(order):
    order.account = ACCOUNT_2
    order.venue = VENUE_2
    order.symbol = SYMBOL_2
    sf.change_api_key(API_KEY_2)
    sf.set_web_url(API_URL_2)
import random, threading, time
import stockfighter_minimal as sf

sf.set_web_url("http://127.0.0.1:8000/ob/api/")
sf.change_api_key("exb123456")

ACCOUNT = "EXB123456"
VENUE = "TESTEX"
SYMBOL = "FOOBAR"


def execute_randomly_forever(verbose = False):

    INFO = sf.Order()

    INFO.account = ACCOUNT
    INFO.venue = VENUE
    INFO.symbol = SYMBOL

    while 1:
        INFO.price = random.randint(4000, 6000)
        INFO.qty = random.randint(1, 100)
        INFO.orderType = random.choice(["limit", "limit", "limit", "limit", "market", "immediate-or-cancel", "fill-or-kill"])
        INFO.direction = random.choice(["buy", "sell"])
        
        result = sf.execute(INFO, verbose = verbose)



def stress_test():
Example #5
0
import json, random, threading, time
import stockfighter_minimal as sf

account = "NOISEBOTS"
venue, symbol = "TESTEX", "FOOBAR"

sf.set_web_url("http://127.0.0.1:8000/ob/api/")
sf.change_api_key("noisekey")

def make_bots():
    for n in range(6):
        newthread = threading.Thread(target = noise, args=(account, venue, symbol))
        newthread.start()

def noise(account, venue, 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"])
        
import json, random, time
import stockfighter_minimal as sf

account = "BLSHBOTS"
venue, symbol = "TESTEX", "FOOBAR"

sf.set_web_url("http://127.0.0.1:8000/ob/api/")
sf.change_api_key("blshkey")

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:
import stockfighter_minimal as sf

sf.change_api_key("yourkeyhere")      # <----------------------- FIX THIS

order = sf.Order()

order.account = "EXB123456"
order.venue = "TESTEX"
order.symbol = "FOOBAR"

print("Reported last trade: (should be 96 @ 360, but sometimes is 5 @ 237)")

while 1:

	# Clear the book.......................

	order.orderType = "market"

	order.direction = "sell"
	order.qty = 999999
	order.price = 1

	sf.execute(order)

	order.direction = "buy"
	order.qty = 999999
	order.price = 1

	sf.execute(order)

	# -------------------------------------