Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Run an Equity Algorithm using the Stock Analysis Engine
import pandas as pd
from analysis_engine.algo import EquityAlgo
ticker = 'SPY'
demo_algo = EquityAlgo(
ticker=ticker,
balance=1000.00,
commission=6.00,
name='test-{}'.format(ticker))
date_key = '{}_2018-11-05'.format(
ticker)
# mock the data pipeline in redis:
data = {
ticker: [
{
'date': date_key,
'data': {
'daily': pd.DataFrame([
{
'high': 280.01,
'low': 270.01,
'open': 275.01,
'close': 272.02,
'volume': 123,
'date': '2018-11-01 15:59:59'
},
{
'high': 281.01,
'low': 271.01,
'open': 276.01,
'close': 273.02,
'volume': 124,
'date': '2018-11-02 15:59:59'
},
{
'high': 282.01,
'low': 272.01,
'open': 277.01,
'close': 274.02,
'volume': 121,
'date': '2018-11-05 15:59:59'
}
]),
'minute': pd.DataFrame([]),
'news': pd.DataFrame([]),
'options': pd.DataFrame([])
# etc
}
}
]
}
# run the algorithm
demo_algo.handle_data(data=data)
# get the algorithm results
results = demo_algo.get_result()
print(results)
@jay-johnson

This comment has been minimized.

Copy link
Owner Author

commented Nov 8, 2018

this is still a work in progress, but the algorithm is running:

2018-11-08 08:36:56,599 - analysis_engine.algo - INFO - test-SPY - handle - start
2018-11-08 08:36:56,599 - analysis_engine.algo - INFO - test-SPY - handle - tickers=["SPY"]
2018-11-08 08:36:56,600 - analysis_engine.algo - INFO - test-SPY - handle - ticker=SPY 0.0 0/1 - ds=SPY_2018-11-05
2018-11-08 08:36:56,600 - analysis_engine.algo - INFO - test-SPY - az - ticker=SPY 0.0 0/1 - ds=no-ds-name bal=1000.0 shares=None num_buys=0 num_sells=0
2018-11-08 08:36:56,600 - analysis_engine.algo - INFO - test-SPY - buy start SPY@270.0 - shares=None
2018-11-08 08:36:56,600 - analysis_engine.algo - INFO - test-SPY - buy end SPY@270.0 TRADE_FILLED shares=3 cost=816.0 bal=184.0 prev_shares=0 prev_bal=1000.0
2018-11-08 08:36:56,600 - analysis_engine.algo - INFO - test-SPY - sell start SPY@270.0
2018-11-08 08:36:56,600 - analysis_engine.algo - INFO - test-SPY - sell end SPY@270.0 TRADE_FILLED shares=3 cost=816.0 bal=1000.0 prev_shares=3 prev_bal=184.0
2018-11-08 08:36:56,600 - analysis_engine.algo - INFO - test-SPY - handle - end tickers=1
{'name': 'test-SPY', 'created': '2018-11-08 08:36:56', 'updated': '2018-11-08 08:36:56', 'open_positions': {'SPY': {'shares': 3, 'buys': [{'ticker': 'SPY', 'status': 11, 'balance': 184.0, 'shares': 3, 'buy_price': 816.0, 'prev_balance': 1000.0, 'prev_shares': 0, 'close': 270.0, 'details': {'name': 'ticker=SPY 0.0 0/1', 'close': 270.0, 'date': '2018-11-02'}, 'reason': 'testing', 'date': '2018-11-02', 'created': '2018-11-08 08:36:56', 's3_bucket': 'algobuys', 's3_key': 'SPY_2018-11-02', 'redis_key': 'SPY_2018-11-02', 's3_enabled': True, 'redis_enabled': True, 'version': 1}], 'sells': [{'ticker': 'SPY', 'status': 11, 'balance': 1000.0, 'shares': 0, 'sell_price': 816.0, 'prev_balance': 184.0, 'prev_shares': 3, 'close': 270.0, 'details': {'name': 'ticker=SPY 0.0 0/1', 'close': 270.0, 'date': '2018-11-02'}, 'reason': 'testing', 'date': '2018-11-02', 'created': '2018-11-08 08:36:56', 's3_bucket': 'algosells', 's3_key': 'SPY_2018-11-02', 'redis_key': 'SPY_2018-11-02', 's3_enabled': True, 'redis_enabled': True, 'version': 1}]}}, 'buys': [{'ticker': 'SPY', 'status': 11, 'balance': 184.0, 'shares': 3, 'buy_price': 816.0, 'prev_balance': 1000.0, 'prev_shares': 0, 'close': 270.0, 'details': {'name': 'ticker=SPY 0.0 0/1', 'close': 270.0, 'date': '2018-11-02'}, 'reason': 'testing', 'date': '2018-11-02', 'created': '2018-11-08 08:36:56', 's3_bucket': 'algobuys', 's3_key': 'SPY_2018-11-02', 'redis_key': 'SPY_2018-11-02', 's3_enabled': True, 'redis_enabled': True, 'version': 1}], 'sells': [{'ticker': 'SPY', 'status': 11, 'balance': 1000.0, 'shares': 0, 'sell_price': 816.0, 'prev_balance': 184.0, 'prev_shares': 3, 'close': 270.0, 'details': {'name': 'ticker=SPY 0.0 0/1', 'close': 270.0, 'date': '2018-11-02'}, 'reason': 'testing', 'date': '2018-11-02', 'created': '2018-11-08 08:36:56', 's3_bucket': 'algosells', 's3_key': 'SPY_2018-11-02', 'redis_key': 'SPY_2018-11-02', 's3_enabled': True, 'redis_enabled': True, 'version': 1}], 'balance': 1000.0, 'commission': 6.0}
@virdesai

This comment has been minimized.

Copy link

commented Nov 8, 2018

nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.