Skip to content

Commit 17ec296

Browse files
#31 finish cleaning examples
1 parent 3a480c6 commit 17ec296

14 files changed

+184
-296
lines changed

dao/deal_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def init_deals_with_logging_speedy(trade_pairs, difference, file_name, processor
8181
msg_queue.add_message(DEAL_INFO_MSG, msg)
8282
log_to_file(msg, file_name)
8383

84-
if YES_I_KNOW_WHAT_AM_I_DOING:
84+
if not YES_I_KNOW_WHAT_AM_I_DOING:
8585
die_hard("init_deals_with_logging_speedy called for {f}".format(f=trade_pairs))
8686

8787
parallel_deals = []
File renamed without changes.

legacy_manual_tests/async_data_retrieval.py renamed to examples/async_data_retrieval.py

+34
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
from profilehooks import timecall
22

33
from utils.time_utils import get_now_seconds_utc
4+
from utils.key_utils import load_keys
5+
from utils.debug_utils import LOG_ALL_DEBUG
46

57
from data_access.classes.connection_pool import ConnectionPool
8+
from data.arbitrage_config import ArbitrageConfig
69

710
from dao.history_utils import get_history_speedup
811
from dao.ohlc_utils import get_ohlc_speedup, get_ohlc
912
from dao.order_book_utils import get_order_book_speedup
1013
from dao.ticker_utils import get_ticker_speedup
14+
from dao.order_utils import get_open_orders_for_arbitrage_pair
15+
16+
from enums.currency_pair import CURRENCY_PAIR
17+
from enums.exchange import EXCHANGE
18+
19+
from constants import API_KEY_PATH
1120

1221

1322
POLL_PERIOD_SECONDS = 900
@@ -52,3 +61,28 @@ def get_order_book_time_fast():
5261
processor = ConnectionPool()
5362
order_book = get_order_book_speedup(end_time, processor)
5463
return order_book
64+
65+
66+
def test_open_orders_retrieval_arbitrage():
67+
68+
sell_exchange_id = EXCHANGE.BINANCE
69+
buy_exchange_id = EXCHANGE.POLONIEX
70+
pair_id = CURRENCY_PAIR.BTC_TO_OMG
71+
threshold = 2.0
72+
reverse_threshold = 2.0
73+
balance_threshold = 5.0
74+
deal_expire_timeout = 60
75+
logging_level = LOG_ALL_DEBUG
76+
77+
cfg = ArbitrageConfig(sell_exchange_id, buy_exchange_id,
78+
pair_id, threshold,
79+
reverse_threshold, balance_threshold,
80+
deal_expire_timeout,
81+
logging_level)
82+
load_keys(API_KEY_PATH)
83+
processor = ConnectionPool(pool_size=2)
84+
print cfg
85+
res = get_open_orders_for_arbitrage_pair(cfg, processor)
86+
print "Length:", len(res)
87+
for r in res:
88+
print r
File renamed without changes.

legacy_manual_tests/orders_reprocessing.py renamed to examples/orders_reprocessing.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
from enums.exchange import EXCHANGE
1414
from enums.currency_pair import CURRENCY_PAIR
1515

16+
from constants import API_KEY_PATH
17+
1618

1719
def test_failed_order_placement_huobi():
1820

19-
load_keys("./secret_keys")
21+
load_keys(API_KEY_PATH)
2022

2123
ts = get_now_seconds_utc()
2224
order = Trade(DEAL_TYPE.SELL, EXCHANGE.HUOBI, CURRENCY_PAIR.BTC_TO_ZIL,
@@ -31,7 +33,7 @@ def test_failed_order_placement_huobi():
3133

3234

3335
def test_failed_order_placement_bittrex():
34-
load_keys("./secret_keys")
36+
load_keys(API_KEY_PATH)
3537

3638
ts = get_now_seconds_utc()
3739
order = Trade(DEAL_TYPE.SELL, EXCHANGE.BITTREX, CURRENCY_PAIR.BTC_TO_ETH,
@@ -46,7 +48,7 @@ def test_failed_order_placement_bittrex():
4648

4749

4850
def test_expired_deal_placement():
49-
load_keys("./secret_keys")
51+
load_keys(API_KEY_PATH)
5052
priority_queue = get_priority_queue()
5153
ts = get_now_seconds_utc()
5254
order = Trade(DEAL_TYPE.SELL, EXCHANGE.BINANCE, CURRENCY_PAIR.BTC_TO_STRAT, price=0.001, volume=5.0,
@@ -60,7 +62,7 @@ def test_expired_deal_placement():
6062

6163

6264
def test_failed_deal_placement():
63-
load_keys("./secret_keys")
65+
load_keys(API_KEY_PATH)
6466
msg_queue = get_message_queue()
6567
ts = 1517938516
6668
order = Trade(DEAL_TYPE.SELL, EXCHANGE.BITTREX, CURRENCY_PAIR.BTC_TO_STRAT, price=0.000844, volume=5.0,

examples/pool_execution_example.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import gevent
2+
3+
from data_access.classes.connection_pool import ConnectionPool
4+
from utils.time_utils import sleep_for
5+
6+
7+
def test_pool():
8+
9+
def heavy_load():
10+
sleep_for(3)
11+
print "heavy_load"
12+
13+
def more_heavy_load():
14+
sleep_for(30)
15+
print "more_heavy_load"
16+
17+
def batch(iterable, n=1):
18+
l = len(iterable)
19+
for ndx in range(0, l, n):
20+
yield iterable[ndx:min(ndx + n, l)]
21+
processor = ConnectionPool(10)
22+
23+
iters = []
24+
for x in xrange(100):
25+
iters.append(x)
26+
27+
for work_batch in batch(iters, 10):
28+
futures = []
29+
for x in work_batch:
30+
futures.append(processor.network_pool.spawn(more_heavy_load))
31+
gevent.joinall(futures)
32+
33+
for work_batch in batch(iters, 10):
34+
futures = []
35+
for x in work_batch:
36+
futures.append(processor.network_pool.spawn(heavy_load))
37+
gevent.joinall(futures)

legacy_manual_tests/postgres_related.py renamed to examples/postgres_related.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from dao.db import save_order_into_pg, init_pg_connection, is_order_present_in_order_history, \
1414
is_trade_present_in_trade_history
1515

16-
from constants import DB_HOST, DB_PORT, DB_NAME
16+
from constants import DB_HOST, DB_PORT, DB_NAME, API_KEY_PATH
1717

1818
from analysis.data_load_for_profit_report import load_recent_huobi_trades_to_db, \
1919
wrap_with_progress_bar, save_to_pg_adapter
@@ -83,7 +83,7 @@ def load_trades_from_csv_to_db():
8383

8484
def test_trade_history_huobi_methods():
8585

86-
load_keys("./secret_keys")
86+
load_keys(API_KEY_PATH)
8787
key = get_key_by_exchange(EXCHANGE.HUOBI)
8888

8989
time_end = get_now_seconds_utc()
File renamed without changes.

examples/trade_placement.py

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""
2+
Routine that demonstrate usage of some sub-set of functionality
3+
"""
4+
5+
from binance.buy_utils import add_buy_order_binance
6+
7+
from dao.deal_utils import init_deals_with_logging_speedy
8+
from data.trade import Trade
9+
from data.trade_pair import TradePair
10+
from data_access.classes.connection_pool import ConnectionPool
11+
12+
from enums.currency_pair import CURRENCY_PAIR
13+
from enums.deal_type import DEAL_TYPE
14+
from enums.exchange import EXCHANGE
15+
16+
from poloniex.buy_utils import add_buy_order_poloniex
17+
18+
from bittrex.buy_utils import add_buy_order_bittrex
19+
from bittrex.sell_utils import add_sell_order_bittrex
20+
21+
from utils.key_utils import load_keys, get_key_by_exchange
22+
from utils.time_utils import get_now_seconds_utc
23+
from utils.currency_utils import get_currency_pair_name_by_exchange_id
24+
from utils.system_utils import die_hard
25+
26+
from data_access.message_queue import get_message_queue
27+
28+
from constants import YES_I_KNOW_WHAT_AM_I_DOING, API_KEY_PATH
29+
30+
31+
def check_deal_placements():
32+
if not YES_I_KNOW_WHAT_AM_I_DOING:
33+
die_hard("check_deal_placements may issue a real trade!")
34+
35+
create_time = get_now_seconds_utc()
36+
fake_order_book_time1 = -10
37+
fake_order_book_time2 = -20
38+
deal_volume = 5
39+
pair_id = CURRENCY_PAIR.BTC_TO_ARDR
40+
41+
sell_exchange_id = EXCHANGE.POLONIEX
42+
buy_exchange_id = EXCHANGE.BITTREX
43+
44+
difference = "difference is HUGE"
45+
file_name = "test.log"
46+
47+
msg_queue = get_message_queue()
48+
49+
processor = ConnectionPool(pool_size=2)
50+
51+
trade_at_first_exchange = Trade(DEAL_TYPE.SELL, sell_exchange_id, pair_id,
52+
0.00000001, deal_volume, fake_order_book_time1,
53+
create_time)
54+
55+
trade_at_second_exchange = Trade(DEAL_TYPE.BUY, buy_exchange_id, pair_id,
56+
0.00004, deal_volume, fake_order_book_time2,
57+
create_time)
58+
59+
trade_pairs = TradePair(trade_at_first_exchange, trade_at_second_exchange, fake_order_book_time1, fake_order_book_time2, DEAL_TYPE.DEBUG)
60+
61+
init_deals_with_logging_speedy(trade_pairs, difference, file_name, processor, msg_queue)
62+
63+
#
64+
# For testing new currency pair
65+
#
66+
67+
68+
def test_binance_xlm():
69+
if not YES_I_KNOW_WHAT_AM_I_DOING:
70+
die_hard("test_binance_xlm may issue a real trade!")
71+
72+
load_keys(API_KEY_PATH)
73+
key = get_key_by_exchange(EXCHANGE.BINANCE)
74+
pair_id = CURRENCY_PAIR.BTC_TO_XLM
75+
pair_name = get_currency_pair_name_by_exchange_id(pair_id, EXCHANGE.BINANCE)
76+
err, json_repr = add_buy_order_binance(key, pair_name, price=0.00003000, amount=100)
77+
print json_repr
78+
79+
80+
def test_poloniex_doge():
81+
if not YES_I_KNOW_WHAT_AM_I_DOING:
82+
die_hard("test_poloniex_doge may issue a real trade!")
83+
84+
load_keys(API_KEY_PATH)
85+
key = get_key_by_exchange(EXCHANGE.POLONIEX)
86+
pair_id = CURRENCY_PAIR.BTC_TO_DGB
87+
pair_name = get_currency_pair_name_by_exchange_id(pair_id, EXCHANGE.POLONIEX)
88+
err, json_repr = add_buy_order_poloniex(key, pair_name, price=0.00000300, amount=100)
89+
print json_repr
90+
91+
92+
def test_bittrex_strat():
93+
if not YES_I_KNOW_WHAT_AM_I_DOING:
94+
die_hard("test_bittrex_strat may issue a real trade!")
95+
96+
key = get_key_by_exchange(EXCHANGE.BITTREX)
97+
pair_id = CURRENCY_PAIR.BTC_TO_STRAT
98+
pair_name = get_currency_pair_name_by_exchange_id(pair_id, EXCHANGE.BITTREX)
99+
err, json_repr = add_buy_order_bittrex(key, pair_name, price=0.0007, amount=10)
100+
print json_repr
101+
err, json_repr = add_sell_order_bittrex(key, pair_name, price=0.0015, amount=10)
102+
print json_repr
File renamed without changes.

huobi/order_book_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def get_order_book_huobi(pair_name, timest):
3535

3636
def get_order_book_huobi_result_processor(json_document, pair_name, timest):
3737

38-
if is_error(json_document) or json_document["tick"] is None:
38+
if is_error(json_document) or json_document.get("tick") is None:
3939

4040
msg = "get_order_book_huobi_result_processor - error response - {er}".format(er=json_document)
4141
log_to_file(msg, ERROR_LOG_FILE_NAME)

huobi/ticker_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def get_ticker_huobi(pair_name, timest):
3535

3636

3737
def get_ticker_huobi_result_processor(json_document, pair_name, timest):
38-
if is_error(json_document) or json_document["tick"] is None:
38+
if is_error(json_document) or json_document.get("tick") is None:
3939

4040
msg = "get_ticker_huobi_result_processor - error response - {er}".format(er=json_document)
4141
log_to_file(msg, ERROR_LOG_FILE_NAME)

0 commit comments

Comments
 (0)