api - Restart Python Script after BadStatusLine Error -
i have program here streams market prices , executes orders based on prices, however, every once in while (few hours or so) throws error:
exception in thread thread-1: traceback (most recent call last): file "/library/frameworks/python.framework/versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner self.run() file "/library/frameworks/python.framework/versions/2.7/lib/python2.7/threading.py", line 763, in run self.__target(*self.__args, **self.__kwargs) file "/users/mattduhon/trading4.py", line 30, in trade execution.execute_order(event) file "/users/mattduhon/execution.py", line 34, in execute_order response = self.conn.getresponse().read() file "/library/frameworks/python.framework/versions/2.7/lib/python2.7/httplib.py", line 1073, in getresponse response.begin() file "/library/frameworks/python.framework/versions/2.7/lib/python2.7/httplib.py", line 415, in begin version, status, reason = self._read_status() file "/library/frameworks/python.framework/versions/2.7/lib/python2.7/httplib.py", line 379, in _read_status raise badstatusline(line) badstatusline: ''
once error occurs program keeps running without executing orders, streams rates. question how can ensure program keep trading? either restarting program or ignoring error or else? in advance.
additional code:
excecution.py
import httplib import urllib class execution(object): def __init__(self, domain, access_token, account_id): self.domain = domain self.access_token = access_token self.account_id = account_id self.conn = self.obtain_connection() def obtain_connection(self): return httplib.httpsconnection(self.domain) def execute_order(self, event): headers = { "content-type": "application/x-www-form-urlencoded", "authorization": "bearer " + self.access_token } params = urllib.urlencode({ "instrument" : event.instrument, "units" : event.units, "type" : event.order_type, "side" : event.side, "stoploss" : event.stoploss, "takeprofit" : event.takeprofit }) self.conn.request( "post", "/v1/accounts/%s/orders" % str(self.account_id), params, headers ) response = self.conn.getresponse().read() #line34//////////// print response
and trading4.py
import queue import threading import time import json execution import execution settings4 import stream_domain, api_domain, access_token, account_id strategy4 import testrandomstrategy streaming import streamingforexprices #checks events , executes order def trade(events, strategy, execution): while true: try: event = events.get(false) except queue.empty: pass else: if event not none: if event.type == 'tick': strategy.calculate_signals(event) elif event.type == 'order': print execution.execute_order(event) #line30////////////// if __name__ == "__main__": heartbeat = 0 # half second between polling events = queue.queue() # trade 1 unit of eur/usd instrument = "eur_usd" units = 10 prices = streamingforexprices( stream_domain, access_token, account_id, instrument, events ) execution = execution(api_domain, access_token, account_id) strategy = testrandomstrategy(instrument, units, events) #threads trade_thread = threading.thread(target=trade, args=(events, strategy, execution)) price_thread = threading.thread(target=prices.stream_to_queue, args=[]) # stop_thread = threading.thread(target=rates, args=(events,)) # start both threads trade_thread.start() price_thread.start() # stop_thread.start()
catch exception:
httplib import badstatusline ............ try: response = self.conn.getresponse().read() #line34//////////// except badstatusline e: print(e) else: print response
Comments
Post a Comment