API Reference¶
Trading system¶
-
class
alchemist_lib.tradingsystem.
TradingSystem
(name, portfolio, set_weights, select_universe, handle_data, broker, paper_trading=False)[source]¶ Basic class for every trading system. The run method will start the live-trading.
-
name
¶ str – The name of the trading system.
-
portfolio
¶ alchemist_lib.portfolio.* – An istance of a portfolio class.
-
broker
¶ alchemist_lib.broker.* – An instance of a module in alchemist_lib.broker package.
-
_set_weights
¶ callable – The function to set the weights of every asset in the portfolio.
-
_select_universe
¶ callable – The function to select the universe of asset.
-
_handle_data
¶ callable – The function to manage the trading logic.
-
paper_trading
¶ boolean – If this arg is True no orders will be executed, they will be just printed and saved.
-
rebalance_time
¶ int – Autoincrement number, used to manage the frequency of rebalancing.
-
session
¶ sqlalchemy.orm.session.Session – Connection to the database.
-
__init__
(name, portfolio, set_weights, select_universe, handle_data, broker, paper_trading=False)[source]¶ Costructor method. After setting the attributes it will register the trading system in the database. If an sqlalchemy.exc.IntegrityError is raised It will update all data mantaining the name (primary key).
Parameters: - name (str) – Name of the trading system.
- portfolio (alchemist_lib.portfolio.*) – An istance of a portfolio class.
- broker (alchemist_lib.broker.*) – An instance of a module in alchemist_lib.broker package.
- set_weights (callable) – The function to set the weights of every asset in the portfolio.
- select_universe (callable) – The function to select the universe of asset.
- handle_data (callable) – The function to manage the trading logic.
- paper_trading (boolean, optional) – Specify if the trading system has to execute orders or just simulate.
-
handle_data
()[source]¶ Call the _handle_data callable attribute if it’s not None.
Returns: Return a dataframe with an alpha value for every asset. Empty if _handle_data is None. Return type: data (pandas.DataFrame)
-
on_market_open
(timeframe, frequency)[source]¶ Save new data and call the rebalance function.
Parameters:
-
rebalance
(alphas, orders_type, frequency)[source]¶ This method rebalance the portfolio based on the alphas parameters. It also update the current AUM value on the database.
Parameters: - alphas (pandas.DataFrame) – A dataframe with the following columns: * asset (alchemist_lib.database.asset.Asset): Must be the index. * alpha (decimal.Decimal): The value that will be used to calculate the weight of the asset within the portfolio.
- orders_type (str) – Order type identifier.
- frequency (int) – Frequency of rebalancing.
-
run
(delay, frequency)[source]¶ This method manages the “event-driven” interface. Start every method at the right time.
Parameters:
-
Factor¶
Factor autoclass
Datafeed¶
__init__¶
-
alchemist_lib.datafeed.
get_data_sources_dict
(session)[source]¶ Remember to change this method every time you add a module.
Parameters: session (sqlalchemy.orm.session.Session) – Connection to the database. Returns: Return a dictionary. The key must be the name of the data source in the database and the value must be an instance of the module charged to collect data. Return type: dsd (dict)
-
alchemist_lib.datafeed.
get_last_price
(assets)[source]¶ Return the last trade price for every asset. The last price is retrived based on
alchemist_lib.datafeed.get_data_sources_dict()
method.Parameters: assets (alchemist_lib.database.asset.Asset, list[Asset]) – List of assets which we want last traded price of. Returns: - A dataframe with the following columns:
- asset (alchemist_lib.database.asset.Asset): Must be the index.
- last_price (decimal.Decimal): The last price of the associated asset.
Return type: df (pandas.DataFrame)
-
alchemist_lib.datafeed.
save_ohlcv
(session, assets, start_date, timeframe)[source]¶ This method collects and saves OHLCV data from the specified data source.
Parameters: - session (sqlalchemy.orm.session.Session) – Database connection.
- assets (alchemist_lib.database.asset.Asset, list[Asset]) – List of assets which we want informations of.
- start_date (datetime.datetime) – Datetime to start collecting data from.
- timeframe (str) – Timeframe identifier.
-
alchemist_lib.datafeed.
save_last_ohlcv
(session, assets, timeframe)[source]¶ This method collects and saves the last OHLCV candle from the specified data source.
Parameters: - assets (alchemist_lib.database.asset.Asset, list[Asset]) – List of assets which we want informations of.
- timeframe (str) – Timeframe identifier.
ohlcv¶
-
class
alchemist_lib.datafeed.ohlcv.
OhlcvBaseClass
(session)[source]¶ Abstract class used by modules that manage OHLCV data.
- Abstract methods:
- get_last_price(assets): It has to return a dataframe (pandas.DataFrame) with the following columns:
- asset (alchemist_lib.database.asset.Asset): Must be the index.
- last_price (decimal.Decimal): The last price of the associated asset.
get_ohlcv(assets, start_date, end_date, timeframe): It has to return a list of Ohlcv (alchemist_lib.database.ohlcv.Ohlcv).
-
session
¶ sqlalchemy.orm.session.Session – Database connection.
-
__init__
(session)[source]¶ Costructor method.
Parameters: session (sqlalchemy.orm.session.Session) – Connection to the database.
-
_save
(data)[source]¶ Save records in the database.
This method doesn’t use
self.session.add_all(data)
because some objects could raise sqlalchemy.exc.IntegrityError and so nothing will be saved. Save an object per time allow us to pass away IntegrityError.Parameters: data (list[obj]) – List of map class instances.
-
get_last_ohlcv
(assets, timeframe)[source]¶ This method collects the last OHLCV candle from the data source.
Parameters: - assets (alchemist_lib.database.asset.Asset, list[Asset]) – List of assets which we want informations of.
- timeframe (str) – Timeframe identifier.
Returns: List of ohlcv data.
Return type: candles (list[alchemist_lib.database.ohlcv.Ohlcv])
-
save_last_ohlcv
(assets, timeframe)[source]¶ This method collects and saves last OHLCV candle from the data source.
Parameters: - assets (alchemist_lib.database.asset.Asset, list[Asset]) – List of assets which we want informations of.
- timeframe (str) – Timeframe identifier.
-
save_ohlcv
(assets, start_date, timeframe, end_date=datetime.datetime(2018, 4, 12, 17, 34, 9, 81402))[source]¶ This method collects and saves OHLCV data from the data source.
Parameters: - assets (alchemist_lib.database.asset.Asset, list[Asset]) – List of assets which we want informations of.
- start_date (datetime.datetime) – Datetime to start collecting data from.
- end_date (datetime.datetime, optional) – Datetime to end collecting data from. Default is utcnow().
- timeframe (str) – Timeframe identifier.
poloniexdatafeed¶
-
class
alchemist_lib.datafeed.poloniexdatafeed.
PoloniexDataFeed
(session)[source]¶ Class that manages data collecting from Poloniex. Inherits from alchemist_lib.datafeed.ohlcv.OhlcvBaseClass.
Website: https://poloniex.com/
Api documentation: https://poloniex.com/support/api/
Api wrapper: https://github.com/s4w3d0ff/python-poloniex
-
session
¶ sqlalchemy.orm.session.Session – Database connection.
-
polo
¶ poloniex.Poloniex – Communication object.
-
__init__
(session)[source]¶ Costructor method.
Parameters: session (sqlalchemy.orm.session.Session) – Connection to the database.
-
get_assets
()[source]¶ Return the list of asset traded.
Returns: List of assets. Return type: assets (list[alchemist_lib.database.asset.Asset]) Note
Return only pairs with bitcoin as base currency.
-
get_last_price
(assets)[source]¶ Retrieve last price of the list of assets.
Parameters: assets (alchemist_lib.database.asset.Asset, list[Asset]) – List of assets which we want the last trade price of. Returns: - A dataframe with the following columns:
- asset (alchemist_lib.database.asset.Asset): Must be the index.
- last_price (decimal.Decimal): The last price of the associated asset.
If some prices are not retrieved the last_price attribute will be 0.
Return type: df (pandas.DataFrame)
-
get_ohlcv
(assets, start_date, timeframe, end_date=datetime.datetime(2018, 4, 12, 17, 34, 25, 251506))[source]¶ Collect ohlcv data from start_date to end_date for every asset.
Parameters: - assets (list[alchemist_lib.database.asset.Asset]) – List of assets.
- start_date (datetime.datetime) – Datetime to start collecting data from.
- end_date (datetime.datetime, optional) – Datetime to end collecting data from. Default is utcnow().
- timeframe (str) – Timeframe identifier.
Returns: List of ohlcv data.
Return type: candles (list[alchemist_lib.database.ohlcv.Ohlcv])
-
bittrexdatafeed¶
-
class
alchemist_lib.datafeed.bittrexdatafeed.
BittrexDataFeed
(session)[source]¶ Class that manages data collecting from Bittrex. Inherits from alchemist_lib.datafeed.ohlcv.OhlcvBaseClass.
Website: https://bittrex.com/
Api documentation: https://bittrex.com/Home/Api
Api wrapper: https://github.com/ericsomdahl/python-bittrex
-
session
¶ sqlalchemy.orm.session.Session – Database connection.
-
bittrex
¶ bittrex.bittrex.Bittrex – Communication object.
-
__init__
(session)[source]¶ Costructor method.
Parameters: session (sqlalchemy.orm.session.Session) – Connection to the database.
-
get_assets
()[source]¶ Return the list of asset traded.
Returns: List of assets. Return type: assets (list[alchemist_lib.database.asset.Asset]) Note
Return only pairs with bitcoin as base currency.
-
get_last_price
(assets)[source]¶ Retrieve last price of the list of assets passed.
Parameters: assets (alchemist_lib.database.asset.Asset, list[Asset]) – List of assets which we want the last trade price of. Returns: - A dataframe with the following columns:
- asset (alchemist_lib.database.asset.Asset): Must be the index.
- last_price (decimal.Decimal): The last price of the associated asset.
If some prices are not retrieved the last_price attribute will be 0.
Return type: df (pandas.DataFrame)
-
get_ohlcv
(assets, start_date, timeframe, end_date=datetime.datetime(2018, 4, 12, 17, 34, 26, 176169))[source]¶ Collect ohlcv data from start_date to end_date for every asset.
Parameters: - assets (list[alchemist_lib.database.asset.Asset]) – List of assets.
- start_date (datetime.datetime) – Datetime to start collecting data from.
- end_date (datetime.datetime, optional) – Datetime to end collecting data from. Default is utcnow().
- timeframe (str) – Timeframe identifier.
Returns: List of ohlcv data.
Return type: candles (list[alchemist_lib.database.ohlcv.Ohlcv])
-
Broker¶
broker¶
-
class
alchemist_lib.broker.broker.
BrokerBaseClass
[source]¶ Abstract class used by broker modules.
- Abstract methods:
- place_order(allocs, amount, operation, order_type): It has to place an order based on the parameters.
-
session
¶ sqlalchemy.orm.session.Session – Database connection. Default is None.
-
execute
(allocs, ts_name, curr_ptf, orders_type='MKT')[source]¶ Method to execute orders for all portfolio. Before the SELL orders and after the BUY orders in order to have enought liquidity.
Parameters: - allocs (list[alchemist_lib.database.ptf_allocation.PtfAllocation]) – List of allocations to execute on the market.
- orders_type (str, optional) – Type of order. Default is MKT.
- ts_name (str) – Name of the trading system.
- curr_ptf (list[alchemist_lib.database.ptf_allocation.PtfAllocation]) – List of allocations currently in the portfolio.
poloniexbroker¶
-
class
alchemist_lib.broker.poloniexbroker.
PoloniexBroker
(api_key=None, secret_key=None)[source]¶ Inherits from alchemist_lib.broker.broker.BrokerBaseClass.
Website: https://poloniex.com/
Api documentation: https://poloniex.com/support/api/
Api wrapper: https://github.com/s4w3d0ff/python-poloniex
-
session
¶ sqlalchemy.orm.session.Session – Database connection.
-
polo
¶ poloniex.Poloniex – Communication object.
-
place_order
(asset, amount, order_type)[source]¶ Method to place an order.
Parameters: - asset (alchemist_lib.database.asset.Asset) – The asset we want exchange for BTC.
- amount (decimal.Decimal) – The amount we want to exchange.
- order_type (str) – Type of order.
Returns: The order identifier, if some error occurs returns -1.
Return type: order_id (int)
-
bittrexbroker¶
-
class
alchemist_lib.broker.bittrexbroker.
BittrexBroker
(api_key=None, secret_key=None)[source]¶ Inherits from alchemist_lib.broker.broker.BrokerBaseClass.
Website: https://bittrex.com/
Api documentation: https://bittrex.com/Home/Api
Api wrapper: https://github.com/ericsomdahl/python-bittrex
-
session
¶ sqlalchemy.orm.session.Session – Database connection.
-
bittrex
¶ bittrex.bittrex.Bittrex – Communication object.
-
place_order
(asset, amount, order_type)[source]¶ Method to place an order.
Parameters: - asset (alchemist_lib.database.asset.Asset) – The asset we want exchange for BTC.
- amount (decimal.Decimal) – The amount we want to exchange.
- order_type (str) – Type of order.
Returns: The order identifier, if some error occurs returns int(-1).
Return type: order_id (str, int)
-
Portfolio¶
portfolio¶
-
class
alchemist_lib.portfolio.portfolio.
PortfolioBaseClass
(capital)[source]¶ Abstract class used by modules that manage the portfolio costructor.
- Abstract methods:
- normalize_weights(df): It has to return a dataframe (pandas.DataFrame) with the following columns:
- asset (alchemist_lib.database.asset.Asset): Must be the index.
- weight (decimal.Decimal): The weight of the specified asset in the portfolio. The sum of all weights in the dataframe must be near 100 (or 1).
set_allocation(session, name, df): It has to return a list of allocations (alchemist_lib.database.ptf_allocation.PtfAllocation) based on the type of portfolio you want.
-
capital
¶ decimal.Decimal – Capital allocated for the portfolio.
-
__init__
(capital)[source]¶ Costructor method.
Parameters: capital (int, float, str, decimal.Decimal) – Capital allocated for the portfolio.
-
load_ptf
(session, name)[source]¶ Load the current portfolio from the database.
Parameters: - session (sqlalchemy.orm.session.Session) – Database connection.
- name (str) – Name of the trading system which manages the portfolio.
Returns: List of allocations of the specified trading system.
Return type: allocs (list[PtfAllocation])
-
rebalance
(curr_ptf, target_ptf)[source]¶ This method returns a list of PtfAllocation (alchemist_lib.database.ptf_allocation.PtfAllocation) that will be executed in order to mantain the portfolio rebalanced.
Parameters: - curr_ptf (alchemist_lib.database.ptf_allocation.PtfAllocation, list[PtfAllocation]) – Current portfolio, loaded from the database.
- target_ptf (alchemist_lib.database.ptf_allocation.PtfAllocation, list[PtfAllocation]) – Ideal portfolio.
Returns: List of PtfAllocation to execute in order to get the ideal portfolio.
Return type: new_ptf (list[alchemist_lib.database.ptf_allocation.PtfAllocation])
longsonly¶
-
class
alchemist_lib.portfolio.longsonly.
LongsOnlyPortfolio
(capital)[source]¶ Class that manages the creation of a portfolio of longs-only positions. Inherits from alchemist_lib.portfolio.portfolio.PortfolioBaseClass.
-
capital
¶ decimal.Decimal – Capital allocated for the portfolio.
-
__init__
(capital)[source]¶ Costructor method.
Parameters: capital (int, float, str, decimal.Decimal) – Capital allocated for the portfolio.
-
set_allocation
(session, name, df)[source]¶ Return a list of allocations (alchemist_lib.database.ptf_allocation.PtfAllocation) based on the weight of every asset.
Parameters: - session (sqlalchemy.orm.session.Session) – Connection to the database.
- name (str) – Name of the trading system.
- df (pandas.DataFrame) – A dataframe with the following columns: * asset (alchemist_lib.database.asset.Asset): Must be the index. * weight (decimal.Decimal): The weight of the specified asset in the portfolio. The sum of all weights in the dataframe must be near 1.
Returns: List of allocations (ideal portfolio).
Return type: allocs (list[PtfAllocation])
-
Exchange¶
exchange¶
-
class
alchemist_lib.exchange.exchange.
ExchangeBaseClass
[source]¶ Abstract class for every exchange class.
- Abstract methods:
- are_tradable(assets): It has to filter the args and returns just assets that are tradable.
- get_min_trade_size(asset): It has to returns the minimum order size based on the specified market.
__init__¶
-
alchemist_lib.exchange.
get_exchanges_dict
()[source]¶ Remember to change this method every time you add a module.
Returns: Return a dictionary. The key must be the name of the exchange in the database and the value must be an instance of the module charged to collect data. Return type: dsd (dict)
-
alchemist_lib.exchange.
are_tradable
(assets, exchange_name)[source]¶ Remove not tradable asset from the list passed as argument.
Parameters: - assets (alchemist_lib.database.asset.Asset, list[Asset]) – List of assets to filter.
- exchange_name (str) – The name of the exchange as is saved in the database.
Returns: Return the same list of assets passed without not tradable assets. If exchange_name is not correct the return is an empty string.
Return type: universe (list[Asset])
-
alchemist_lib.exchange.
get_assets
(session, exchange_name)[source]¶ Return all assets traded in a specified exchange.
Parameters: - session (sqlalchemy.orm.session.Session) – Database connection.
- exchange_name (str) – Name of the exchange.
Returns: List of Asset instances.
Return type: universe (list[alchemist_lib.database.asset.Asset])
poloniexexchange¶
-
class
alchemist_lib.exchange.poloniexexchange.
PoloniexExchange
[source]¶ Class that manages Poloniex metadata. Inherits from alchemist_lib.exchange.exchange.ExchangeBaseClass.
Website: https://poloniex.com/
Api documentation: https://poloniex.com/support/api/
Api wrapper: https://github.com/s4w3d0ff/python-poloniex
-
polo
¶ poloniex.Poloniex – Communication object.
-
are_tradable
(assets)[source]¶ Filter tradable assets.
Parameters: assets (alchemist_lib.database.asset.Asset, list[Asset]) – List of assets to check. Returns: Return all tradable asset (remove not tradable assets from the arg). Return type: tradable (list[Asset]) Note
Check just pairs with BTC as base currency.
-
get_min_order_size
(asset)[source]¶ This method would return the minimum order size for a specific market, but It’s not specified in the Poloniex documentation. https://poloniex.com/support/api/
Parameters: asset (alchemist_lib.database.asset.Asset) – The asset traded again BTC. Returns: Minimum order size. Default is 0. Return type: size (decimal.Decimal)
-
bittrexexchange¶
-
class
alchemist_lib.exchange.bittrexexchange.
BittrexExchange
[source]¶ Class that manages Bittrex metadata. Inherits from alchemist_lib.exchange.exchange.ExchangeBaseClass.
Website: https://bittrex.com/
Api documentation: https://bittrex.com/Home/Api
Api wrapper: https://github.com/ericsomdahl/python-bittrex
-
bittrex
¶ bittrex.bittrex.Bittrex – Communication object.
-
are_tradable
(assets)[source]¶ Filter tradable assets.
Parameters: assets (alchemist_lib.database.asset.Asset, list[Asset]) – List of assets to check. Returns: Return all tradable asset (remove not tradable assets from the arg). Return type: tradable (list[Asset]) Note
Check just pairs with BTC as base currency.
-
Populate¶
saver¶
-
class
alchemist_lib.populate.saver.
Saver
(session)[source]¶ Class that saves data on the database.
-
session
¶ sqlalchemy.orm.session.Session – Database connection.
-
__init__
(session)[source]¶ Costructor method.
Parameters: session (sqlalchemy.orm.session.Session) – Database connection.
-
asset
(ticker, instrument_id, name, exchanges)[source]¶ Save an asset on the db.
Parameters: Returns: Map class instance.
Return type: tt (alchemist_lib.database.asset.Asset)
-
broker
(name, site)[source]¶ Save a broker on the db.
Parameters: Returns: Map class instance.
Return type: broker (alchemist_lib.database.broker.Broker)
-
data_source
(name, site, timeframes)[source]¶ Save a data_source on the db.
Parameters: Returns: Map class instance.
Return type: ds (alchemist_lib.database.price_data_source.PriceDataSource)
-
exchange
(name, website, data_source, timetable, brokers)[source]¶ Save an exchange on the db.
Parameters: - name (str) – Name of the exchange.
- website (str) – Website of the exchange.
- data_source (alchemist_lib.database.price_data_source.PriceDataSource) – PriceDataSource instance associated with this exchange. Specify how to get price informations.
- timetable (alchemist_lib.database.timetable.Timetable) – Timetable of this exchange.
- brokers (list[alchemist_lib.database.broker.Broker]) – Brokers that allow trading on this exchange.
Returns: Map class instance.
Return type: exchange (alchemist_lib.database.exchange.Exchange)
-
instrument
(kind)[source]¶ Save an instrument on the db.
Parameters: kind (str) – Kind of instrument to save. (instrument_type) Returns: Map class instance. Return type: instrument (alchemist_lib.database.instrument.Instrument)
-
timeframe
(id, description)[source]¶ Save a timeframe on the db.
Parameters: Returns: Map class instance.
Return type: tf (alchemist_lib.database.timeframe.Timeframe)
-
populate¶
-
class
alchemist_lib.populate.populate.
PopulateBaseClass
(saver)[source]¶ Abstract class for every populate class.
- Abstract methods:
- get_exchange_instance(): Save all data associated with an exchange and returns an instance of alchemist_lib.database.exchange.Exchange.
- populate(): Save all assets traded in a specific exchange.
- update_asset_list(): Update the list of assets tradable in an exchange, remove delisted assets and add new assets.
-
saver
¶ alchemist_lib.populate.saver.Saver – Instance of the saver class.
__init__¶
-
alchemist_lib.
populate
¶ alias of
alchemist_lib.populate
poloniexpopulate¶
-
class
alchemist_lib.populate.poloniexpopulate.
PoloniexPopulate
(saver)[source]¶ Class that manages the population of the database with data from Poloniex. Inherits from alchemist_lib.populate.populate.PopulateBaseClass.
-
saver
¶ alchemist_lib.populate.saver.Saver – Saver class instance.
-
__init__
(saver)[source]¶ Costructor method.
Parameters: saver (alchemist_lib.populate.saver.Saver) – Saver class instance.
-
bittrexpopulate¶
-
class
alchemist_lib.populate.bittrexpopulate.
BittrexPopulate
(saver)[source]¶ Class that manages the population of the database with data from Bittrex. Inherits from alchemist_lib.populate.populate.PopulateBaseClass.
-
saver
¶ alchemist_lib.populate.saver.Saver – Saver class instance.
-
__init__
(saver)[source]¶ Costructor method.
Parameters: saver (alchemist_lib.populate.saver.Saver) – Saver class instance.
-