def processDBTrades(self, trades): ''' Run the methods to create the new DataFrame and fill in the data for the new trade- centric (as opposed to transaction-centric, trades may contain multiple transactgions) DataFrame. ''' rc = self._frc # Process the output file DataFrame trades = self.addFinReqCol(trades) rccolumns = rc.columns.copy() rccolumns = self.appendCols(rccolumns) newTrades = trades[rccolumns] newTrades.copy() nt = newTrades.sort_values([rc.ticker, rc.acct, rc.date]) # nt = self.writeShareBalance(nt) nt = self.addStartTimeDB(nt) # nt.Date = pd.to_datetime(nt.Date) nt = nt.sort_values([rc.start, rc.ticker, rc.acct, rc.date, rc.time], ascending=True) nt = self.addTradeIndex(nt) nt = self.addTradePL(nt) nt = self.addTradeDurationDB(nt) nt = self.addTradeNameDB(nt) ldf, nt = self.postProcessingDB(self.getTradeList(nt)) nt = DataFrameUtil.addRows(nt, 2) nt = self.addSummaryPL(nt) dframe = DataFrameUtil.addRows(nt, 2) return dframe, ldf
def imageData(self, df, ldf, ft="png"): ''' Gather the image names and determine the locations in the Excel doc to place them. Excel has a few things at top followed by trade summaries, charts and tables for each trade. Return with the image name/location data structure. The structure can be used for the Excel DataFrame-- to navigate summary form locations and just for the names :params df: The DataFrame representing the input file plus some stuff added in processOutputFile :params ldf: A list of dataFrames. Each encapsulates a trade. :parmas ft: Image filetype extension. (NOT USED) :return (Imagelocation, df): ImageLocation contains information about the excel document locations of trade summaries and image locations. The dataFrame df is the outline used to create the workbook, ImageLocation will be used to stye it and fill in the stuff. ''' # Add rows and append each trade, leaving space for an image. Create a list of # names and row numbers to place images within the excel file (imageLocation # data structure). # Number of rows between trade summaries frq = FinReqCol() newdf = DataFrameUtil.createDf(df, self.topMargin) df = newdf.append(df, ignore_index=True) imageLocation = list() count = 0 for tdf in ldf: imageName = '{0}_{1}_{2}_{3}.{4}'.format( tdf[frq.tix].unique()[-1].replace(' ', ''), tdf[frq.name].unique()[-1].replace(' ', '-'), tdf[frq.start].unique()[-1], tdf[frq.dur].unique()[-1], ft) # Holds location, deprected name, image name, trade start time, trade duration as delta imageLocation.append([ len(tdf) + len(df) + self.spacing, tdf.Tindex.unique()[0].replace(' ', '') + '.' + ft, imageName, tdf.Start.unique()[-1], tdf.Duration.unique()[-1] ]) count = count + 1 # Append the mini trade table then add rows to fit the tradeSummary form df = df.append(tdf, ignore_index=True) df = DataFrameUtil.addRows(df, self.summarySize) return imageLocation, df
def test_dfUtil_addRow(self): '''Test method DataFrameUtil.addRows ''' cols2 = ['Its', 'the', 'end', 'of', 'the', 'world', 'as', 'we', 'know', 'it'] numRow = 9 fill = 'something silly' fill2 = 'sillier' y = DataFrameUtil.createDf(cols2, numRow, fill=fill) y = DataFrameUtil.addRows(y, numRow, fill=fill2) self.assertEqual(len(y), numRow * 2) for i in range(numRow): for ii in y.iloc[i]: self.assertEqual(ii, fill) for i in range(numRow, numRow * 2): for ii in y.iloc[i]: self.assertEqual(ii, fill2)
def __init__(self, df, interview, sf): ''' Create a dataframe that includes all the summary material for review. Some of this data comes from the program and some of it comes from the user. The user will determine which parts to fill out from a couple of options. :params:df: A DataFrame that includes the transactions, or tickets, from a singel trade. ''' self.interview = interview col = list(sf.tfcolumns.keys()) col.append('Date') TheTrade = pd.DataFrame(columns=col) TheTrade = DataFrameUtil.addRows(TheTrade, 1) self.sf = sf ix = df.index[-1] ix0 = df.index[0] # TODO This list should be retrieved from TheStrategyObject strats = [ 'ORB', 'ABCD', 'VWAP Reversal', 'Bull Flag', 'Fallen Angel', 'VWAP False Breakout', 'VWAP Reversal', '15 Minute Reversal', 'VWAP MA trend', 'Other', 'Skip' ] side = df.loc[ix0][frc.side] self.df = df self.TheTrade = TheTrade self.ix = ix self.ix0 = ix0 self.strats = strats self.side = side self.shares = 0 self.chartSlot1 = None self.chartSlot2 = None self.chartSlot3 = None self.settings = QSettings('zero_substance', 'structjour')
def layoutExcelData(self, df, ldf, imageNames): ''' 1) Determine the locations in the Excel doc to place trade summaries, trade tables and images. 2) Create the empty rows and place the trade tables in the df according to the locations. :params df: We requre the df as a whole because we are adding rows to it. :params ldf: A list of dataframes, each a trade, each one is placed into our new skeletal layout for excel :return (Imagelocation, df): ImageLocation contains [ [list of image location], # up to 3 per trade [list of image names], # up to 3 per trade Start time, trade dur, ] ''' imageLocation = list() srf = SumReqFields() sumSize = srf.maxrow() + 5 summarySize = sumSize spacing = 3 # Image column location c1col = 13 c2col = 1 c3col = 9 frq = FinReqCol() newdf = DataFrameUtil.createDf(df, self.topMargin) df = newdf.append(df, ignore_index=True) deleteme = [] for i, tdf in enumerate(ldf): theKey = tdf[frq.tix].unique()[-1] if len(theKey) == 0: deleteme.append(i) continue imageName = imageNames[theKey] xtraimage = 0 # Add space for second/third image if len(imageName) > 1: xtraimage = 21 ilocs = [] # Need 1 entry even if there are no images ilocs.append((c1col, len(tdf) + len(df) + spacing)) for i in range(0, len(imageName)): if i == 1: ilocs.append((c2col, len(tdf) + len(df) + spacing + 20)) elif i == 2: ilocs.append((c3col, len(tdf) + len(df) + spacing + 20)) # Holds image locations, image name, trade start time, trade duration as delta imageLocation.append([ ilocs, imageName, tdf.Start.unique()[-1], tdf.Duration.unique()[-1] ]) # Append the mini trade table then add rows to fit the tradeSummary form df = df.append(tdf, ignore_index=True) df = DataFrameUtil.addRows(df, summarySize + xtraimage) for d in deleteme: ldf.pop(d) return imageLocation, df