Mocks and Testing

Known Issues

Note

There is a known pandas issue that logs a warning about _timelex, and it will show as a warning until it is fixed in pandas. Please ignore this warning for now.

DeprecationWarning: _timelex is a private class and may break without warning, it will be moved and or renamed in future versions.

Run All Tests

py.test --maxfail=1

Mock S3 Boto Utilities

These are testing utilities for mocking S3 functionality without having an s3 endpoint running.

Mock boto3 s3 objects

class analysis_engine.mocks.mock_boto3_s3.MockBotoS3Bucket(name)[source]
put_object(Key=None, Body=None)[source]
Parameters:
  • Key – new Key name
  • Body – new Payload in Key
class analysis_engine.mocks.mock_boto3_s3.MockBotoS3AllBuckets[source]
add(bucket_name)[source]
Parameters:bucket_name – bucket name to add
all()[source]
class analysis_engine.mocks.mock_boto3_s3.MockBotoS3(name='mock_s3', endpoint_url=None, aws_access_key_id=None, aws_secret_access_key=None, region_name=None, config=None)[source]
Bucket(name)[source]
Parameters:name – name of new bucket
create_bucket(Bucket=None)[source]
Parameters:bucket_name – name of the new bucket
analysis_engine.mocks.mock_boto3_s3.build_boto3_resource(name='mock_s3', endpoint_url=None, aws_access_key_id=None, aws_secret_access_key=None, region_name=None, config=None)[source]
Parameters:
  • name – name of client
  • endpoint_url – endpoint url
  • aws_access_key_id – aws access key
  • aws_secret_access_key – aws secret key
  • region_name – region name
  • config – config object
analysis_engine.mocks.mock_boto3_s3.mock_s3_read_contents_from_key_ev(s3, s3_bucket_name, s3_key, encoding, convert_as_json)[source]

mock_s3_read_contents_from_key

Parameters:
  • s3 – s3 client
  • s3_bucket_name – bucket name
  • s3_key – key
  • encoding – utf-8
  • convert_as_json – convert to json
analysis_engine.mocks.mock_boto3_s3.mock_publish_from_s3_to_redis(work_dict)[source]
Parameters:work_dict – dictionary for driving the task
analysis_engine.mocks.mock_boto3_s3.mock_publish_from_s3_to_redis_err(work_dict)[source]
Parameters:work_dict – dictionary for driving the task
analysis_engine.mocks.mock_boto3_s3.mock_publish_from_s3_exception(work_dict)[source]
Parameters:work_dict – dictionary for driving the task

Mock Redis Utilities

These are testing utilities for mocking Redis’s functionality without having a Redis server running.

Mock redis objects

class analysis_engine.mocks.mock_redis.MockRedis(host=None, port=None, password=None, db=None)[source]
get(name=None)[source]

mock redis get

Parameters:name – name of the key to check
set(name=None, value=None, ex=None, px=None, nx=False, xx=False)[source]

mock redis set

Parameters:
  • name – cache key name
  • value – value to cache
  • ex – expire time
  • px – redis values
  • nx – redis values
  • xx – redis values
class analysis_engine.mocks.mock_redis.MockRedisFailToConnect(host=None, port=None, password=None, db=None)[source]

Mock Yahoo Utilities

These are testing utilities for mocking Yahoo’s functionality without having internet connectivity to fetch data from Yahoo.

Mock Pinance Object for unittests

analysis_engine.mocks.mock_pinance.mock_get_options(ticker=None, contract_type=None, exp_date_str=None, strike=None)[source]
Parameters:
  • ticker – ticker to lookup
  • exp_date_strYYYY-MM-DD expiration date format
  • strike – optional strike price, None returns all option chains
  • contract_typeC calls or P for puts, if strike=None then the contract_type is ignored
class analysis_engine.mocks.mock_pinance.MockPinance(symbol='SPY')[source]
get_news()[source]
get_options(ticker=None, contract_type=None, exp_date_str=None, strike=None)[source]
Parameters:
  • ticker – ticker to lookup
  • exp_date_strYYYY-MM-DD expiration date format
  • strike – optional strike price, None returns all option chains
  • contract_typeC calls or P for puts, if strike=None then the contract_type is ignored
get_quotes()[source]

Mock IEX Utilities

These are testing utilities for mocking IEX functionality without having internet connectivity to fetch data from IEX.

Mocking data fetch api calls

analysis_engine.mocks.mock_iex.mock_minute(url, token=None, version=None, verbose=False)[source]

mock minute history for a chart

Parameters:
  • url – IEX resource url
  • token – optional - string token for your user’s account
  • version – optional - version string
  • verbose – optional - boolean debug logging
analysis_engine.mocks.mock_iex.mock_quote(url, token=None, version=None, verbose=False)[source]

mock quote

Parameters:
  • url – IEX resource url
  • token – optional - string token for your user’s account
  • version – optional - version string
  • verbose – optional - boolean debug logging
analysis_engine.mocks.mock_iex.mock_stats(url, token=None, version=None, verbose=False)[source]

mock stats

Parameters:
  • url – IEX resource url
  • token – optional - string token for your user’s account
  • version – optional - version string
  • verbose – optional - boolean debug logging
analysis_engine.mocks.mock_iex.mock_peers(url, token=None, version=None, verbose=False)[source]

mock peers

Parameters:
  • url – IEX resource url
  • token – optional - string token for your user’s account
  • version – optional - version string
  • verbose – optional - boolean debug logging
analysis_engine.mocks.mock_iex.mock_news(url, token=None, version=None, verbose=False)[source]

mock news

Parameters:
  • url – IEX resource url
  • token – optional - string token for your user’s account
  • version – optional - version string
  • verbose – optional - boolean debug logging
analysis_engine.mocks.mock_iex.mock_financials(url, token=None, version=None, verbose=False)[source]

mock financials

Parameters:
  • url – IEX resource url
  • token – optional - string token for your user’s account
  • version – optional - version string
  • verbose – optional - boolean debug logging
analysis_engine.mocks.mock_iex.mock_earnings(url, token=None, version=None, verbose=False)[source]

mock earnings

Parameters:
  • url – IEX resource url
  • token – optional - string token for your user’s account
  • version – optional - version string
  • verbose – optional - boolean debug logging
analysis_engine.mocks.mock_iex.mock_dividends(url, token=None, version=None, verbose=False)[source]

mock dividends

Parameters:
  • url – IEX resource url
  • token – optional - string token for your user’s account
  • version – optional - version string
  • verbose – optional - boolean debug logging
analysis_engine.mocks.mock_iex.mock_company(url, token=None, version=None, verbose=False)[source]

mock company

Parameters:
  • url – IEX resource url
  • token – optional - string token for your user’s account
  • version – optional - version string
  • verbose – optional - boolean debug logging

Mock TA Lib

These are mock talib functions to help test indicators using talib.

Mock TA-Lib objects

analysis_engine.mocks.mock_talib.MockWILLRBuy(high=None, low=None, close=None, timeperiod=None)[source]

build a mock wiliams r object that will report an buy value to test indicators without having talib installed

Parameters:
  • high – list of highs
  • low – list of lows
  • close – list of closes
  • timeperiod – integer number of values in high, low and close
analysis_engine.mocks.mock_talib.MockWILLRSell(high=None, low=None, close=None, timeperiod=None)[source]

build a mock wiliams r object that will report an sell value to test indicators without having talib installed

Parameters:
  • high – list of highs
  • low – list of lows
  • close – list of closes
  • timeperiod – integer number of values in high, low and close
analysis_engine.mocks.mock_talib.MockWILLRIgnore(high=None, low=None, close=None, timeperiod=None)[source]

build a mock wiliams r object that will report an ignore value to test indicators without having talib installed

Parameters:
  • high – list of highs
  • low – list of lows
  • close – list of closes
  • timeperiod – integer number of values in high, low and close

Mock Trading Tools for Developing Algorithms and Indicators

These are mock helper functions for patching the BaseAlgo object to simulate various test cases

Mock Algorithm Methods for unittesting things like previously-owned shares or sell-side indicators without owning shares

analysis_engine.mocks.mock_algo_trading.mock_algo_owns_shares_in_ticker_before_starting(obj, ticker)[source]

Support mocking owned shares to test indicator selling

If you can modify your algorithm config_dict you can also set a positions dictionary like:

algo_config_dict = {
    # other values omitted for docs
    'positions': {
        'SPY': {
            'shares': 10000,
            'buys': [],
            'sells': []
        }
    }
}

Use with your custom algorithm unittests:

import mock
import analysis_engine.mocks.mock_algo_trading as mock_trading

@mock.patch(
    ('analysis_engine.algo.BaseAlgo.get_ticker_positions'),
    new=mock_trading.mock_algo_owns_shares_in_ticker_before_starting)
Parameters:
  • obj – algorithm object
  • ticker – ticker symbol