import pandas as pd
from pypm import data_io, portfolio

symbol = 'AWU'
df = data_io.load_eod_data(symbol)
shares_to_buy = 50

for i, row in enumerate(df.itertuples()):
    date = row.Index
    price = row.close

    if i == 123:
        position = portfolio.Position(symbol, date, price, shares_to_buy)
    elif 123 < i < 234:
        position.record_price_update(date, price)
    elif i == 234:
        position.exit(date, price)

position.print_position_summary()

# Returns ...
# AWU       Trade summary
# Date:     Wed Jun 30, 2010 -> Tue Dec 07, 2010 [111 days]
# Price:    $220.34 -> $305.98 [38.9%]
# Value:    $11017.0 -> $15299.0 [$4282.0]

def calculate_money_flow_volume(df: pd.DataFrame, n: int = 20) -> pd.Series:
    """
    Calculates money flow volume, or q_t in our formula
    """
    return calculate_money_flow_volume_series(df).rolling(n).sum()


def calculate_chaikin_money_flow(df: pd.DataFrame, n: int = 20) -> pd.Series:
    """
    Calculates the Chaikin money flow
    """
    return calculate_money_flow_volume(df, n) / df['volume'].rolling(n).sum()


if __name__ == '__main__':
    data = load_eod_data('AWU')
    closes = data['close']
    sma = calculate_simple_moving_average(closes, 10)
    macd = calculate_macd_oscillator(closes, 5, 50)

    bollinger_bands = calculate_bollinger_bands(closes, 100)
    bollinger_bands = bollinger_bands.assign(closes=closes)
    bollinger_bands.plot()

    cmf = calculate_chaikin_money_flow(data)
    # cmf.plot()

    import matplotlib.pyplot as plt
    plt.show()
Пример #3
0
from pypm import data_io, metrics

df = data_io.load_eod_data('AWU')
print(metrics.calculate_cagr(df['close']))