BLD: more live trading tests and fixed related issues

This commit is contained in:
fredfortier
2017-12-03 00:04:15 -05:00
parent f995f451a7
commit 96a27d083c
5 changed files with 27 additions and 20 deletions
+4 -4
View File
@@ -37,7 +37,7 @@ def initialize(context):
context.base_price = None
context.current_day = None
context.RSI_OVERSOLD = 25
context.RSI_OVERSOLD = 55
context.RSI_OVERBOUGHT = 82
context.CANDLE_SIZE = '5T'
@@ -239,7 +239,7 @@ def analyze(context=None, perf=None):
if __name__ == '__main__':
# The execution mode: backtest or live
MODE = 'backtest'
MODE = 'live'
if MODE == 'backtest':
folder = os.path.join(
@@ -251,7 +251,7 @@ if __name__ == '__main__':
out = os.path.join(folder, '{}.p'.format(timestr))
# catalyst run -f catalyst/examples/mean_reversion_simple.py -x bitfinex -s 2017-10-1 -e 2017-11-10 -c usdt -n mean-reversion --data-frequency minute --capital-base 10000
run_algorithm(
capital_base=0.5,
capital_base=0.1,
data_frequency='minute',
initialize=initialize,
handle_data=handle_data,
@@ -267,7 +267,7 @@ if __name__ == '__main__':
elif MODE == 'live':
run_algorithm(
capital_base=0.5,
capital_base=0.1,
initialize=initialize,
handle_data=handle_data,
analyze=analyze,
+16 -7
View File
@@ -321,6 +321,7 @@ class CCXT(Exchange):
else:
params['symbol'] = self.get_catalyst_symbol(market)
# TODO: add as an optional column
params['leverage'] = 1.0
return TradingPair(**params)
@@ -340,7 +341,8 @@ class CCXT(Exchange):
is_local=asset_def[1]
)
self.assets.append(asset)
except TypeError as e:
except TypeError:
pass
def get_balances(self):
@@ -405,7 +407,11 @@ class CCXT(Exchange):
# order_id = str(order_status['info']['clientOrderId'])
order_id = order_status['id']
symbol = order_status['info']['symbol']
# TODO: this won't work, redo the packages with a different key.
symbol = order_status['info']['symbol'] \
if 'symbol' in order_status['info'] \
else order_status['info']['Exchange']
order = Order(
dt=date,
@@ -455,10 +461,9 @@ class CCXT(Exchange):
if 'info' not in result:
raise ValueError('cannot use order without info attribute')
# order_id = str(result['info']['clientOrderId'])
order_id = result['id']
order = Order(
dt=from_ms_timestamp(result['info']['transactTime']),
dt=pd.Timestamp.utcnow(),
asset=asset,
amount=amount,
stop=style.get_stop_price(is_buy),
@@ -490,7 +495,7 @@ class CCXT(Exchange):
def _get_asset_from_order(self, order_id):
open_orders = self.portfolio.open_orders
order = next(
(order for order in open_orders if order.id == order_id),
(open_orders[id] for id in open_orders if id == order_id),
None
) # type: Order
return order.asset if order is not None else None
@@ -508,12 +513,12 @@ class CCXT(Exchange):
symbol = self.get_symbol(asset_or_symbol) \
if asset_or_symbol is not None else None
order_status = self.api.fetch_order(id=order_id, symbol=symbol)
order, _ = self._create_order(order_status)
order, executed_price = self._create_order(order_status)
except Exception as e:
raise ExchangeRequestError(error=e)
return order
return order, executed_price
def cancel_order(self, order_param, asset_or_symbol=None):
order_id = order_param.id \
@@ -555,6 +560,10 @@ class CCXT(Exchange):
ticker['last_traded'] = from_ms_timestamp(ticker['timestamp'])
if 'last_price' not in ticker:
# TODO: any more exceptions?
ticker['last_price'] = ticker['last']
# Using the volume represented in the base currency
ticker['volume'] = ticker['baseVolume'] \
if 'baseVolume' in ticker else 0
+2 -2
View File
@@ -270,7 +270,6 @@ class Exchange:
asset = a
if asset is None:
supported_symbols = sorted([
asset.symbol for asset in self.assets
])
@@ -704,8 +703,9 @@ class Exchange:
# TODO: convert if the position is not in the base currency
ticker = tickers[asset]
position = portfolio.positions[asset]
position.last_sale_price = ticker['last_price']
position.last_sale_date = ticker['timestamp']
position.last_sale_date = ticker['last_traded']
portfolio.positions_value += \
position.amount * position.last_sale_price
+2 -4
View File
@@ -1,5 +1,4 @@
import os
import os
import shutil
from datetime import datetime, timedelta
from functools import partial
@@ -28,10 +27,9 @@ from catalyst.exchange.exchange_bcolz import BcolzExchangeBarReader, \
from catalyst.exchange.exchange_errors import EmptyValuesInBundleError, \
TempBundleNotFoundError, \
NoDataAvailableOnExchange, \
PricingDataNotLoadedError, DataCorruptionError, ExchangeSymbolsNotFound, \
PricingDataValueError
PricingDataNotLoadedError, DataCorruptionError, PricingDataValueError
from catalyst.exchange.exchange_utils import get_exchange_folder, \
get_exchange_symbols, save_exchange_symbols, mixin_market_params
save_exchange_symbols, mixin_market_params
from catalyst.utils.cli import maybe_show_progress
from catalyst.utils.paths import ensure_directory
+3 -3
View File
@@ -153,11 +153,11 @@ def get_pretty_stats(stats_df, recorded_cols=None, num_rows=10):
def format_positions(positions):
parts = []
for position in positions:
msg = '{amount:.2f}{market} cost basis {cost_basis:.4f}{base}'.format(
msg = '{amount:.2f}{base} cost basis {cost_basis:.4f}{quote}'.format(
amount=position['amount'],
market=position['sid'].market_currency,
base=position['sid'].base_currency,
cost_basis=position['cost_basis'],
base=position['sid'].base_currency
quote=position['sid'].quote_currency
)
parts.append(msg)
return ', '.join(parts)