Rolling Metrics + Python Code for TBlox Integration

I’ve always been fascinated coming up with a good idea and testing it. But the goodness of the idea must go through enough stages of evaluation before I know that it isn’t a fluke. A lot of performance measures that gets calculated after a test in the summary page is static in nature. They only give you a number that represents a segment of time that you specified for the simulation. As traders, we don’t operate in the same way. By that I mean we don’t usually develop a system and them follow it blindly for a specified period of time in to the future and look back. Instead we take all the signals the system generates, bad or good ones, and live through all the draw-downs. Therefore it is usually useful to take apart the performance and view it over time to see ‘consistency.’  Below is a simply python code that I wrote that integrates with TBlox. It takes the monthly equity log from TBlox and generates a rolling ‘n’ month return.

The above chart was generated for a simple TB supplied trading system called “Triple Move Average System” The chart is a rolling 12 month percentage return.

Python Code:

#Author: Michael Guan / freely distribute

 import xlrd #package for reading excel file
 import matplotlib.pyplot as plt #package for plot in python

 #note that this package only takes on a .xls excel extension
 #TB's Monthly Equity log is .csv; change the extension before
 #using

 def main():
  list = []
  filename = raw_input('Please enter a file name: ')
  months = input('Please specify the rolling months (integer): ')
  totalmonths = input('Please specify total months of test (integer): ')
  metric = input('Please Enter one of the following (4-Close Equity | 5-Total Equity | 9-Total Risk): ')
  wb = xlrd.open_workbook(filename)
  sheet1 = wb.sheet_by_index(0)

  for x in range(1,totalmonths-(months-2)): #loop to loop over excel spread sheet
   initial = sheet1.cell(x,metric).value
   final = sheet1.cell(x+(months-1),metric).value
   temp = (final - initial)/initial
   result = round(temp, 6)
   list.append(result)

  plt.plot(list)
  plt.axhline(y=0,color='r')
  plt.show()

 if __name__ == '__main__':
  main()

 [/sourcecode]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s