def nextHistoricalDataOperation_req(self, reqId, errorCode=None):
        conn2 = db.DB()
        conn2.query(
            "UPDATE algo_trade.ib_request SET status=%s WHERE action='collect_data' AND req_id=%s;",
            [errorCode if errorCode is not None else -1, reqId])
        conn2.commit()

        ib_requests = conn2.query(
            "SELECT * FROM algo_trade.ib_request WHERE action='collect_data' AND status=0 LIMIT 1;"
        )
        for req in ib_requests:
            watchlist = conn2.query(
                "SELECT * FROM algo_trade.watchlist WHERE symbol=%s AND priority>-1 LIMIT 1;",
                [req['symbol']])
            if watchlist[0]['instr_type'] == 'STK':
                contract = Contract()
                contract.symbol = req['symbol']
                contract.secType = watchlist[0]['instr_type']
                contract.currency = "USD"
                contract.exchange = watchlist[0]['exchange']
                req_content = json.loads(req['req_content'])
                self.reqHistoricalData(
                    req['req_id'], contract, req_content['queryTime'],
                    req_content['durationString'],
                    req_content['barSizeSetting'], req_content['whatToShow'],
                    req_content['useRTH'], req_content['formatDate'],
                    True if req_content['keepUpToDate'] == "True" else False,
                    [])
            else:
                conn2.query("UPDATE ib_request SET status=-1 WHERE req_id=%s",
                            [req['req_id']])
                conn2.commit()
        conn2.close()
Ejemplo n.º 2
0
def check_user_cred(
    username,
    password,
):
    d = db.DB()
    data = d.fetch("SELECT * from student WHERE name='%s' AND password='******';" %
                   (username, password))
    if len(data) > 0:
        return True
    return False
 def error(self, reqId: TickerId, errorCode: int, errorString: str):
     super().error(reqId, errorCode, errorString)
     if reqId > -1:
         conn2 = db.DB()
         conn2.query(
             "UPDATE algo_trade.ib_request SET status=%s WHERE req_id=%s;",
             [errorCode, reqId])
         conn2.commit()
         conn2.close()
         print("Error. Id:", reqId, "Code:", errorCode, "Msg:", errorString)
         self.finish()
Ejemplo n.º 4
0
def add_student_details(name, age, gender, contact_no, course, address,
                        qualification_10, qualification_12):
    print "Adding students................."
    print "insert into student(name,age, gender, contact_no, course, address, qualification_10, qualification_12) values ('%s', %s, '%s', '%s', '%s', '%s', %s, %s);" % (
        name, age, gender, contact_no, course, address, qualification_10,
        qualification_12)
    d = db.DB()
    data = d.insert(
        "insert into student(name,age, gender, contact_no, course, address, qualification_10, qualification_12) values ('%s', %s, '%s', '%s', '%s', '%s', %s, %s);"
        % (name, age, gender, contact_no, course, address, qualification_10,
           qualification_12))
    d.close()
 def historicalData(self, reqId: int, bar: BarData):
     conn2 = db.DB()
     results = conn2.query(
         'SELECT * FROM algo_trade.ib_request WHERE req_id=%s LIMIT 1;',
         [reqId])
     conn2.close()
     print("HistoricalData. ReqId:", reqId, "BarData.", bar)
     for r in results:
         self.results.append((
             bar,
             r,
         ))
Ejemplo n.º 6
0
def get_teacher_details():
    d = db.DB()
    data = d.fetch("SELECT * from teacher;")
    teacher_data = {"teachers": []}
    for teacher in data:
        info = {}
        info["id"] = teacher[0]
        info["name"] = teacher[1]
        info["age"] = teacher[2]
        info["gender"] = teacher[3]
        info["mobile"] = teacher[4]
        info["course"] = teacher[5]
        teacher_data["teachers"].append(info)
    print teacher_data
    print "In get_teacher_details..........."
    d.close()
    return teacher_data
Ejemplo n.º 7
0
def get_student_details():
    d = db.DB()
    data = d.fetch("SELECT * from student;")
    student_data = {"students": []}
    for student in data:
        info = {}
        info["id"] = student[0]
        info["name"] = student[1]
        info["age"] = student[2]
        info["gender"] = student[3]
        info["mobile"] = student[4]
        info["course"] = student[5]
        info["address"] = student[6]
        info["10th"] = student[7]
        info["12th"] = student[8]
        student_data["students"].append(info)
    print student_data
    print "In get_student_details..........."
    d.close()
    return student_data
Ejemplo n.º 8
0
def main():
    args = docopt(__doc__, version=__version__)
    config = util.parse_config(args['--config'])
    config['config_file'] = args['--config']
    config['daemon']['app_path'] = app_path
    logger = log.setup_logging(config)
    config['logger'] = logger
    my_db = db.DB(config)
    config['db'] = my_db
    daemon = util.Daemon(config)
    if args['start']:
        logger.info('edgy_critsd starting...')
        signal.signal(signal.SIGTERM, util.signal_handler)
        daemon.start()
    elif args['stop']:
        logger.info('edgy_critsd stopping...')
        daemon.stop()
    elif args['restart']:
        logger.info('edgy_critsd restarting...')
        daemon.restart()
