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, [])
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,[])
for l in rpu_rp.CsvToLines('pricenow'): pricebid = float(l[0]) sleep(1) ## allows oderide to write to window remove later order_id = get_orderid() if tickrounder != 0: pricenow = round(float(ask),roundfactor) ######################################################## sigprice = round(float(priceinsignal)/float(decimalboost),roundfactor) ######################## flipsign = int(-1) if tside == 'SELL': flipsign = int(1) entryprice = pricenow + (flipsign * addamt) orderstring = str(order_id) + ',' + tside + ',' + str(tsize) + ',' + sym + ',' + str(entryprice) ## print orderstring entryorder = ibutiles.create_order('LMT', tsize, tside, entryprice,tranflag,entryprice,entryprice,'entry') print orderstring prevsigid = sigid 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' ## distance to last bar = ## distance to last signal
if tside == 'SELL': treverseside = 'BUY' profadd = profmult * (-1) * profsig fadeamount = absfadeamount else: fadeamount = absfadeamount * int(-1) profadd = profmult * 1 * profsig fadedentryprice = pricenow + fadeamount mktentryprice = pricenow ################################# tranflag = False # True # True ### <<<<<<<<<this needs to be flipped for transmits.....!!!!!! ################################# orderstring = str(parent_order_id) + ',' + tside + ',' + str(tsize) + ',' + sym + ',' + str(fadedentryprice) print 'placing order', orderstring rpu_rp.WriteStringsToFileAppend(TMP +'Entry.orders.Sent.csv',orderstring) fadedentryorder = ibutiles.create_order('LMT', tsize, tside, fadedentryprice,tranflag,fadedentryprice,fadedentryprice,'entry') mktentryorder = ibutiles.create_order('LMT', tsize, tside, mktentryprice,tranflag,mktentryprice,mktentryprice,'entry') stopprice = mktentryprice - (stpmult*profadd) profprice = mktentryprice + profadd bracketorder = ibutiles.create_bracket_order('LMT', tsize,treverseside,profprice,tranflag,profprice,profprice,'profittxt', parent_order_id) bracketorderSTP = ibutiles.create_bracket_order('STP', tsize,treverseside,stopprice,tranflag,stopprice,stopprice,'stoptxt', parent_order_id) if online == 'online': tws_conn.placeOrder(parent_order_id, symcontract, fadedentryorder) tws_conn.placeOrder(parent_order_id+1, symcontract, bracketorder) tws_conn.placeOrder(parent_order_id+2, symcontract, bracketorderSTP) sleep(1) tws_conn.placeOrder(parent_order_id, symcontract, mktentryorder) tws_conn.orderStatus(parent_order_id) entryordsstatus_dict[parent_order_id] = 'openstatus' entryordside_dict[parent_order_id] = tside #{}
fadeamount = absfadeamount else: fadeamount = absfadeamount * int(-1) profadd = profmult * 1 * profsig fadedentryprice = pricenow + fadeamount mktentryprice = pricenow ################################# tranflag = False # True # True ### <<<<<<<<<this needs to be flipped for transmits.....!!!!!! ################################# orderstring = str(parent_order_id) + ',' + tside + ',' + str( tsize) + ',' + sym + ',' + str(fadedentryprice) print 'placing order', orderstring rpu_rp.WriteStringsToFileAppend(TMP + 'Entry.orders.Sent.csv', orderstring) fadedentryorder = ibutiles.create_order('LMT', tsize, tside, fadedentryprice, tranflag, fadedentryprice, fadedentryprice, 'entry') mktentryorder = ibutiles.create_order('LMT', tsize, tside, mktentryprice, tranflag, mktentryprice, mktentryprice, 'entry') stopprice = mktentryprice - (stpmult * profadd) profprice = mktentryprice + profadd profprice2 = mktentryprice + profadd + profadd tsizeprof1 = (tsize / 3) * 2 tsizeprof2 = (tsize / 3) * 1 bracketorder = ibutiles.create_bracket_order( 'LMT', tsizeprof1, treverseside, profprice, tranflag, profprice, profprice, 'profittxt', parent_order_id) bracketorder2 = ibutiles.create_bracket_order( 'LMT', tsizeprof2, treverseside, profprice2, tranflag,
def scan_for_fills(): entrysSentFile = TMP + 'Entry.orders.Sent.csv' filledNoProfFile = TMP + 'Entry.orders.FilledNoProf.csv' filledNProfSentFile = TMP + 'Entry.orders.FilledNProfSent.csv' ProfSentFile = TMP + 'Profit.orders.Sent.csv' openarray = [] fillNoProf = [] filledNProfSent = [] replys = rpu_rp.CsvToLines( TMP + 'entryreplys') ## this reads the replies from the sigcreate login for l in rpu_rp.CsvToLines(entrysSentFile): if len(l) > 0: print l status ='open' listordid = l[0] #poll for status tws_conn.orderStatus(listordid) sleep(3) ### give it time to read list ## filledstring = 'orderStatus orderId='+listordid +', ' status=Filled' for rep in replys: ## CAPTURE FILL PRICE HERE AND USE INSTEAD OF ENTRYPRICE IN FILE if len(rep) > 1 and rep[1] == ' status=Filled' and rep[0] == '<orderStatus orderId='+listordid: print 'found a fill in entry orders', listordid ## status='filled' if len(rep) > 1 and rep[1] == ' status=Cancelled' and rep[0] == '<orderStatus orderId='+listordid: ## print 'found a fill in entry orders', listordid status='cxld' if status == 'filled': fillNoProf.append(l) pass elif status == 'cxld': ## print 'was cxld, deleting from list',l pass else: openarray.append(l) ## print 'is open still',l rpu_rp.WriteArrayToCsvfile(entrysSentFile,openarray) rpu_rp.WriteArrayToCsvfile(filledNoProfFile,fillNoProf) ## enter proforders if any profsneeded = rpu_rp.CsvToLines(filledNoProfFile) profsplaced = [] profitticks = 3 stopticks = 14 profmult = 1 for entryorder in profsneeded: print profsneeded print 'those were all the profs needed' 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' profsplaced.append(entryorder) sleep(4) rpu_rp.WriteArrayToCsvfileAppend(filledNProfSentFile,profsplaced) rpu_rp.WriteArrayToCsvfile(filledNoProfFile,[])
################################## symcontract = symcontrdict[sym] ########################################## pricenow = limitprice ######################## if tside == 'SELL': entrybuffer = addamt else: entrybuffer = addamt * int(-1) entryprice = pricenow + entrybuffer ################################## tranflag = False ### <<<<<<<<<this needs to be flipped for transmits.....!!!!!! ######################################################## orderstring = str(order_id) + ',' + tside + ',' + str(tsize) + ',' + sym + ',' + str(entryprice) rpu_rp.WriteStringsToFileAppend(TMP +'Entry.orders.Sent.csv',orderstring) entryorder = ibutiles.create_order('LMT', tsize, tside, entryprice,tranflag,entryprice,entryprice,'entry') if testmode == 'online': tws_conn.placeOrder(order_id, symcontract, entryorder) print 'placing order' order_id = order_id +1 sleep(4) ########## end of if clause sig file changed loop +=1 shutil.copyfile(siglistfile, 'temp') os.remove(siglistfile) print 'entry trader heartbeat..',loop, loopmax sleep(cycledelay) ############# print 'finished ',loopmax,' loops by Signal Creator...dead since..' print 'disconnecting.. done..' if testmode == 'online':
if 'entry' == 'entry': # always true remove after new version tside = 'BUY' flipsign = int(-1) flipside = 'SELL' if action == 'negcrossmcd': tside = 'SELL' flipsign = int(1) flipside = 'BUY' entryprice = pricenow + (flipsign * addamt) profprice = orderprice - (profitticks * onetick *flipsign) - (flipsign *addamt) stopprice = orderprice + (profitticks * onetick *flipsign) + (flipsign *addamt) ########### orderstring = str(order_id) + ',' + tside + ',' + str(tsize) + ',' + sym + ',' + str(orderprice) print orderstring entryorder = ibutiles.create_order('LMT', tsize, tside, orderprice,tranflag,orderprice,orderprice,'entry') profitorder = ibutiles.create_order('LMT', tsize, flipside, profprice,tranflag,profprice,profprice,'profit') stoporder = ibutiles.create_order('STPLMT', tsize, flipside, stopprice,tranflag,stopprice,stopprice,'stop') if mode == 'NOTRADE-setting': print 'flag said do not place!!!!!' else: tws_conn.placeOrder(order_id, symcontract, entryorder) tws_conn.placeOrder(order_id+1, symcontract, profitorder) rpu_rp.WriteStringsToFileAppend('entryorderssent',orderstring) print 'placing order' sleep(5) print 'disconnecting.. done..' tws_conn.disconnect() ####reqGlobalCancel() ## enter a trailstop as profit order...
if action == 'negcrossmcd': tside = 'SELL' entryprice = pricenow + addamt flipside = 'BUY' profprice = round(entryprice - (profitticks * onetick),roundfactor) stoptrailprice = round(entryprice + (profitticks * onetick * 3),roundfactor) ########### orderstring = str(order_id) + ',' + tside + ',' + str(tsize) + ',' + sym + ',' + str(pricenow) print orderstring if mode == 'NOTRANSMIT-setting': tranflag = False else: tranflag = True entryorder = ibutiles.create_order('LMT', tsize, tside, entryprice,tranflag,entryprice,entryprice,'entry') profitorder = ibutiles.create_order('LMT', tsize, flipside, profprice,tranflag,profprice,profprice,'profit') trailamount = onetick * 8.0 ## profitTrailSTP = ibutiles.create_order('TRAIL', tsize, flipside, profprice,tranflag,trailamount,profprice,'trailrp') if mode == 'NOTRADE-setting': print 'flag said do not place!!!!!' else: if sigid != prevsigid: tws_conn.placeOrder(order_id, symcontract, entryorder) tws_conn.placeOrder(order_id+1, symcontract, profitorder) prevsigid = sigid rpu_rp.WriteStringsToFileAppend('entryorderssent',orderstring) ## check_for_entry_fills(profitorder,order_id) print 'placing order' else:
def scan_for_fills(): entrysSentFile = TMP + 'Entry.orders.Sent.csv' filledNoProfFile = TMP + 'Entry.orders.FilledNoProf.csv' filledNProfSentFile = TMP + 'Entry.orders.FilledNProfSent.csv' ProfSentFile = TMP + 'Profit.orders.Sent.csv' openarray = [] fillNoProf = [] filledNProfSent = [] replys = rpu_rp.CsvToLines( TMP + 'entryreplys') ## this reads the replies from the sigcreate login for l in rpu_rp.CsvToLines(entrysSentFile): if len(l) > 0: print l status = 'open' listordid = l[0] #poll for status tws_conn.orderStatus(listordid) sleep(3) ### give it time to read list ## filledstring = 'orderStatus orderId='+listordid +', ' status=Filled' for rep in replys: ## CAPTURE FILL PRICE HERE AND USE INSTEAD OF ENTRYPRICE IN FILE if len(rep) > 1 and rep[1] == ' status=Filled' and rep[ 0] == '<orderStatus orderId=' + listordid: print 'found a fill in entry orders', listordid ## status='filled' if len(rep) > 1 and rep[1] == ' status=Cancelled' and rep[ 0] == '<orderStatus orderId=' + listordid: ## print 'found a fill in entry orders', listordid status = 'cxld' if status == 'filled': fillNoProf.append(l) pass elif status == 'cxld': ## print 'was cxld, deleting from list',l pass else: openarray.append(l) ## print 'is open still',l rpu_rp.WriteArrayToCsvfile(entrysSentFile, openarray) rpu_rp.WriteArrayToCsvfile(filledNoProfFile, fillNoProf) ## enter proforders if any profsneeded = rpu_rp.CsvToLines(filledNoProfFile) profsplaced = [] profitticks = 3 stopticks = 14 profmult = 1 for entryorder in profsneeded: print profsneeded print 'those were all the profs needed' 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' profsplaced.append(entryorder) sleep(4) rpu_rp.WriteArrayToCsvfileAppend(filledNProfSentFile, profsplaced) rpu_rp.WriteArrayToCsvfile(filledNoProfFile, [])
sigprice = price onetick = float(1/decimalboost) tside = 'BUY' flipside = 'SELL' profprice = round(pricenow + (profitticks * onetick),roundfactor) stoptrailprice = round(pricenow - (profitticks * onetick * 3),roundfactor) if action == 'negcrossmcd': tside = 'SELL' flipside = 'BUY' profprice = round(pricenow - (profitticks * onetick),roundfactor) stoptrailprice = round(pricenow + (profitticks * onetick * 3),roundfactor) ########### orderstring = str(order_id) + ',' + tside + ',' + str(tsize) + ',' + sym + ',' + str(pricenow) print orderstring entryorder = ibutiles.create_order('LMT', tsize, tside, pricenow,True,pricenow,pricenow,'entry') profitorder = ibutiles.create_order('LMT', tsize, flipside, profprice,True,profprice,profprice,'profit') trailamount = onetick * 8.0 profitTrailSTP = ibutiles.create_order('TRAIL', tsize, flipside, profprice,True,trailamount,profprice,'trailrp') if mode == 'notrade': print 'flag said do not place!!!!!' else: if sigid != prevsigid: tws_conn.placeOrder(order_id, symcontract, entryorder) tws_conn.placeOrder(order_id+1, symcontract, profitTrailSTP) prevsigid = sigid rpu_rp.WriteStringsToFileAppend('entryorderssent',orderstring) ## check_for_entry_fills(profitorder,order_id) print 'placing order'
# (The clientId is chosen by us and we will need # separate IDs for both the execution connection and # market data connection) tws_conn = Connection.create(port=7496, clientId=100) tws_conn.connect() tws_conn.register(error_handler, 'Error') tws_conn.registerAll(reply_handler) ################ timelimit = 2 timer = 1 while timer < timelimit: print 'running next loop' # Create an order ID which is 'global' for this session. This # will need incrementing once new orders are submitted. order_id = timer symcontract = ibutiles.create_ticksym(14,'EUR', 'CASH', 'IDEALPRO', 'USD','ignoreexp') sym_order = ibutiles.create_order('LMT', 100000, 'SELL', 1.1498) tws_conn.reqMktData(14,symcontract,'BID',True) # Use the connection to the send the order to IB tws_conn.placeOrder(order_id, symcontract, sym_order) sleep(5) tws_conn.cancelOrder(order_id) timer += 1 sleep(5) # Disconnect from TWS print 'disconnecting..loop is done..' tws_conn.disconnect() ##reqGlobalCancel()
flipside = 'BUY' profprice = round(entryprice - (profitticks * onetick), roundfactor) stoptrailprice = round(entryprice + (profitticks * onetick * 3), roundfactor) ########### orderstring = str(order_id) + ',' + tside + ',' + str( tsize) + ',' + sym + ',' + str(pricenow) print orderstring if mode == 'NOTRANSMIT-setting': tranflag = False else: tranflag = True entryorder = ibutiles.create_order('LMT', tsize, tside, entryprice, tranflag, entryprice, entryprice, 'entry') profitorder = ibutiles.create_order('LMT', tsize, flipside, profprice, tranflag, profprice, profprice, 'profit') trailamount = onetick * 8.0 ## profitTrailSTP = ibutiles.create_order('TRAIL', tsize, flipside, profprice,tranflag,trailamount,profprice,'trailrp') if mode == 'NOTRADE-setting': print 'flag said do not place!!!!!' else: if sigid != prevsigid: tws_conn.placeOrder(order_id, symcontract, entryorder) tws_conn.placeOrder(order_id + 1, symcontract, profitorder) prevsigid = sigid rpu_rp.WriteStringsToFileAppend('entryorderssent', orderstring)
if action == 'negcrossmcd': tside = 'SELL' flipsign = int(1) flipside = 'BUY' entryprice = pricenow + (flipsign * addamt) profprice = orderprice - (profitticks * onetick * flipsign) - (flipsign * addamt) stopprice = orderprice + (profitticks * onetick * flipsign) + (flipsign * addamt) ########### orderstring = str(order_id) + ',' + tside + ',' + str( tsize) + ',' + sym + ',' + str(orderprice) print orderstring entryorder = ibutiles.create_order('LMT', tsize, tside, orderprice, tranflag, orderprice, orderprice, 'entry') profitorder = ibutiles.create_order('LMT', tsize, flipside, profprice, tranflag, profprice, profprice, 'profit') stoporder = ibutiles.create_order('STPLMT', tsize, flipside, stopprice, tranflag, stopprice, stopprice, 'stop') if mode == 'NOTRADE-setting': print 'flag said do not place!!!!!' else: tws_conn.placeOrder(order_id, symcontract, entryorder) tws_conn.placeOrder(order_id + 1, symcontract, profitorder) rpu_rp.WriteStringsToFileAppend('entryorderssent', orderstring) print 'placing order' sleep(5)