Esempio n. 1
0
def get_position_local(key):
    # 获取本地某个账号对应某个策略的持仓信息
    sql = "select key,symbol,price,total_amount from position_local " \
          "where key='{key}'".format(key=key)
    db = dbserbser()
    data = db.read_data_from_db_to_df(sql=sql)
    return data
Esempio n. 2
0
def update_position(key, action, symbol, amount,log):
    # 更新本地持仓列表
    # 判断key是否存在(key=JQ+策略+账户)
    db = dbserbser()
    sql = "select 1 from position_local where key='{key}' and symbol='{symbol}'".format(key=key, symbol=symbol)
    log.info(sql)
    data = db.read_data_from_db_to_df(sql=sql)
    # 如果不存在,则插入该表记录
    add_time = str(datetime.datetime.now())[:19]
    if len(data) == 0:
        sql = "insert into position_local (key,symbol,total_amount,add_time) " \
              "values ('{key}','{symbol}',{amount},'{add_time}')". \
            format(key=key, symbol=symbol, amount=amount, add_time=add_time)
    # 如果已存在,则更新
    elif action == 'BUY':
        sql = "update position_local set total_amount=total_amount+{amount} , add_time='{add_time}'" \
              "where key='{key}' and symbol='{symbol}'".format(amount=amount, key=key, add_time=add_time,symbol=symbol)
    elif action == 'SELL':
        sql = "update position_local set total_amount=total_amount-{amount}, add_time='{addtime}' " \
              "where key='{key}' and symbol='{symbol}' ".format(amount=amount, key=key,add_time=add_time,symbol=symbol)
    db.execute(sql)
    log.info(sql)
    # 最后删除那些持仓未0的记录
    sql = "delete from position_local where total_amount<=0"
    db.execute(sql)
    log.info(sql)
Esempio n. 3
0
def order():
    """接收get请求,解析url中的参数"""
    log.info(request.url)
    data = request.args.to_dict()
    if 'account' in data.get('key', '0'):
        print(data['key'])
        db = dbserbser()
        db.dict_into_db(tb='orders', data=data)
    return 'I have received you data at time '\
           +str(datetime.datetime.now())[:19]
Esempio n. 4
0
def read():
    # 每隔0.05秒读取表数据
    while True:
        db = dbserbser()
        try:
            sql = "select * from testsqlite3"
            db.execute(sql)
            time.sleep(0.01)
            print('read')
        except Exception as e:
            traceback.print_exc()
Esempio n. 5
0
def delete():
    # 每隔7秒中删除表记录
    while True:
        db = dbserbser()
        try:
            sql = "delete from testsqlite3"
            db.execute(sql)
            time.sleep(0.01)
            print('delete')
        except Exception as e:
            traceback.print_exc()
Esempio n. 6
0
def sync():
    # 接收同步持仓数据的页面
    log.info(request.url)
    data = request.args.to_dict()
    if 'account' in data.get('key', '0'):
        db = dbserbser()
        sql = "delete from position_remote where key='{key}'".format(
            key=data['key'])  # 入库前先删除
        db.execute(sql)
        db.dict_into_db(tb='position_remote', data=data)
        return 'I have received you data at time ' \
           + str(datetime.datetime.now())[:19]
Esempio n. 7
0
def write():
    # 每隔5秒钟写一条记录
    while True:
        db = dbserbser()
        try:
            sql="insert into testsqlite3 (t) values ('%s')"%\
                str(datetime.datetime.now())[:19]
            db.execute(sql)
            time.sleep(0.01)
            print('write ')
        except Exception as e:
            traceback.print_exc()
Esempio n. 8
0
"""
从数据库读取信号,然后执行对应的操作
"""

import traceback
import time
import datetime

from autotrade.trade_api.order_api import trade as trade_api
from autotrade.db.dbserver import dbserbser
from autotrade.common.log.logger import mylogger

# 配置全局的数据库连接和日志
log = mylogger('order.log', name='order')
db = dbserbser()


def get_orders():
    # 从信号表中读取未执行的交易信号
    sql = " select * from orders where done=0 "  # 不对-1失败的单操作
    data = db.read_data_from_db_to_df(sql=sql)
    data = data.to_dict(orient='record')  # 转成list-dict格式
    return data

def update_orders(order,code,log):
    # 如果成功执行买卖指令,那么就更新orders信号表
    # 更新signal表,成功done=1,失败done=-1
    if code == 200:
        sql = "update orders set done=1 where order_id='{order_id}'".format(order_id=order['order_id'])
    else:
        sql = "update orders set done=-1 where order_id='{order_id}'".format(order_id=order['order_id'])
Esempio n. 9
0
def get_position_remote():
    # 读取sync回来的JQ持仓表数据
    sql = "select key,symbol,price,total_amount from position_remote"
    db = dbserbser()
    data = db.read_data_from_db_to_df(sql=sql)
    return data