somethings are pretty much in there twice or needlessly as they were failed attempts. Please help
from time import sleep, strftime, localtime
from ib.opt import Connection, message
from ib.ext.Contract import Contract
from ib.ext.Order import Order
from ib.ext.EWrapper import EWrapper
import pandas as pd
import numpy as np
import time
import datetime as dt
import logging
import csv
logging.basicConfig(filename='nig.txt', level=logging.INFO)
#---------------------------------------------------------------------------------error /// reply----------------------------------------------------------------------
def error_handler(msg):
print("Server Error: %s" % msg)
def reply_handler(msg):
print("Server Response: %s, %s" % (msg.typeName, msg))
def print_message_from_ib(msg):
print(msg)
def my_account_handler(msg):
print(msg)
#-------------------------------------------------------------------------------------OrderID---------------------------------------------------------------------------
time_received_next_valid_order_id = None
next_valid_order_id = None
def next_valid_order_id_handler(msg):
global time_received_next_valid_order_id, next_valid_order_id
next_valid_order_id = msg.orderId
time_received_next_valid_order_id = time.time()
def get_next_valid_order_id(con):
global time_received_next_valid_order_id, next_valid_order_id
last_time = time_received_next_valid_order_id
next_valid_order_id = con.reqIds(1) # Always keep arg set to 1 (cruft)
while last_time == time_received_next_valid_order_id:
time.sleep(0.2)
return(next_valid_order_id)
#-------------------------------------------------------------------------------Make Order//Contract//Data-------------------------------------------------------------
def make_contract(symbol, sec_type, prim_exch, exch, curr):
contract = Contract()
Contract.m_localSymbol = symbol
Contract.m_secType = sec_type
Contract.m_primaryExch = prim_exch
Contract.m_exchange = exch
Contract.m_currency = curr
return(contract)
CL = make_contract('TSLA', 'STK', 'SMART', 'SMART', 'USD')
def make_order(action, quantity):
order = Order()
order.m_orderType = 'MKT'
order.m_action = action
order.m_transmit = True
order.m_totalQuantity = quantity
return(order)
#-------------------------------------------------------------------------------------Price-----------------------------------------------------------------------------
def my_BidAsk(msg):
if msg.field == 1:
print ('%s:%s: bid: %s' % msg.price))
elif msg.field == 2:
print ('%s:%s: ask: %s' % msg.price))
def reqMktData(self):
contract = Contract() #
contract.m_symbol = 'TSLA'
contract.m_currency = 'USD'
contract.m_secType = 'SKT'
contract.m_exchange = 'SMART'
con.reqMktData(3, contract, '', False)
def reqHistoricalData(self):
print("======================================")
contract = Contract()
contract.m_symbol = 'TSLA'
contract.m_secType = 'STK'
contract.m_exchange = 'SMART'
endtime = strftime('%Y%m%d %H:%M:%S')
self.con.reqHistoricalData(tickerId=1,contract=contract,
endDateTime=endtime,durationStr='1 D',
barSizeSetting='1 min',whatToShow='TRADES',
useRTH=0,formatDate=1)
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------Algo Register and Connect--------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------Connect-------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------
if __name__ == '__main__':
con = Connection.create(port=7496, clientId=333)
con.connect()
endtime = strftime('%Y%m%d %H:%M:%S')
x = 2
#--------------------------------------------------------------------------Register Functions//Req Market Data------------------------------------------------------------
con.register(reqHistoricalData)
con.register(reqMktData)
con.registerAll(print_message_from_ib)
con.registerAll(reply_handler)
con.register(error_handler, 'Error')
con.register(next_valid_order_id_handler, 'NextValidId')
con.register(my_account_handler, 'UpdateAccountValue')
con.reqMarketDataType(3)
con.reqMktData(3, CL, '', True)
#-------------------------------------------------------------------------------------------order---------------------------------------------------------------------------------------------
long1 = make_order("BUY", 1)
long2 = make_order("BUY", 2)
#-----------------------------------------------------------------------------------self program--------------------------------------------------------------------------------------
global overnight_low_oil
global overnight_high_oil
global yesterday_high_oil
global yesterday_low_oil
global high_of_day
global low_of_day
overnight_low = ()
overnight_high = ()
yesterday_low = ()
yesterday_high = ()
high_of_day = ()
low_of_day = ()
#--------------------------------------------------------------------------------------trading-----------------------------------------------------------------------------
def tsl_oil():
oid = get_next_valid_order_id(con)
con.placeOrder(oid, CL, long1)
if x is 2:
sleep(60)
tsl_oil()
tsl_oil()
con.disconnect()