<s> '''<STR_LIT>''' <EOL> import DataAccess as da <EOL> import tables as pt <EOL> import math <EOL> import numpy as np <EOL> class Optimizer ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , listOfStocks ) : <EOL> '''<STR_LIT>''' <EOL> self . listOfLoosingStocks = list ( ) <EOL> self . noOfDaysStockHasBeenLoosingValue = list ( ) <EOL> self . listOfStocks = list ( listOfStocks ) <EOL> self . DAY = <NUM_LIT> <EOL> staticDataItemsList = list ( ) <EOL> staticDataItemsList . append ( "<STR_LIT>" ) <EOL> dataItemsList = list ( ) <EOL> dataItemsList . append ( "<STR_LIT>" ) <EOL> self . minCom = <NUM_LIT> <EOL> self . ComPerShare = <NUM_LIT> <EOL> def strategyOne ( self , portfolio , positions , timestamp , stockInfo , dataAccess ) : <EOL> output = [ ] <EOL> adjOpenData = dataAccess . getMatrix ( self . listOfStocks , "<STR_LIT>" , timestamp - <NUM_LIT:2> * self . DAY , timestamp - self . DAY ) <EOL> if adjOpenData is not None : <EOL> ctr = <NUM_LIT:0> <EOL> currentBiggestLoss = - float ( "<STR_LIT>" ) <EOL> currentBiggestLoserIndex = - <NUM_LIT:1> <EOL> while ( ctr < len ( self . listOfStocks ) ) : <EOL> if ( adjOpenData [ <NUM_LIT:0> ] [ ctr ] > adjOpenData [ <NUM_LIT:1> ] [ ctr ] ) : <EOL> if ( ( adjOpenData [ <NUM_LIT:0> ] [ ctr ] - adjOpenData [ <NUM_LIT:1> ] [ ctr ] ) / adjOpenData [ <NUM_LIT:0> ] [ ctr ] > currentBiggestLoss ) : <EOL> currentBiggestLoss = ( adjOpenData [ <NUM_LIT:0> ] [ ctr ] - adjOpenData [ <NUM_LIT:1> ] [ ctr ] ) <EOL> currentBiggestLoserIndex = ctr <EOL> ctr += <NUM_LIT:1> <EOL> if ( currentBiggestLoserIndex != - <NUM_LIT:1> ) : <EOL> order = stockInfo . OutputOrder ( ) <EOL> order . symbol = self . listOfStocks [ currentBiggestLoserIndex ] <EOL> order . volume = <NUM_LIT:10> <EOL> order . task = '<STR_LIT>' <EOL> order . orderType = '<STR_LIT>' <EOL> order . duration = self . DAY <EOL> newOrder = order . getOutput ( ) <EOL> if newOrder != None : <EOL> output . append ( newOrder ) <EOL> else : <EOL> print "<STR_LIT>" <EOL> else : <EOL> print "<STR_LIT>" <EOL> currentPositions = positions . getPositions ( ) <EOL> for stock in portfolio . currStocks : <EOL> for pos in currentPositions : <EOL> if ( str ( pos [ '<STR_LIT>' ] ) == str ( stock ) ) : <EOL> if ( ( pos [ '<STR_LIT>' ] ) < timestamp - <NUM_LIT:2> * self . DAY ) : <EOL> temp = dataAccess . getStockDataItem ( pos [ '<STR_LIT>' ] , '<STR_LIT>' , timestamp ) <EOL> if not ( np . isnan ( temp ) ) : <EOL> if ( ( pos [ '<STR_LIT>' ] + ( pos [ '<STR_LIT>' ] * self . ComPerShare ) ) < temp ) : <EOL> order = stockInfo . OutputOrder ( ) <EOL> order . symbol = stock <EOL> order . volume = pos [ '<STR_LIT>' ] <EOL> order . task = '<STR_LIT>' <EOL> order . orderType = '<STR_LIT>' <EOL> order . closeType = '<STR_LIT>' <EOL> order . duration = self . DAY <EOL> newOrder = order . getOutput ( ) <EOL> if newOrder != None : <EOL> output . append ( newOrder ) <EOL> else : <EOL> print "<STR_LIT>" <EOL> return output <EOL> def strategyTwo ( self , portfolio , positions , timestamp , stockInfo , dataAccess ) : <EOL> output = [ ] <EOL> adjOpenData = dataAccess . getMatrixFromTS ( self . listOfStocks , "<STR_LIT>" , timestamp , - <NUM_LIT:1> ) <EOL> if ( adjOpenData is not None ) : <EOL> ctr = <NUM_LIT:0> <EOL> while ( ctr < len ( self . listOfStocks ) ) : <EOL> if ( adjOpenData [ <NUM_LIT:0> ] [ ctr ] > adjOpenData [ <NUM_LIT:1> ] [ ctr ] ) : <EOL> try : <EOL> index2 = self . listOfLoosingStocks . index ( self . listOfStocks [ ctr ] ) <EOL> self . noOfDaysStockHasBeenLoosingValue [ index2 ] += <NUM_LIT:1> <EOL> except : <EOL> self . listOfLoosingStocks . append ( self . listOfStocks [ ctr ] ) <EOL> self . noOfDaysStockHasBeenLoosingValue . append ( <NUM_LIT:1> ) <EOL> currentPositions = positions . getPositions ( ) <EOL> for pos in currentPositions : <EOL> try : <EOL> index2 = self . listOfLoosingStocks . index ( pos [ '<STR_LIT>' ] ) <EOL> if ( self . noOfDaysStockHasBeenLoosingValue [ index2 ] > <NUM_LIT:2> ) : <EOL> print str ( pos [ '<STR_LIT>' ] ) + "<STR_LIT>" + str ( self . noOfDaysStockHasBeenLoosingValue [ index2 ] ) + "<STR_LIT>" <EOL> order = stockInfo . OutputOrder ( ) <EOL> order . symbol = pos [ '<STR_LIT>' ] <EOL> order . volume = pos [ '<STR_LIT>' ] <EOL> order . task = '<STR_LIT>' <EOL> order . orderType = '<STR_LIT>' <EOL> order . closeType = '<STR_LIT>' <EOL> order . duration = self . DAY <EOL> newOrder = order . getOutput ( ) <EOL> if newOrder != None : <EOL> output . append ( newOrder ) <EOL> else : <EOL> print "<STR_LIT>" <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> try : <EOL> index1 = self . listOfLoosingStocks . index ( self . listOfStocks [ ctr ] ) <EOL> if ( self . noOfDaysStockHasBeenLoosingValue [ index1 ] > <NUM_LIT:3> ) : <EOL> order = stockInfo . OutputOrder ( ) <EOL> order . symbol = self . listOfStocks [ ctr ] <EOL> order . volume = min ( <NUM_LIT:10> * self . noOfDaysStockHasBeenLoosingValue [ index1 ] , <NUM_LIT:100> ) <EOL> order . task = '<STR_LIT>' <EOL> order . orderType = '<STR_LIT>' <EOL> order . closeType = '<STR_LIT>' <EOL> order . duration = self . DAY <EOL> newOrder = order . getOutput ( ) <EOL> if newOrder != None : <EOL> output . append ( newOrder ) <EOL> else : <EOL> print "<STR_LIT>" <EOL> self . listOfLoosingStocks . pop ( index1 ) <EOL> self . noOfDaysStockHasBeenLoosingValue . pop ( index1 ) <EOL> except ValueError : <EOL> pass <EOL> ctr += <NUM_LIT:1> <EOL> return output <EOL> def execute ( self , portfolio , positions , timestamp , stockInfo , dataAccess ) : <EOL> '''<STR_LIT>''' <EOL> output = [ ] <EOL> output = self . strategyTwo ( portfolio , positions , timestamp , stockInfo , dataAccess ) <EOL> return output </s>
