Skip to content

Commit

Permalink
push 1.0.11
Browse files Browse the repository at this point in the history
  • Loading branch information
shinny-mayanqiong committed Feb 14, 2019
1 parent 4041d53 commit 9d9163d
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 125 deletions.
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
/test/

nohup.out
webpack.config.js
babel.config.js
179 changes: 140 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,90 +1,191 @@

# TQSDK - JS

JavaScript 版本对天勤 DIFF 协议的封装。
天勤 DIFF 协议的封装(JavaScript 语言版本)

[DIFF 协议 https://www.shinnytech.com/diff/ ](https://www.shinnytech.com/diff/)
DIFF 协议[https://www.shinnytech.com/diff/](https://www.shinnytech.com/diff/)

## Install
## Install

### 方案一

Html 文件添加

```html
<script src="lib/tqsdk-x.x.x.js"></script>
```

JavaScript 文件中可以使用
JavaScript 文件中可以直接使用:

```js
var tqsdk = new TQSDK();
```

## API

### 初始化
### 方案二

```js
var tqsdk = new TQSDK({
采用 es6 开发,项目根目录下运行

})
```bash
npm install tqsdk
```

### on
js 中引用

```js
tqsdk.on('eventName', fn)
import TQSDK from 'tqsdk'

let tqsdk = new TQSDK({})
```

支持的事件:
## 如何使用

### 1. 初始化

+ ready: 收到合约基础数据(全局只出发一次)
+ rtn_brokers: 收到期货公司列表
+ notify: 收到通知对象
+ rtn_data: 数据更新(每一次数据更新触发)
+ error: 发生错误(目前只有一种:合约服务下载失败)
```js
var tqsdk = new TQSDK({
reconnectInterval, // websocket 最大重连时间间隔 默认 3000
reconnectMaxTimes // websocket 最大重连次数 默认 5
})

// 全部使用默认参数
var tqsdk = new TQSDK()
```

### update_data
### 2. on 事件监听

```js
tqsdk.update_data(dataObject)
// 添加事件监听
tqsdk.on(eventName, cb)

// 取消事件监听
tqsdk.off(eventName, cb)
```

手动更新数据集内容,支持自定义数据集
支持的事件:

|eventName|cb 回调函数参数|事件触发说明|
|---|---|---|
|ready | | 收到合约基础数据|
|rtn_brokers | (array) -- 期货公司列表 | 收到期货公司列表|
|notify | (object) -- 单个通知对象 | 收到通知对象|
|rtn_data | | 数据更新(每一次数据更新触发)|
|error | error | 发生错误(目前只有一种:合约服务下载失败)|

### subscribe_quote 订阅合约
### 3. 操作

#### subscribe_quote 订阅合约

```js
tqsdk.subscribe_quote([string|array])
```

### set_chart 订阅图表
#### set_chart 订阅图表

```js
tqsdk.set_chart([object])
```

### insert_order 下单
#### insert_order 下单

#### auto_insert_order 自动平昨平今

#### cancel_order 撤单

### auto_insert_order 自动平昨平今
#### login 登录

### cancel_order 撤单
#### confirm_settlement 确认结算单

### login 登录
#### transfer 银期转帐

### confirm_settlement 确认结算单
### 4. 获取数据 api

### transfer 银期转帐
#### get_account: ƒ get_account()
#### get_account_id: ƒ get_account_id()
#### get_accounts: ƒ get_accounts()
#### get_by_path: ƒ get_by_path(_path)
#### get_order: ƒ get_order(order_id)
#### get_orders: ƒ get_orders()
#### get_position: ƒ get_position(symbol)
#### get_positions: ƒ get_positions()
#### get_quote: ƒ get_quote(symbol)
#### get_quotes_by_input: ƒ get_quotes_by_input(_input)
#### get_trades: ƒ get_trades()
#### get_trading_day: ƒ get_trading_day()
#### is_changed: ƒ is_changed(target, source)
#### is_logined: ƒ is_logined()

## 关于监听事件

### get_by_path
### get_quote
### get_account_id
### get_positions
### get_position
### get_orders
### get_order
### get_accounts
### get_account
### get_trading_day
监听 `rtn_data` 事件,可以实时对行情数据变化作出响应。但是需要在相应组件 destory 的时候取消监听对应事件。

### Vue Plugin

TQSDK - JS 封装为 Vue 插件,可以在组件中监听事件,不需要在单独取消监听。


```js
import Vue from 'vue'
import TQSDK from 'tqsdk'

let tqsdk = new TQSDK()

const NOOP = () => {}
let tqVmEventMap = {}
let tqsdkRE = /^tqsdk:/

function mixinEvents(Vue) {
let on = Vue.prototype.$on
let emit = Vue.prototype.$emit

Vue.prototype.$on = function proxyOn(eventName, fn = NOOP) {
const vm = this
if (Array.isArray(eventName)) {
eventName.forEach((item) => vm.$on(item, fn));
} else if (tqsdkRE.test(eventName)) {
if (!tqVmEventMap[vm._uid]) tqVmEventMap[vm._uid] = {}
let tq_eventName = eventName.match(/^tqsdk:(.*)/)[1]
if (!tqVmEventMap[vm._uid][tq_eventName]) tqVmEventMap[vm._uid][tq_eventName] = []
tqVmEventMap[vm._uid][tq_eventName].push(fn)
tqsdk.on(tq_eventName, fn.bind(vm))
} else {
on.call(vm, eventName, fn)
}
return vm
}
}

function applyMixin(Vue) {
Vue.mixin({
beforeDestroy() {
const vm = this
const tqevents = tqVmEventMap[vm._uid] || {};
for (let eventName in tqevents) {
let eventsArr = tqevents[eventName]
eventsArr.forEach((fn) => {
tqsdk.removeEventListener(eventName, fn)
})
}
delete tqVmEventMap[vm._uid];
}
})
}

function plugin(Vue) {
if (VERSION < 2) {
console.error('[vue-event-proxy] only support Vue 2.0+');
return;
}
// Exit if the plugin has already been installed.
if (plugin.installed) return
plugin.installed = true
mixinEvents(Vue)
applyMixin(Vue)
}

Vue.use(plugin)

Vue.$tqsdk = tqsdk
Vue.prototype.$tqsdk = tqsdk

export default tqsdk;
```
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "tqsdk",
"version": "1.0.10",
"version": "1.0.11",
"description": "",
"main": "lib/tqsdk-1.0.10.js",
"main": "lib/tqsdk-1.0.11.js",
"scripts": {
"build": "babel src/event.js --out-dir lib",
"dev": "webpack --mode development",
Expand Down
24 changes: 0 additions & 24 deletions src/datastructure.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@
/* eslint-disable vue/no-parsing-error */

// Number.EPSILON
// Number.MAX_SAFE_INTEGER
// Number.MAX_VALUE
// Number.MIN_SAFE_INTEGER
// Number.MIN_VALUE
//
// Number.NaN
// Number.POSITIVE_INFINITY
// Number.NEGATIVE_INFINITY
//
// Number.isFinite()
// Number.isInteger()
// Number.isNaN()
// Number.isSafeInteger()
// Number.parseFloat()
// Number.parseInt()
// Number.prototype.toExponential()
// Number.prototype.toFixed()
// Number.prototype.toLocaleString()
// Number.prototype.toPrecision()
// Number.prototype.toSource()
// Number.prototype.toString()
// Number.prototype.valueOf()

class QUOTE {
constructor() {
this.instrument_id = ''; // 'SHFE.au1906'
Expand Down
57 changes: 56 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import TqWebsocket from './tqwebsocket'
import Datamanager from './datamanage'
import EventPrototype from './event'
import { RandomStr } from './utils'

/**
* 事件类型
+ ready: 收到合约基础数据(全局只出发一次)
+ rtn_brokers: 收到期货公司列表
+ notify: 收到通知对象
+ rtn_data: 数据更新(每一次数据更新触发)
+ error: 发生错误(目前只有一种:合约服务下载失败)
*/

import { UnifyArrayStyle, IsEmptyObject, RandomStr } from './utils'

class TQSDK extends EventPrototype{
constructor({
Expand Down Expand Up @@ -149,6 +158,52 @@ class TQSDK extends EventPrototype{
return this.dm._getByPath(_path)
}

get_quotes_by_input (_input) {
if (typeof _input !== 'string' && !_input.input) return []
let option = {
input: (typeof _input === 'string') ? _input.toLowerCase() : _input.input.toLowerCase(),
instrument_id: _input.instrument_id ? _input.instrument_id : true, // 是否根据合约ID匹配
pinyin: _input.pinyin ? _input.pinyin : true, // 是否根据拼音匹配
include_expired: _input.include_expired ? _input.include_expired : false, // 匹配结果是否包含已下市合约
FUTURE: _input.future ? !!_input.future : true, // 匹配结果是否包含期货合约
FUTURE_INDEX: _input.future_index ? !!_input.future_index: false, // 匹配结果是否包含期货指数
FUTURE_CONT: _input.future_cont ? !!_input.future_cont: false, // 匹配结果是否包含期货主连
OPTION: _input.option ? !!_input.option: false, // 匹配结果是否包含期权
COMBINE: _input.combine ? !!_input.combine: false // 匹配结果是否包含组合
}

let filterSymbol = function (filterOption, quote, by) {
if (filterOption[quote.class] && (filterOption.include_expired || (!filterOption.include_expired && !quote.expired))) {
if (by === 'instrument_id' && (quote.product_id.toLowerCase() === filterOption.input || quote.instrument_id.toLowerCase() === filterOption.input)) {
return true
} else if (by === 'pinyin' && quote.py.split(',').indexOf(filterOption.input) > -1) {
return true
} else {
return false
}
}
return false
}

let result = []
if (option.instrument_id) {
for(let symbol in this.quotesInfo) {
if (filterSymbol(option, this.quotesInfo[symbol], 'instrument_id')){
result.push(symbol)
}
}
}
if (option.pinyin) {
for(let symbol in this.quotesInfo) {
if(filterSymbol(option, this.quotesInfo[symbol], 'pinyin')){
result.push(symbol)
}
}
}
return result
}


get_quote (symbol) {
if (symbol === '') return {}
let symbolObj = this.dm.setDefault('quote', 'quotes', symbol)
Expand Down
Loading

0 comments on commit 9d9163d

Please sign in to comment.