def tradebyvol(ledgerin, timestamp, instrument, symbol, optiontyp, expirydt, strikepr, volume): price = float( p4fns.findvalue(timestamp, instrument, symbol, optiontyp, expirydt, strikepr, 'CLOSE')) price = round(price, 2) ledgerdf = appendledger(ledgerin, timestamp, symbol, instrument, optiontyp, expirydt, strikepr, volume, price) return ledgerdf
def getportfolio(ledgerdf, timestamp): ledgerdf = [ row[:PLS['TURNOVER']] + row[PLS['TURNOVER'] + 1:] for row in ledgerdf[1:] ] portfoliodf = [PORTFCOL] for row in ledgerdf: sectype = [ row[PLS['SYMBOL']], row[PLS['INSTRUMENT']], row[PLS['OPTION_TYP']], row[PLS['EXPIRY_DT']], row[PLS['STRIKE_PR']] ] updated = False for prow in portfoliodf: secptype = [ prow[PPS['SYMBOL']], prow[PPS['INSTRUMENT']], prow[PPS['OPTION_TYP']], prow[PPS['EXPIRY_DT']], prow[PPS['STRIKE_PR']] ] if (secptype == sectype): updated = True if (updated == False): cum_vol = 0 for lrow in ledgerdf: secltype = [ lrow[PLS['SYMBOL']], lrow[PLS['INSTRUMENT']], lrow[PLS['OPTION_TYP']], lrow[PLS['EXPIRY_DT']], lrow[PLS['STRIKE_PR']] ] if (secltype == sectype): cum_vol = cum_vol + lrow[PLS['VOLUME']] if (cum_vol != 0): if (row[PLS['SYMBOL']] == 'CASH'): price = 1 else: price = float( p4fns.findvalue(timestamp, row[PLS['INSTRUMENT']], row[PLS['SYMBOL']], row[PLS['OPTION_TYP']], row[PLS['EXPIRY_DT']], row[PLS['STRIKE_PR']], 'CLOSE')) value = round(price * cum_vol, 2) cum_vol = round(cum_vol, 2) prow = [ row[PLS['SYMBOL']], row[PLS['INSTRUMENT']], row[PLS['OPTION_TYP']], row[PLS['EXPIRY_DT']], row[PLS['STRIKE_PR']], cum_vol, value ] portfoliodf.append(prow) return portfoliodf
def updateledger(ledgerin, timestamp, operator): ledgerdf = ledgerin ledger = p4fns.filterdf(ledgerin[1:], PLS, 'INSTRUMENT', ['OPTIDX', 'OPTSTK', 'FUTIVX', 'FUTSTK', 'FUTIDX']) ledger = p4fns.filterts(ledger, PLS, 'EXPIRY_DT', timestamp, operator) appenddf = [] for row in ledger: instrument = row[PLS['INSTRUMENT']] symbol = row[PLS['SYMBOL']] optiontyp = row[PLS['OPTION_TYP']] expirydt = row[PLS['EXPIRY_DT']] strikepr = row[PLS['STRIKE_PR']] idf = p4fns.filterdf(ledger, PLS, 'INSTRUMENT', [instrument]) idf = p4fns.filterdf(idf, PLS, 'SYMBOL', [symbol]) idf = p4fns.filterdf(idf, PLS, 'OPTION_TYP', [optiontyp]) idf = p4fns.filterdf(idf, PLS, 'EXPIRY_DT', [expirydt]) idf = p4fns.filterdf(idf, PLS, 'STRIKE_PR', [strikepr]) cumvol = 0 for irow in idf: cumvol = cumvol + irow[PLS['VOLUME']] adf = p4fns.filterdf(appenddf, PLS, 'INSTRUMENT', [instrument]) adf = p4fns.filterdf(adf, PLS, 'SYMBOL', [symbol]) adf = p4fns.filterdf(adf, PLS, 'OPTION_TYP', [optiontyp]) adf = p4fns.filterdf(adf, PLS, 'EXPIRY_DT', [expirydt]) adf = p4fns.filterdf(adf, PLS, 'STRIKE_PR', [strikepr]) appvol = 0 for prow in adf: appvol = appvol + prow[PLS['VOLUME']] if (appvol + cumvol != 0): expvalue = float( p4fns.findvalue(expirydt, instrument, symbol, optiontyp, expirydt, strikepr, 'CLOSE')) appenddf = appendledger(appenddf, expirydt, symbol, instrument, optiontyp, expirydt,\ strikepr, (-appvol-cumvol), expvalue) ledgerdf = ledgerdf + appenddf return ledgerdf
close2 = close2[-mlen:][:dur] if symbol1 in cnxlist: instrm1 = 'EQ' else: instrm1 = 'IX' if symbol2 in cnxlist: instrm2 = 'EQ' else: instrm2 = 'IX' startdate = timeser[0] enddate = timeser[-1] capital = 1000000 startprice1 = float( p4fns.findvalue(startdate, instrm1, symbol1, 'XX', 'XX', 'XX', 'CLOSE')) startprice2 = float( p4fns.findvalue(startdate, instrm2, symbol2, 'XX', 'XX', 'XX', 'CLOSE')) if (mode == 'T') or (mode == 'C') or (mode == 'D'): print startdate print enddate # Trading strategy # ============================================================== state = 'PA' own1 = False own2 = False own = [0] * tlen ledgerdf = p8fns.genledger(startdate, capital)