#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
import seaborn
get_ipython().run_line_magic('matplotlib', 'inline')
from IPython.display import Image
from IPython.core.display import HTML
# In[2]:
file = 'ExperimentFullResults_noPercentage.csv'
df = pd.read_csv(file)
# # Experiment results analysis.
#
# ## Clean data and descriptive statistics.
#
# From the 1250 runs, 260 were erroneous, mainly because the portfolio value went below zero (and this creates a stackoverflow error when estimating the final statistics, I think this error is fixed in a recent patch).
# In[3]:
df[df.TotalTrades == 0].shape[0]
# In[4]:
df = df[df.TotalTrades != 0]
# In[5]:
df.describe()
# ## Sharpe ratio distribution
#
# I'll analyze the effect of the algorithm parameters making focus just in the Sharpe ratio (all the data is available [here](https://drive.google.com/file/d/0B9-kA56h5JCMT2c5UHA1Q0F3YXM/view?usp=sharing), so you can make your own analysis if you want to).
# ### Sharpe ratio density grouped by Maximum exposure
# In[6]:
Image(url="http://i.imgur.com/zcN9XSU.png")
# ### Sharpe ratio density grouped by Leverage
# In[7]:
Image(url='http://i.imgur.com/eidGdcp.png')
# ### Sharpe ratio density grouped by PairsToTrade
# In[8]:
Image(url='http://i.imgur.com/i37YEZa.png')
# ### Sharpe ratio density grouped by InitialCash
# In[9]:
Image(url='http://i.imgur.com/FefKZJS.png')
# ### Sharpe ratio density grouped by Broker
#
# Finally the main question!
# In[10]:
Image(url='http://i.imgur.com/lEPT2kU.png')
# At first sight, this plot shows a better performance of FXCM relative to Oanda.
#
# Now, let's try to figure out why FXCM has that second smaller peak.
# ### Sharpe ratio vs. Annual SD by broker, by PairsToTrade
# In[11]:
Image(url='http://i.imgur.com/bcO00pa.png')
# I used Annual SD in this projection because it helps us to separate the FXCM observations from the density second peak. The points shape are defined by PairstoTrade, and is clear that the FXCM observations to the right are mostly by runs where only the pairs with the biggest and the lowest excess returns were traded.
# ### Sharpe ratio box plot by Broker
# In[12]:
Image(url='http://i.imgur.com/idX7TqI.png')
# This plot shows:
# * The mean (the dark blue vertical line)
# * Border values for the standard deviation of the mean. The blue highlighted area is the entire standard deviation of the mean.
# * The median (yellow vertical line). The thin blue line represents the area between the first (25%) and the third (75%) quantile, while the thin dotted line represents the entire range of values (from the lowest to the highest value in the data set for the selected parameter).
#
# Here we can have a measure of the difference in Sharpe ratio because of the Broker selection. We already know that FXCM has, in average, a better performance. Here is clear that the difference is small if the means are compared; if we compare the medians, the difference is even smaller.
#
# Finally, lets make some simple statistical tests in order to know if those small differences are significance.
# In[13]:
from scipy.stats import ttest_ind, ttest_rel
fxcm = df[df['Broker']=='fxcm']
oanda = df[df['Broker']=='oanda']
# In[14]:
statistics = df.columns[:17]
for statistic in statistics:
test = ttest_ind(fxcm.get(statistic), oanda.get(statistic), equal_var=False)[1]
print(statistic)
print("\t=> Is difference significative at 95%? {1}".format(statistic, test<0.05))
print("\t=> Is difference significative at 99%? {1}".format(statistic, test<0.01))
# Well, seems FXCM has a significantly better performance than OANDA.
#
# ### Being statistically correct one can say: _**this experiment failed to show that FXCM and OANDA have the same impact in trading.**_
# ## Extras
#
# ### Take care! High exposure + high leverage = huge drawdowns
# In[15]:
Image(url='http://i.imgur.com/sSDuZhu.png')
# In[16]:
Image(url='http://i.imgur.com/Qpq2LDL.png')
# The first plot is the Drawdown box plot grouped by MaxExposure, the second is Drawdown gruped by Leverage.
#
# ### So, FXCM is cheaper, uh? Let's see how it scales.
# #### FXCM fees grouped by InitialCash.
# In[17]:
Image(url='http://i.imgur.com/FlARWUo.png')
# In[ ]: