Skip to content

Commit

Permalink
fixed spread calc; fixed market snapshot output; added test
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Shabanov committed Mar 20, 2017
1 parent 772b412 commit e260eb0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
52 changes: 33 additions & 19 deletions FIX44.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,17 @@ def reject_handler(self, message: BaseMessage):

def market_data_snapshot_handler(self, message: BaseMessage):
prices = message.get_group(Field.Groups.MDEntry_Snapshot)

if len(prices) < 2:
self.logger.warn("No ask or bid in price update.")
return

ask_idx = 1 if prices[0][Field.MDEntryType] == '0' else 0
bid_idx = (ask_idx + 1) % 2
digits = pow(10, max(
len(prices[ask_idx][Field.MDEntryPx][prices[ask_idx][Field.MDEntryPx].find('.') + 1:]),
len(prices[bid_idx][Field.MDEntryPx][prices[bid_idx][Field.MDEntryPx].find('.') + 1:]),
))
spread = int(
float(prices[ask_idx][Field.MDEntryPx]) * digits - float(prices[bid_idx][Field.MDEntryPx]) * digits
)
self.logger.info("Symbol: {0}, Server Time: {1}, BID: {2}, ASK: {3}, SPREAD: {4}".format(
Symbol.NAME[int(message.get_field(Field.Symbol))],
spread = calculate_spread(prices[bid_idx][Field.MDEntryPx], prices[ask_idx][Field.MDEntryPx])
name = Symbol.NAME[int(message.get_field(Field.Symbol))] if int(message.get_field(Field.Symbol)) in Symbol.NAME else int(message.get_field(Field.Symbol))
self.logger.info("Symbol: {0: <7}\tServer Time: {1}\tBID: {2: <10}\tASK: {3: <10}\tSPREAD: {4}".format(
name,
message.get_field(Field.SendingTime),
prices[bid_idx][Field.MDEntryPx],
prices[ask_idx][Field.MDEntryPx],
Expand All @@ -204,16 +204,22 @@ def market_data_snapshot_handler(self, message: BaseMessage):
def market_data_refresh_handler(self, message: BaseMessage):
results = message.get_group(Field.Groups.MDEntry_Refresh)
actions = {'0': 'New', '2': 'Delete'}
self.logger.info(
"Price Update:\n\t\t\t"
"Symbol: {0}, Type: {1}, Price: {2}, Size: {3}, Action: {4}\n\t\t\t"
"Symbol: {5}, Type: {6}, Price: {7}, Size: {8}, Action: {9}".format(
Symbol.NAME[int(results[0][Field.Symbol])], results[0][Field.MDEntryID], results[0][Field.MDEntryPx],
results[0][Field.MDEntrySize], actions[results[0][Field.MDUpdateAction]],
Symbol.NAME[int(results[1][Field.Symbol])], results[1][Field.MDEntryID], results[1][Field.MDEntryPx],
results[1][Field.MDEntrySize], actions[results[1][Field.MDUpdateAction]]
)
)
types = {'0': 'BID', '1': 'ASK'}

message = "Price Update:"
for r in results:
name = Symbol.NAME[int(r[Field.Symbol])] if int(r[Field.Symbol]) in Symbol.NAME else r[Field.Symbol]
if actions[r[Field.MDUpdateAction]] == 'New':
message += "\n\t\t\tSymbol: {0: <7}, Type: {5}, ID: {1}, Price: {2 <10}, Size: {3}, Action: {4}".format(
name, r[Field.MDEntryID], r[Field.MDEntryPx], r[Field.MDEntrySize],
actions[r[Field.MDUpdateAction]], types[r[Field.MDEntryType]]
)
else:
message += "\n\t\t\tSymbol: {0: <7}, ID: {1}, Action: {2}".format(
name, r[Field.MDEntryID], actions[r[Field.MDUpdateAction]]
)

self.logger.info(message)

def logout_handler(self, message: BaseMessage):
self.logger.critical('Logout reason: {0}'.format(message.get_field(Field.Text)))
Expand All @@ -238,3 +244,11 @@ def symbol_unsubscribe(self, symbol_id):
False,
self.session
))


def calculate_spread(bid: str, ask: str):
digits = max(len(ask), len(bid))
if bid.find('.') != -1 or ask.find('.') != -1:
digits -= 1
spread = int(ask.replace('.', '').ljust(digits, '0')) - int(bid.replace('.', '').ljust(digits, '0'))
return spread
8 changes: 8 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import FIX44
import unittest


class TestCalculations(unittest.TestCase):
def testSpread(self):
self.assertEqual(FIX44.calculate_spread('113', '113.015'), 15)
self.assertEqual(FIX44.calculate_spread('1.09553', '1.09553'), 0)

0 comments on commit e260eb0

Please sign in to comment.