Skip to content

Instantly share code, notes, and snippets.

@troyscott
Created March 13, 2018 16:48
Show Gist options
  • Save troyscott/9d1ba51ca0fc2842b922732cc059dfa7 to your computer and use it in GitHub Desktop.
Save troyscott/9d1ba51ca0fc2842b922732cc059dfa7 to your computer and use it in GitHub Desktop.
Calculate simple moving average using talib and pandas.
import datetime
import time
import config
import json
from pymongo import MongoClient
from cryptolib import utils
# technical analysis libraries
import talib as ta
import pandas as pd
import numpy as np
client = MongoClient()
db = client.cryptodisco
prices = db.crypto_prices
cs = db.crypto_candlesticks
print('crypto scripts ...')
cursor = cs.aggregate([
{'$match': {'ticker': 'LTC'}},
{'$sort': {'utc_timestamp': -1, 'open_time': 1}},
{'$limit': 50}, {'$project': {'_id': 0, 'open': 1, 'close': 1, 'high': 1,
'low': 1, 'volume': 1, 'ticker': 1, 'batch_id': 1}}
])
# Prepare data for numpy and talib
d = pd.DataFrame(list(cursor))
close_float = [float(x) for x in d.close]
open_float = [float(x) for x in d.open]
close_price = np.array(close_float)
open_price = np.array(open_float)
volume = np.array(d.volume)
print("SMA 5")
out = ta.SMA(close_price, timeperiod=5)
print(out[-1])
print("SMA 8")
out = ta.SMA(close_price, timeperiod=8)
print(out[-1])
print("SMA 13")
out = ta.SMA(close_price, timeperiod=13)
print(out[-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment