class Test_MarketState(unittest.TestCase): """ Unit tests for the class MarketState """ def setUp(self): self.ms = MarketState(1.24010, 1.24000) def test_correct_init(self): self.assertEqual(self.ms.bid, 1.24010) self.assertEqual(self.ms.ask, 1.24000) def test_correct_update(self): self.ms.update_bid_ask(1.24020, 1.24010) self.assertEqual(self.ms.bid, 1.24020) self.assertEqual(self.ms.ask, 1.24010)
def __init__(self, domain, access_token, account_id, instruments, events_queue, stoprequest): self.domain = domain self.access_token = access_token self.account_id = account_id self.instruments = instruments self.events_queue = events_queue self.stoprequest = stoprequest #set up current market state per instrument self.cur_prices = {} for instr in instruments: self.cur_prices[instr] = MarketState(None, None) self.logger = logging.getLogger(__name__)
def stream_to_queue(self): #check if file exists try: f = open(self.csv_file, 'rb') f.close() except Exception as e: self.logger.critical( "Caught exception while opening backtesting file %s\n", str(e)) return #open file and read from it file = open(self.csv_file, 'rb') try: for row in csv.reader(file, delimiter=','): # check if we have received a stoprequest if self.stoprequest.isSet(): break instrument, timestamp, bid, ask = row #update cur_prices if it exists for this instrument, else create it bid = float(bid) ask = float(ask) if instrument in self.cur_prices: self.cur_prices[instrument].update_bid_ask(bid, ask) else: self.cur_prices[instrument] = MarketState(bid, ask) tev = TickEvent(instrument, timestamp, bid, ask) self.events_queue.put(tev) time.sleep(.05) #do not flood the queue except Exception as e: self.logger.critical( "Caught exception while reading from backtesting file %s\n", str(e)) return finally: file.close()
def setUp(self): self.ms = MarketState(1.24010, 1.24000)
def __init__(self, events_queue, stoprequest): self.events_queue = events_queue self.stoprequest = stoprequest self.cur_prices = {"EUR_USD": MarketState(None, None)}