from sysdata.sim.csv_futures_sim_data import csvFuturesSimData
import matplotlib.pyplot as plt
%matplotlib inline

"""
Explanation: Simple Trading Rule
End of explanation
"""


data = csvFuturesSimData()
data

"""
Explanation: Work up a minimum example of a trend following system
Let's get some data
We can get data from various places; however for now we're going to use
prepackaged 'legacy' data stored in csv files
End of explanation
"""


print(data.get_instrument_list())
print(data.get_raw_price("EDOLLAR").tail(5))

"""
Explanation: We get stuff out of data with methods
End of explanation
"""


data['SP500']

data.keys()

"""
Explanation: data can also behave in a dict like manner (though it's not a dict)
End of explanation
"""


data.get_instrument_raw_carry_data("EDOLLAR").tail(6)

"""
Explanation: ... however this will only access prices
(note these prices have already been backadjusted for rolls)
We have extra futures data here
End of explanation
"""


import pandas as pd
from sysquant.estimators.vol import robust_vol_calc


def calc_ewmac_forecast(price, Lfast, Lslow=None):
    """
    Calculate the ewmac trading rule forecast, given a price and EWMA speeds
    Lfast, Lslow and vol_lookback

    """
    # price: This is the stitched price series
    # We can't use the price of the contract we're trading, or the volatility
    # will be jumpy
    # And we'll miss out on the rolldown. See
    # https://qoppac.blogspot.com/2015/05/systems-building-futures-rolling.html

    price = price.resample("1B").last()

    if Lslow is None:
        Lslow = 4 * Lfast

    # We don't need to calculate the decay parameter, just use the span
    # directly
    fast_ewma = price.ewm(span=Lfast).mean()
    slow_ewma = price.ewm(span=Lslow).mean()
    raw_ewmac = fast_ewma - slow_ewma
    vol = robust_vol_calc(price.diff())
    return raw_ewmac / vol

"""
Explanation: Technical note: csvFuturesSimData inherits from FuturesData which itself inherits from simData
The chain is 'data specific' <- 'asset class specific' <- 'generic'
Let's create a simple trading rule
No capping or scaling
End of explanation
"""


instrument_code = 'EDOLLAR'
price = data.daily_prices(instrument_code)
ewmac = calc_ewmac_forecast(price, 32, 128)
ewmac.columns = ['forecast']
ewmac.tail(5)

ewmac.plot();
plt.title('Forecast')
plt.ylabel('Position')
plt.xlabel('Time')

"""
Explanation: Try it out
(this isn't properly scaled at this stage of course)
End of explanation
"""


from systems.accounts.account_forecast import pandl_for_instrument_forecast
account = pandl_for_instrument_forecast(forecast=ewmac, price = price)
account.curve().plot();
plt.title('Profit and Loss')
plt.ylabel('PnL')
plt.xlabel('Time');

account.percent.stats()

"""
Explanation: Did we make money?
End of explanation
"""


