Last active
March 7, 2018 21:01
-
-
Save hokie-sam/b2fe330c4ff7418b09a396b1862b04bf to your computer and use it in GitHub Desktop.
Simple text-based limit order vizualizer
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
import math | |
def vizOr(): | |
''' | |
The buy orders are shown as a plus (+) sign, | |
the sell orders are shown as a minus (-) sign. | |
Obviously, you would want to provide a live | |
market price and list of your orders to the | |
function. | |
''' | |
markP = 100 # Set the market price | |
consoleWidth = 80 # Width of your console | |
pList = [97.5, 91, 101.5, 90, 130.25] # A list of prices of all your limit orders; both buys and sells. | |
strBuy =''; strSell = '' | |
maxChar = consoleWidth - 2*len(pList) - 3*len(str(markP)) - 6 # Adjusts maxChar to ensure the text stays on 1 line | |
if len(pList) > 0: | |
pList.append(markP) | |
pList.sort() | |
minP = min(pList); maxP = max(pList); pRange = maxP - minP | |
for i in range(0, len(pList) - 1): | |
if pList[i] < markP: | |
strBuy = strBuy + '+' + ' ' * round(maxChar*(pList[i + 1] - pList[i])/pRange) | |
elif pList[i] == markP: | |
strSell = strSell + str(markP) + ' ' * round(maxChar*(pList[i + 1] - pList[i])/pRange) | |
elif pList[i] > markP: | |
strSell = strSell + '-' + ' ' * round(maxChar*(pList[i + 1] - pList[i])/pRange) | |
if minP < markP: strBuy = ' ' + str(math.floor(minP)) + ' ' + strBuy | |
if maxP > markP: strSell = strSell + '-' + ' ' + str(math.ceil(maxP)) | |
print('\n'*2 + strBuy + strSell + '\n'*2) | |
else: print('No open orders....') | |
#end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is what the output from above will look like...