In [1]:
# import prices from a csv file
import pandas as pd
prices = pd.read_csv("data/price.csv", index_col=0, header=0, parse_dates=True)
In [2]:
# Connect with MongoDB (here using a mock database)
# Here you could go wild and point to an existing MongoDB Server.
from mongoengine import *
connect(db="test", host='mongomock://localhost', alias='default')
Out[2]:
In [3]:
# Symbols are arranged in groups. Hence we need a group first
from pyutil.mongo.engine.symbol import Symbol, Group
g = Group(name="US Stock").save()
# if you get a duplicate error with your MongoMock database just restart the kernel
In [4]:
# Create a bunch of symbols
for key, data in prices.items():
symbol = Symbol(name=key, group=g, internal=key, price=data.dropna()).save()
In [5]:
# Loop over all symbols to do flips and shit
for symbol in Symbol.objects:
print(symbol.name)
print(symbol.group.name)
print(symbol.internal)
print(symbol.price.tail(3))
In [6]:
# Fish for a particular symbol and create some reference data
s1 = Symbol.objects(name="A").get()
print(s1.name)
print(s1.group.name)
s1.reference["AA"] = 20.0
s1.reference["BB"] = 30.0
s1.save()
s2 = Symbol.objects(name="C").get()
print(s2.name)
print(s2.group.name)
s2.reference["BB"] = 40.0
s2.reference["CC"] = 30.0
s2.save()
Out[6]:
In [7]:
# Construct a frame with all reference data for a bunch of symbols
Symbol.reference_frame(products=[s1,s2])
Out[7]:
In [8]:
# and again but for all symbols
Symbol.reference_frame()
Out[8]:
In [9]:
# extract a frame of time series data
Symbol.pandas_frame(item="price", products=[s1,s2])
Out[9]:
In [10]:
# and again but for all symbols
Symbol.pandas_frame(item="price")
Out[10]:
In [11]:
# and again but for all symbols
Symbol.pandas_frame(item="price")
Out[11]:
In [12]:
# and again but for all symbols
Symbol.pandas_frame(item="price")
Out[12]:
In [13]:
# and again but for all symbols
Symbol.pandas_frame(item="price")
Out[13]:
In [ ]: