Code Monkey home page Code Monkey logo

qifiaccount's Introduction

QIFIAccount

QIFI协议下的Account实现

QIFIAccount兼容 QIFI协议标准DIFF协议标准

pip install qifiaccount

QIFIAccount/ QIFI的订书机的时间序列处理模式将兼容回测

--- timeseries => [QIFI, QIFI, QIFI, ... , QIFI..]

具体可以参考QIFIManager中的管理代码

QIFIAccount 中引入了 frozen 冻结的概念, 因此在 send_order 用法的时候, 会进行 order_check

--  平仓的时候的仓位影响会体现在 frozen 中

--  receive_deal 的时候再结算  恢复frozen | 仓位结算

用法:

参见 /usage 文件夹下的ipython notebook

from QIFIAccount import QIFI_Account, ORDER_DIRECTION
acc = QIFI_Account("x1", "x1")
acc.initial()


print(acc.message)


acc.send_order


acc.cancel_order


acc.get_position

QIFI_Account的最优实践是 QIFI_Strategy, 因为QIFI_Account已经将账户层完全实现, 并且支持多市场, 所以 QIFI_Strategy可以直接进行 订阅行情/ 重写on_bar/on_tick 的策略逻辑/ 以及进行仓位/风险管理

StockSim Account

仅限QA模拟盘用户使用(需要申请)

qifiaccount's People

Contributors

bomby9012 avatar vensentzhou avatar yutiansut avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

qifiaccount's Issues

关于qifiacc重新加载报错的问题

在创建qifiacc并运行后,关闭账户,再次登录次账户时 在acc.initial() 时会报错,应该是重新加载账户信息时有个地方格式不对
image

[bug] QIFIAccount.balance属性,在减仓操作时存在计算逻辑bug

issue: 对一只标的进行减仓处理时,会导致account.balance出现异常。

issue context:
QIFIAccount version:2021-09-03时点,master主分支

step1:期初账户100万
step2:100元价格,买入1000股,期末余额100万, 正确
step3:以100元价格,卖出500股,期末余额变为105万, bug

import QUANTAXIS as QA
from QIFIAccount.QAQIFIAccount import QIFI_Account,ORDER_DIRECTION
from qaenv import  mongo_ip

acc=QIFI_Account(username='bug_test',password='stockpasswd0',model='BACKTEST',init_cash=1000000,trade_host=mongo_ip,nodatabase=False)
acc.initial()

od=acc.send_order(code='000001',amount=1000,price=100,towards=ORDER_DIRECTION.BUY, datetime='2021-09-01 11:30:00')
print("---------------after order------------------------------")

print(acc.money)
print(acc.available)
print(acc.balance)


acc.make_deal(od)
print("---------------after trade------------------------------")

print(acc.money)
print(acc.available)
print(acc.balance)
acc.settle()
    
    
od=acc.send_order(code='000001',amount=500,price=100,towards=ORDER_DIRECTION.SELL, datetime='2021-09-02 11:30:00')
print("---------------after order------------------------------")

print(acc.money)
print(acc.available)
print(acc.balance)


acc.make_deal(od)
print("---------------after trade------------------------------")

print(acc.money)
print(acc.available)
print(acc.balance)
acc.settle()
print(acc.balance)
Create new Account
account order_check
{'volume_long': 0, 'volume_short': 0, 'volume_long_frozen': 0, 'volume_short_frozen': 0}
{'volume_long': 0, 'volume_short': 0}
order check success
下单成功 3e7c8c50-59a4-447c-8978-dacdb9410f9f
---------------after order------------------------------
900000.0
900000.0
1000000.0
全部成交 3e7c8c50-59a4-447c-8978-dacdb9410f9f
update trade
---------------after trade------------------------------
900000.0
900000.0
1000000.0
settle
account order_check
{'volume_long': 1000, 'volume_short': 0, 'volume_long_frozen': 0, 'volume_short_frozen': 0}
{'volume_long': 1000, 'volume_short': 0}
order check success
下单成功 865d37f3-e9cd-40a6-8c1f-644bf89c23fa
---------------after order------------------------------
900000.0
900000.0
950000.0
全部成交 865d37f3-e9cd-40a6-8c1f-644bf89c23fa
update trade
---------------after trade------------------------------
950000.0
950000.0
1050000.0
settle
1050000.0

QAQIFIAccount 中A股交易的qapos的问题

关于当日买入后, volume_frozen_today , volume_today 值问题

  1. 空仓建仓后应为
assert pos.volume_long_his == 0
assert pos.volume_long == amount
assert pos.volume_long_today == amount  # 此项源码作为结算使用
assert pos.volume_long_frozen_today == 0  # TODO 买入是否需要冻结?
  1. 当日空仓建仓后应无法卖出
r = acc.send_order(code, amount=amount, price=price, towards=ORDER_DIRECTION.BUY).copy()
acc.make_deal(r)
r1 = acc.send_order(code, amount=amount, price=price, towards=ORDER_DIRECTION.SELL).copy()
assert r1 == False  # 此处应为交易前置检查失败
acc.make_deal(r1)
  1. 测试源码
def test_sell():
    # 委托状态:委托全部成交
    code, price, amount = '000001', 10, 1000
    acc: QIFI_Account = QIFI_Account("x4", "x4")
    acc.initial()
    r = acc.send_order(code, amount=amount, price=price, towards=ORDER_DIRECTION.BUY).copy()
    acc.make_deal(r)
    pos: QA_Position = acc.positions.get(code)
    volume_his = pos.volume_long_his
    print("建仓买入", pos.hold_detail)
    assert pos.volume_long_his == 0
    assert pos.volume_long == amount
    assert pos.volume_long_today == amount
    assert pos.volume_long_frozen_today == 0  # TODO 买入是否需要冻结?
    if volume_his == 0:
        r = acc.send_order(code, amount=amount, price=price, towards=ORDER_DIRECTION.SELL).copy()
        print("卖出委托", r)
        assert r == False  # 此处应为交易前置检查失败
        if r:
            acc.make_deal(r)
        # {'volume_long_today': 0, 'volume_long_his': 0, 'volume_long': 1000, 'volume_short_today': 0, 'volume_short_his': 0, 'volume_short': 0, 'volume_long_frozen_today': 1000, 'volume_short_frozen_today': 0}
        print("卖出委托后", pos.hold_detail)
        assert pos.volume_long == amount
        assert pos.volume_long_today == amount
        assert pos.volume_long_frozen_today == 0  # TODO 买入是否需要冻结?
    acc.settle()
    print("结算后", pos.hold_detail)
    assert pos.volume_long == amount
    assert pos.volume_long_his == amount
    assert pos.volume_long_today == 0
    assert pos.volume_long_frozen_today == 0

卖出委托只有部分成交会报错

  1. 卖出委托,部分成交执行make_deal 报找不到字段coeff错
  2. 卖出委托 部分成交后(未结算)持仓的状态量值问题
    assert pos.volume_long == amount - part_deal  # 1000 - 300 股
    assert pos.volume_long_his == amount - part_deal  # 1000 - 300股
    assert pos.volume_long_today == -amount  # 当日委托卖出1000股
    assert pos.volume_long_frozen_today == amount  # 冻结 1000 股
  1. 卖出委托部分成交结算的状态量问题
    根据 qa_position 中 settle 函数,执行settle后,被直接按卖出委托全部成功处理持仓量变量(实际只成交了部分)
    assert pos.volume_long == amount - part_deal  # 1000 - 300 股
    assert pos.volume_long_his == amount - part_deal  # 1000 - 300股
    assert pos.volume_long_today == 0 # 重置
    assert pos.volume_long_frozen_today == 0  # 重置
  1. 测试代码
def test_part_sell():
    code, price, amount = '000001', 10, 1000
    part_deal = 300
    acc: QIFI_Account = QIFI_Account("x4", "x4")
    acc.initial()
    r = acc.send_order(code, amount=amount, price=price, towards=ORDER_DIRECTION.BUY).copy()
    acc.make_deal(r)
    pos: QA_Position = acc.positions.get(code)
    volume_his = pos.volume_long_his
    print("建仓买入", pos.hold_detail)
    assert pos.volume_long_his == 0
    assert pos.volume_long == amount
    assert pos.volume_long_today == amount
    assert pos.volume_long_frozen_today == 0
    acc.settle()
    print("买入结算后", pos.hold_detail)
    assert pos.volume_long_his == amount
    assert pos.volume_long == amount
    assert pos.volume_long_today == 0
    assert pos.volume_long_frozen_today == 0
    r = acc.send_order(code, amount=amount, price=price, towards=ORDER_DIRECTION.SELL).copy()
    r['volume_left'] = part_deal
    acc.make_deal(r)
    print("卖出委托部分成交后", pos.hold_detail)
    assert pos.volume_long == amount - part_deal  # 1000 - 300 股
    assert pos.volume_long_his == amount - part_deal  # 1000 - 300股
    assert pos.volume_long_today == -amount  # 当日委托卖出1000股
    assert pos.volume_long_frozen_today == amount  # 冻结 1000 股
    acc.settle()
    print("结算后", pos.hold_detail)
    assert pos.volume_long == amount - part_deal
    assert pos.volume_long_his == amount - part_deal
    assert pos.volume_long_today == 0
    assert pos.volume_long_frozen_today == 0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.