Exemple #1
0
def RTDepth_reply_handler(msg):
    depthpricearray = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
    depthsizearray = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
    if msg.typeName == 'updateMktDepth' or msg.typeName == 'tickString' or msg.typeName == 'tickSize' or msg.typeName == 'tickPrice':
        current_time = datetime.now().time()
        timenow = current_time.isoformat()
        reqid = (((str(msg)).split()[1]).split('=')[1]).replace(',', '')
        sym = symTickerIddict[reqid]
        onerow = (str(msg)).split(',')
        onerow.append(timenow)

    if msg.typeName == 'updateMktDepth':
        ##        print msg
        ##        print onerow
        domprice = onerow[4].replace(' price=', '')
        domsize = onerow[5].replace(' size=', '').replace('>', '')
        domposition = int(onerow[1].replace(' position=', ''))
        depthpricearray[domposition] = domprice
        depthsizearray[domposition] = domsize
        print depthpricearray
        print depthsizearray
        rpu_rp.WriteArrayToCsvfileAppend(
            DataDown + today + '.' + sym + '.RTMktDepth.csv', [onerow])
    elif msg.typeName == 'tickString' or msg.typeName == 'tickSize' or msg.typeName == 'tickPrice':
        rpu_rp.WriteArrayToCsvfileAppend(
            DataDown + today + '.' + sym + '.RTtickData.csv', [onerow])
##    elif  msg.typeName == 'tickString': if msg.tickType == 48:
    else:
        print str(msg)
        current_time = datetime.now().time()
        timenow = current_time.isoformat()
        rpu_rp.WriteStringsToFileAppend(TMP + 'replys.RTtickDOMs', str(msg))
    if msg.typeName == 'tickString':
        ##        print str(msg)
        pass
def parse_signalsNEW(rulesetoutput):
    ##    [['result', 'SELL', 'stochkeasy1minSELL.rules.csv', ' 2015-09-23 08:16:05', 'CL', 46.36]]
    dur = ''
    if len(rulesetoutput) > 0:
        sigcount = 0
        for sig in rulesetoutput:
            if len(sig) > 0 and sig[1] != 'nomatch':
                sig.append(now)
                sigtime = sig[3]
                nowepoch = TicksUtile.time_to_epoch(now)
                ##                print sig
                sigepoch = TicksUtile.time_to_epoch(sigtime)
                elapsed = nowepoch - sigepoch
                ##                print elapsed
                ##                print sig,'sigline',elapsed
                sigtime = sig[3]
                indvalue = 0.0  #float(sig[len(sig)-2])   <<<<<<<<<<<<<<<<<<
                sym = sig[4]
                sigtype = sig[2]
                priceinsignal = float(sig[5])  ### need to unboost the price...
                dboost = dboostdict[sym]
                newprice = priceinsignal / float(dboost)
                sig[5] = newprice
                tside = sig[1]
                dur = '0mins'  # sig[8]   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                livesigid = sym + tside + dur + sigtype
                sig.append(livesigid)
                showflag = look_for_dupe_sig(livesigid, sigtime,
                                             delaydupetime)  #'notsupress'
                sigcount += 1
                ######                print sym,tside,sigtype,sigtime,elapsed,str(newprice)
                ##                if btmode == 'BACKTEST':
                ##                    soundarea = 'gg'
                if showflag != 'supress':
                    if tside == 'SELL':
                        ####                        beep(soundarea+'sell')
                        print sig
                        ####                        beep(soundarea+sym)
                        print '==============='
##                        Mbox('BuySignal', sig, style)
                    elif tside == 'BUY':
                        ####                        beep(soundarea+'buy')
                        print sig
                        ####                        beep(soundarea+sym)
                        print '==============='
##                        Mbox('BuySignal', sig, style)
                    else:
                        print 'supressing'
    ######                RP_Snapshot.snapshot_sym(sym,date)
                    frsigline = []
                    rpu_rp.WriteArrayToCsvfileAppend(
                        sigarea + date + '.recentsigs.csv', [sig])
                    rpu_rp.WriteArrayToCsvfileAppend(
                        sigarea + date + '.recentsigsexec.csv', [sig])
Exemple #3
0
def reply_handler(msg):
    if msg.typeName == 'realtimeBar':
        msgarray = (str(msg)).split()
        ##    print msgarray, 'rawmessg'
        reqid = (msgarray[1]).split('=')[1].replace(',', '')
        ##    fieldnum = (msgarray[2]).split('=')[1].replace(',','')
        sym = symTickerIddict[reqid]
        onerow = (str(msg)).split(',')
        ##    print onerow,'onerow split by comma'
        timenow = datetime.now().time().isoformat()
##        print msg.typeName, sym, timenow
    if msg.typeName == 'realtimeBar':
        cleanonerow = TicksUtile.clean_RTTick5secBars(onerow, sym)
        rpu_rp.WriteArrayToCsvfileAppend(
            DataDown + today + '.' + sym + '.RTticks.csv', [cleanonerow])
        rpu_rp.WriteArrayToCsvfile(
            DataDown + today + '.' + sym + '.RTtickslastquote.csv',
            [cleanonerow])
########    elif msg.typeName == 'tickString' or msg.typeName == 'tickSize' or msg.typeName == 'tickPrice'  :
########        onerow.append(timenow)
########        rpu_rp.WriteArrayToCsvfileAppend(DataDown +today+'.'+sym+ '.RTtickData.csv',[onerow])
########    elif msg.typeName == 'updateMktDepth':
########        onerow.append(timenow)
########        rpu_rp.WriteArrayToCsvfileAppend(DataDown +today+'.'+sym+ '.RTMktDepth.csv',[onerow])
    else:
        print str(msg)
        rpu_rp.WriteStringsToFileAppend(TMP + 'replys.RTticks', str(msg))
Exemple #4
0
def process_ticks(lines, sym, dur):
    bs = strip1float(lines, 5, sym)  ##raw close price
    bshighs = strip1float(lines, 3, sym)
    bslows = strip1float(lines, 4, sym)
    timestamparray = strip1string(lines, 1)
    symarray = strip1string(lines, 0)
    sym = symarray[1]
    durarray = []
    for b in symarray:
        durarray.append(dur)
### create pivots rs and ss ###
    piv = pivotpoint(bs, bshighs, bslows)
    ##    R1 = R1(piv,bshighs)
    ########################################
    signbs = show_sign(bs, 'price')
    slopebs = show_slope(bs, 'price')
    ##### MA Cross ##
    macrossval = difftwoarrays(EMAmvavgToArray(bs, 9), EMAmvavgToArray(bs, 21))
    signmcd = show_sign(macrossval, 'mcd')
    crossesmcd = show_crossover(signmcd, 'mcd')
    slopemcd = show_slope(macrossval, 'mcd')
    MDarray = makearrayJust2(timestamparray, symarray, durarray, bs,
                             macrossval, crossesmcd, signmcd)
    ma = rpu_rp.grep_array_to_array(MDarray, 'cross')
    rpu_rp.WriteArrayToCsvfile(
        sigarea + today + '.' + sym + '.' + dur + '.sigs.csv', ma)
    rpu_rp.WriteArrayToCsvfileAppend(sigarea + today + '.sigs.csv',
                                     [ma[len(ma) - 1]])
    return ma
Exemple #5
0
def parse_signalsNEW(rulesetoutput):
    ##    recentsigs = ['ES', 'SELL', 'testname', '1945.75', 'idstr', ' 2015-09-18 21:45:05']
    ##    [['result', 'SELL', 'stochkeasy1minSELL.rules.csv', ' 2015-09-23 08:16:05', 'CL', 46.36]]
    dur = ''
    if len(rulesetoutput) > 0:
        sigcount = 0
        for sig in rulesetoutput:
            if len(sig) > 0 and sig[1] != 'nomatch':
                sig.append(now)
                ##                print sig,'sigline'
                sigtime = sig[3]
                indvalue = 0.0  #float(sig[len(sig)-2])   <<<<<<<<<<<<<<<<<<
                sym = sig[4]
                sigtype = sig[2]
                priceinsignal = float(sig[5])  ### need to unboost the price...
                dboost = dboostdict[sym]
                newprice = priceinsignal / float(dboost)
                sig[5] = newprice
                tside = sig[1]
                dur = '0mins'  # sig[8]   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                livesigid = sym + tside + dur + sigtype
                sig.append(livesigid)
                showflag = look_for_dupe_sig(livesigid, sigtime)  #'notsupress'
                sigcount += 1
                if showflag != 'supress':
                    if tside == 'SELL':
                        beep(soundarea + 'sell')
                        ##                        print sig
                        beep(soundarea + sym)
                        ##                    beep(soundarea+dur)
                        print '==============='
                    elif tside == 'BUY':
                        beep(soundarea + 'buy')
                        print sig
                        beep(soundarea + sym)
                        ##                    beep(soundarea+dur)
                        print '==============='
                    else:
                        print 'supressing'
                        pass

    ######                RP_Snapshot.snapshot_sym(sym,today)
                    frsigline = []
                    rpu_rp.WriteArrayToCsvfileAppend(
                        sigarea + today + '.recentsigs.csv', [sig])
                    rpu_rp.WriteArrayToCsvfileAppend(
                        sigarea + today + '.recentsigsexec.csv', [sig])
Exemple #6
0
def parse_signals(recentsigs):
    dur = ''
    if len(recentsigs) > 0:
        sigcount = 0
        for sig in sorted(recentsigs):
            sigtime = sig[0]
            indvalue = float(sig[len(sig) - 2])
            sym = sig[6]
            sigtype = sig[5]
            ##            barToPrev=sig[len(sig)-2]
            ##            barToNow = sig[len(sig)-3]
            priceinsignal = float(sig[len(sig) - 1])
            action = sig[7]
            dur = sig[8]
            livesigid = sym + action + dur.replace(' ', '') + sigtype
            showflag = look_for_dupe_sig(livesigid, sigtime)  #'notsupress'
            sigcount += 1
            if showflag != 'supress':
                tside = 'BUY'
                if 'negcrxx' in action:
                    tside = 'SELL'
                if tside == 'SELL':
                    beep(soundarea + 'sellStocks')
                else:
                    beep(soundarea + 'buyStocks')
                beep(soundarea + sym)
                beep(soundarea + dur)
                ######                RP_Snapshot.snapshot_sym(sym,today)
                print '==============='
                print sym, tside, sigtype, dur, sigtime, now, priceinsignal, indvalue
                ########                rpInd.create_lines(sym,bid)
                ##                print symNEWSdict[sym]
                frsigline = []
                itemlist = [
                    sym, tside, priceinsignal, livesigid, sigtime, now,
                    sigtype, indvalue
                ]
                for i in itemlist:
                    frsigline.append(i)
                rpu_rp.WriteArrayToCsvfileAppend(
                    sigarea + today + '.recentsigs.csv', [frsigline])
                rpu_rp.WriteArrayToCsvfileAppend(
                    sigarea + today + '.recentsigsexec.csv', [frsigline])
def parse_signalsNEW(rulesetoutput,btmode,date):
    dur = ''
    if len(rulesetoutput) > 0:
        sigcount =0
        for sig in rulesetoutput:
            if len(sig) > 0  and sig[1] != 'nomatch':
##                sig = sym,buysell,rulename,sigtime,(sigrealprice)(curprice)(sval),livesigid,boostprice
                sig.append(now)
                priceinsignal = float(sig[4]) ### need to unboost the price...
                curprice = float(sig[5]) ### need to unboost the price...
                sym = sig[0]
                tside =sig[1]
                sigtime = sig[3]
                sigtype = sig[2]
                nowepoch  =  TicksUtile.time_to_epoch(now)
                sigepoch  =  TicksUtile.time_to_epoch(sigtime)
                elapsed = nowepoch - sigepoch
                sval = float(sig[6])  # <<<<<<<<<<<<<<<<<<
                dboost = dboostdict[sym]
                boostprice = priceinsignal / float(dboost)
                livesigid = sym+tside+sigtype
                sig.append(livesigid)
                sig.append(boostprice)
                showflag = look_for_dupe_sig(livesigid,sigtime,delaydupetime)   #'notsupress'
                sigcount+=1
                if btmode == 'BACKTEST':
                    showflag = 'supress'
                if showflag != 'supress':
                    if tside == 'SELL':
####                        beep(soundarea+'sell')
##                        print sig
                        pass
####                        beep(soundarea+sym)
                    elif tside == 'BUY':
####                        beep(soundarea+'buy')
##                        print sig
                        pass
####                        beep(soundarea+sym)
                    else:
                        print 'supressing'
                    frsigline=[]
                    rpu_rp.WriteArrayToCsvfileAppend(sigarea + date +'.recentsigs.csv', [sig])
                    rpu_rp.WriteArrayToCsvfileAppend(sigarea + date +'.recentsigsexec.csv', [sig])
Exemple #8
0
def RTBar_reply_handler(msg):
    if msg.typeName == 'realtimeBar':
        reqid=(((str(msg)).split()[1]).split('=')[1]).replace(',','')
        sym=symTickerIddict[reqid]
        onerow = (str(msg)).split(',')
        cleanonerow = TicksUtile.clean_RTTick5secBars(onerow,sym)
        rpu_rp.WriteArrayToCsvfileAppend(DataDown +today+'.'+sym+ '.RTticks.csv',[cleanonerow])
        rpu_rp.WriteArrayToCsvfile(DataDown +today+'.'+sym+ '.RTtickslastquote.csv',[cleanonerow])
    else:
        print str(msg)
        rpu_rp.WriteStringsToFileAppend(TMP +'replys.RTticks',str(msg))
def prepare_rtTICKbar(date,sym,startmode):
    durinseconds= 1
    singleslist = glob.glob(DataDown+ date + '.' + sym  +'.rtTICKbar.*')
    newsingles =[]
    #. .. this creates the recent file  used in merge_bar
    filesinglebars = DataDown +date+'.'+sym+'.'+'TICKrtbar'+'.recent.csv'
    for f in singleslist:
        fline = rpu_rp.CsvToLines(f)
        newsingles.append(fline[0])
        os.remove(f)       
        rpu_rp.WriteArrayToCsvfileAppend(filesinglebars,newsingles)        
def prepare_rtDOMbars(date,sym,startmode):
##    print 'preparing 5secs in tickutiles...',startmode
    durinseconds= 1
    singleslist = glob.glob(DataDown+ date + '.' + sym  +'.rtDOMbar.*')
    newsingles =[]
    #. .. this creates the recent file 5secs used in merge_bar
    filesinglebars = DataDown +date+'.'+sym+'.'+'DOMS'+'.recent.csv'
    for f in singleslist:
        fline = rpu_rp.CsvToLines(f)
        newsingles.append(fline[0])
        os.remove(f)       
        rpu_rp.WriteArrayToCsvfileAppend(filesinglebars,newsingles)        
Exemple #11
0
def parse_signalsNEW(recentsigs):
    ##    recentsigs = ['ES', 'SELL', 'testname', '1945.75', 'idstr', ' 2015-09-18 21:45:05']
    dur = ''
    if len(recentsigs) > 0:
        sigcount = 0
        for sig in sorted(recentsigs):
            sig.append(now)
            ##            print sig
            sigtime = sig[3]
            indvalue = 0.0  #float(sig[len(sig)-2])   <<<<<<<<<<<<<<<<<<
            sym = sig[4]
            sigtype = sig[2]
            priceinsignal = float(sig[5])
            tside = sig[1]
            dur = '5mins'  # sig[8]   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            livesigid = sym + tside + dur + sigtype
            sig.append(livesigid)
            showflag = look_for_dupe_sig(livesigid, sigtime)  #'notsupress'
            sigcount += 1
            if showflag != 'supress':
                if tside == 'SELL':
                    beep(soundarea + 'sell')
                    print sig
                    ##                    beep(soundarea+sym)
                    ##                    beep(soundarea+dur)
                    pass
                elif tside == 'BUY':
                    beep(soundarea + 'buy')
                    print sig
##                    beep(soundarea+sym)
##                    beep(soundarea+dur)
                else:
                    pass
######                RP_Snapshot.snapshot_sym(sym,today)
                print '==============='
                frsigline = []
                rpu_rp.WriteArrayToCsvfileAppend(
                    sigarea + today + '.recentsigs.csv', [sig])
                rpu_rp.WriteArrayToCsvfileAppend(
                    sigarea + today + '.recentsigsexec.csv', [sig])
Exemple #12
0
def RTDepth_reply_handler(msg):
    if msg.typeName == 'tickString' or msg.typeName == 'tickSize' or msg.typeName == 'tickPrice':
        ##        current_time = datetime.now().time()
        ##        timenow=current_time.isoformat()
        timenow = datetime.now().time().isoformat()
        reqid = (((str(msg)).split()[1]).split('=')[1]).replace(',', '')
        sym = symTickerIddict[reqid]
        onerow = (str(msg)).split(',')
        onerow.append(timenow)
        rpu_rp.WriteArrayToCsvfileAppend(
            DataDown + today + '.' + sym + '.RTtickData.csv', [onerow])
    elif msg.typeName == 'updateMktDepth':
        ##        current_time = datetime.now().time()
        timenow = datetime.now().time().isoformat()
        reqid = (((str(msg)).split()[1]).split('=')[1]).replace(',', '')
        sym = symTickerIddict[reqid]
        onerow = (str(msg)).split(',')
        onerow.append(timenow)
        rpu_rp.WriteArrayToCsvfileAppend(
            DataDown + today + '.' + sym + '.RTMktDepth.csv', [onerow])
    else:
        print str(msg)
        rpu_rp.WriteStringsToFileAppend(TMP + 'replys.RTtickDOMs', str(msg))
Exemple #13
0
def send_prof_orders():
    ## enter proforders if any
    profsneeded = rpu_rp.CsvToLines(filledNoProfFile)
    profsplaced = []
    profitticks = 3
    stopticks = 14
    profmult = 1
    ##    print profsneeded
    ##    print 'those were all the profs needed'
    for entryorder in profsneeded:
        ##        print entryorder, 'entryorder'
        flipsign = int(1)
        tside = 'BUY'
        if entryorder[1] == 'BUY':
            tside = 'SELL'
            flipsign = int(-1)
        tsize = int(entryorder[2])
        sym = entryorder[3]
        profitticks = int(profticksdict[sym])
        print 'original signal order = ', entryorder
        orderprice = float(entryorder[4])
        decimalboost = float(dboostdict[sym])
        onetick = float(tickdict[sym])
        addamt = onetick * int(entrywiderdict[sym])

        tranflag = False
        profprice = orderprice - (profmult * profitticks * onetick *
                                  flipsign) - (flipsign * addamt)
        stopprice = orderprice + (stopticks * onetick * flipsign) + (flipsign *
                                                                     addamt)
        ##        print profprice,stopprice
        profitorder = ibutiles.create_order('LMT', tsize, tside, profprice,
                                            tranflag, profprice, profprice,
                                            'profit')
        stoporder = ibutiles.create_order('STP', tsize, tside, stopprice,
                                          tranflag, stopprice, stopprice,
                                          'stop')
        order_id = get_orderid()

        symcontract = ibutiles.create_ticksym(
            23, sym)  ## might need to vary this number at some point
        tws_conn.placeOrder(order_id, symcontract, profitorder)
        tws_conn.placeOrder(order_id + 1, symcontract, stoporder)
        print 'placed a profit and stop order here '
        print sym, tside, str(tsize), str(profprice), 'PROFITORDER', order_id
        profsplaced.append(entryorder)
        sleep(2)
        rpu_rp.WriteArrayToCsvfileAppend(filledNProfSentFile, profsplaced)
    rpu_rp.WriteArrayToCsvfile(filledNoProfFile, [])
Exemple #14
0
def parse_tickSnapshot(snapshotmsgline):
    if msg.field == 1:
        quotearray[1] = msg.price
    if bidstringsize in str(msg):
        quotearray[2] = msg.size
    if askstring in str(msg):
        quotearray[3] = msg.price
    if askstringsize in str(msg):
        quotearray[4] = msg.size
        sym = symlist[msg.tickerId - 1]
        quotearray[5] = sym
        quotearray[6] = today
    if msg.typeName == 'tickSnapshotEnd':
        quotearray[0] = datetime.now().time().isoformat()
        linearray = []
        linearray.append(quotearray)
        rpu_rp.WriteArrayToCsvfileAppend(fname, linearray)
def reply_handler(msg):
    if msg.typeName == 'realtimeBar':
        reqid = (((str(msg)).split()[1]).split('=')[1]).replace(',', '')
        sym = symTickerIddict[reqid]
        onerow = (str(msg)).split(',')
        cleanonerow = TicksUtile.format_RTTickoneline_to_5secBars(onerow, sym)
        rpu_rp.WriteArrayToCsvfileAppend(
            DataDown + today + '.' + sym + '.RTticks.csv', [cleanonerow])
        rpu_rp.WriteArrayToCsvfile(
            DataDown + today + '.' + sym + '.RTtickslastquote.csv',
            [cleanonerow])
    else:
        if 'connection is OK' in str(msg):
            pass
        else:
            print str(msg)
            rpu_rp.WriteStringsToFileAppend(replyfname, str(msg))
def findDup(parentFolder):
    # Dups in format {hash:[names]}
    dups = []
    c = 0
    for dirName, subdirs, fileList in os.walk(parentFolder):
        ##        print('Scanning %s...' % dirName)
        for filename in fileList:
            dupline = []
            c += 1
            # Get the path to the file
            path = os.path.join(dirName, filename)
            # Calculate hash
            ##            file_hash = hashfile(path)
            ##            dupline.append(str(c))
            dupline.append(path)
            dups.append(dupline)
    rpu_rp.WriteArrayToCsvfileAppend('bla.csv', dups)
    return dups
def prepare_tickfilesto5secBars(date,sym,startmode):
##    print 'preparing 5secs in tickutiles...',startmode
    durinseconds= 1
    singleslist = glob.glob(DataDown+ date + '.' + sym  +'.rtimebar.*')
    newsingles =[]
    #. .. this creates the recent file 5secs used in merge_bar
    filesinglebars = DataDown +date+'.'+sym+'.'+'5secs'+'.recent.csv'
    for f in singleslist:
        fline = rpu_rp.CsvToLines(f)
        newsingles.append(fline[0])
##        print fline[0]
##        movefile = f.replace(DataDown,DataDown+'tempsingles/') ##        movearea = DataDown+'tempsingles' ##        shutil.move(f,movearea)
        os.remove(f)       
        rpu_rp.WriteArrayToCsvfileAppend(filesinglebars,newsingles)        
    outputbothFile = DataDown +date+'.'+sym+'.'+'5secs'+'.both.csv'
    file1 = outputbothFile.replace('.both.','.ddload.')
    file2 = outputbothFile.replace('.both.','.recent.')
    cutoffmintime = int(2)
    print 'merging recent file to dload in Ticksutile.prepare....mod'
    merge_bar_files(file1,file2,outputbothFile,cutoffmintime) ## this creates both file
def reply_handler(msg):
    """Handles of server replies"""
    if '<historicalData' not in str(msg):
        print "Server Response: %s, %s" % (msg.typeName, msg)
    if msg.typeName == 'tickPrice' and bidstring in str(msg):
        quotearray[1] = msg.price
    if bidstringsize in str(msg):
        quotearray[2] = msg.size
    if askstring in str(msg):
        quotearray[3] = msg.price
    if askstringsize in str(msg):
        quotearray[4] = msg.size
    if 'SnapshotEnd' in str(msg):
        quotearray[0] = datetime.now().time().isoformat()
        print quotearray
        ##        fullline= str(quotearray)
        ##        print fullline
        linearray = []
        linearray.append(quotearray)
        rpu_rp.WriteArrayToCsvfileAppend(fname, linearray)
        linearray = []
def reply_handler(msg):
    if msg.typeName == 'tickPrice' or msg.typeName == 'tickSize':
        tickpos = int((msg.tickerId) - 1)
        sym = ibsymbol_list[tickpos]
        if msg.field == 1:
            quotearray[1] = msg.price
        if bidstringsize in str(msg):
            quotearray[2] = msg.size
        if askstring in str(msg):
            quotearray[3] = msg.price
        if askstringsize in str(msg):
            quotearray[4] = msg.size
            quotearray[5] = sym
            quotearray[6] = today
    if msg.typeName == 'tickSnapshotEnd':
        quotearray[0] = datetime.now().time().isoformat()
        linearray = []
        linearray.append(quotearray)
        rpu_rp.WriteArrayToCsvfileAppend(fname, linearray)
        pass
    if msg.typeName != 'tickPricexxx' and msg.typeName != 'tickSizexxx':
        print "Server Response: %s, %s" % (msg.typeName, msg)
Exemple #20
0
def prepare_tickfilesto5secBars(today, sym, startmode):

    ##    print 'preparing 5secs in tickutiles...',startmode
    durinseconds = 1
    singleslist = glob.glob(DataDown + today + '.' + sym + '.rtimebar.*')
    newsingles = []
    filesinglebars = DataDown + today + '.' + sym + '.' + '5secs' + '.singlesb.csv'

    for f in singleslist:
        fline = rpu_rp.CsvToLines(f)
        newsingles.append(fline[0])
        ##        movefile = f.replace(DataDown,DataDown+'tempsingles/')
        ##        movearea = DataDown+'tempsingles'
        ##        shutil.move(f,movearea)
        os.remove(f)
        rpu_rp.WriteArrayToCsvfileAppend(filesinglebars, newsingles)

    basisdur = 'RTticks'
    outputdur = '5secs'
    basisFile = filesinglebars  # DataDown +today+'.'+sym+ '.'+ basisdur +'.csv'
    if startmode == 'initialize':
        basisBars = rpu_rp.CsvToLines(basisFile)
    else:
        basisBars = rpu_rp.tail_array_to_array(rpu_rp.CsvToLines(basisFile),
                                               40)

    create_bars_from_bars(basisBars, today, sym, outputdur, durinseconds,
                          'initialize')
    #. .. this creates the recent file 5secs used in merge_bar
    ####    print 'got here in preparer creator finished'

    outputbothFile = DataDown + today + '.' + sym + '.' + outputdur + '.both.csv'
    file1 = DataDown + today + '.' + sym + '.' + '5secs' + '.ddload.csv'
    file2 = DataDown + today + '.' + sym + '.' + '5secs' + '.recent.csv'
    cutoffmintime = int(2)
    print 'got here in preparer'

    merge_bar_files(file1, file2, outputbothFile,
                    cutoffmintime)  ## this creates both file
def local_reply_handler(msg):
    value = 'empty'
    fnum = 45
    if  msg.typeName == 'tickString':
        fnum = msg.tickType
        value = msg.value
    if msg.typeName == 'tickPrice':
        fnum = msg.field
        value = msg.price       
        quotearray[5] = symTickerIddict[msg.tickerId]
    if msg.typeName == 'tickSize':
        fnum = msg.field
        value =msg.size
    quotearray[int(fielddict[str(fnum)])] = value
    if msg.typeName == 'tickSnapshotEnd':
        sym = quotearray[5] 
        quotearray[6] = today
        quotearray[0] = datetime.now().time().isoformat()
        linearray = []
        linearray.append(quotearray)
        rpu_rp.WriteArrayToCsvfileAppend(DataDown +today+'.'+sym+ '.ticksnaps.csv',linearray)
    rpu_rp.WriteStringsToFileAppend(TMP +'replys.snapshots',str(msg))
Exemple #22
0
                BothBars5Sec.append(b)
            BothBars5SecClean= TicksUtile.CleanBarOverlap(BothBars5Sec)

##            rpu_rp.WriteArrayToCsvfile(DataDown +'blasigs.csv', BothBars5SecClean)

            
            lastHistBartime = TicksUtile.time_of_last_histbar(today,sym,dur,'newformat')
            TicksUtile.create_bars_from_barsAll(BothBars5SecClean,today,sym,dur,durinseconds,lastHistBartime,'normalmode')
            ## create recent here above
            
            
            filetomerge = DataDown +today+'.'+sym+'.'+barsize+'.ddload.csv' 
            lines = TicksUtile.merge_bar_files(filetomerge)
            
            ma = rpInd.process_ticks(lines,decimalboost,barsize)
            rpu_rp.WriteArrayToCsvfileAppend(sigarea +sym+'.sigs.csv', ma)     
            rpu_rp.WriteArrayToCsvfileAppend(sigarea +today+'.sigs.csv', ma)
##############################
            prevt = 0
            numsigs = len(ma)
            signum =0
            import datetime as dt  
            now = datetime.strftime(datetime.now(),time_format)
            now_dt = dt.datetime.strptime(now, time_format)
            prevbart_dt = now_dt
            for l in ma:
                sym = l[1] #.split()[0])
                sigprice = float(l[3]) #.split()[0])
                signum +=1
                if len(l[0].split()) == 2:  
                    currentbar =  l[0].split()[1]
loopmax = 400 ##<<<<<<<<<<<<<
recentlimit = 60
###############################
def createlines(fname):
    recentfile = fname.replace('.csv','.recent.csv')
    rpInd.prepare_tick_files(fname,fname.replace('.csv','both.csv'),recentfile)
    lines = rpu_rp.CsvToLines(fname.replace('.csv','both.csv'))
    return lines
###############################
global decimalboost
##########################################
import ctypes  # An included library with Python install.
def Mbox(title, text, style):
    ctypes.windll.user32.MessageBoxA(0, text, title, style)
########################
rpu_rp.WriteArrayToCsvfileAppend(sym+'.sigs.csv', ma) 

from time import sleep, strftime, localtime  
##############################
time_format = "%H:%M:%S"
import datetime as dt
from datetime import datetime
current_time = datetime.now()
current_time_dt = dt.datetime.strptime(current_time, time_format)
print current_time
prevt = 0
numsigs = len(ma)
signum =0
for symbol in symbol_list:
    for tf in barlist:
        bardur = bardict[tf]
Exemple #24
0
            for indicator in indlist:  
                indarr = rpInd.GetKupper(DurBoth,sym,indicator)
                statename = sym+'.'+dur.replace(' ','')+'.'
                statefile = statearea +statename + indicator  + '.state.csv'
                rpu_rp.WriteArrayToCsvfile(statefile, indarr)          
####################
            macdTriggers = rpInd.Trigger_MACD(DurBoth,sym,barsize)
            maCrossTriggers = rpInd.Trigger_MACross(DurBoth,sym,barsize)
            for a in maCrossTriggers:
                macdTriggers.append(a)              
            state = 'seefile'
##            sigbart = ((lline[0])[0])
##            sigbart_epoch = int(time.mktime(time.strptime(sigbart, spaceYtime_format)))
##            barage = str(round((now_epoch - sigbart_epoch)/int(durinseconds),2))
            barage = 'need barage'
            rpu_rp.WriteArrayToCsvfileAppend(sigarea +sym+'.sigs.csv', macdTriggers)
############################         
            prevt = 0
            numsigs = len(macdTriggers)
            signum =0                      
            prevbart_dt = now_dt
            prevbart_epoch = now_epoch
            for l in macdTriggers:
                bart =  l[0]
                dur = l[2]
                bart_dt = dt.datetime.strptime(bart, spaceYtime_format)
                bart_epoch = int(time.mktime(time.strptime(bart, spaceYtime_format)))         
                barToNow = now_epoch - bart_epoch
                barToPrev =  bart_epoch - prevbart_epoch
                prevbart_epoch = bart_epoch   
                if barToNow < recentlimit and dur != '1 day':
            date, startmode, symbol_list, barlist
        )  #def make_bars_no_5sec(date,startmode,symbol_list,barlist)
        make_states(date, ['ES'], barlist,
                    indlist)  #make_states('fullist',date,['ES'])
        make_states(date, symbol_list, barlist,
                    indlist)  #make_states('fullist',date,symbol_list)
        rpInd.CreateIndvalueTable(indlist_part1, ['1min', '5mins', '15mins'],
                                  ['ES', 'FDAX', 'CL'],
                                  libarea + 'indlevels.csv')
        indlistnew = ['pivot']  #indlist_lines2
        for indline in indlistnew:
            lineval = rpInd.ShowLastBarofInd('ES', '1day', indline)[1]
            print lineval, indline
            rpu_rp.WriteArrayToCsvfile(sigarea + 'ES.daily.spotlines2.csv',
                                       [['1', '']])
            rpu_rp.WriteArrayToCsvfileAppend(
                sigarea + 'ES.daily.spotlines2.csv', [[lineval, indline]])
    factor = 1
    limitlines = 5.0
    if float(float(loop) / factor) == round((loop / factor), 1):
        print 'cycle ', loop
        sym = 'ES'
        spotfile = libarea + 'spotlines.' + sym + '.csv'
        show_spots(sym, date, limitlines, spotfile)
        spotfile = sigarea + 'ES.daily.spotlines2.csv'
        show_spots(sym, date, limitlines, spotfile)
########        symd = 'FDAX'
########        limitlinesd = 10.0
########        show_spots(symd,date,limitlinesd)
#####################
    if btmode != 'BACKTEST':
        make_bars_just_5sec(date, startmode, symbol_list)
##                fheaderdefvalue = rarray[2].replace('DEFAULT',';').split(';')[1]
####            fheaderformat = rarray[2].split()[0]
##            fheaderformat = ''#rarray[0].split()[0]
##            fheader +='|'
##            fheaderarray.append(fheadname)
##            fheaderformatarray.append(fheaderformat)
##            fheadformatdict[fheadname] = fheaderformat.replace(',','')
##            fheaddefvaluedict[fheadname] = fheaderdefvalue.replace(',','')
##            fheaderdefvaluearray.append(fheaderdefvalue)
##
    if 'INSERT INTO' in str(l):  # and tablename == 'acl_roles':
        records = rline.split('|')
        numrecordsnew = len(records)
        ######        print fheaderarray
        if tablename == tablenametarget:
            rpu_rp.WriteArrayToCsvfileAppend(
                outputarea + tablename + 'dbtable.csv', fheaderarray)
        if tablename == tablenametarget:
            for record in records:
                bla.append(record.split(','))
##                print record
            rpu_rp.WriteArrayToCsvfileAppend(
                outputarea + tablename + 'dbtable.csv', bla)
        numrecords = len(records)
    if newfieldsflag == 'y' and 'r' in tablename and tablename != prevtablename:
        print tablename, numrecordsnew
    if 'VALUES' in str(l):
        contentsflag = 'y'
        fheaderflag = 'n'
    if 'ALTER TABLE' in str(l):
        contentsflag = 'n'
##    if 'Dumping data for table' in line:
Exemple #27
0
def create_bars_from_bars(bararrayin, today, sym, dur, durinseconds):
    #############
    import time
    startbartime = ddload_cutoff_epoch(
        DataDown + today + '.' + sym + '.' + dur.replace(' ', '') +
        '.ddload.csv', 'normal')
    barhi = float(0)
    barlo = float(99999999)
    tickcount = tottickcount = 0
    bararrayout = []
    timearray = []
    prevhi = prevlo = 0
    totallength = len(bararrayin)
    prevbar_time = ' 2015-06-01 00:01:01'
    prevbar_time_dt = dt.datetime.strptime(prevbar_time, spaceYtime_format)
    prevbar_time_epoch = int(
        time.mktime(time.strptime(prevbar_time, spaceYtime_format)))
    prevdurbar_epoch = 0
    ##################
    recentfile = DataDown + today + '.' + sym + '.' + dur.replace(
        ' ', '') + '.recent.csv'
    ##    flush the recentfile
    rpu_rp.WriteArrayToCsvfile(recentfile, [])
    ######################
    firsttripflag = 'notstarted'
    for line in bararrayin:
        if len(line) > 3:
            ##        print line
            bar_time = line[1]
            if bar_time == '1':
                bar_time = ' 2015-06-01 00:01:01'
            bar_time_dt = dt.datetime.strptime(bar_time, spaceYtime_format)
            bar_time_epoch = int(
                time.mktime(time.strptime(bar_time, spaceYtime_format)))
            openpr = float(line[2])
            highpr = float(line[3])
            lowpr = float(line[4])
            closepr = float(line[5])
            ############
            barTobarElapsed = bar_time_epoch - prevbar_time_epoch
            barToprevdurbarElapsed = bar_time_epoch - prevdurbar_epoch
            prevbar_time_epoch = bar_time_epoch
            ##################
            tottickcount += 1
            ##########
            testval = int(durinseconds) - barToprevdurbarElapsed
            tickcount += 1
            if highpr > barhi:
                barhi = highpr
            if lowpr < barlo:
                barlo = lowpr
            if tickcount == 1:
                baropen = openpr
                newbartime = bar_time
                #############
            setflag = 'skip'
            if int(durinseconds) > 5:
                resetamt = 5
                pass
            else:
                resetamt = 0
            if tottickcount == 1:
                setflag = 'noreset'
            elif bar_time_epoch > (startbartime + int(durinseconds) -
                                   resetamt) and firsttripflag != 'started':
                ##            print 'tripped',dur,sym,bar_time_epoch,startbartime,bar_time
                setflag = 'reset'
                firsttripflag = 'started'
            elif testval <= 0:
                setflag = 'reset'
            elif tottickcount == totallength:
                setflag = 'reset'
            else:
                setflag = 'skip'
            if setflag == 'reset' and len(line) > 3:
                tickcount = 0
                barline = []
                ##                barclose = closepr
                partbar = True
                partflag = 'full'
                if tottickcount == totallength:
                    time = ' 2019-03-03 03:03:03'
                    partbar = True
                    partflag = 'partial'
                    setflag = 'reset'
                barline.append(sym)
                barline.append(newbartime)
                barline.append(baropen)
                barline.append(barhi)
                barline.append(barlo)
                barline.append(closepr)
                barline.append(partflag)
                barline.append(barToprevdurbarElapsed)
                prevdurbar_epoch = bar_time_epoch
                if partbar == True:
                    bararrayout.append(barline)
                barhi = 0
                barlo = 99999999

    rpu_rp.WriteArrayToCsvfileAppend(recentfile, bararrayout)
    return bararrayout
def create_slicendice():
    #analyze
prevcycledelay = 2
########################
print 'got to loop'
while loop < loopmax:
    if loop == 0:
        create_statesdaily()
    if (round((loop/10),-1) - loop) == 0:
        print 'cycle ', loop
    fileage = check_for_CP_change(cpfname)
    if fileage < 50:
        command = read_vars('Setting',cpfname) 
        cycledelay  = int(read_vars('CycleTime',cpfname))
        recentlimit = int(read_vars('TimeLimitRecentSigs',cpfname))
        print 'cycle delay changed to...',cycledelay
        # change to cycledelay later      
    now = datetime.strftime(datetime.now(),spaceYtime_format)
    now_epoch = int(time.mktime(time.strptime(now, spaceYtime_format)))      
    now_dt = dt.datetime.strptime(now, spaceYtime_format)   
##    print 'ccreate sigs searching for sigs in last ',recentlimit
    ###############
    recentsigs =[]
##    symbol_list = ['ES']
    for sym in symbol_list:
##        print sym
        rpu_rp.WriteArrayToCsvfile(sigarea +sym+'.sigs.csv', []) # flush the file to keep all sigs
        TicksUtile.prepare_tickfilesto5secBars(today,sym) ## merge the 5secddload with 5sec recents > 5sec boths
        ####################################
        rpu_rp.WriteArrayToCsvfile(sigarea +sym+'.sigs.csv', [])

        for barsize in barlist :            
            timeframe = bardict[barsize]
            durinseconds = secdict[barsize]
            barsizeNtimeframe = timeframe + barsize
            dur = barsize
            TicksUtile.assemble_dur_bars(today,sym,dur,durinseconds)
            DurBoth = rpu_rp.CsvToLines( DataDown+ today + '.'+sym+'.' + dur.replace(' ','') + '.both.csv')
            indlist = ['MACROSS', 'MCDiv', 'price']
            for indicator in indlist:  
                indarr = rpInd.GetStates(DurBoth,sym,indicator)
                statename = sym+'.'+dur.replace(' ','')+'.'
                statefile = statearea +statename + indicator  + '.state.csv'
                rpu_rp.WriteArrayToCsvfile(statefile, indarr)
            indlist = ['klower', 'kmid', 'kupper']
            for indicator in indlist:  
                indarr = rpInd.GetKupper(DurBoth,sym,indicator)
                statename = sym+'.'+dur.replace(' ','')+'.'
                statefile = statearea +statename + indicator  + '.state.csv'
                rpu_rp.WriteArrayToCsvfile(statefile, indarr)          
####################
            macdTriggers = rpInd.Trigger_MACD(DurBoth,sym,barsize)
            maCrossTriggers = rpInd.Trigger_MACross(DurBoth,sym,barsize)
            for a in maCrossTriggers:
                macdTriggers.append(a)              
            state = 'seefile'
##            sigbart = ((lline[0])[0])
##            sigbart_epoch = int(time.mktime(time.strptime(sigbart, spaceYtime_format)))
##            barage = str(round((now_epoch - sigbart_epoch)/int(durinseconds),2))
            barage = 'need barage'
            rpu_rp.WriteArrayToCsvfileAppend(sigarea +sym+'.sigs.csv', macdTriggers)
############################         
            prevt = 0
            numsigs = len(macdTriggers)
            signum =0                      
            prevbart_dt = now_dt
            prevbart_epoch = now_epoch
            for l in macdTriggers:
                bart =  l[0]
                bart_dt = dt.datetime.strptime(bart, spaceYtime_format)
                bart_epoch = int(time.mktime(time.strptime(bart, spaceYtime_format)))         
                barToNow = now_epoch - bart_epoch
                barToPrev =  bart_epoch - prevbart_epoch
                prevbart_epoch = bart_epoch   
                if barToNow < recentlimit:
                    onesig = l
                    sym =onesig[1]
                    lasttick = recenttick(sym)
                    onesig.append(barToNow)
                    onesig.append(barToPrev)
                    onesig.append(lasttick)
                    recentsigs.append(onesig)
    dur = ''
    if len(recentsigs) > 0:
##        macrossstate = statefile = sigarea +statename +'.MACROSS.state.csv'
        sigcount =0
        for sig in sorted(recentsigs):
            sigtime = sig[0]
            sym = sig[1]
            sigtype = sig[5]
            barToPrev=sig[len(sig)-2]
            barToNow = sig[len(sig)-3]
            bid = sig[len(sig)-1]
            action =sig[5]
            dur = sig[2]
            livesigid = sym+action+dur.replace(' ','')
            
            showflag = look_for_dupe_sig(livesigid)
                                 
            sigcount+=1
##            if sigcount == len(recentsigs):
            if showflag != 'supress':
                indlist = ['MACROSS']#, 'R', 'S']
##                indlist = ['MACROSS', 'MCDiv', 'price', 'kupper', 'kmid', 'klower', 'pivot', 'R', 'S']
                durstatelist =['1hour', '15mins' ]
##                durstatelist =['1hour', '15mins', '3mins']
                stateinfo = ''
                for durstate in durstatelist:
                    stateinfo += durstate #+ '\n'
                    for indicator in indlist:  
                        sfile = rpu_rp.CsvToLines(statearea +sym+'.' + durstate +'.'+ indicator +'.state.csv')
                        hline = (rpu_rp.tail_array_to_array(sfile,1))[0]
                        stateinfo += str(hline[1:4])+'\n'
    ##############################            
                tside = 'BUY'
                if 'negcrs' in action:
                    tside = 'SELL'
                if tside == 'SELL':
                    beep(soundarea+'sellStocks')
                else:
                    beep(soundarea+'buyStocks')
                beep(soundarea+sym)
                priceinsignal = float(sig[3]/1)
                pricedrift = round(priceinsignal - float(bid),4)
                timedrift = barToNow
                print '==============='
                
                print sym, tside, sigtype, dur, pricedrift, sigtime, now
                create_lines(sym,bid)
##                print symNEWSdict[sym]
##                print timedrift,sym, tside,dur, priceinsignal,barToPrev,sigtime,pricedrift,now
                print stateinfo
                ######################
                ttype = 'LIM'
                limitprice = bid
                tfactor = float(0.5)
                tsize = int(max(1,(int(tsizedict[sym]) * tfactor)))
                tickvalue = float(tickvaluedict[sym])
                showdecimal = int(showdecimaldict[sym])
                addamt = tickvalue * int(entrywiderdict[sym])

                frsigline=[]                                          
                frsigline.append(sym)
                frsigline.append(showdecimal)
                frsigline.append(tside)
                frsigline.append(tsize)
                frsigline.append(ttype)
                frsigline.append(limitprice)
                frsigline.append(addamt)
                frsigline.append(livesigid)
                frsigline.append(sigtime)
                frsigline.append(tickvalue)
                frsigline.append(now)
                rpu_rp.WriteArrayToCsvfileAppend(sigarea + today +'.recentsigs.csv', [frsigline]) 
                rpu_rp.WriteArrayToCsvfileAppend(sigarea + today +'.recentsigsexec.csv', [frsigline]) 
    loop +=1
####################
    sleep(cycledelay)
print 'finished ',loopmax,' loops  by Signal Creator...dead since..',now
#############
def create_report(Sigfile,sym,barsize):
    barfile = DataDown + today + '.'+sym+ '.'+ barsize +'both.csv'
    lines = rpu_rp.CsvToLines(barfile)
    numberBars = len(lines)   
    siglines = rpu_rp.CsvToLines(Sigfile)
    numsigs = len(siglines)
    print barsize,sym,'number bars studied=',numberBars,numsigs,'=numsigs' 
text = 'poscrxx'
position = '8'  ## 4 is slope,,1 is ind val,
minval = 0
maxval = 20
side = 'BUY'
rulename = ind + dur + text + str(minval) + 'x' + str(maxval) + side
fnametag = 'mcrosssimple'
ilist = [ind, dur, test, text, position, minval, maxval, side, rulename]
outfile = fnameind + '.' + dur + '.' + side + '.' + fnametag + '.rules.csv'
lines = []
ruleline = []
for i in ilist:
    ruleline.append(i)
lines.append(ruleline)
print ruleline
rpu_rp.WriteArrayToCsvfileAppend(outfile, lines)
##########

side = 'SELL'
text = 'slopedn'
text = 'negcrxx'
minval = 80
maxval = 10100
lines = []
ruleline = []
rulename = ind + dur + text + str(minval) + 'x' + str(maxval) + side
ilist = [ind, dur, test, text, position, minval, maxval, side, rulename]
outfile = fnameind + '.' + dur + '.' + side + '.' + fnametag + '.rules.csv'
for i in ilist:
    ruleline.append(i)
lines.append(ruleline)
Exemple #30
0
def create_bars_from_bars(
        bararrayin, today, sym, dur, durinseconds,
        startmode):  #startmodes[initialize,bartobar,addonbartoboth]
    ##    print 'start creator'
    import time
    if startmode == 'bartobar':  ## no need for recent or rootfile as prev both dur bars have 5ticks in them
        startbartime = 0
##        print 'set mode here'
    if startmode == 'initialize':
        rootname = '.ddload.csv'
        rootfile = DataDown + today + '.' + sym + '.' + dur.replace(
            ' ', '') + rootname
    if startmode == 'addonebartoboth':
        rootname = '.both.csv'
        rootfile = DataDown + today + '.' + sym + '.' + dur.replace(
            ' ', '') + rootname
##    print 'getting startbar'
    startbartime = last_bar_epoch(
        rootfile, 'rounded')  ####ddload_cutoff_epoch(rootfile,'normal')
    ########    print startbartime
    bar_percentage_required = 0.10  ###<<<<<<<<<<<<<
    ########    print 'creating bars startmode,',startmode,startbartime
    barhi = float(0)
    barlo = float(99999999)
    tickcount = tottickcount = 0
    bararrayout = []
    timearray = []
    prevhi = prevlo = 0
    totallength = len(bararrayin)
    prevbar_time = ' 2015-06-01 00:00:00'
    ##    if dur == '5secs':
    ##        prevbar_time = ' 2015-11-09 10:03:55'
    prevbar_time_epoch = convertTime(prevbar_time, 'dashspace', 'timetoepoch')
    print prevbar_time_epoch, prevbar_time
    prevdurbar_epoch = 0
    ##################
    ##    if filemode == 'test':
    ##        recentfile = DataDown +today+'.'+sym+ '.' +dur.replace(' ','')+  '.rtest.csv'
    ##    else:
    recentfile = DataDown + today + '.' + sym + '.' + dur.replace(
        ' ', '') + '.recent.csv'
    ##    flush the recentfile
    rpu_rp.WriteArrayToCsvfile(recentfile, [])
    ##    print recentfile
    ######################
    firsttripflag = 'notstarted'
    hardlimit = 4000
    if dur == '5secs':
        hardlimit = 99000000000
    barlimiter = min(4000, len(bararrayin))
    if startmode == 'addonebartoboth':
        taillines = rpu_rp.tail_array_to_array(
            bararrayin, barlimiter
        )  ##the one hour needs 720 rt msec ticks..so this might savesomething
    else:
        taillines = rpu_rp.tail_array_to_array(
            bararrayin, barlimiter
        )  ##the one hour needs 720 rt msec ticks..so this might savesomething
    totallength = len(taillines)
    lc = 1
    barvolumetot = 0
    tickval = float(tickvaluedict[sym])
    prevhaopenpr = prevhaclosepr = openpr = 1950.0

    for line in taillines:
        ##        print line
        if lc == 2:
            prevhaopenpr = float(line[2])
            prevhaclosepr = float(line[5])
        lc += 1
        tottickcount += 1
        if len(line) > 3:
            ##            print line, 'this line'
            bar_time = line[1]
            if bar_time == '1':
                bar_time = ' 2015-06-01 00:00:00'
            bar_time_epoch = convertTime(bar_time, 'dashspace', 'timetoepoch')
            openpr = float(line[2])
            highpr = float(line[3])
            lowpr = float(line[4])
            closepr = float(line[5])

            barvolume = int(line[6])
            barvolumetot += barvolume
            ############
            barTobarElapsed = bar_time_epoch - prevbar_time_epoch
            barToprevdurbarElapsed = bar_time_epoch - prevdurbar_epoch
            prevbar_time_epoch = bar_time_epoch
            ##################
            ##            if int(durinseconds) > -1:
            ##                lessthanval = -1
            ##            else:
            lessthanval = 0
            testval = int(durinseconds) - barToprevdurbarElapsed - lessthanval
            ##            print testval
            tickcount += 1
            if highpr > barhi:
                barhi = highpr
            if lowpr < barlo:
                barlo = lowpr
            if tickcount == 1:
                habaropenpr = openpr
                newbartime = bar_time
                #############
            setflag = 'skip'

            haclosepr = quarterround(
                ((habaropenpr + barhi + barlo + closepr) / 4))  #
            habaropenpr = quarterround(((prevhaopenpr + prevhaclosepr) / 2))
            habarhi = max(highpr, habaropenpr, haclosepr)
            habarlo = min(lowpr, habaropenpr, haclosepr)
            if int(
                    durinseconds
            ) == 60 and firsttripflag != 'started':  ######<<<<<<<<< this sets up the 5 second delay
                resetamt = 5
            elif int(durinseconds) > 60 and firsttripflag != 'started':
                resetamt = 60


##                print resetamt
            else:
                resetamt = 0
            partflag = 'full'
            if tottickcount == 1:
                setflag = 'noreset'
            if bar_time_epoch >= (startbartime -
                                  resetamt) and firsttripflag != 'started':
                ##                print 'tripped',dur,sym,bar_time_epoch,startbartime,bar_time
                setflag = 'reset'
                firsttripflag = 'started'
            if testval <= 0:
                setflag = 'reset'
            partbar = True
            barperc = float(
                float(barToprevdurbarElapsed) / float(durinseconds))
            if tottickcount == totallength and setflag != 'reset' and barperc > bar_percentage_required:
                ##                print round(barperc,3), barToprevdurbarElapsed, int(durinseconds)
                time = ' 2019-03-03 03:03:03'
                partflag = 'partial'
                setflag = 'reset'
                partbar = True

            if setflag == 'reset' and len(line) > 3:
                tickcount = 0
                barline = []
                bardir = 'grn'
                bartdesc = 'tail'
                bardesc = 'shortbar'
                barlen = haclosepr - habaropenpr
                if barlen < tickval:
                    bardir = 'red'

                barhead = habarhi - max(habaropenpr, haclosepr)
                bartail = habarlo - min(habaropenpr, haclosepr)
                barbigmult = 2
                if dur == '3mins':
                    barbigmult = 1

                barminbig = (tickval * barbigmult) - (tickval * 0.10)
                if abs(barlen) > barminbig:
                    bardesc = 'bigbar'
                if barhead == 0 and bardir == 'red':
                    bartdesc = 'notail'
                if bartail == 0 and bardir == 'grn':
                    bartdesc = 'notail'

                bardesc = bardir + 'N' + bartdesc + 'N' + bardesc
                ilist = [sym, newbartime,openpr,barhi,barlo,closepr,barvolumetot,partflag,barToprevdurbarElapsed,\
                         habaropenpr,habarhi,habarlo,haclosepr,barlen,barhead,bartail,bardesc]
                for item in ilist:
                    barline.append(item)
                prevdurbar_epoch = bar_time_epoch
                if partbar == True:
                    bararrayout.append(barline)
                barhi = 0
                barlo = 99999999
                habarhi = 0  #max(highpr,habaropenpr,haclosepr)
                habarlo = 9999999  #min(lowpr,habaropenpr,haclosepr)
                barvolumetot = 0
                partflag = 'full'

                prevhaopenpr = habaropenpr
                prevhaclosepr = haclosepr

    rpu_rp.WriteArrayToCsvfileAppend(recentfile, bararrayout)
    ##    print 'just wrote recent file with bars t bars in tickutiles'
    return bararrayout