Blueshift Library ================== Blueshift library functions (from `blueshift.library`) provide a select set of functionalities unique to the platform, or wrapped from other packages for ease of use on the platform. These functions are divided in categories as follow: - Technicals Includes technical indicators and automatic technical pattern identification functionalities. - Statistical Include perceptually important points and change points algorithms. Also include a collection of functions that wraps various functionalities from other useful packages. - Pipelines A useful collection of ready-to-use pipeline filtering and factoring functions. - Timeseries A collection of functions and models useful for timeseries analysis and timeseries transformations. - Machine learning A useful collection of ready-to-use machine learning functionalities that wraps various other useful packages. - Models A useful collection of statistical and pricing models. - Execution Algorithms A selection of useful execution algorithms that can be directly used in a strategy to place orders. The features and functionalities of the blueshift library functions are frequently updated and revised. Technical Indicators -------------------- You can import all the technical indicators supported by the TA-Lib module from `blueshift.library.technicals.indicators`. .. note:: Use the uppercase name (as defined by TA-Lib) for functions that return vectorized computation. Use the lowercase name (with the same signature) for functions that return the last observation. The latter is useful for writing event-driven strategy. The TA-Lib functions, when imported from `blueshift.library.technicals.indicators`, can automatically identify required columns from pandas DataFrame and have additional error handling. See examples below. .. code-block:: python import talib as ta from blueshift.library.technicals.indicators import ADX, adx def initialize(context): ... def signal_function(asset, price): # we assume price is a dataframe with OHLC columns # to call the TA-Lib function, we must extract the required # columns x1 = ta.ADX(price.high, price.low, price.close) # not needed for the blueshift library version, the required # columns will be automatically extracted x2 = ADX(price) # but we can still use the TA-Lib signature if we want to # both will work x3 = ADX(price.high, price.low, price.close) # X1, X2 and X3 above are pandas Series. To get the last computed # value, use the lowercase version. X4 is a float. x4 = adx(price) Additional Indicators ++++++++++++++++++++++ Apart from the TA-Lib indicators, a few additional indicators are available as below. .. py:module:: blueshift.library.technicals.indicators .. autofunction:: blueshift.library.technicals.indicators.MA_XOVER .. autofunction:: blueshift.library.technicals.indicators.EMA_XOVER .. autofunction:: blueshift.library.technicals.indicators.BOLLINGER_BAND_DIST .. autofunction:: blueshift.library.technicals.indicators.HEIKIN_ASHI .. autofunction:: blueshift.library.technicals.indicators.ICHIMOKU_CLOUD .. autofunction:: blueshift.library.technicals.indicators.TREND_STALL .. autofunction:: blueshift.library.technicals.indicators.TREND_SET .. autofunction:: blueshift.library.technicals.indicators.SUPER_TREND These functions follow the same naming convention, i.e. the uppercase function names return vectorized output, and lower case for the last observation. An exception is `HEIKIN_ASHI` (returns DataFrame) which has no lowercase implementation. Technical patterns ------------------- .. py:module:: blueshift.library.technicals .. autofunction:: find_support_resistance .. autofunction:: search_chart_patterns Statistical Functions ---------------------- .. py:module:: blueshift.library.statistical .. autofunction:: find_imp_points .. autofunction:: find_trends .. autofunction:: get_hmm_state .. autofunction:: hedge_ratio .. autofunction:: z_score Timeseries Functions and Models -------------------------------- .. py:module:: blueshift.library.timeseries .. autofunction:: blueshift.library.timeseries.intraday_seasonality_func .. autofunction:: blueshift.library.timeseries.deseasonalize .. autofunction:: blueshift.library.timeseries.reseasonalize .. autoclass:: blueshift.library.timeseries.OnlineAutoARIMA .. autofunction:: blueshift.library.timeseries.auto_arima .. autofunction:: blueshift.library.timeseries.transform.resample .. autofunction:: blueshift.library.timeseries.transform.endpoints .. autofunction:: blueshift.library.timeseries.transform.split .. autofunction:: blueshift.library.timeseries.transform.period_apply .. autofunction:: blueshift.library.timeseries.transform.rollapply .. autofunction:: blueshift.library.timeseries.transform.cumulative_apply Statstical and Pricing Models ------------------------------ .. py:module:: blueshift.library.models .. autofunction:: blueshift.library.models.bs.bs_plain_vanilla_option .. autofunction:: blueshift.library.models.bs.bs_implied_vol .. autofunction:: blueshift.library.models.bs.bs_plain_vanilla_greek Machine Learning Functions --------------------------- .. py:module:: blueshift.library.ml .. autofunction:: estimate_random_forest .. autofunction:: predict_random_forest Pipeline Functions ------------------- .. py:module:: blueshift.library.pipelines .. autofunction:: select_universe .. autofunction:: average_volume_filter .. autofunction:: filter_assets .. autofunction:: filter_universe .. autofunction:: exclude_assets .. autofunction:: returns_factor .. autofunction:: filtered_returns_factor .. autofunction:: technical_factor Strategy Templates ------------------- These strategy template class makes it easier to develop and test strategies quickly. In general, to use a template, instantiate an object of the tempalte you need, and then make sure you call the object's `initialize` and `before_trading_start` methods in the corresponding functions of your strategy. For finer control you can call the `run_strategy` or other methods directly. Please read the caveats in the documentation of the respective methods to see if this will work for your particular case. .. py:module:: blueshift.library.algos.strategies .. autoclass:: blueshift.library.algos.strategies.SimpleStrategy .. automethod:: SimpleStrategy.run_strategy .. automethod:: SimpleStrategy.is_initialized .. automethod:: SimpleStrategy.is_complete .. automethod:: SimpleStrategy.start_entry .. automethod:: SimpleStrategy.stop_entry .. automethod:: SimpleStrategy.exit_all .. automethod:: SimpleStrategy.terminate .. autoclass:: blueshift.library.algos.strategies.IntradayStrategy .. autoclass:: blueshift.library.algos.strategies.PositionalStrategy Execution Algorithms --------------------- These execution algorithms are designed to be used with the :ref:`order` to place advanced algorithmic orders. These advanced order objects are different from the simpler :ref:`Order` object, but follow similar interface and attributes with equivalent meaning. .. py:module:: blueshift.library.algos .. autoclass:: blueshift.library.algos.executions.VanillaOrder .. autoclass:: blueshift.library.algos.executions.PassiveAggressiveOrder .. autoclass:: blueshift.library.algos.executions.AdaptiveOrder .. autoclass:: blueshift.library.algos.executions.MarketIfTouched .. autoclass:: blueshift.library.algos.executions.LimitIfTouched .. autoclass:: blueshift.library.algos.executions.ConditionalOrder .. autoclass:: blueshift.library.algos.executions.IcebergOrder .. autoclass:: blueshift.library.algos.executions.TwapOrder .. autoclass:: blueshift.library.algos.executions.PairOrder .. autoclass:: blueshift.library.algos.executions.BasketOrder An example of use case is shown below .. code-block:: python from blueshift.api import symbol, schedule_function, date_rules, time_rules from blueshift.api import get_datetime, order, terminate, schedule_once from blueshift.library.algos import IcebergOrder def initialize(context): asset = symbol('ACC') algo = IcebergOrder(asset, 50, 10, order_type='market', timeout=300) context.algo = algo schedule_once(place_order) schedule_function(strategy, date_rules.every_day(), time_rules.every_nth_minute(1)) def place_order(context, data): order(context.algo) def strategy(context, data): if not context.algo.is_open(): msg = f'{get_datetime()}:exiting strategy, algo execution terminated.' terminate(msg) In the above example, the strategy instantiates an Iceberg order and places the order as soon as possible. Once the order is placed, it periodically checks the status, and once completed, exits the strategy. Library Objects ---------------- .. py:module:: blueshift.library.common .. autoclass:: Signal .. autoattribute:: Signal.LONG_ENTRY .. autoattribute:: Signal.LONG_ENTRY_STRONG .. autoattribute:: Signal.SHORT_ENTRY .. autoattribute:: Signal.SHORT_ENTRY_STRONG .. autoattribute:: Signal.LONG_EXIT .. autoattribute:: Signal.SHORT_EXIT .. autoattribute:: Signal.EXIT .. autoattribute:: Signal.NO_SIGNAL .. autoclass:: LineType .. autoattribute:: LineType.SUPPORT .. autoattribute:: LineType.RESISTANCE .. autoattribute:: LineType.TREND .. autoclass:: Line .. autoattribute:: Line.type .. autoattribute:: Line.line .. autoattribute:: Line.score .. autoattribute:: Line.slope .. autoattribute:: Line.intercept .. automethod:: Line.is_breakout .. automethod:: Line.get_level .. automethod:: Line.plot .. autoclass:: Pattern .. automethod:: Pattern.plot Library utility functions -------------------------- .. autofunction:: str2bool .. autofunction:: get_upfront_capital