forked from rdegges/skele-cli
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* introduce price command * add lykkex service
- Loading branch information
Showing
15 changed files
with
183 additions
and
83 deletions.
There are no files selected for viewing
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
File renamed without changes.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .balance import * | ||
from .price import * |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
"""The balance command.""" | ||
|
||
from .base import Base | ||
|
||
|
||
class Balance(Base): | ||
"""Get lykkex balance""" | ||
|
||
def run(self): | ||
print('Getting balance...') |
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"""The price command.""" | ||
|
||
import services.lykkex_service as ly | ||
from .base import Base | ||
|
||
|
||
class Price(Base): | ||
"""Get asset price""" | ||
|
||
def run(self): | ||
asset_id = self.options["<asset_id>"] | ||
_, buy_price, buy_volume = ly.LykkexService.get_price(asset_id, 'BUY') | ||
_, sell_price, sell_volume = ly.LykkexService.get_price(asset_id, 'SELL') | ||
print("{} Price Volume".format(asset_id)) | ||
print("BUY: {:10.4f} {:10.2f}".format(buy_price, buy_volume)) | ||
print("SELL: {:10.4f} {:10.2f}".format(sell_price, -sell_volume)) |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import datetime | ||
import logging as log | ||
|
||
import lykkex | ||
|
||
from lykke.commands.services.time_service import get_current_time | ||
|
||
|
||
class LykkexService(object): | ||
@staticmethod | ||
def get_balance(api_key): | ||
log.info("Retrieve current balance.") | ||
time_stamp = get_current_time() | ||
balance = lykkex.get_balance(api_key) | ||
log.info("Number of assets: {}".format(len(balance))) | ||
for x in range(0, len(balance)): | ||
log.info(format('Current wealth ' + balance[x]['AssetId'].encode() + ': ' + str(balance[x]['Balance']))) | ||
return time_stamp, balance | ||
|
||
@staticmethod | ||
def get_pending_orders(api_key): | ||
log.info("Get pending orders.") | ||
time_stamp = get_current_time() | ||
pending_orders = lykkex.get_pending_orders(api_key) | ||
if not pending_orders: | ||
log.info("No pending orders") | ||
return time_stamp, pending_orders | ||
|
||
@staticmethod | ||
def send_market_order(api_key, asset_pair, asset, order_action, volume): | ||
log.info("Send market order - {}".format(asset)) | ||
time_stamp = get_current_time() | ||
response = lykkex.send_market_order(api_key, asset_pair, asset, order_action, volume) | ||
if response['Error']: | ||
log.info("Error: Market order not successful") | ||
raise RuntimeError("Error in sending market order. Check response {}".format(response)) | ||
final_price = response['Result'] | ||
log.info("Trade successful at price {}".format(final_price)) | ||
return time_stamp, final_price | ||
|
||
@staticmethod | ||
def send_limit_order(api_key, asset_pair, asset, price, order_action='BUY', volume='0.1'): | ||
log.info("Send market order - {}".format(asset)) | ||
time_stamp = get_current_time() | ||
response = lykkex.send_limit_order(api_key, asset_pair, asset, price, order_action, volume) | ||
log.info("Limit order placed") | ||
order_id = str(response) | ||
return time_stamp, order_id | ||
|
||
@staticmethod | ||
def control_limit_order(api_key, order_id): | ||
log.info("Check status of limit order {}", order_id) | ||
time_stamp = get_current_time() | ||
content = lykkex.get_order_status(api_key, order_id) | ||
status = content['Status'] | ||
return time_stamp, status | ||
|
||
@staticmethod | ||
def get_price(asset_pair_id, side='BUY'): | ||
log.info("Retrieve price: {}".format(side)) | ||
time_stamp = get_current_time() | ||
order_book = lykkex.get_order_book(asset_pair_id) | ||
price = LykkexService.get_asset_price(order_book, side) | ||
volume = LykkexService.get_asset_trading_volume(order_book, side) | ||
|
||
log.info("Timestamp: {}".format(time_stamp)) | ||
log.info("Price: {}".format(price)) | ||
return time_stamp, price, volume | ||
|
||
def get_latency(self, asset_pair_id): | ||
time_stamp = get_current_time() | ||
order_book = lykkex.get_order_book(asset_pair_id) | ||
time_ob = self.get_time_stamp_from_order_books(order_book) | ||
time_delta = (time_stamp - time_ob).total_seconds() | ||
log.info("System latency: {} secs".format(time_delta)) | ||
|
||
@staticmethod | ||
def get_asset_trading_volume(order_books, side): | ||
if side == 'BUY': | ||
return order_books[1]['Prices'][0]['Volume'] | ||
elif side == 'SELL': | ||
return order_books[0]['Prices'][0]['Volume'] | ||
else: | ||
return log.error('No valid input') | ||
|
||
@staticmethod | ||
def get_time_stamp_from_order_books(order_books): | ||
time_stamp_ob = order_books[1]['Timestamp'] | ||
val = datetime.datetime.strptime(time_stamp_ob, '%Y-%m-%dT%H:%M:%S.%f') | ||
return val | ||
|
||
@staticmethod | ||
def get_asset_price(order_books, side): | ||
try: | ||
if side == 'BUY': | ||
price = order_books[1]['Prices'][-1]['Price'] | ||
elif side == 'SELL': | ||
price = order_books[0]['Prices'][0]['Price'] | ||
except IndexError as e: | ||
log.error("Could not extract price from order books.") | ||
log.error("{}".format(order_books)) | ||
raise RuntimeError(e.message) | ||
return price | ||
|
||
def __init__(self): | ||
log.info("Initialize Lykkex connector.") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import datetime | ||
|
||
TIME_FORMAT = "%Y-%m-%d %H:%M:%S.%f" | ||
|
||
|
||
def get_current_time(): | ||
return datetime.datetime.now() | ||
|
||
def get_current_time_as_string(): | ||
return get_current_time().strftime(TIME_FORMAT) |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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