Ejemplo n.º 9
0
def main():
    args = docopt(__doc__, version=__version__)
    config = util.parse_config(args['--config'])
    config['config_file'] = args['--config']
    logger = log.setup_logging(config)
    config['logger'] = logger
    my_db = db.DB(config)
    config['db'] = my_db
    config['daemon']['app_path'] = app_path
    if args['--c2e']:
        if args['--src'] in config['crits']['sites'].keys() \
           and args['--dest'] in config['edge']['sites'].keys():
            logger.info('initiating crits=>edge sync between %s and %s' %
                        (args['--src'], args['--dest']))
            crits2edge(config, args['--src'], args['--dest'])
    elif args['--e2c']:
        if args['--src'] in config['edge']['sites'].keys() and \
           args['--dest'] in config['crits']['sites'].keys():
            logger.info('initiating edge=>crits sync between %s and %s' %
                        (args['--src'], args['--dest']))
            edge2crits(config, args['--src'], args['--dest'])
    def storeHistoricalData(self):
        db_input = []

        conn2 = db.DB()
        for result in self.results:
            req_content = json.loads(result[1]['req_content'])
            try:
                timestamp = time.mktime(
                    datetime.datetime.strptime(result[0].date,
                                               "%Y%m%d").timetuple())
            except ValueError:
                timestamp = time.mktime(
                    datetime.datetime.strptime(result[0].date,
                                               "%Y%m%d %H:%M:%S").timetuple())

            frequency = None
            if req_content["barSizeSetting"] == "1 day":
                frequency = "1d"
            elif req_content['barSizeSetting'] == '1 hour':
                frequency = '1h'
            elif req_content['barSizeSetting'] == '30mins':
                frequency = '30min'
            elif req_content['barSizeSetting'] == '15 mins':
                frequency = '15min'
            elif req_content['barSizeSetting'] == '5 mins':
                frequency = '5min'
            elif req_content['barSizeSetting'] == '1 min':
                frequency = '1min'
            else:
                frequency = req_content["barSizeSetting"]
            db_input.append([
                'ib', result[1]['symbol'], frequency, timestamp,
                result[0].open, result[0].high, result[0].low, result[0].close,
                result[0].volume, None
            ])
        conn2.query(
            f'INSERT INTO algo_trade.historical_data (source, symbol, frequency, time, open, high, low, close, volume, adj_close) VALUES {("%s,"*len(db_input))[:-1]} ON DUPLICATE KEY UPDATE source=VALUES(source),symbol=VALUES(symbol),frequency=VALUES(frequency),time=VALUES(time),open=VALUES(open), high=VALUES(high),low=VALUES(low),close=VALUES(close),volume=VALUES(volume),adj_close=VALUES(adj_close);',
            db_input)
        conn2.commit()
        conn2.close()
    def historicalDataEnd(self, reqId: int, start: str, end: str):
        super().historicalDataEnd(reqId, start, end)
        print("HistoricalDataEnd. ReqId:", reqId, "from", start, "to", end)
        conn2 = db.DB()
        t = threading.Thread(target=self.storeHistoricalData())
        t.start()

        request_symbol = conn2.query(
            'SELECT symbol FROM algo_trade.ib_request WHERE req_id=%s;',
            [reqId])
        # requests = conn.query('SELECT symbol FROM algo_trade.ib_request WHERE symbol=%s AND (status=0 OR status=1)',[request_symbol[0]['symbol']])
        # if len(requests)<=0:

        conn2.query(
            "UPDATE algo_trade.ib_request SET status=-1 WHERE req_id=%s;",
            [reqId])
        conn2.commit()
        conn2.query(
            'UPDATE algo_trade.watchlist SET last_update_time=UNIX_TIMESTAMP() WHERE symbol=%s',
            [request_symbol[0]['symbol']])
        conn2.commit()
        conn2.close()
        self.finish()
    def historicalDataOperations_req(self):
        conn2 = db.DB()
        for req in self.ib_requests:
            watchlist = conn2.query(
                "SELECT * FROM algo_trade.watchlist WHERE symbol=%s LIMIT 1;",
                [req['symbol']])
            contract = Contract()
            contract.symbol = req['symbol']
            contract.secType = watchlist[0]['instr_type']
            contract.currency = "USD"
            contract.exchange = "SMART"

            # ! [reqhistoricaldata]
            req_content = json.loads(req['req_content'])
            conn2.query("UPDATE ib_request SET status=1 WHERE req_id=%s",
                        [req['req_id']])
            conn2.commit()
            self.reqHistoricalData(
                req['req_id'], contract, req_content['queryTime'],
                req_content['durationString'], req_content['barSizeSetting'],
                req_content['whatToShow'], req_content['useRTH'],
                req_content['formatDate'],
                True if req_content['keepUpToDate'] == "True" else False, [])
        conn2.close()
import json
import datetime
import csv
from lib import db

conn = db.DB()

with open('config/keysAndConfig.json') as f:
    keysAndConfig = json.load(f)


def importWatchListData():
    for path in keysAndConfig['path']['watchlist_csv']:
        with open(path, newline='') as csv_file:
            watchlist_reader = csv.reader(csv_file, delimiter=',')
            l = list()
            for idx, row in enumerate(watchlist_reader):
                l.append(row)
                if idx > 0:
                    ibDescription = row[1].lower()
                    instrType = None
                    if 'stock' in ibDescription:
                        instrType = 'STK'
                    elif 'futures' in ibDescription:
                        instrType = 'FUT'
                    elif 'options' in ibDescription:
                        instrType = 'OPT'
                    elif 'commodity' in ibDescription:
                        instrType = 'CMDTY'

                    conn.query(
def main():
    SetupLogger()
    logging.debug("now is %s", datetime.datetime.now())
    logging.getLogger().setLevel(logging.ERROR)

    cmdLineParser = argparse.ArgumentParser("api tests")
    # cmdLineParser.add_option("-c", action="store_True", dest="use_cache", default = False, help = "use the cache")
    # cmdLineParser.add_option("-f", action="store", type="string", dest="file", default="", help="the input file")
    cmdLineParser.add_argument("-p",
                               "--port",
                               action="store",
                               type=int,
                               dest="port",
                               default=7497,
                               help="The TCP port to use")
    cmdLineParser.add_argument("-C",
                               "--global-cancel",
                               action="store_true",
                               dest="global_cancel",
                               default=False,
                               help="whether to trigger a globalCancel req")
    args = cmdLineParser.parse_args()
    print("Using args", args)
    logging.debug("Using args %s", args)
    # print(args)

    # enable logging when member vars are assigned
    from ibapi import utils
    Order.__setattr__ = utils.setattr_log
    Contract.__setattr__ = utils.setattr_log
    DeltaNeutralContract.__setattr__ = utils.setattr_log
    TagValue.__setattr__ = utils.setattr_log
    TimeCondition.__setattr__ = utils.setattr_log
    ExecutionCondition.__setattr__ = utils.setattr_log
    MarginCondition.__setattr__ = utils.setattr_log
    PriceCondition.__setattr__ = utils.setattr_log
    PercentChangeCondition.__setattr__ = utils.setattr_log
    VolumeCondition.__setattr__ = utils.setattr_log

    # from inspect import signature as sig
    # import code code.interact(local=dict(globals(), **locals()))
    # sys.exit(1)

    # tc = TestClient(None)
    # tc.reqMktData(1101, ContractSamples.USStockAtSmart(), "", False, None)
    # print(tc.reqId2nReq)
    # sys.exit(1)

    taskList = []

    conn2 = db.DB()
    while True:
        ib_requests = conn2.query(
            "SELECT * FROM algo_trade.ib_request WHERE action='collect_data' AND status=0 LIMIT 50;"
        )
        if len(ib_requests) == 0:
            break

        for idx, req in enumerate(ib_requests):
            conn2.query("UPDATE ib_request SET status=1 WHERE req_id=%s",
                        [req['req_id']])
            conn2.commit()
            t = threading.Thread(target=runApp,
                                 args=(args, [copy.deepcopy(req)], idx))
            t.start()
            taskList.append(t)

            if len(taskList) >= 20:
                for t in taskList:
                    t.join()
                taskList = []

        if len(taskList) > 0:
            for t in taskList:
                t.join()
            taskList = []
    conn2.close()
Ejemplo n.º 15
0
    MAIL_PASSWORD=os.environ['MAIL_PASS']
)

mail = Mail()

mail.init_app(application)


# application.config['MYSQL_DATABASE_USER'] = appconfig.MYSQLDB.USER
# application.config['MYSQL_DATABASE_PASSWORD'] = appconfig.MYSQLDB.PASSWORD
# application.config['MYSQL_DATABASE_DB'] = appconfig.MYSQLDB.DB
# application.config['MYSQL_DATABASE_HOST'] = appconfig.MYSQLDB.HOST
# application.config['MYSQL_DATABASE_PORT'] = appconfig.MYSQLDB.PORT
# mysql = MySQL()
# mysql.init_app(application)
dbo = db.DB()

login_manager = LoginManager()
login_manager.init_app(application)

### ### ### ### ### ### ###

@application.route('/')
def index():
    return render_template('index.html', page='home')

@application.route('/home')
def home():
	return render_template('index.html', page='home')

@application.route('/test')