Created
March 23, 2014 18:54
-
-
Save greedo/9727799 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import sys | |
import datetime | |
import time | |
import subprocess | |
import passwords | |
from sqlalchemy import * | |
from sqlalchemy.orm import create_session | |
from sqlalchemy.ext.declarative import declarative_base | |
from sqlalchemy import exc | |
from sqlalchemy.sql import text | |
from sqlalchemy.types import TIMESTAMP, FLOAT | |
from sqlalchemy.sql import func | |
from sqlalchemy.schema import Sequence | |
import psycopg2 | |
Base = declarative_base() | |
engine = create_engine(passwords.Database.engine+'://'+passwords.Database.username+':'+passwords.Database.password+'@'+passwords.Database.hostname+':5432/'+passwords.Database.db) | |
metadata = MetaData(bind=engine) | |
#Create a session to use the tables | |
session = create_session(bind=engine) | |
#Reflect each database table we need to use, using metadata | |
class Trade(Base): | |
__table__ = Table('trade', metadata, autoload=True) | |
def partial_order(s, quantity): | |
connection = engine.connect() | |
connection.execute(text('update trade set status=\'partial\' where trade_id='+str(order.trade_id))) | |
return filling_quantity | |
def fill_limit_order(order, filling_quantity): | |
connection = engine.connect() | |
connection.execute(text('update trade set status=\'completed\' where trade_id='+str(order.trade_id))) | |
return filling_quantity | |
def fill_market_order(order): | |
remaining_quantity = order.quantity | |
if order.position=="buy": | |
sell_orders = session.query(Trade).filter(Trade.position=="sell").filter(Trade.status=="pending").order_by(Trade.price.desc()).order_by(Trade.date_created).all() | |
for s in sell_orders: | |
amount_filled = 0.0 | |
if remaining_quantity <= 0.0: | |
break | |
else: | |
try: | |
amount_filled = fill_limit_order(s,remaining_quantity) | |
except NameError: | |
return 0 | |
connection = engine.connect() | |
connection.execute(text('update trade set quantity='+str(amount_filled)+' where trade_id='+str(s.trade_id))) | |
if amount_filled <= 0.0: | |
try: | |
amount_filled = partial_order(s,remaining_quantity) | |
except NameError: | |
return 0 | |
connection = engine.connect() | |
connection.execute(text('update trade set quantity='+str(amount_filled)+' where trade_id='+str(s.trade_id))) | |
remaining_quantity -= amount_filled | |
break | |
if order.position=="sell": | |
buy_orders = session.query(Trade).filter(Trade.position=="buy").filter(Trade.status=="pending").order_by(Trade.price.desc()).order_by(Trade.date_created).all() | |
for s in buy_orders: | |
amount_filled = 0.0 | |
if remaining_quantity <= 0.0: | |
break | |
else: | |
try: | |
amount_filled = fill_limit_order(s,remaining_quantity) | |
except NameError: | |
return 0 | |
connection = engine.connect() | |
connection.execute(text('update trade set quantity='+str(amount_filled)+' where trade_id='+str(s.trade_id))) | |
if amount_filled <= 0.0: | |
try: | |
amount_filled = partial_order(s,remaining_quantity) | |
except NameError: | |
return 0 | |
connection = engine.connect() | |
connection.execute(text('update trade set quantity='+str(amount_filled)+' where trade_id='+str(s.trade_id))) | |
remaining_quantity -= amount_filled | |
break | |
def fill_limit_order(order, filling_quantity): | |
connection = engine.connect() | |
connection.execute(text('update trade set status=\'completed\' where trade_id='+str(order.trade_id))) | |
return filling_quantity | |
def main(): | |
# MARKET ORDERS | |
market_orders = session.query(Trade).filter(Trade.order_type=="market").filter(Trade.status=="pending").order_by(Trade.price.desc()).order_by(Trade.date_created).all() | |
for m in market_orders: | |
fill_market_order(m) | |
# LIMIT ORDERS - get a buy order and try to fill it | |
try: | |
bid = session.query(Trade).filter(Trade.position=="buy").filter(Trade.status=="pending").order_by(Trade.price.desc()).order_by(Trade.date_created).first() | |
except AttributeError: | |
return 0 | |
try: | |
limit_orders = session.query(Trade).filter(Trade.position=="sell").filter(Trade.status=="pending").filter(Trade.price<=bid.price).order_by(Trade.price.desc()).order_by(Trade.date_created).all() | |
except AttributeError: | |
return 0 | |
remaining_quantity = bid.quantity | |
for s in limit_orders: | |
amount_filled = 0.0 | |
if remaining_quantity <= 0.0: | |
break | |
else: | |
try: | |
amount_filled = fill_order(s,remaining_quantity) | |
except NameError: | |
return 0 | |
connection = engine.connect() | |
connection.execute(text('update trade set quantity='+str(amount_filled)+' where trade_id='+str(bid.trade_id))) | |
if amount_filled <= 0.0: | |
try: | |
amount_filled = partial_order(s,remaining_quantity) | |
except NameError: | |
return 0 | |
connection = engine.connect() | |
connection.execute(text('update trade set quantity='+str(amount_filled)+' where trade_id='+str(bid.trade_id))) | |
remaining_quantity -= amount_filled | |
break | |
remaining_quantity -= amount_filled | |
if __name__ == '__main__': | |
while True: | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment