Beginning to back-test



import datetime
import pandas as pd
import pandas.io.data
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
from matplotlib import style
import numpy as np
import time
import math
import zipline as zp
from zipline.finance.slippage import FixedSlippage


style.use('ggplot')

def outlier_fixing(stock_name,ma1=100,ma2=250,ma3=500,ma4=5000):

    df = pd.read_csv('X:/stocks_sentdex_dates_short.csv', index_col='time', parse_dates=True)
    print df.head()

    df = df[df.type == stock_name.lower()]

    std = pd.rolling_std(df['close'], 25, min_periods=1)
    print std

    df['std'] = pd.rolling_std(df['close'], 25, min_periods=1)

    df = df[df['std'] < 17]
    
    MA1 = pd.rolling_mean(df['value'], ma1)
    MA2 = pd.rolling_mean(df['value'], ma2)
    MA3 = pd.rolling_mean(df['value'], ma3)
    MA4 = pd.rolling_mean(df['value'], ma4)
    
    ax1 = plt.subplot(3, 1, 1)
    df['close'].plot(label='Price')
    
    ax2 = plt.subplot(3, 1, 2, sharex = ax1)
    MA1.plot(label=(str(ma1)+'MA'))
    MA2.plot(label=(str(ma2)+'MA'))
    MA3.plot(label=(str(ma3)+'MA'))
    MA4.plot(label=(str(ma4)+'MA'))

    ax3 = plt.subplot(3, 1, 3, sharex = ax1)
    df['std'].plot(label='Deviation')

    plt.legend()
    plt.show()
    
#outlier_fixing('btcusd',ma1=100,ma2=2500,ma3=5000,ma4=50000)

def single_stock(stock_name,ma1=100,ma2=250,ma3=500,ma4=5000):

    df = pd.read_csv('X:/stocks_sentdex_dates_full.csv', index_col='time', parse_dates=True)
    print df.head()

    df = df[df.type == stock_name.lower()]

    MA1 = pd.rolling_mean(df['value'], ma1)
    MA2 = pd.rolling_mean(df['value'], ma2)
    MA3 = pd.rolling_mean(df['value'], ma3)
    MA4 = pd.rolling_mean(df['value'], ma4)
    

    ax1 = plt.subplot(2, 1, 1)
    df['close'].plot(label='Price')
    
    ax2 = plt.subplot(2, 1, 2, sharex = ax1)
    MA1.plot(label=(str(ma1)+'MA'))
    MA2.plot(label=(str(ma2)+'MA'))
    MA3.plot(label=(str(ma3)+'MA'))
    MA4.plot(label=(str(ma4)+'MA'))

    plt.legend()
    plt.show()


# new function

def calc_position(ma1, ma2, ma3, ma4):
    if ma4 > ma1 > ma2 > ma3:
        return 1
    elif ma1 > ma4 > ma2 > ma3:
        return 2
    elif ma1 > ma2 > ma4 > ma3:
        return 3
    elif ma1 > ma2 > ma3 > ma4:
        return 4
    elif ma1 < ma2 < ma3 < ma4:
        return -4
    elif ma1 < ma2 < ma4 < ma3:
        return -3
    elif ma1 < ma4 < ma2 < ma3:
        return - 2
    elif ma4 < ma1 < ma2 < ma3:
        return -1
    else:
        return None


def single_stock_auto_MA(stock_name, div1=275, div2=110, div3=55, div4=5.5):


    df = pd.read_csv('X:/stocks_sentdex_dates_full.csv', index_col='time', parse_dates=True)
    
    df = df[df.type == stock_name.lower()]
    count = df['type'].value_counts()
    count = int(count[stock_name])

    MA1 = pd.rolling_mean(df['value'], (count/div1))
    MA2 = pd.rolling_mean(df['value'], (count/div2))
    MA3 = pd.rolling_mean(df['value'], (count/div3))
    MA4 = pd.rolling_mean(df['value'], (count/div4))

    SP = int(math.ceil(count/div4))

    df['MA1'] = MA1
    df['MA2'] = MA2
    df['MA3'] = MA3
    df['MA4'] = MA4

    df = df[SP:]
    del df['MA100']
    del df['MA250']
    del df['MA500']
    del df['MA5000']

    df['Pos'] = map(calc_position, df['MA1'],df['MA2'],df['MA3'],df['MA4'])

    df['Change'] = df['Pos'].diff()

    #print df.head()

    return df





def backTest(datas, closei, changei):
    stockHoldings = 0
    startingCapital = datas['close'][0] * 8
    funds = startingCapital
    currentValuation = funds

    
    for row in datas.iterrows():
        try:
            index, data = row
            rowData = data.tolist()
            #print rowData
            price = rowData[closei]
            change = int(rowData[changei])
            #print change

            # if change is something other than 0, and NaN
            if isinstance(change, (int, long)) and change != 0:

                # if we are thinking of buying. 
                if change > 0:
                    # Can we afford to buy??
                    if (change * price) < funds:
                        #print 'We will be buying',change,'shares for',price,'each'
                        funds -= (change * price)
                        stockHoldings += change
                        currentValuation = funds + (stockHoldings * price)
                    else:
                        pass
                        #print 'Awwww...Wanted to buy, but ran out of funds!!!!!!!!!!'

                    
                # if we are thinking of selling
                elif change < 0:
                    change = abs(change)
                    #print 'thinking of selling', change,'shares'
                    # can we actually sell?
                        
                    if (stockHoldings - change) < 0:
                        change = stockHoldings

                    if stockHoldings == 0:
                        pass
                        #print 'We wanted to sell, but ran out of shares already to sell!'

                    else:
                        stockHoldings -= change
                        funds += (change * price)
                        currentValuation = funds + (stockHoldings * price)

                    #else:
                    #    print 'We wanted to sell, but ran out of shares already to sell!'

      
        except Exception, e:
            pass
            #print str(e)
            #time.sleep(5555)



    print 'Holdings:',stockHoldings
    print 'Funds:',funds
    print 'Current Valuation:',currentValuation
    print 'Strategy Percent growth:', ((currentValuation - startingCapital)/startingCapital*100)
    
stock_list = []
data = single_stock_auto_MA('c')
backTest(data, closei=3, changei=11)
    





		

The next tutorial:





  • Python and Pandas with Sentiment Analysis Database
  • Pandas Basics
  • Looking at our Data
  • Data Manipulation
  • Removing Outlier Plots
  • Basics for a Strategy
  • Dynamic Moving Averages
  • Strategy Function
  • Mapping function to dataframe
  • Beginning to back-test
  • More Analysis
  • Conclusion