def processOutputDframe(self, trades): ''' Run the methods to create the new DataFrame and fill in the data for the new trade- centric DataFrame. ''' c = self._frc # Process the output file DataFrame trades = self.addFinReqCol(trades) newTrades = trades[c.columns] newTrades.copy() nt = newTrades.sort_values([c.ticker, c.acct, c.time]) nt = self.writeShareBalance(nt) nt = self.addStartTime(nt) nt.Date = pd.to_datetime(nt.Date) nt = nt.sort_values([c.ticker, c.acct, c.start, c.date, c.time], ascending=True) nt = self.addTradeIndex(nt) nt = self.addTradePL(nt) nt = self.addTradeDuration(nt) nt = self.addTradeName(nt) # ldf is a list of DataFrames, one per trade ldf = self.getTradeList(nt) ldf, nt = self.postProcessing(ldf) nt = DataFrameUtil.addRows(nt, 2) nt = self.addSummaryPL(nt) # Get the length of the original input file before adding rows for processing Workbook # later (?move this out a level) inputlen = len(nt) dframe = DataFrameUtil.addRows(nt, 2) return inputlen, dframe, ldf
def __init__(self, df, interview, srf): ''' 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 = srf.tfcolumns.keys() TheTrade = pd.DataFrame(columns=col) TheTrade = DataFrameUtil.addRows(TheTrade, 1) self.srf = srf 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
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 insertOvernightRow(self, dframe, swTrade): ''' Insert non-transaction rows that show overnight transactions. Set Side to one of: HOLD+, HOLD-, HOLD+B, HOLD_B :params dframe: The trades dataframe. :params swTrade: A data structure holding information about tickers with unbalanced shares. ''' rc = ReqCol() newdf = DataFrameUtil.createDf(dframe, 0) for ldf in self.getListTickerDF(dframe): # print(ldf[rc.ticker].unique()[0], ldf[rc.acct].unique()[0]) for trade in swTrade: if (trade['ticker'] == ldf[rc.ticker].unique()[0] and (trade['acct'] == ldf[rc.acct].unique()[0])): # msg = "Got {0} with the balance {1}, before {2} and after {3} in {4}" # print(msg.format(trade['ticker'], trade['shares'], trade['before'], # trade['after'], trade['acct'])) # insert a non transaction HOLD row before transactions of the same ticker if trade['before'] != 0: newldf = DataFrameUtil.createDf(dframe, 1) for j, dummy in newldf.iterrows(): if j == len(newldf) - 1: newldf.at[j, rc.time] = '00:00:01' newldf.at[j, rc.ticker] = trade['ticker'] if trade['before'] > 0: newldf.at[j, rc.side] = "HOLD-B" else: newldf.at[j, rc.side] = "HOLD+B" newldf.at[j, rc.price] = float(0.0) newldf.at[j, rc.shares] = -trade['before'] # ZeroSubstance' newldf.at[j, rc.acct] = trade['acct'] newldf.at[j, rc.PL] = 0 ldf = newldf.append(ldf, ignore_index=True) break # Insert a non-transaction HOLD row after transactions from the same ticker # Reusing ldf for something different here...bad form ... maybe ... # adding columns then appending and starting over if trade['after'] != 0: # print("Are we good?") ldf = DataFrameUtil.addRows(ldf, 1) for j, dummy in ldf.iterrows(): if j == len(ldf) - 1: ldf.at[j, rc.time] = '23:59:59' ldf.at[j, rc.ticker] = trade['ticker'] if trade['after'] > 0: ldf.at[j, rc.side] = "HOLD+" else: ldf.at[j, rc.side] = "HOLD-" ldf.at[j, rc.price] = float(0.0) # -trade makes the share balance work in excel # for shares held after close ldf.at[j, rc.shares] = 0 # -trade['after'] # 'ZeroSubstance' ldf.at[j, rc.acct] = trade['acct'] ldf.at[j, rc.PL] = 0 newdf = newdf.append(ldf, ignore_index=True, sort=False) return newdf