Example #1
0
def qntpair(symbol, period, deltaP, deltaN, rwindow, mwindow, pairlist):
    title = ['PAIR', 'NORM', 'DWSTAT']
    maxper = period + rwindow + mwindow - 1
    table = []

    datadb = p4fns.filterdf(p4fns.read_csv(NSEEQSDBDIR + symbol + CSV), PQS,
                            'SERIES', REGEQSERIES)[-maxper:]
    pclose = [math.log(float(row[PQS['CLOSE']])) for row in datadb]
    pvwap = [math.log(float(row[PQS['VWAP']])) for row in datadb]
    dsize = len(pclose)
    if (dsize >= rwindow + mwindow + 40):
        for pair in pairlist:
            reffdb = p4fns.read_csv(NSEEQSDBDIR + pair + CSV)[-maxper:]
            pvwapR = [math.log(float(row[PQS['VWAP']])) for row in reffdb]

            regr = p4fns.rolling_regress(pvwap[-dsize:], pvwapR[-dsize:],
                                         rwindow)
            rlen = len(regr)
            error = [
                round((a / b - 1) * 100, 2)
                for a, b in zip(pclose[-rlen:], regr[-rlen:])
            ]
            mu = p4fns.rolling_smean(error, mwindow)
            sg = p4fns.rolling_sstdd(error, mwindow)
            mlen = len(sg)
            error = error[-mlen:]
            normdist = int(
                p4fns.cumnormdist((error[-1] - mu[-1]) / sg[-1]) * 100)
            et_t1 = sum([
                math.pow((error[i] - error[i - 1]), 2) for i in range(1, mlen)
            ])
            et_sq = sum([math.pow(error[i], 2) for i in range(0, mlen)])
            dwstat = round(et_t1 / et_sq, 2)
            table.append([pair, normdist, dwstat])

        p4fns.write_csv(NSEPAIRDIR + symbol + CSV, [title] + table, 'w')
        p4fns.write_json(JSONPAIRDIR + symbol + JSON, [title] + table, [])
Example #2
0
def qnttech(symbol, name):
    techtable = []
    eqsdb = p4fns.filterdf(p4fns.read_csv(NSEEQSDBDIR + symbol + CSV), PQS,
                           'SERIES', REGEQSERIES)

    ## Price Bands
    ## ============================================== ##
    price = [float(row[PQS['CLOSE']]) for row in eqsdb]
    vwap = [float(row[PQS['VWAP']]) for row in eqsdb]
    pb1m = int((price[-1] - min(price[-21:])) /
               (max(price[-21:]) - min(price[-21:])) *
               100) if len(price) > 21 else '-'
    pb3m = int((price[-1] - min(price[-63:])) /
               (max(price[-63:]) - min(price[-63:])) *
               100) if len(price) > 63 else '-'
    pb6m = int((price[-1] - min(price[-126:])) /
               (max(price[-126:]) - min(price[-126:])) *
               100) if len(price) > 126 else '-'
    pb1y = int((price[-1] - min(price[-252:])) /
               (max(price[-252:]) - min(price[-252:])) *
               100) if len(price) > 252 else '-'

    ## Bollinger Bands
    ## ============================================== ##
    dsize = len(price)
    period = [21, 63, 126, 252]
    bb = ['-'] * 4
    for i in range(0, 4):
        if (dsize > period[i] + 1):
            mu = p4fns.rolling_emean(vwap[-(period[i] + 1):], period[i])[-1]
            sg = p4fns.rolling_sstdd(vwap[-(period[i] + 1):], period[i])[-1]
            bb[i] = int(p4fns.cumnormdist((price[-1] - mu) / sg) * 100)

    techtable.append(
        [symbol, name, pb1m, pb3m, pb6m, pb1y, bb[0], bb[1], bb[2], bb[3]])
    p4fns.write_csv(NSETECHDIR + 'NSETech' + CSV, techtable, 'a')
Example #3
0
    datadb = p4fns.read_csv(NSEIXSDBDIR + symbol1 + CSV)[-days:]
    price1 = [math.log(float(row[PXS['CLOSE']])) for row in datadb]
    vwap1 = price1
    instrm1 = 'IX'
if symbol2 in cnxlist:
    datadb = p4fns.filterdf(p4fns.read_csv(NSEEQSDBDIR + symbol2 + CSV), PQS,
                            'SERIES', REGEQSERIES)[-days:]
    price2 = [math.log(float(row[PQS['CLOSE']])) for row in datadb]
    instrm2 = 'EQ'
else:
    datadb = p4fns.read_csv(NSEIXSDBDIR + symbol2 + CSV)[-days:]
    price2 = [math.log(float(row[PXS['CLOSE']])) for row in datadb]
    instrm2 = 'IX'
dsize = min(len(price1), len(price2))
mu = p4fns.rolling_smean(vwap1[-dsize:], mwindow)
sg = p4fns.rolling_sstdd(vwap1[-dsize:], mwindow)
mlen = len(sg)
timeser = [row[PQS['TIMESTAMP']] for row in datadb][-mlen:][:dur]
price1 = price1[-mlen:][:dur]
price2 = price2[-mlen:][:dur]
mu = mu[-mlen:][:dur]
sg = sg[-mlen:][:dur]
tlen = len(timeser)

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'))
startprice1 = price1[0]
startprice2 = price1[0]
Example #4
0
    mwindow = 94
    close = [float(row[PQS['CLOSE']]) for row in eqsdb]
    pvwap = [math.log(float(row[PQS['VWAP']])) for row in eqsdb]
    if (eqsize >= rwindow + mwindow + 40):
        title = ['TIMESTAMP', 'CLOSE', 'MEAN', 'SIGMA']
        reffdb = p4fns.read_csv(NSEIXSDBDIR + 'NIFTY' + CSV)[-eqsize:]
        pvwapR = [math.log(float(row[PXS['CLOSE']])) for row in reffdb]
        regr = p4fns.rolling_regress(pvwap[-eqsize:], pvwapR[-eqsize:],
                                     rwindow)
        predict = [round(math.exp(x), 2) for x in regr]
        mu = p4fns.rolling_smean(predict, mwindow)
        rlen = len(predict)
        error = [
            round((a - b), 2) for a, b in zip(close[-rlen:], predict[-rlen:])
        ]
        sg = p4fns.rolling_sstdd(error, mwindow)
        mu = mu[-dsize:]
        sg = sg[-dsize:]
        eqdata = eqsdb[-dsize:]
        table = []
        for i in range(0, dsize):
            srow = []
            srow.append(eqdata[i][PQS['TIMESTAMP']])
            srow.append(eqdata[i][PQS['CLOSE']])
            srow.append(mu[i])
            srow.append(sg[i])
            table.append(srow)

    p4fns.write_csv(NSEDIR + 'TECHNICAL/CRR/' + symbol + '_NIFTY' + CSV,
                    [title] + table, 'w')
Example #5
0
def qntgenl(symbol, name, sector, industry, mktcap, mcpercent):
    techtitle      = ['SYMBOL','PRICE','GAIN','NAME','SECTOR','INDUSTRY','MKT_CAP','MC_PERCENT',\
                      'VOLATILITY','MAX_VTY','MIN_VTY','VOLUME','MAX_VOL','MIN_VOL']
    techtable = []
    srow = []
    srow.append(symbol)
    eqsdb = p4fns.filterdf(p4fns.read_csv(NSEEQSDBDIR + symbol + CSV), PQS,
                           'SERIES', REGEQSERIES)

    ## Current Values
    ## ============================================== ##
    curprice = eqsdb[-1][PQS['CLOSE']]
    srow.append(curprice)
    change = round(float(eqsdb[-1][PQS['GAIN']]), 2)
    srow.append(change)
    srow.append(name)
    srow.append(sector)
    srow.append(industry)
    srow.append(mktcap)
    srow.append(mcpercent)

    ## Volatility
    ## ============================================== ##
    if path.isfile(NSEDVSDBDIR + symbol + CSV):
        dvsdb      = p4fns.filterdf(p4fns.filterdf(p4fns.filterdf(p4fns.read_csv(NSEDVSDBDIR+symbol+CSV),\
                                                   PDS, 'INSTRUMENT', ['OPTSTK']),\
                                                   PDS, 'TIMESTAMP', [today]),\
                                                   PDS, 'T2E', [str(x) for x in range(1,50)])
        ivlist = [float(row[PDS['IV']]) for row in dvsdb]
        wtlist = [float(row[PDS['VAL_INLAKH']]) for row in dvsdb]
        if sum(wtlist) >= 100:
            avgiv = round(p4fns.wmean(ivlist, wtlist), 2)
        else:
            avgiv = 0
    else:
        avgiv = 0
    eqdata = eqsdb[-756:]
    gain = [float(row[PQS['GAIN']]) for row in eqdata]
    cum_gain = p4fns.rolling_sum(gain, 21)
    rol_stdd = p4fns.rolling_sstdd(cum_gain, 21)
    if (avgiv == 0):
        stdd1m = round(p4fns.sstdd(cum_gain) * math.sqrt(12), 2)
        volatility = stdd1m
    else:
        volatility = avgiv
    max_stdd = max([volatility, round(max(rol_stdd) * math.sqrt(12), 2)])
    min_stdd = min([volatility, round(min(rol_stdd) * math.sqrt(12), 2)])
    srow.append(volatility)
    srow.append(max_stdd)
    srow.append(min_stdd)

    ## Volume
    ## ============================================== ##
    eqdata = eqsdb[-252:]
    turnover = [
        round(float(row[PQS['TURNOVER']]) / 10000000, 2) for row in eqdata
    ]
    volume = p4fns.rolling_emean(turnover, 3)
    max_vol = max(volume)
    min_vol = min(volume)
    srow.append(turnover[-1])
    srow.append(max_vol)
    srow.append(min_vol)

    ## Create JSON File
    ## ============================================== ##
    techtable.append(srow)
    p4fns.write_csv(NSEGENLDIR + symbol + CSV, [techtitle] + techtable, 'w')
    p4fns.write_json(JSONGENLDIR + symbol + JSON, [techtitle] + techtable,
                     TECHCOLTYP)
    genltable = []
    grow = []
    grow.append(symbol)
    grow.append(name)
    grow.append(sector)
    grow.append(industry)
    grow.append(curprice)
    grow.append(change)
    grow.append(mktcap)
    grow.append(turnover[-1])
    grow.append(volatility)
    genltable.append(grow)
    p4fns.write_csv(NSETECHDIR + 'NSEGenl' + CSV, genltable, 'a')
Example #6
0
def pltcrosregres(symbol, period, deltaP, deltaN, rwindow, mwindow):
    maxper = period + rwindow + mwindow - 1
    datadb = p4fns.filterdf(p4fns.read_csv(NSEEQSDBDIR + symbol + CSV), PQS,
                            'SERIES', REGEQSERIES)[-maxper:]
    ptimestamp = [
        date2num(datetime.strptime(row[PQS['TIMESTAMP']], '%Y-%m-%d'))
        for row in datadb
    ]
    pclose = [math.log(float(row[PQS['CLOSE']])) for row in datadb]
    pvwap = [math.log(float(row[PQS['VWAP']])) for row in datadb]
    dsize = len(ptimestamp)
    if (dsize >= rwindow + mwindow + 40):
        #        pairlist   = [row[0] for row in p4fns.read_csv(NSEPAIRDIR+symbol+CSV)]+['NIFTY']
        pairlist = ['NIFTY']
        for pair in pairlist:
            if pair in ixlist:
                reffdb = p4fns.read_csv(NSEIXSDBDIR + pair + CSV)[-maxper:]
                pvwapR = [math.log(float(row[PXS['CLOSE']])) for row in reffdb]
            else:
                reffdb = p4fns.read_csv(NSEEQSDBDIR + pair + CSV)[-maxper:]
                pvwapR = [math.log(float(row[PQS['VWAP']])) for row in reffdb]

            regr = p4fns.rolling_regress(pvwap[-dsize:], pvwapR[-dsize:],
                                         rwindow)
            rlen = len(regr)
            error = [
                round((a / b - 1) * 100, 2)
                for a, b in zip(pclose[-rlen:], regr[-rlen:])
            ]
            stimestamp = ptimestamp[-rlen:]
            mu = p4fns.rolling_smean(error, mwindow)
            sg = p4fns.rolling_sstdd(error, mwindow)
            mlen = len(sg)
            error = error[-mlen:]
            stimestamp = stimestamp[-mlen:]
            mu = mu[-mlen:]
            sg = sg[-mlen:]
            upl = [mu[i] + sg[i] * deltaP for i in range(mlen)]
            lwl = [mu[i] - sg[i] * deltaN for i in range(mlen)]

            majorl = MonthLocator()
            xformat = DateFormatter('%b')

            fig = plt.figure(figsize=(6, 3))
            gs = gridspec.GridSpec(1, 1)

            ax1 = plt.subplot(gs[0])
            plt.title(symbol, loc='left', color=textc, weight='bold')
            plt.title('StatArb [' + symbol + ' vs ' + pair + ']',
                      loc='left',
                      color=textc,
                      weight='bold',
                      size='small')
            ax1.xaxis.set_major_locator(majorl)
            ax1.xaxis.set_major_formatter(xformat)
            ax1.yaxis.tick_right()
            ax1.grid(b=True, which='major', color=gridc, linestyle=':')
            ax1.patch.set_facecolor(backc)
            ax1.spines['bottom'].set_color(labelc)
            ax1.spines['top'].set_color(backc)
            ax1.spines['right'].set_color(labelc)
            ax1.spines['left'].set_color(backc)
            ax1.tick_params(axis='x', colors=labelc)
            ax1.tick_params(axis='y', colors=labelc)
            for label in (ax1.get_xticklabels() + ax1.get_yticklabels()):
                label.set_fontsize(6)
            ax1.plot(stimestamp, error, color='deepskyblue', linewidth=1.5)
            ax1.xaxis_date()
            ax1.autoscale_view()
            ax1.set_aspect('auto')
            plt.setp(ax1.get_xticklabels(),
                     horizontalalignment='center',
                     fontsize=8)

            ax2 = plt.subplot(gs[0])
            ax2.plot(stimestamp, mu, color='royalblue', linewidth=1.5)

            ax3 = plt.subplot(gs[0])
            ax3.plot(stimestamp, upl, color='yellowgreen')

            ax4 = plt.subplot(gs[0])
            ax4.plot(stimestamp, lwl, color='orangered')

            plt.figtext(0.94,
                        0.94,
                        '$\copyright$ piby4.com ' + today,
                        color=sitec,
                        size='xx-small',
                        ha='right')
            gs.tight_layout(fig)
            #            plt.savefig(IMGCRRDIR+symbol+'_'+pair+'.png', facecolor=(backc))
            plt.savefig('aaa.png', facecolor=(backc))
            plt.close(fig)
Example #7
0
    price2 = [math.log(float(row[PQS['CLOSE']])) for row in datadb]
    vwap2 = [math.log(float(row[PQS['VWAP']])) for row in datadb]
else:
    datadb = p4fns.read_csv(NSEIXSDBDIR + symbol2 + CSV)[-days:]
    close2 = [float(row[PXS['CLOSE']]) for row in datadb]
    price2 = [math.log(float(row[PXS['CLOSE']])) for row in datadb]
    vwap2 = price2
dsize = min(len(price1), len(price2))
regrP = p4fns.rolling_regress(vwap1[-dsize:], vwap2[-dsize:], rwindow)
rlen = len(regrP)
errorP = [
    round((a / b - 1) * 100, 2) for a, b in zip(price1[-rlen:], regrP[-rlen:])
]
timeser = [row[PQS['TIMESTAMP']] for row in datadb][-rlen:]
muP = p4fns.rolling_smean(errorP, mwindow)
sgP = p4fns.rolling_sstdd(errorP, mwindow)
mlen = len(sgP)
errorP = errorP[-mlen:][:dur]
timeser = timeser[-mlen:][:dur]
muP = muP[-mlen:][:dur]
sgP = sgP[-mlen:][:dur]
tlen = len(timeser)

close1 = close1[-mlen:][:dur]
close2 = close2[-mlen:][:dur]

if symbol1 in cnxlist:
    instrm1 = 'EQ'
else:
    instrm1 = 'IX'
if symbol2 in cnxlist: