Skip to content

Commit

Permalink
Set Data Tests Complete
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavwalia committed Mar 30, 2022
1 parent 0390cea commit 1868180
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ def euclideanDistance(a,b):
- Generates a signal according to a threshold
- Outputs a backtest according to a brokerage fee template
'''
class DistanceApproach():
class BasicDistanceApproach():

def __init__(self) -> None:
self.pairsData = None
self.tradeData = None
self.NotEnoughColumnsException = 'Dataframe is missing columns. Check that you have both securities and date columns'
self.dateTimeException = 'Left-Most Column is not of type datetime64'
self.nonNumericalException = 'Detected non-numerical data-types to the right of date column'



'''
This Function sets the data set for generating possible pairs.
Expand Down Expand Up @@ -60,10 +66,11 @@ def setTradeData(self, data: pd.DataFrame) -> bool:
'''
@staticmethod
def isDataWellFormed(data: pd.DataFrame) -> bool:

t = lambda x: is_numeric_dtype(x)
if len(data.columns) <= 1:
raise Exception('Dataframe is missing columns. Check that you have both securities and date columns')
elif data.dtypes[0] != 'datetime64':
elif data.dtypes[0] != 'datetime64[ns]':
raise Exception('Left-Most Column is not of type datetime64')
#Check if there is a non-numerical column to the right of date column
elif False in list(data.apply(t)[1:]):
Expand All @@ -72,9 +79,10 @@ def isDataWellFormed(data: pd.DataFrame) -> bool:
return True

'''
Generates Pairs
Generates Pairs:
Returns a dataframe with all the pair names and their distance values
'''
def generatePairs(self,function):
def generatePairs(self,distanceFunc):
pass


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
File renamed without changes.
38 changes: 38 additions & 0 deletions PairsTrading/DistanceApproach/TestBasicDistanceApproach.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from datetime import datetime
from turtle import distance
from BasicDistanceApproach import BasicDistanceApproach
import unittest
import pandas as pd

class TestDistanceApproach(unittest.TestCase):

'''
- Tests that the correct exceptions are raised when trying to pass flawed data to setPairsData or setTradeData
- Tests that a well formed dataframe is excepted by setPairsData and setTradeData
'''
def testSetData(self):
distanceClass = BasicDistanceApproach()
#Checks that an error exception is returned from a distance class with the appropriate messsage
def assertExceptionMessage(message: str, data):
with self.assertRaises(Exception) as cm:
distanceClass.setPairsData(data)
self.assertEqual(cm.exception.args[0],message)
with self.assertRaises(Exception) as cm:
distanceClass.setTradeData(data)
self.assertEqual(cm.exception.args[0],message)

exampleDate = pd.to_datetime(datetime(2020,4,5))
dfNotEnoughColumns = pd.DataFrame({'Dates' : ['date']})
dfFirstNotDateTime = pd.DataFrame({'Price' : [1], 'Date' : [exampleDate]})
dfNoneNumericalColumn = pd.DataFrame({'Date' : [exampleDate], 'Price1' : [1], 'Price2' : ['dog']})
wellFormedDF = pd.DataFrame({'Date': [exampleDate], 'Price1' : [1], 'Price2' : [2.4494]})

assertExceptionMessage(distanceClass.NotEnoughColumnsException,dfNotEnoughColumns)
assertExceptionMessage(distanceClass.dateTimeException,dfFirstNotDateTime)
assertExceptionMessage(distanceClass.nonNumericalException, dfNoneNumericalColumn)
self.assertEqual(distanceClass.setPairsData(wellFormedDF),True)
self.assertEqual(distanceClass.setTradeData(wellFormedDF),True)


if __name__ == '__main__':
unittest.main()
30 changes: 0 additions & 30 deletions PairsTrading/TestDistanceApproach.py

This file was deleted.

0 comments on commit 1868180

Please sign in to comment.