<s> from django . core . management . base import BaseCommand <EOL> from django . conf import settings <EOL> from history . models import Trade <EOL> from history . tools import print_and_log <EOL> import datetime <EOL> import warnings <EOL> warnings . filterwarnings ( "<STR_LIT:ignore>" , category = DeprecationWarning ) <EOL> warnings . filterwarnings ( "<STR_LIT:ignore>" , category = RuntimeWarning ) <EOL> class Command ( BaseCommand ) : <EOL> help = '<STR_LIT>' <EOL> def handle ( self , * args , ** options ) : <EOL> from history . poloniex import poloniex <EOL> from history . models import Price <EOL> import time <EOL> poo = poloniex ( settings . API_KEY , settings . API_SECRET ) <EOL> if settings . MAKE_TRADES : <EOL> time . sleep ( <NUM_LIT> ) <EOL> for t in Trade . objects . filter ( created_on__lt = datetime . datetime . now ( ) , status = '<STR_LIT>' ) : <EOL> action = t . type <EOL> price = Price . objects . filter ( symbol = t . symbol ) . order_by ( '<STR_LIT>' ) . first ( ) <EOL> if action == '<STR_LIT>' : <EOL> rate = price . lowestask * <NUM_LIT> <EOL> else : <EOL> rate = price . highestbid * <NUM_LIT> <EOL> t . price = rate <EOL> if action == '<STR_LIT>' : <EOL> try : <EOL> response = { } if not settings . MAKE_TRADES else poo . buy ( t . symbol , rate , t . amount ) <EOL> except Exception as e : <EOL> print_and_log ( '<STR_LIT>' + str ( e ) ) <EOL> elif action == '<STR_LIT>' : <EOL> try : <EOL> response = { } if not settings . MAKE_TRADES else poo . sell ( t . symbol , rate , t . amount ) <EOL> except Exception as e : <EOL> print_and_log ( '<STR_LIT>' + str ( e ) ) <EOL> t . response = response <EOL> t . orderNumber = response . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> t . status = '<STR_LIT:error>' if response . get ( '<STR_LIT:error>' , False ) else '<STR_LIT>' <EOL> t . calculatefees ( ) <EOL> t . calculate_exchange_rates ( ) <EOL> t . save ( ) <EOL> ot = t . opposite_trade <EOL> ot . opposite_price = rate <EOL> ot . net_profit = ( ( rate * t . amount ) - ( ot . price * ot . amount ) if action == '<STR_LIT>' else <EOL> ( ot . price * ot . amount ) - ( rate * t . amount ) ) - ot . fee_amount - t . fee_amount <EOL> ot . calculate_profitability_exchange_rates ( ) <EOL> ot . save ( ) </s>
