def reloadit(self): from journal.statement import Statement_DAS as Ticket from journal.statement import Statement_IBActivity from journal.pandasutil import InputDataFrame infile = self.jf.inpathfile if not os.path.exists(infile): print("There is a problem. Unable to fully save this file.") return None if self.jf.inputType == 'IB_HTML': statement = Statement_IBActivity(self.jf) df = statement.getTrades_IBActivity(self.jf.inpathfile) elif self.jf.inputType == 'DAS': tkt = Ticket(self.jf) df, self.jf = tkt.getTrades() # trades = pd.read_csv(self.jf.inpathfile) else: #Temporary print('Opening a non standard file name in DAS') tkt = Ticket(self.jf) df, self.jf = tkt.getTrades() idf = InputDataFrame() trades, success = idf.processInputFile(df, self.jf.theDate, self.jf) self.df = trades if not success: return
def setupForTheTradeObject(self, getMax=False, infile="trades.8.csv"): '''Set up the DataFrames''' jf = JournalFiles(mydevel=True, infile=infile, indir="data/", outdir="out/") tkt = Statement_DAS(jf) trades, jf = tkt.getTrades() idf = InputDataFrame() trades, success = idf.processInputFile(trades) tu = DefineTrades() (dummy_len, dummy_df, ldf) = tu.processOutputDframe(trades) srf = SumReqFields() maxlen = 0 maxindex = -1 #The index that yieds the maximumbal from ldf[i] for i in range(len(ldf)): if len(ldf[i]) > maxlen: maxlen = len(ldf[i]) maxindex = i index = randint(0, len(ldf) - 1) if getMax: index = maxindex self.atrade = ldf[index] self.tto = TheTradeObject(self.atrade, True, srf) return self.tto
def test_NewSingleTxPerTicket(self): ''' Test the method Statement_DAS.newSingleTxPerTicket. That method creates a new csv file reducing multi row transactions to a single row, averaging the prices, totaling the amounts. Explicitly tests: A newFile has been created and made the infile of JournalFiles. The PL summary is the same between the two files . The shares total for each symbol/account/buy/sell is the same ''' rc = ReqCol() for infile in self.infiles: outdir = 'out/' indir = 'data/' indir = os.path.realpath(indir) jf = JournalFiles(indir=indir, infile=infile, outdir=outdir) origdframe = pd.read_csv(jf.inpathfile) originfile = jf.infile tkt = Statement_DAS(jf) newDF, jf = tkt.getTrades() self.assertNotEqual(originfile, jf.infile) newdframe = pd.read_csv(jf.inpathfile) self.assertAlmostEqual(origdframe[rc.PL].sum(), newdframe[rc.PL].sum(), places=10) self.assertAlmostEqual(newDF[rc.PL].sum(), newdframe[rc.PL].sum(), places=10) for symbol in origdframe[rc.ticker].unique(): for accnt in origdframe[rc.acct].unique(): d = origdframe n = newDF d = d[d[rc.ticker] == symbol] d = d[d[rc.acct] == accnt] dbuy = d[d[rc.side].str.startswith('B')] dsell = d[d[rc.side].str.startswith('S')] n = n[n[rc.ticker] == symbol] n = n[n[rc.acct] == accnt] nbuy = n[n[rc.side].str.startswith('B')] nsell = n[n[rc.side].str.startswith('S')] self.assertEqual(dbuy[rc.shares].sum(), nbuy[rc.shares].sum()) self.assertEqual(dsell[rc.shares].sum(), nsell[rc.shares].sum())
def runnit(self): print('gonna runnit gonna runnit') self.initialize() if not self.indir: print('What file is supposed to load?') return jf = JournalFiles(indir=self.indir, outdir=self.outdir, theDate=self.theDate, infile=self.infile, inputType = self.inputtype, infile2=self.positions, mydevel=True) if self.inputtype == 'IB_HTML': jf.inputType = 'IB_HTML' statement = Statement_IBActivity(jf) df = statement.getTrades_IBActivity(jf.inpathfile) elif self.inputtype == 'DAS': tkt = Ticket(jf) df, jf = tkt.getTrades() # trades = pd.read_csv(jf.inpathfile) else: #Temporary print('Opening a non standard file name in DAS') tkt = Ticket(jf) df, jf = tkt.getTrades() idf = InputDataFrame() trades, success = idf.processInputFile(df, jf.theDate, jf) if not success: return tu = DefineTrades(self.inputtype) inputlen, dframe, ldf = tu.processOutputDframe(trades) # Process the openpyxl excel object using the output file DataFrame. Insert # images and Trade Summaries. margin = 25 lf = LayoutForms(self.sc, jf, dframe) tradeSummaries = lf.runSummaries(ldf)
def run(infile='trades.csv', outdir=None, theDate=None, indir=None, infile2=None, mydevel=True): ''' Run structjour. Temporary picker for input type based on filename. If infile has 'activity' in it and ends in .html, then its IB Activity Statement web page (as a file on this system) :params infile: Name of the input file. Default trades.csv--a DAS export from the trades window. If infile contains the string 'trades', input type is set to DAS. If infile contains the string 'activity', input type is set to IB Activity. Default will try DAS :params outdir: Location to write the output file. :params theDate: The date of this input file. If trades lack a Date, this date be the trade date. :params indir: Location of the input file. :parmas infile2: Name of the DAS positions file. Will default to indir/positions.csv :params mydevel: If True, use a specific file structure and let structjour create it. All can be overriden by using the specific parameters above. ''' settings = QSettings('zero_substance', 'structjour') settings.setValue('runType', 'CONSOLE') # indir=None, outdir=None, theDate=None, infile='trades.csv', mydevel=False jf = JournalFiles(indir=indir, outdir=outdir, theDate=theDate, infile=infile, infile2=infile2, mydevel=mydevel) name, ext = os.path.splitext(jf.infile.lower()) if name.find('activity') > -1 and ext == '.html': jf.inputType = 'IB_HTML' statement = Statement_IBActivity(jf) df = statement.getTrades_IBActivity(jf.inpathfile) elif name.find('trades') > -1 and ext == '.csv': # This could be an IB CSV--so this is temporary-- when I enable some sort of IB CSV, will # probably do some kind of class heirarchy here for statements. tkt = Ticket(jf) df, jf = tkt.getTrades() # trades = pd.read_csv(jf.inpathfile) else: #Temporary print('Opening a non standard file name in DAS') tkt = Ticket(jf) df, jf = tkt.getTrades() idf = InputDataFrame() trades, success = idf.processInputFile(df, jf.theDate, jf) if not success: print('Failed. Between you and me, I think its a programming error') return jf tu = DefineTrades() inputlen, dframe, ldf = tu.processOutputDframe(trades) # Process the openpyxl excel object using the output file DataFrame. Insert # images and Trade Summaries. margin = 25 # Create the space in dframe to add the summary information for each trade. # Then create the Workbook. ls = LayoutSheet(margin, inputlen) imageLocation, dframe = ls.imageData(dframe, ldf) wb, ws, nt = ls.createWorkbook(dframe) tf = TradeFormat(wb) ls.styleTop(ws, len(nt.columns), tf) assert len(ldf) == len(imageLocation) mstkAnchor = (len(dframe.columns) + 2, 1) mistake = MistakeSummary(numTrades=len(ldf), anchor=mstkAnchor) mistake.mstkSumStyle(ws, tf, mstkAnchor) mistake.dailySumStyle(ws, tf, mstkAnchor) tradeSummaries = ls.runSummaries(imageLocation, ldf, jf, ws, tf) # app = QApplication(sys.argv) # qtf = QtForm() # qtf.fillForm(tradeSummaries[1]) # app.exec_() ls.populateMistakeForm(tradeSummaries, mistake, ws, imageLocation) ls.populateDailySummaryForm(tradeSummaries, mistake, ws, mstkAnchor) ls.save(wb, jf) print("Processing complete. Saved {}".format(jf.outpathfile)) return jf