trader

package
v1.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2024 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	InvalidFee    = float64(0) // 无效的费用
	InvalidVolume = int(0)     // 无效的股数
	UnknownVolume = int(1)     // 未知的股数
)

Variables

This section is empty.

Functions

func AddCodeToBlackList added in v0.8.0

func AddCodeToBlackList(code string, secureType SecureType)

AddCodeToBlackList 新增黑白名单成分股

func CacheSync added in v0.7.7

func CacheSync()

CacheSync 缓存同步

func CalculateAvailableFund added in v0.7.7

func CalculateAvailableFund(strategyParameter *config.StrategyParameter) float64

CalculateAvailableFund 计算一只股票的可动用资金量

参数:

totalBalance: 总账户余额
marketValue: 股票当前市值
averageCost: 平均买入成本(用于计算盈亏)
commission: 交易佣金率

返回值:

availableFund: 可动用资金量

func CalculateBuyPriceLimit added in v0.7.9

func CalculateBuyPriceLimit(price float64) float64

CalculateBuyPriceLimit 计算合适的买入价格

价格笼子, +2%和+0.10哪个大
目前使用, 当前价格+0.05

func CalculateFundForStrategy added in v0.7.7

func CalculateFundForStrategy(model models.Strategy) float64

计算策略标的的可用资金

func CalculatePriceCage added in v1.0.7

func CalculatePriceCage(strategyParameter config.StrategyParameter, direction Direction, price float64) float64

CalculatePriceCage 计算价格笼子

func CalculateSellPriceLimit added in v0.7.9

func CalculateSellPriceLimit(price float64) float64

CalculateSellPriceLimit 计算卖出价格笼子

func CancelOrder

func CancelOrder(orderId int) error

CancelOrder 撤单

func CheckForBuy added in v0.8.0

func CheckForBuy(code string) bool

CheckForBuy 检查是否可以买

func CheckForSell added in v0.8.0

func CheckForSell(code string) bool

CheckForSell 检查是否可卖

func DirectOrder added in v0.7.7

func DirectOrder(direction Direction, strategyName, orderRemark, securityCode string, priceType PriceType, price float64, volume int) (int, error)

直接下单(透传)

func GetOrderFilename added in v1.0.7

func GetOrderFilename(date ...string) string

获得订单文件名

qmt/账户id/orders.yyyy-mm-dd

func PlaceOrder

func PlaceOrder(direction Direction, model models.Strategy, securityCode string, priceType PriceType, price float64, volume int) (int, error)

PlaceOrder 下委托订单

func ProhibitBuyingToBlackList added in v0.8.0

func ProhibitBuyingToBlackList(code string)

func ProhibitSellingToBlackList added in v0.8.0

func ProhibitSellingToBlackList(code string)

func ProhibitTradingToBlackList added in v0.8.0

func ProhibitTradingToBlackList(code string)

ProhibitTradingToBlackList 禁止交易 - 双向

func SyncLoadListOfBlackAndWhite added in v0.8.0

func SyncLoadListOfBlackAndWhite()

SyncLoadListOfBlackAndWhite 同步黑白名单

func SyncPositions added in v0.7.7

func SyncPositions()

SyncPositions 同步持仓

func UpdatePositions added in v0.7.7

func UpdatePositions()

UpdatePositions 更新持仓

func UsageOfSecureType added in v0.8.0

func UsageOfSecureType() string

Types

type AccountDetail

type AccountDetail struct {
	TotalAsset  float64 `name:"总金额" json:"total_asset"`
	Cash        float64 `name:"可用" json:"cash"`
	MarketValue float64 `name:"市值" json:"market_value"`
	FrozenCash  float64 `name:"冻结" json:"frozen_cash"`
}

AccountDetail 账户信息

func QueryAccount

func QueryAccount() (*AccountDetail, error)

QueryAccount 查询账户信息

type AccountStatus added in v0.7.7

type AccountStatus = int

AccountStatus 账号状态

const (
	ACCOUNT_STATUS_INVALID       AccountStatus = -1 // 无效
	ACCOUNT_STATUS_OK            AccountStatus = 0  // 正常
	ACCOUNT_STATUS_WAITING_LOGIN AccountStatus = 1  // 连接中
	ACCOUNT_STATUSING            AccountStatus = 2  // 登陆中
	ACCOUNT_STATUS_FAIL          AccountStatus = 3  // 失败
	ACCOUNT_STATUS_INITING       AccountStatus = 4  // 初始化中
	ACCOUNT_STATUS_CORRECTING    AccountStatus = 5  // 数据刷新校正中
	ACCOUNT_STATUS_CLOSED        AccountStatus = 6  // 收盘后
	ACCOUNT_STATUS_ASSIS_FAIL    AccountStatus = 7  // 穿透副链接断开
	ACCOUNT_STATUS_DISABLEBYSYS  AccountStatus = 8  // 系统停用(总线使用-密码错误超限)
	ACCOUNT_STATUS_DISABLEBYUSER AccountStatus = 9  // 用户停用(总线使用)
)

type AccountType added in v1.0.7

type AccountType = int

AccountType 账号类型

const (
	FUTURE_ACCOUNT        AccountType = 1     // 期货
	SECURITY_ACCOUNT      AccountType = 2     // 股票
	CREDIT_ACCOUNT        AccountType = 3     // 信用
	FUTURE_OPTION_ACCOUNT AccountType = 5     // 期货期权
	STOCK_OPTION_ACCOUNT  AccountType = 6     // 股票期权
	HUGANGTONG_ACCOUNT    AccountType = 7     // 沪港通
	INCOME_SWAP_ACCOUNT   AccountType = 8     // 美股收益互换
	NEW3BOARD_ACCOUNT     AccountType = 10    // 全国股转账号
	SHENGANGTONG_ACCOUNT  AccountType = 11    // 深港通
	AT_OFFSITEBANKING     AccountType = 13    // 场外理财账户
	AT_OUTTER_FUTURE      AccountType = 1001  // 期货外盘
	AT_IB                 AccountType = 1002  // IB
	AT_NS_TRUSTBANK       AccountType = 15001 // 场外托管
	AT_INTERBANK          AccountType = 15002 // 银行间账号
	AT_BANK               AccountType = 15003 // 银行账号
	AT_OTC                AccountType = 15005 // 场外账号
)

type BlackAndWhite added in v0.8.0

type BlackAndWhite struct {
	Code string     `name:"证券代码" dataframe:"code"`
	Type SecureType `name:"类型" dataframe:"type"`
}

BlackAndWhite 黑白名单

type Direction

type Direction string

Direction 交易方向

const (
	BUY  Direction = "buy"  // 买入
	SELL Direction = "sell" // 卖出
	JUNK Direction = "junk" // 废单
)

func (Direction) Flag added in v0.7.7

func (d Direction) Flag() string

交易类型标志

func (Direction) String

func (d Direction) String() string

type OrderDetail

type OrderDetail struct {
	AccountType   int     `name:"账户类型" json:"account_type"`  // 账户类型
	AccountId     string  `name:"资金账户" json:"account_id"`    // 资金账号
	OrderTime     string  `name:"委托时间" json:"order_time"`    // 报单时间
	StockCode     string  `name:"证券代码" json:"stock_code"`    // 证券代码, 例如"600000.SH"
	OrderType     int     `name:"订单类型" json:"order_type"`    // 委托类型, 23:买, 24:卖
	Price         float64 `name:"委托价格" json:"price"`         // 报价价格, 如果price_type为指定价, 那price为指定的价格, 否则填0
	PriceType     int     `name:"报价类型" json:"price_type"`    // 报价类型, 详见帮助手册
	OrderVolume   int     `name:"委托量" json:"order_volume"`   // 委托数量, 股票以'股'为单位, 债券以'张'为单位
	OrderId       int     `name:"订单ID" json:"order_id"`      // 委托编号
	OrderSysid    string  `name:"合同编号" json:"order_sysid"`   // 柜台编号
	TradedPrice   float64 `name:"成交均价" json:"traded_price"`  // 成交均价
	TradedVolume  int     `name:"成交数量" json:"traded_volume"` // 成交数量, 股票以'股'为单位, 债券以'张'为单位
	OrderStatus   int     `name:"订单状态" json:"order_status"`  // 委托状态
	StatusMessage string  `name:"委托状态描述" json:"status_msg"`  // 委托状态描述, 如废单原因
	StrategyName  string  `name:"策略名称" json:"strategy_name"` // 策略名称
	OrderRemark   string  `name:"委托备注" json:"order_remark"`  // 委托备注
}

OrderDetail 委托订单

func QueryOrders

func QueryOrders() ([]OrderDetail, error)

QueryOrders 查询当日委托

func (OrderDetail) SecurityCode added in v0.7.7

func (d OrderDetail) SecurityCode() string

SecurityCode 获取证券代码

type OrderResult

type OrderResult struct {
	ProxyResult
	OrderId int `json:"order_id"`
}

OrderResult 结果

type OrderStatus added in v0.7.7

type OrderStatus = int

OrderStatus 委托状态

const (
	ORDER_UNREPORTED      OrderStatus = 48  // 未报
	ORDER_WAIT_REPORTING  OrderStatus = 49  // 待报
	ORDER_REPORTED        OrderStatus = 50  // 已报
	ORDER_REPORTED_CANCEL OrderStatus = 51  // 已报待撤
	ORDER_PARTSUCC_CANCEL OrderStatus = 52  // 部成待撤
	ORDER_PART_CANCEL     OrderStatus = 53  // 部撤
	ORDER_CANCELED        OrderStatus = 54  // 已撤
	ORDER_PART_SUCC       OrderStatus = 55  // 部成
	ORDER_SUCCEEDED       OrderStatus = 56  // 已成
	ORDER_JUNK            OrderStatus = 57  // 废单
	ORDER_UNKNOWN         OrderStatus = 255 // 未知
)

type OrderType added in v1.0.7

type OrderType = int

OrderType 订单类型

const (
	STOCK_BUY                        OrderType = 23
	STOCK_SELL                       OrderType = 24
	CREDIT_BUY                       OrderType = 23 // 担保品买入
	CREDIT_SELL                      OrderType = 24 // 担保品卖出
	CREDIT_FIN_BUY                   OrderType = 27 // 融资买入
	CREDIT_SLO_SELL                  OrderType = 28 // 融券卖出
	CREDIT_BUY_SECU_REPAY            OrderType = 29 // 买券还券
	CREDIT_DIRECT_SECU_REPAY         OrderType = 30 // 直接还券
	CREDIT_SELL_SECU_REPAY           OrderType = 31 // 卖券还款
	CREDIT_DIRECT_CASH_REPAY         OrderType = 32 // 直接还款
	CREDIT_FIN_BUY_SPECIAL           OrderType = 40 // 专项融资买入
	CREDIT_SLO_SELL_SPECIAL          OrderType = 41 // 专项融券卖出
	CREDIT_BUY_SECU_REPAY_SPECIAL    OrderType = 42 // 专项买券还券
	CREDIT_DIRECT_SECU_REPAY_SPECIAL OrderType = 43 // 专项直接还券
	CREDIT_SELL_SECU_REPAY_SPECIAL   OrderType = 44 // 专项卖券还款
	CREDIT_DIRECT_CASH_REPAY_SPECIAL OrderType = 45 // 专项直接还款
)

type Position added in v0.7.7

type Position struct {
	AccountType     int     `name:"账户类型" dataframe:"account_type"`     // 账户类型
	AccountId       string  `name:"资金账户" dataframe:"account_id"`       // 资金账号
	StrategyCode    int     `name:"策略编码" dataframe:"strategy_code"`    // 策略编码
	OrderFlag       string  `name:"订单标识" dataframe:"order_flag"`       // 订单标识
	SecurityCode    string  `name:"证券代码" dataframe:"stock_code"`       // 证券代码, 例如"sh600000"
	Volume          int     `name:"持仓数量" dataframe:"volume"`           // 持仓数量,股票以'股'为单位, 债券以'张'为单位
	CanUseVolume    int     `name:"可卖数量" dataframe:"can_use_volume"`   // 可用数量, 股票以'股'为单位, 债券以'张'为单位
	OpenPrice       float64 `name:"开仓价" dataframe:"open_price"`        // 开仓价
	MarketValue     float64 `name:"市值" dataframe:"market_value"`       // 市值
	FrozenVolume    int     `name:"冻结数量" dataframe:"frozen_volume"`    // 冻结数量
	OnRoadVolume    int     `name:"在途股份" dataframe:"on_road_volume"`   // 在途股份
	YesterdayVolume int     `name:"昨夜拥股" dataframe:"yesterday_volume"` // 昨夜拥股
	AvgPrice        float64 `name:"成本价" dataframe:"avg_price"`         // 成本价
	CreateTime      string  `name:"创建时间" dataframe:"create_time"`      // 创建时间
	LastOrderId     string  `name:"前订单ID" dataframe:"last_order_id"`   // 前订单ID
	BuyTime         string  `name:"买入时间" dataframe:"buy_time"`         // 买入时间
	BuyPrice        float64 `name:"买入价格" dataframe:"buy_price"`        // 买入价格
	BuyVolume       int     `name:"买入数量" dataframe:"buy_volume"`       // 买入数量
	SellTime        string  `name:"卖出时间" dataframe:"sell_time"`        // 卖出时间
	SellPrice       float64 `name:"卖出价格" dataframe:"sell_price"`       // 卖出价格
	SellVolume      int     `name:"卖出数量" dataframe:"sell_volume"`      // 卖出数量
	CancelTime      string  `name:"撤单时间" dataframe:"cancel_time"`      // 撤单时间
	UpdateTime      string  `name:"更新时间" dataframe:"update_time"`      // 更新时间
}

Position 持仓

func (*Position) Key added in v0.7.7

func (p *Position) Key() string

Key 用证券代码作为关键字

func (*Position) MergeFromOrder added in v0.7.7

func (p *Position) MergeFromOrder(order OrderDetail) bool

MergeFromOrder 订单合并到持仓

func (*Position) Sync added in v0.7.7

func (p *Position) Sync(other PositionDetail) bool

type PositionDetail

type PositionDetail struct {
	AccountType     int     `name:"账户类型" json:"account_type"`     // 账户类型
	AccountId       string  `name:"资金账户" json:"account_id"`       // 资金账号
	StockCode       string  `name:"证券代码" json:"stock_code"`       // 证券代码, 例如"600000.SH"
	Volume          int     `name:"持仓数量" json:"volume"`           // 持仓数量,股票以'股'为单位, 债券以'张'为单位
	CanUseVolume    int     `name:"可卖数量" json:"can_use_volume"`   // 可用数量, 股票以'股'为单位, 债券以'张'为单位
	OpenPrice       float64 `name:"开仓价" json:"open_price"`        // 开仓价
	MarketValue     float64 `name:"市值" json:"market_value"`       // 市值
	FrozenVolume    int     `name:"冻结数量" json:"frozen_volume"`    // 冻结数量
	OnRoadVolume    int     `name:"在途股份" json:"on_road_volume"`   // 在途股份
	YesterdayVolume int     `name:"昨夜拥股" json:"yesterday_volume"` // 昨夜拥股
	AvgPrice        float64 `name:"成本价" json:"avg_price"`         // 成本价
}

PositionDetail 持仓信息

func QueryHolding

func QueryHolding() ([]PositionDetail, error)

QueryHolding 查询持仓

type PriceType added in v1.0.7

type PriceType = int

PriceType 报价类型

const (
	LATEST_PRICE                  PriceType = 5  // 最新价
	FIX_PRICE                     PriceType = 11 // 指定价/限价
	MARKET_SH_CONVERT_5_CANCEL    PriceType = 42 // 最优五档即时成交剩余撤销[上交所][股票]
	MARKET_SH_CONVERT_5_LIMIT     PriceType = 43 // 最优五档即时成交剩转限价[上交所][股票]
	MARKET_PEER_PRICE_FIRST       PriceType = 44 // 对手方最优价格委托[上交所[股票]][深交所[股票][期权]]
	MARKET_MINE_PRICE_FIRST       PriceType = 45 // 本方最优价格委托[上交所[股票]][深交所[股票][期权]]
	MARKET_SZ_INSTBUSI_RESTCANCEL PriceType = 46 // 即时成交剩余撤销委托[深交所][股票][期权]
	MARKET_SZ_CONVERT_5_CANCEL    PriceType = 47 // 最优五档即时成交剩余撤销[深交所][股票][期权]
	MARKET_SZ_FULL_OR_CANCEL      PriceType = 48 // 全额成交或撤销委托[深交所][股票][期权]
)

type ProxyResult

type ProxyResult struct {
	Status  int    `json:"status"`
	Message string `json:"message"`
}

type SecureType added in v0.8.0

type SecureType uint8

SecureType 安全类型

白名单: Pure(纯净), Safe(安全), Trustworthy(值得信赖), Approved(已批准), Acceptable(可接受), Permitted(允许)
黑名单: Forbidden(禁止), Unsafe(不安全), Untrustworthy(不值得信赖), Rejected(被拒绝), Unacceptable(不可接受), Prohibited(禁止)
备选: SecureVariety, SecureType, SecureKind
const (
	FreeTrading        SecureType = iota // 自由交易
	ProhibitForBuying                    // 禁止买入
	ProhibitForSelling                   // 禁止卖出
	ProhibitTrading    SecureType = 0xff // 禁止交易, 买入和卖出

)

type TestModel added in v1.0.1

type TestModel struct{}

func (TestModel) Code added in v1.0.1

func (TestModel) Code() models.ModelKind

func (TestModel) Evaluate added in v1.0.1

func (s TestModel) Evaluate(securityCode string, result *concurrent.TreeMap[string, models.ResultInfo])

func (TestModel) Filter added in v1.0.1

func (s TestModel) Filter(ruleParameter config.RuleParameter, snapshot factors.QuoteSnapshot) error

func (TestModel) Name added in v1.0.1

func (s TestModel) Name() string

func (TestModel) OrderFlag added in v1.0.1

func (s TestModel) OrderFlag() string

func (TestModel) Sort added in v1.0.1

func (s TestModel) Sort(snapshots []factors.QuoteSnapshot) models.SortedStatus

type TradeFee added in v0.7.7

type TradeFee struct {
	Direction     Direction // 交易方向
	SecurityCode  string    // 证券代码
	Price         float64   // 价格
	Volume        int       // 数量
	StampDutyFee  float64   // 印花税, 双向, 默认单向, 费率0.1%
	TransferFee   float64   // 过户费, 双向, 默认是0.06%
	CommissionFee float64   // 券商佣金, 按照成交金额计算, 双向, 0.025%
	MarketValue   float64   // 股票市值
	TotalFee      float64   // 支出总费用
}

TradeFee 交易费用

func EvaluateFeeForBuy added in v0.7.7

func EvaluateFeeForBuy(securityCode string, fund, price float64) *TradeFee

EvaluateFeeForBuy 评估买入总费用

func EvaluateFeeForSell added in v0.7.7

func EvaluateFeeForSell(securityCode string, price float64, volume int) *TradeFee

EvaluateFeeForSell 评估卖出费用

func (*TradeFee) CalculateFundFromSell added in v0.7.7

func (f *TradeFee) CalculateFundFromSell(price float64, volume int) float64

CalculateFundFromSell 计算卖出股票后净收益

func (*TradeFee) CalculateNumToBuy added in v0.7.7

func (f *TradeFee) CalculateNumToBuy(fund, price float64) int

CalculateNumToBuy 估算可买股票数量

评估买入操作涉及的所有费用
返回100股的整数倍

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL