Profit calculator


In [3]:
# Direct Python to plot all figures inline (i.e., not in a separate window)
%matplotlib inline

# Load libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats

# Division of two integers in Python 2.7 does not return a floating point result. The default is to round down 
# to the nearest integer. The following piece of code changes the default.
from __future__ import division

In [94]:
def profit_calculator(data, delta_t = 30):
    """Calculate the profit of trading strategy based on precisely the prediction of the model
        Parameters
        ----------
        data : a data frame with "predicted" "P_1_bid" "P_1_ask"
        delta_t : time gap between 

        Returns
        -------
        profit        : a numeric, the net profit at the end
        profit_series : a np.array, time series tracking net profit at each point of time
        
        """    
    data_effective = data.loc[np.arange(len(data)) % delta_t == 0]
    bid = data_effective['P_1_bid']
    ask = data_effective['P_1_ask']
    trade_decision = data_effective["predicted"][:-1]
    buy_profit = np.array(bid[1:]) - np.array(ask[:-1])
    profit = sum(trade_decision * buy_profit)
    return profit

In [95]:
##### test #####

d = {'predicted' : pd.Series([0, 1, 1, -1, 0, 1, 1, -1]),
     'P_1_bid' : pd.Series([1., 2., 3., 4., 1., 2., 3., 4.]),
    'P_1_ask' : pd.Series([1., 2., 3., 4., 1., 2., 3., 4.])}

df = pd.DataFrame(d)
profit_calculator(data = df, delta_t = 1)


Out[95]:
7.0