In [1]:
import pandas as pd
import numpy as np
from beakerx import *
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
In [2]:
# load prices from a csv file
prices=pd.read_csv("data/price.csv", index_col=0, header=0, parse_dates=True)
In [3]:
# plot them using beakerx
SimpleTimePlot(prices, prices.keys())
In [4]:
# construct a 1/n portfolio, assets that come in later will initially not have any weight
def f(x):
# how many assets are alive?
n = x.notnull().sum()
# create a new series (this seems to be slow, there must be a more elegant solution?)
y = pd.Series(index=x.index)
if n > 0:
y[x.notnull()] = 1.0/n
return y
In [16]:
p = pd.DataFrame(index =[pd.Timestamp("today").date()], columns=["A","B","C"], data=[[10.0, 20.0, np.nan]])
g = p.apply(f, axis=1)
g
Out[16]:
In [6]:
from pyutil.portfolio.portfolio import Portfolio
p = Portfolio(prices=prices, weights=prices.ffill().apply(f, axis=1))
It is possible to assign each asset to a sector via sectormaps.
In [7]:
sectormap = {"A": "S1", "B": "S1", "C": "S2", "D": "S2", "E": "S2", "F": "S2", "G": "S3"}
p.tail(5).sector(symbolmap=sectormap)
Out[7]:
In [8]:
x = p.iron_time("3M")
# the position are never really constant as the portfolio class is based on weights. Changing this would be expensive for now...
x.position
Out[8]:
In [9]:
x.cash
Out[9]:
In [10]:
# plot them using beakerx
SimpleTimePlot(x.position, x.assets)
In [ ]:
In [ ]:
In [ ]:
In [ ]: