def check_for_entry_fills(profitorder, order_id): ordersfname = 'entryorderssent' lines = rpu_rp.CsvToLines(ordersfname) for l in lines: executedf = 'open' print l ## order_id = 22 filltag = 'orderId=' + str(order_id) + ', status=Filled' ## for order id, look fora fill for b in rpu_rp.TxtToLines('replys'): if 'orderStatus' in str(b) and filltag in str(b): print b executedf = 'filled' print executedf, 'placing prof ord if filled' executedf = 'filled' ##### hack for testing to force the proforder if executedf == 'filled': print 'sending prof' ## cannot use this order, it is stale, need to rebuild order based on the dictionary ##created in order creation, tag the orders in the dictionary as entry,profit,filled,open and use this ## newpoorder = ibutiles.create_order('LMT', tsize, flipside, profprice,True,profprice,profprice,'profit') ## need to add profit amount based ona dictionary created before tws_conn.placeOrder(order_id + 1, symcontract, profitorder)
def prepare_imp_file(filein,fileout): newlines =[] lines = rpu_rp.CsvToLines(filein) headerline =['Action', 'Quantity', 'Symbol', 'TimeInForce', 'SecType', 'OrderType', 'LmtPrice', 'Exchange', 'Currency', 'CUSIP', 'ISIN', ''] ## itemlist = [Action, Quantity, Symbol, TimeInForce, SecType, OrderType, LmtPrice, Exchange, Currency, CUSIP, ISIN] newlines.append(headerline) for l in lines: newline =[] print l isin = l[6] action = l[0] title = l[3] qty = l[5] price = l[2] c=0 for i in headerline: # itemlist: ## print i ## newline.append(i) newline.append(l[c]) c+=1 newlines.append(newline) rpu_rp.WriteArrayToCsvfile(fileout,newlines)
def show_spots(sym, date, limit, spotfile): curprice = float(TicksUtile.recenttick(sym, 'recent')) spotlines = rpu_rp.CsvToLines(spotfile) for l in spotlines: spotp = float(l[0]) if len(l) > 1: ## print l spotid = l[1] pass else: spotid = '' distance = abs(spotp - curprice) if (spotp - curprice) > 0: underover = 'under' else: underover = 'over' if distance < limit: if underover == 'under': sflag = 'SELL' else: sflag = 'BUY' print('>>%4s at %8.2f %s|%s |%4.2f |%8.2f > pass#? range=%d' % (sflag, spotp, spotid, sym, distance, curprice, limit))
def prepare_imp_file(filein, fileout): newlines = [] lines = rpu_rp.CsvToLines(filein) headerline = [ 'Action', 'Quantity', 'Symbol', 'TimeInForce', 'SecType', 'OrderType', 'LmtPrice', 'Exchange', 'Currency', 'CUSIP', 'ISIN', '' ] headerline = [ 'name', 'roloid', 'isin', 'sugarid', 'tradedcurr', 'assetclass', 'sector' ] newlines.append(headerline) for l in lines: newline = [] newlinebla = [] c = 0 for i in headerline: newvar = locals()[i] if newvar == 'isin': print newvar newline.append(newvar) newlines.append(newline) rpu_rp.WriteArrayToCsvfile(fileout, newlines)
def ShowIndBar(sym, dur, ind, mode, barnum, bartime): barfnumlimit = 500 lastbar = [] statefile = statearea + sym + '.' + dur + '.' + ind + '.state.csv' arrayin = rpu_rp.CsvToLines(statefile) barcount = len(arrayin) lastbar = (rpu_rp.tail_array_to_array(arrayin, 1))[0] nbar = (rpu_rp.tail_array_to_array(arrayin, barnum))[0] lastfewbars = (rpu_rp.tail_array_to_array(arrayin, barfnumlimit)) for bar in lastfewbars: if TicksUtile.time_to_epoch(bar[0]) <= TicksUtile.convertTime( bartime, 'dashspace', 'timetoepoch'): timedbar = bar if mode == 'barcount': output = barcount elif mode == 'LastBar': output = lastbar elif mode == 'nbar': output = nbar elif mode == 'bytime': output = timedbar elif mode == 'barcount': output = barcount return output
def GetStates(arrayin, sym, Indtitle, dur, threshold, date, alternatearrayin): #add barr age from last cross here ## decimalboost = dboostdict[sym] ## print arrayin if len(arrayin) > 3: pass else: print ' small array detected...probaly needs ddload!!', 'getting states for...', Indtitle, dur, sym, date, 'used yesterday' arrayin = alternatearrayin #[['ES', ' 2015-10-20 22:58:05',20.5,20.25,20.5,20.75,'full','60'],['ES', ' 2015-10-20 22:58:05',2019.5,2020.25,2019.5,2019.75,'full','60']] bsclose = strip1float(arrayin, 5, sym) ##raw close price bs = bsclose symES = 'ES' EsFile = rpu_rp.CsvToLines(DataDown + date + '.' + symES + '.' + dur.replace(' ', '') + '.both.csv') DurBothBoostedES = boost_pricearray(EsFile, symES) bsES = strip1float(DurBothBoostedES, 5, 'ES') bsopen = strip1float(arrayin, 2, sym) ##raw open price bshighs = strip1float(arrayin, 3, sym) #### print dur bsvolume = strip1float(arrayin, 6, sym) ## bsvolume = strip1floatfromstring(arrayin,6,sym) bslows = strip1float(arrayin, 4, sym) bsbardiff = difftwoarrays(bsclose, bsopen) barrange = difftwoarrays(bshighs, bslows) barmids = difftwoarraysMIDS(bshighs, bslows) timestamparray = strip1string(arrayin, 1) stochval = 14 stochval2 = 3 rvival = 4 rvival2 = 10 comparebs = rpu_rp.tail_array_to_array(bsclose, 100) compareES = rpu_rp.tail_array_to_array(bsES, 100) comparetimes = rpu_rp.tail_array_to_array(timestamparray, 100) stdvarbb = 1 #.382 bbandma = 20 if Indtitle == 'pivot': indicator = Mod_Indicators.pivotpoint(bshighs, bslows, bsclose) signindicator = indicator elif Indtitle == 'R1': indicator = Mod_Indicators.RS(bshighs, bslows, bsclose, 'R', '1') signindicator = indicator elif Indtitle == 'S1': indicator = Mod_Indicators.RS(bshighs, bslows, bsclose, 'S', '1') signindicator = indicator elif Indtitle == 'S2': indicator = Mod_Indicators.RS(bshighs, bslows, bsclose, 'S', '2') signindicator = indicator elif Indtitle == 'R2': indicator = Mod_Indicators.RS(bshighs, bslows, bsclose, 'R', '2') signindicator = indicator elif Indtitle == 'mcross': indicator = Mod_Indicators.difftwoarrays(EMAmvavgToArray(bsclose, 9), EMAmvavgToArray(bsclose, 21)) signindicator = indicator elif Indtitle == 'AO': indicator = Mod_Indicators.difftwoarrays(SMAmvavgToArray(barmids, 5), SMAmvavgToArray(barmids, 34)) signindicator = indicator elif Indtitle == 'AOAcc': indicator = Mod_Indicators.difftwoarrays( difftwoarrays(SMAmvavgToArray(barmids, 5), SMAmvavgToArray(barmids, 34)), SMAmvavgToArray( difftwoarrays(SMAmvavgToArray(barmids, 5), SMAmvavgToArray(barmids, 34)), 5)) signindicator = indicator elif Indtitle == 'price': indicator = bsclose signindicator = bsbardiff elif Indtitle == 'highs': indicator = bshighs signindicator = bsbardiff elif Indtitle == 'lows': indicator = bslows signindicator = bsbardiff elif Indtitle == 'mcd': indicator = Mod_Indicators.MACDdiverg(bsclose) signindicator = indicator elif Indtitle == 'kupper': indicator = Mod_Indicators.keltner_channels(bshighs, bslows, bsclose, 2, 'upper') signindicator = indicator elif Indtitle == 'klower': indicator = Mod_Indicators.keltner_channels(bshighs, bslows, bsclose, 2, 'lower') signindicator = indicator elif Indtitle == 'stddev': indicator = stddev(bshighs, bslows, bsclose, bbandma) signindicator = indicator elif Indtitle == 'bbandlower': indicator = Mod_Indicators.bbands(bshighs, bslows, bsclose, bbandma, stdvarbb, 'lower') #simpleAvgVal,stdvariable) signindicator = indicator elif Indtitle == 'bbandupper': indicator = Mod_Indicators.bbands(bshighs, bslows, bsclose, bbandma, stdvarbb, 'upper') #simpleAvgVal,stdvariable) signindicator = indicator elif Indtitle == 'kmid': indicator = Mod_Indicators.keltner_channel_mid(bshighs, bslows, bsclose, 2) signindicator = indicator elif Indtitle == 'ema': indicator = Mod_Indicators.EMAmvavgToArray(bsclose, 21) signindicator = indicator elif Indtitle == 'sma200': indicator = mvavgToArray(bsclose, 200) signindicator = indicator elif Indtitle == 'sma50': indicator = mvavgToArray(bsclose, 50) signindicator = indicator elif Indtitle == 'sma100': indicator = mvavgToArray(bsclose, 100) signindicator = indicator elif Indtitle == 'diffvES': indicator = Mod_Indicators.diffvES(comparebs, compareES) ## timestamparray = comparetimes signindicator = indicator elif Indtitle == 'RSI': indicator = Mod_Indicators.RSI(bsclose, 14) signindicator = indicator elif Indtitle == 'ROC': indicator = Mod_Indicators.ROC(bsclose, 9) signindicator = indicator elif Indtitle == 'StochD': indicator = Mod_Indicators.StochD( (Mod_Indicators.StochK(bshighs, bslows, bsclose, stochval)), stochval2) signindicator = indicator elif Indtitle == 'StochK': indicator = Mod_Indicators.StochK(bshighs, bslows, bsclose, stochval) signindicator = indicator elif Indtitle == 'Stoch_CROSS': indicator = Mod_Indicators.Stoch_CROSS( Mod_Indicators.StochK(bshighs, bslows, bsclose, stochval), Mod_Indicators.StochD( Mod_Indicators.StochK(bshighs, bslows, bsclose, stochval), stochval2)) signindicator = indicator elif Indtitle == 'ATR': indicator = Mod_Indicators.ATR(bshighs, bslows, bsclose, 14) signindicator = indicator elif Indtitle == 'RVIline': ind1 = EMAmvavgToArray( Mod_Indicators.RVIsignal(bsopen, bshighs, bslows, bsclose), rvival2) indicator = Mod_Indicators.RVIline(ind1, rvival) signindicator = indicator elif Indtitle == 'RVIsignal': indicator = EMAmvavgToArray( Mod_Indicators.RVIsignal(bsopen, bshighs, bslows, bsclose), rvival2) signindicator = indicator elif Indtitle == 'RVI_CROSS': ind1 = EMAmvavgToArray( Mod_Indicators.RVIsignal(bsopen, bshighs, bslows, bsclose), rvival2) ind2 = Mod_Indicators.RVIline(ind1, rvival) indicator = Mod_Indicators.RVI_CROSS(ind1, ind2) signindicator = indicator pass elif Indtitle == 'CMF': maVal = 20 ## indicator = bsvolume #20 #'20' #CMF(bsclose,bslows,bshighs,bsvolume,maVal) indicator = Mod_Indicators.CMF(bsclose, bslows, bshighs, bsvolume, maVal) signindicator = indicator elif Indtitle == 'OBV': maVal = 20 indicator = Mod_Indicators.OBV( bsclose, bsvolume, maVal ) # bsvolume #20 #'20' #CMF(bsclose,bslows,bshighs,bsvolume,maVal) signindicator = indicator else: print 'unknown indicator', Indtitle sign = show_sign(signindicator, Indtitle, threshold) if Indtitle == 'RSI': sign = show_sign2thresholds(signindicator, Indtitle, 21, 81) durarray = lableToarray(bsclose, dur) slope = show_slope(indicator, 'value') slopetag = show_slope(indicator, 'tagstyle') showcrxx = show_cross(sign, 'noage') crossages = show_cross(sign, 'crossage') stdval = stddev(1, 1, indicator, 50) if Indtitle == 'diffvES': arrayout = joinArraysStates(comparetimes, indicator, sign, slope, slopetag, Indtitle, sym, showcrxx, durarray, bshighs, bslows, bsclose, crossages, stdval) else: arrayout = joinArraysStates(timestamparray, indicator, sign, slope, slopetag, Indtitle, sym, showcrxx, durarray, bshighs, bslows, bsclose, crossages, stdval) alist = timestamparray, indicator, sign, slope, slopetag, Indtitle, sym, showcrxx, durarray, bshighs, bslows, bsclose, crossages, stdval prevlena = 0 return arrayout
def StatesArrayToAllStats(sym, dur, ind, fieldnum): arrayfull = rpu_rp.CsvToLines(statearea + sym + '.' + dur + '.' + ind + '.state.csv') arrayin = strip1float(arrayfull, fieldnum, sym) stats = ArrayToAllStats(arrayin, 50) ## gives std,max,avg return stats
def MaxMinABSAvgIndArray(sym, dur, ind, fieldnum): arrayfull = rpu_rp.CsvToLines(statearea + sym + '.' + dur + '.' + ind + '.state.csv') arrayin = strip1float(arrayfull, fieldnum, sym) stats = maxminABSAvgToArray(arrayin) return stats
((os.getcwd().replace('EXE', '|')).split('|'))[0] + 'EXE' + localtag ] ######################################### import ENVdicts, rpu_rp nd = {} nd = ENVdicts.ENVdicts(localtag) for var in nd.keys(): ## print var locals()[var] = nd[var] ################## ############################# global sym, symbol_list, symdict ######################################## import fileinput lines = rpu_rp.CsvToLines('vardefsin.csv') typelable = 'addfin' typelablenew = 'rolo' for l in lines: if l[1] == typelable: newf = typelablenew + '_' + l[0] print '\'' + newf + '\'' + '=> array(' print '\'name\' => ' + '\'' + newf + '\',' print '\'type\' => ' + '\'' + 'varchar' + '\',' print '\'vname\' => ' + '\'LBL_DB_PROD_' + newf.upper() + '\',' print '\'len\' => ' + '\'' + '35' + '\',' print '),' for l in lines: if l[1] == typelable:
def get_orderid(): tws_conn.reqIds(100) sleep(1) for l in rpu_rp.CsvToLines(TMP + 'OrderIdsSavedlocalsigcreate.csv'): order_id = int(l[0]) return order_id
if round(1000 * a2[barnum], 2) > -0.03 and round(1000 * a2[barnum], 2) < 0.03: print a1[barnum], round(1000 * a2[barnum], 2), round(float(a3[barnum]), 4) barnum += 1 ####################################### files = [ 'ibhistdata.15 mins.2 D.20150428.GBP.csv', 'ibhistdata.15 mins.2 D.20150428.EUR.csv' ] for inputfile in files: print inputfile ##inputfile = 'ibhistdata.15 mins.2 D.20150428.EUR.csv' lines = rpu_rp.CsvToLines(inputfile) ########################################### bs = strip1value(lines, 5) sma26 = EMAmvavgToArray(bs, 26) sma12 = EMAmvavgToArray(bs, 12) ##print len(sma26), len(sma12) diffs = difftwoarrays(sma12, sma26) ## macd is the mvavg9 on this diff macdavg = EMAmvavgToArray(diffs, 9) macddiverg = difftwoarrays(diffs, macdavg) datearray = strip1string(lines, 1) rawprice = strip1value(lines, 5) print_arrays(datearray, macddiverg, rawprice) ###########################
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'
' ', '') + '.ddload.csv' shutil.copyfile(tmp, fname) TicksUtile.throw_out_lastbar(fname) ############### ##blist = bardictweekly.keys() ##blist=['1daylong'] smode = 'allsyms' bmode = 'normal' if smode == 'allsyms': slist = ['SPX', 'FDAX'] ## slist = rpu_rp.CsvToLines(TMP+'symlist.csv')[0] else: slist = rpu_rp.CsvToLines(TMP + 'symlist.csv')[0] if bmode == 'weekly': blist = ['1day'] #bardictweekly.keys() pass else: blist = barlist_Allw5sec strikelist = ['1'] #rpu_rp.CsvToLines('strikelist.csv')[0] explist = ['1'] #rpu_rp.CsvToLines('expirylist.csv')[0] dload(slist, blist, strikelist, explist) print 'disconnecting hdownload flex' tws_conn.disconnect()
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
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] totalsecs = secdict[tf] dur = bardur + tf htickfile = DataDown+ today + '.'+symbol+'.' + dur + '.csv') #[date 5 mins.2 D.GBP.csv Snaptickfile = DataDown + today + '.' + symbol + '.ticksnaps.csv' SnapBarsFname = DataDown + today + '.' +dur + '.' + symbol + '.SnapBars.csv' Sigfile = symbol+'.sigs.csv' snaplines = rpu_rp.CsvToLines(DataDown + today + '.' + sym + '.ticksnaps.csv') ########## lastline = rpu_rp.tail_to_txtfile(htickfile,2,'outfile') ## get last line of historical file for time check lastlineof_hticks = rpu_rp.catstring('outfile') ## this is the last line try: timeofbar = (lastlineof_hticks.split(',')[1]).split()[1] except: timeofbar ='23:59:58' time_of_last_hbar_dt = dt.datetime.strptime(timeofbar, time_format) snapbars = rpInd.create_bars(ticklines,50000,timebarforsnaps,'fullbar','snapshot', sym,time_of_last_hbar_dt,SnapBarsFname)# also creates a file rpu_rp.WriteArrayToCsvfile(DataDown+today + '.' + sym + '.'+ dur + '.recent.csv',newbars) lines = createlines(htickfile) decimalboost = dboostdict[sym] create_report(sigfile,lines,decimalboost,tf) '''
def show_bar8(today, sym): filein = DataDown + today + '.' + sym + '.5mins.both.csv' lines = rpu_rp.CsvToLines(filein) ## oneline = grep '16:05:00' filein onelinearray = rpu_rp.grep_array_to_array(lines, '16:05:00') print onelinearray
def checkRTnonbarticksNEW(sym, fnums): fullarray = [] filein = DataDown + today + '.' + sym + '.RTtickData.csv' prevtotdiff = ask = bid = 0 prevtime = '' string = '' prevtradeuid = '' time = '' size = '' tprice = 0.0 tsize = singletrade = totdaysize = vwap = '' totdowns = totups = 0 tottickups = 0 nettickhits = 0 lastsize = lastprice = bidsize = asksize = 0 prevtick = nettickups = totHits = totLifts = 0 for line in rpu_rp.tail_array_to_array(rpu_rp.CsvToLines(filein), 4000000): if 'tickString' in str(line) and 'tickType=48' in str(line): time = line[3] string = line[2].split(';') if len(string) > 2: tprice = float(string[0].replace('value=', '')) tsize = float(string[1]) singletrade = string[5] totdaysize = string[3] vwaps = string[4] if len(vwaps) > 0: vwap = round(float(string[4]), 2) else: vwap = 0.0 ## value=1984.50;1;1441796865858;207069;1982.49963539;true> ## print tsize,tprice,time,'trade', bid, ask, bidsize, asksize if 'tickSize' in str(line): ## 0 BID_SIZE tickSize() ## 1 BID_PRICE tickPrice() ## 2 ASK_PRICE tickPrice() ## 3 ASK_SIZE tickSize() ## 4 LAST_PRICE tickPrice() ## 5 LAST_SIZE tickSize() fs = line[1].replace(' field=', '') sizetime = line[3] value = int((line[2].replace(' size=', '')).replace('>', '')) size = value ######## print sizetime,size,fs,'size' if fs == '0': bidsize = value elif fs == '3': asksize = value elif fs == '5': lastsize = value else: pass if 'tickPrice' in str(line): pricetime = line[4] value = float(line[2].replace(' price=', '')) fp = line[1].replace(' field=', '') if fp == '1': bid = value pass elif fp == '2': ask = value else: lastprice = value ## if bid == 0.0: tradeuid = time + str(tsize) + str(tprice) if 'tickString' in str(line) and len( string ) > 2 and bid > 0 and ask > 0 and tradeuid != prevtradeuid: if tprice > prevtick: tickdir = 'uptick' elif tprice < prevtick: tickdir = 'downtick' else: tickdir = 'neutral' if bid == tprice: tickflag = 'HitBid' else: tickflag = 'LiftOffer' aout = [] prevtick = tprice if tsize > 0 and time != 'xxx': aout.append(tickflag) aout.append(tickdir) aout.append(tprice) aout.append(tsize) aout.append(bid) aout.append(ask) aout.append(time) fullarray.append(aout) ### how many upticks in a row? prevtime = time prevtradeuid = tradeuid ## print fullarray c = 0 x = len(fullarray) ## print x limiter = 50 f = x - limiter ## print f netlifts = netticks = 0 for l in fullarray: c += 1 if c > f: ## print l lifttag = l[0] tsize = l[2] ticktag = l[1] if lifttag == 'HitBid': netlifts = netlifts - 1 pass else: netlifts += 1 if ticktag == 'downtick': netticks = netticks - 1 pass elif ticktag == 'uptick': netticks += 1 else: pass if c > (x - 8): print l print netlifts, netticks, x, limiter
############################# global sym, symbol_list, symdict ######################################## heredir = os.getcwd() print heredir newfile = downloads +'suitecrm modulebuilder TransEXT - Sheet5.csv' newfile = downloads + 'Untitled spreadsheet - Sheet1 (1).csv' #'fields list WIP new Suitecrm - DB_productsRolotec.csv' newfile = downloads + 'temp' #'fields list WIP new Suitecrm - DB_productsRolotec.csv' newfile = downloads + 'fields list WIP new Suitecrm - Organizations.csv' #newfile = heredir + '/fieldsin.csv' import fileinput newfile = 'newfields.txt' lines = rpu_rp.CsvToLines(newfile) typelable = 'addfin' typelable = 'addfin' typelable2 = 'tx' typelablenew = 'tx' modulename ='TransEXT' modulename ='Organizations' mnamelower = modulename.lower() print '$dictionary[\'' + modulename + '\'] = array( \n\ \'table\'=>\''+ mnamelower +'\',\n\ \'audited\'=>true,\n\ \'duplicate_merge\'=>true,\n\ \'fields\'=>array (\n\ ' lablearray =[]
################## global timedate_format, nextorderID, date, today,recentlimit, time_format from time import sleep, strftime, localtime import rpu_rp, rpInd, ibutiles, TicksUtile, RP_Snapshot, glob, csv, subprocess, datetime, shutil, time, BarUtiles from time import sleep, strftime, localtime import RulesEngine from datetime import datetime import ctypes ################ date = today #'20160219' #yesterday # today ######## <<<<<<< style = '' symlist = ['ES','FDAX'] ##symlist = ['ES']#,'FDAX'] for sym in symlist: print sym,'========' bla = TicksUtile.recenttick(sym,'recent') print bla,' is latest tick' flist = [sym+'.RTticks.csv',sym+'.5mins.both.csv'] for f in flist: print f,'====' tickfile5 = DataDown + date +'.'+f l = rpu_rp.CsvToLines(tickfile5) lastlines = rpu_rp.tail_array_to_array(l,5) for e in lastlines: print e[0],e[1] ##print lastlines raw_input('close...')
############ sizemult = 3 ################# while loop < loopmax: loop += 1 check_status() print 'entry trader hbeat.', loop, loopmax sleep(cycledelay) siglistfile = sigarea + today + '.recentsigsexecHARD.csv' if os.path.isfile(siglistfile): sigfileage = check_for_CP_change(siglistfile) pass else: sigfileage = 99999999 if os.path.isfile(siglistfile) and sigfileage < (cycledelay + agelimit): recentsigs = rpu_rp.CsvToLines(siglistfile) if online == 'online': parent_order_id = get_orderidfromfile() else: parent_order_id = '888' #################3 for lastsig in recentsigs: sym = lastsig[0] profsig = float(lastsig[12]) tsizesig = float(lastsig[13]) showdecimal = int(showdecimaldict[sym]) tside = lastsig[1] ## profsig = int(lastsig[10]) ## stopsig = profsig * 4 ## stopsig = int(lastsig[11]) ## tsize = (int(tsizedict[sym])* sizemult
ordid =0 contractdict ={} for sym in symbol_list: ordid+=1 create_contract_dictionary(ordid,sym) sleep(1) ##################################### cycletime = 15 ## will need to be increase for more products because of delay delay = 1 usedtime = ((len(symbol_list))*delay) cyclepause = cycletime - usedtime loopmax = 36000 # = allday loop = 1 controlfname = TMP + 'QUOTEcontroller.txt' while loop < loopmax: command = (rpu_rp.CsvToLines(controlfname)[0])[0] controlflag = True if command != 'RUN-setting': controlflag = False print 'got the order to exit program....!!! <<<' sys.exit() loopmax = 1 ############### sym_id = 1 current_time = datetime.now().time() for sym in symbol_list: contract = contractdict[sym] ## contract = ibutiles.create_ticksym(sym_id,sym) quotearray =[1,0,0,0,0,'bla','somedate',1,1,1,1,1,1,1,222,333] tws_conn.reqMktData(sym_id,contract,'BID',True)
def checkRTnonbarticksNEW(sym,fnums): ## fullarray =[] filein = DataDown +today+'.'+sym+ '.RTtickData.csv' prevtotdiff = ask = bid =0 string ='' time = '' size = '' tprice = 0.0 tsize = singletrade = totdaysize= vwap ='' totdowns = totups = 0 tottickups=0 for line in rpu_rp.tail_array_to_array(rpu_rp.CsvToLines(filein),400): ## print line if 'tickString' in str(line) and 'tickType=48': time = line[3] string = line[2].split(';') if len(string) > 2: ## print string tprice = float(string[0].replace('value=','')) tsize = float(string[1]) singletrade = string[5] totdaysize = string[3] vwaps = string[4] if len(vwaps) > 0: vwap = round(float(string[4]),2) else: vwap =0.0 ## value=1984.50;1;1441796865858;207069;1982.49963539;true> ## print string if 'tickPrice' in str(line): ## print line time = line[4] if line[1] == ' field=1': bid= float(line[2].replace(' price=','')) pass if line[1] == ' field=2': ask = float(line[2].replace(' price=','')) pass ## if bid == 0.0: ## print line if 'tickSize' in str(line): time = line[3] size = line[2] if 'tickString' in str(line) and len(string) > 2 and bid > 0 and ask > 0: if bid == tprice: bflag = 'down' ## print '>>>>>>>>','hitbid',tsize,'@', tprice,bid,ask tag = '>>>>>>>>'+' '+'hitbid'+' '+str(tsize)+' '+'@'+' '+ str(tprice)+ ' '+str(bid)+' '+str(ask) totdowns += tsize tottickups = tottickups -1 pass else: bflag = 'uptick' ## print 'XXXXXXXX','uptick',tsize,'@', tprice,bid,ask tag = 'XXXXXXXX'+' uptick '+str(tsize)+' @ '+ str(tprice)+' '+ str(bid)+' '+str(ask) totups += tsize tottickups +=1 aout =[] if tsize > 50: aout.append(tag) fullarray.append(aout) ## print fullarray c=0 x = len(fullarray) for l in fullarray: c+=1 if c > (x - 4): print l
def MaxStdAvgIndArray(sym, dur, ind, fieldnum): arrayfull = rpu_rp.CsvToLines(statearea + sym + '.' + dur + '.' + ind + '.state.csv') arrayin = strip1float(arrayfull, fieldnum, sym) stats = maxStdAvgToArray(arrayin) ## gives std,max,avg return stats
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)
def Trigger_from_states(sym, dur, label): statefile = statearea + sym + '.' + dur.replace( ' ', '') + '.' + label + '.state.csv' mdarray = rpu_rp.CsvToLines(statefile) array_of_crosses = rpu_rp.grep_array_to_array(mdarray, 'crxx') return array_of_crosses
exchdict = rpu_rp.create_dict(libsyms, 0, 2) typedict = rpu_rp.create_dict(libsyms, 0, 5) currdict = rpu_rp.create_dict(libsyms, 0, 3) expiredict = rpu_rp.create_dict(libsyms, 0, 4) dboostdict = rpu_rp.create_dict(libsyms, 0, 6) tickdict = rpu_rp.create_dict(libsyms, 0, 8) tsizedict = rpu_rp.create_dict(libsyms, 0, 7) roundfactordict = rpu_rp.create_dict(libsyms, 0, 9) symdict = rpu_rp.create_dict(libsyms, 0, 1) symbol_list = rpsymdict.keys() ################################### timelimit = 2 timer = 1 ################# while timer < timelimit: sigs = rpu_rp.CsvToLines('sigs.csv') onesig = rpu_rp.tail_array_to_array(sigs, 2) for sig in onesig: tws_conn.reqIds(100) sleep(1) for l in rpu_rp.CsvToLines('OrderIdsSaved.csv'): order_id = int(l[0]) ################## print sig # ['13:51:58', 'USD.CAD', '30 secs', '12436.5002', '-0.007', 'negcrossmcd', 'necd']] sym = sig[1] action = sig[5] decimalboost = float(dboostdict[sym]) onetick = float(tickdict[sym]) roundfactor = int(roundfactordict[sym]) print decimalboost, onetick, roundfactor price = round(float((sig[3])) / float(decimalboost), roundfactor)
def gatherlineNEW(sym, ind, dur): print sym, ind, dur ## dur = '1day' indfile = statearea + sym + '.' + dur + '.' + ind + '.state.csv' lineprice = rpu_rp.tail_array_to_array(rpu_rp.CsvToLines(indfile), 1)[0] return lineprice
def checkRTnonbarticksNEW(sym, fnums): recentlimit = 20000 fullarray = [] filein = DataDown + today + '.' + sym + '.RTtickData.csv' prevtotdiff = ask = bid = 0 prevtime = '' string = '' prevtradeuid = '' time = '' size = '' tprice = 0.0 tsize = singletrade = totdaysize = vwap = '' totdowns = totups = 0 tottickups = 0 nettickhits = 0 lastsize = lastprice = bidsize = asksize = 0 prevtick = nettickups = totHits = totLifts = 0 for line in rpu_rp.tail_array_to_array(rpu_rp.CsvToLines(filein), recentlimit): if 'tickString' in str(line) and 'tickType=48' in str(line): print line time = line[3] string = line[2].split(';') if len(string) > 2: tprice = float(string[0].replace('value=', '')) tsize = float(string[1]) singletrade = string[5] totdaysize = string[3] vwaps = string[4] if len(vwaps) > 0: vwap = round(float(string[4]), 2) else: vwap = 0.0 ## value=1984.50;1;1441796865858;207069;1982.49963539;true> if 'tickSize' in line[0] or 'tickPrice' in line[0]: print line ## 0 BID_SIZE tickSize() 1 BID_PRICE tickPrice() ## 2 ASK_PRICE tickPrice() 3 ASK_SIZE tickSize() ## 4 LAST_PRICE tickPrice() 5 LAST_SIZE tickSize() fs = line[1].replace(' field=', '') sizetime = line[3] value = int((line[2].replace(' size=', '')).replace('>', '')) size = value if fs == '0': bidsize = value elif fs == '3': asksize = value elif fs == '5': lastsize = value else: pass if 'tickPrice' in str(line): pricetime = line[4] value = float(line[2].replace(' price=', '')) fp = line[1].replace(' field=', '') if fp == '1': bid = value pass elif fp == '2': ask = value else: lastprice = value ## if bid == 0.0: tradeuid = time + str(tsize) + str(tprice) if 'tickString' in str(line) and len( string ) > 2 and bid > 0 and ask > 0 and tradeuid != prevtradeuid: if tprice > prevtick: tickdir = 'uptick' nettickups += 1 elif tprice < prevtick: tickdir = 'downtick' nettickups = nettickups - 1 else: tickdir = 'neutral' if bid == tprice: tickflag = 'HitBid' nettickhits = nettickhits - 1 else: tickflag = 'LiftOffer' nettickhits = nettickhits + 1 totLifts += tsize aout = [] prevtick = tprice if tsize > 0 and time != 'xxx': minute = time[0:5] #'20:47:32.488000' hour = time[0:4] #### minute = tprice# time[0:5]#'20:47:32.488000' aout.append(tickflag) aout.append(tprice) aout.append(tsize) aout.append(bid) aout.append(ask) aout.append(tickdir) ## aout.append(totHits) aout.append(nettickhits) aout.append(nettickups) aout.append(minute) aout.append(hour) fullarray.append(aout) ### how many upticks in a row? prevtime = time prevtradeuid = tradeuid return fullarray
return 3000 ############################ def get_orderid(): ################ tws_conn.reqIds(100) ## sleep(1) for l in rpu_rp.CsvToLines(TMP + 'OrderIdsSavedlocalsigcreate.csv'): order_id = int(l[0]) ## order_id = 122 return order_id ################################### recentlimit = read_vars() print 'recent limit is now.. ', recentlimit ################## loopmax = 2 loop = 0 sigs = rpu_rp.CsvToLines(sigarea + today +'.sigs.csv') ######################## controlfname = TMP + 'LT_Controller.txt' ##delaytime = 5 cycledelay = int(3) rpu_rp.WriteArrayToCsvfile(sigarea +today+'.LTsigs.csv', '') # flush the file to keep all sigs print 'delay is set at .. ', cycledelay print 'THIS IS LONGTERM SEIGNALS HOURLEY ETC' while loop < loopmax: ############### loop +=1 sleep(cycledelay) now = datetime.strftime(datetime.now(),time_format) recentsigs =[] for sym in symbol_list: for barsize in barlist:
def checkDOMticks(sym, fnums): filein = DataDown + today + '.' + sym + '.RTMktDepth.csv' buylineprice = prepare_empty_array(fnums) buylinesize = prepare_empty_array(fnums) selllineprice = prepare_empty_array(fnums) selllinesize = prepare_empty_array(fnums) prevtotdiff = 0 for line in rpu_rp.tail_array_to_array(rpu_rp.CsvToLines(filein), 15000): ## print line position = int(line[1].replace('position=', '').replace('>', '').replace(' ', '')) size = float(line[5].replace('size=', '').replace('>', '').replace(' ', '')) operation = line[2].replace('operation=', '').replace('>', '').replace(' ', '') price = float(line[4].replace('price=', '').replace('>', '').replace(' ', '')) time = line[6].replace(' ', '') zflag = 'clean' if price == 0 or size == 0: ## print 'zeros' zflag = 'haszeros' if 'side=1' in str(line): buylineprice[position] = price buylinesize[position] = size buylinesizewtd = buylineprice[position] * buylinesize[position] else: selllineprice[position] = price selllinesize[position] = size ## return buylineprice, buylinesize,selllineprice,selllinesize,timenow sizeweightedbid = sum_2arrays_weighted(buylineprice, buylinesize, 'bla') sizeweightedsell = sum_2arrays_weighted(selllineprice, selllinesize, 'bla') totalbuy = float(sum_array(buylinesize, 'all')) totalsell = float(sum_array(selllinesize, 'all')) totdiff = totalbuy - totalsell totalratio = totalbuy / totalsell topbuy = float(sum_array(buylinesize, 'toponly')) topsell = float(sum_array(selllinesize, 'toponly')) topdiff = topbuy - topsell if totdiff == 0: totdiff = 1 if totalbuy > 0: totdiffperc = 100 * (round(float(totdiff / (totalsell + totalbuy)), 3)) else: totdiffperc = 0.0 ############ if topdiff == 0: topdiff = 1 if topbuy > 0: topdiffperc = 100 * (round(float(topdiff / (topsell + topbuy)), 3)) else: topdiffperc = 0.0 ## print totdiffperc, totdiff, totalbuy difftoprev = totdiff - prevtotdiff prevtotdiff = totdiff buytopprice = buylineprice[0] selltopprice = selllineprice[0] ## print 'SELL', selllinesize, totalsell, totdiff showflag = 'no' if abs(totalratio) > 1.20 or totalratio < .95: showflag = 'show' if abs(difftoprev) > 2000000: showflag = 'show' if abs(topdiffperc) > 20000: showflag = 'show' if showflag == 'show': ## print buytopprice,'BUY', buylinesize, totalbuy, totdiff, totalsell, selllinesize, 'SELL',selltopprice,time,difftoprev,str(totdiffperc) ## print 'tot',totalbuy,'x',totalsell, totdiff, str(totdiffperc),'%', difftoprev print 'topBOOK>>', topbuy, 'x', topsell, 'Topbuy ratio =', round( topbuy / (topsell + 1), 2), 'Totalbook Ratio >>>', round(totalratio, 2)