import datetime

import matplotlib.pyplot as plt
import pandas as pd

import pinkfish as pf
import strategy

# Format price data.
pd.options.display.float_format = '{:0.2f}'.format
pd.set_option('display.max_rows', None)

%matplotlib inline

# Set size of inline plots
'''note: rcParams can't be in same cell as import matplotlib
   or %matplotlib inline
   
   %matplotlib notebook: will lead to interactive plots embedded within
   the notebook, you can zoom and resize the figure
   
   %matplotlib inline: only draw static images in the notebook
'''
plt.rcParams["figure.figsize"] = (10, 7)

"""
Explanation: Futures Trend Following Portfolio
1. The Security closes with 50/100 ma &gt; 0, buy.
2. If the Security closes 50/100 ma &lt; 0, sell your long position.

(For a Portfolio of futures.)

NOTE: pinkfish does not yet have full support for futures backtesting, and
the futures data from yahoo finance isn't very good.
End of explanation
"""


# symbol: (description, multiplier)


micro_futures = {
    'MES=F': 'Micro E-mini S&P 500 Index Futures',
    'MNQ=F': 'Micro E-mini Nasdaq-100 Index Futures',
    'M2K=F': 'Micro E-mini Russell 2000 Index Futures',
    'MYM=F': 'Micro E-mini Dow Jones Futures',
    'MGC=F': 'Micro Gold Futures',
    'SIL=F': 'Micro Silver Futures',
    'M6A=F': 'Micro AUD/USD Futures',
    'MSF=F': 'Micro CHF/USD Futures',
    'MCD=F': 'Micro CAD/USD Futures',
    'MSF=F': 'Micro CHF/USD Futures',
    'M6E=F': 'Micro EUR/USD Futures',
    'M6B=F': 'Micro GBP/USD Futures',
    'MIR=F': 'Micro INR/USD Futures'
}

symbols = list(micro_futures)
#symbols = ['MES=F']
capital = 100_000
start = datetime.datetime(1900, 1, 1)
end = datetime.datetime.now()

options = {
    'use_adj' : False,
    'use_cache' : True,
    'sell_short' : False,
    'force_stock_market_calendar' : True,
    'margin' : 2,
    'sma_timeperiod_slow': 50,
    'sma_timeperiod_fast': 10,
    'use_vola_weight' : True
}

"""
Explanation: MICRO FUTURES
End of explanation
"""


s = strategy.Strategy(symbols, capital, start, end, options=options)
s.run()

"""
Explanation: Run Strategy
End of explanation
"""


s.rlog.head()

s.tlog.head()

s.dbal.tail()

"""
Explanation: View log DataFrames: raw trade log, trade log, and daily balance
End of explanation
"""


pf.print_full(s.stats)

"""
Explanation: Generate strategy stats - display all available stats
End of explanation
"""


weights = {symbol: 1 / len(symbols) for symbol in symbols}
totals = s.portfolio.performance_per_symbol(weights=weights)
totals

corr_df = s.portfolio.correlation_map(s.ts)
corr_df

"""
Explanation: View Performance by Symbol
End of explanation
"""


benchmark = pf.Benchmark('SPY', s.capital, s.start, s.end, use_adj=True)
benchmark.run()

"""
Explanation: Run Benchmark, Retrieve benchmark logs, and Generate benchmark stats
End of explanation
"""


pf.plot_equity_curve(s.dbal, benchmark=benchmark.dbal)

"""
Explanation: Plot Equity Curves: Strategy vs Benchmark
End of explanation
"""


df = pf.plot_bar_graph(s.stats, benchmark.stats)
df

"""
Explanation: Bar Graph: Strategy vs Benchmark
End of explanation
"""


kelly = pf.kelly_criterion(s.stats, benchmark.stats)
kelly

"""
Explanation: Analysis: Kelly Criterian
End of explanation
"""


