strategy

package
v1.8.8 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2022 License: Apache-2.0 Imports: 33 Imported by: 0

Documentation

Index

Constants

View Source
const (
	STOP    = 0
	LOADING = 1
	RUNNING = 2
	REPLAY  = 3
)

* * @Description: 策略状态

View Source
const (
	TICKTYPE  = "TICK"
	KLINETYPE = "KLINE"
)

* * @Description: Tick类型

Variables

View Source
var HandshakeConfig = plugin.HandshakeConfig{
	ProtocolVersion:  1,
	MagicCookieKey:   "BASIC_PLUGIN",
	MagicCookieValue: "hello",
}

插件握手协议

View Source
var LastTimeStamp int64

测试用

Functions

This section is empty.

Types

type Base

type Base struct {
	Contract Contract // 策略相关合约结构

	T T // Tick数据结构

	K K // K线数据结构

	Exchange   map[string]string  // 交易所Map结构,Key:合约代码 Value:交易所代码
	Multiplier map[string]int64   // 合约乘数Map结构,Key:品种代码 Value:合约乘数
	MinMove    map[string]float64 // 合约最小变动价Map结构,Key:品种代码 Value:最小变动价

	Extend map[string]interface{}
	// contains filtered or unexported fields
}

func (*Base) Active

func (s *Base) Active() string

Active

@Description: 激活插件
@receiver s
@return string 返回值

func (*Base) AddIndicator

func (s *Base) AddIndicator(pushflag bool, mainlabels []string, sublabels []string, columnvalues ...interface{})

AddIndicator

@Description: 记录回测自定义数据到CSV
@receiver s
@param pushflag 推送标记,true:既推送给前端又写文件。false:只推送给前端不写文件
@param mainlabels 主图指标标签
@param sublabels 附图指标标签
@param columnvalues 键值对

func (*Base) Buy

func (s *Base) Buy(symbol string, price float64, volume int64, ptype string, offset string) (string, error)

Buy

@Description: 买单
@receiver s
@param symbol 合约代码
@param price 价格
@param volume 数量
@param ptype 订单价格类型 PRICEMARKET:市价/PRICELIMIT:限价
@param offset 开平仓 TRADEOPEN:开仓/TRADECLOSE:平仓
@return string 订单号
@return error 错误信息

func (*Base) Cancel

func (s *Base) Cancel(ordersn string) error

Cancel

@Description: 撤单
@receiver s
@param ordersn 订单号
@return error

func (*Base) DetectDaemonAlive

func (s *Base) DetectDaemonAlive()

DetectDaemonAlive

@Description: 检测daemon是否存活
@receiver s

func (*Base) GetInstrument

func (s *Base) GetInstrument(symbol string) (error, string, string, int64, float64)

GetInstrument

@Description: 获取合约信息
@receiver s
@param symbol 合约代码
@return error 返回错误
@return string 合约代码
@return string 交易所代码
@return int64 合约乘数
@return float64 最小变动价

func (*Base) GetInstrumentMultiple

func (s *Base) GetInstrumentMultiple(symbolT string) (error, string, int64, float64)

GetInstrumentMultiple

@Description: 获取合约乘数
@receiver s
@param symbolT 品种代码
@return error 返回错误
@return string 交易所代码
@return int64 合约乘数
@return float64 最小变动价

func (*Base) GetLastOrderSN added in v1.6.9

func (s *Base) GetLastOrderSN() (string, error)

GetLastOrderSN

@Description: 获取最新成交订单号
@receiver s
@return string
@return error

func (*Base) GetLastPrice

func (s *Base) GetLastPrice(symbol string) (common.LastPrice, error)

GetLastPrice

@Description: 获取最新价
@receiver s
@param symbol 合约代码
@return common.LastPrice 最新价结构
@return error 返回值

func (*Base) GetMainKlineHistory

func (s *Base) GetMainKlineHistory(len int) string

GetMainKlineHistory

@Description: 获取主合约历史k线数据
@receiver s
@param len 数据长度
@return string 返回值

func (*Base) GetMainLongOrderPosition added in v1.2.5

func (s *Base) GetMainLongOrderPosition() int64

GetMainLongOrderPosition

@Description: 获取主交易合约多头报单持仓
@receiver s
@return int64 返回值

func (*Base) GetMainLongRealPosition added in v1.2.7

func (s *Base) GetMainLongRealPosition() int64

GetMainLongRealPosition

@Description: 获取主交易合约多头实际持仓
@receiver s
@return int64 返回值

func (*Base) GetMainShortOrderPosition added in v1.2.5

func (s *Base) GetMainShortOrderPosition() int64

GetMainShortOrderPosition

@Description: 获取主交易合约空头报单持仓
@receiver s
@return int64 返回值

func (*Base) GetMainShortRealPosition added in v1.2.7

func (s *Base) GetMainShortRealPosition() int64

GetMainShortRealPosition

@Description: 获取主交易合约空头实际持仓
@receiver s
@return int64 返回值

func (*Base) GetMainTickBuffer added in v1.2.8

func (s *Base) GetMainTickBuffer() string

GetMainTickBuffer

@Description: 从缓存里获取主Tick数据
@receiver s
@return string 返回值

func (*Base) GetMainTickHistory

func (s *Base) GetMainTickHistory(len int) string

GetMainTickHistory

@Description: 获取主Tick历史数据
@receiver s
@param len 数据长度
@return string 返回值

func (*Base) GetMainVirtualPosition

func (s *Base) GetMainVirtualPosition() int64

GetMainVirtualPosition

@Description: 获取主交易合约虚拟持仓
@receiver s
@return int64 返回值

func (*Base) GetOrder added in v1.7.2

func (s *Base) GetOrder(ordersn string) (common.OrderBase, error)

GetOrder

@Description: 根据订单号获取订单信息
@receiver s
@param ordersn
@return common.OrderBase
@return error

func (*Base) GetOrderBook added in v1.7.9

func (s *Base) GetOrderBook() ([]common.OrderBook, []common.OrderBook)

GetOrderBook

@Description: 获取主次合约orderbook列表
@receiver s
@return []common.OrderBook
@return []common.OrderBook

func (*Base) GetOrderStatus added in v1.6.0

func (s *Base) GetOrderStatus(ordersn string) string

GetOrderStatus

@Description: 获取订单状态
@receiver s
@return string

func (*Base) GetState added in v1.4.0

func (s *Base) GetState() int64

GetState

@Description: 获取策略状态
@receiver s
@return int64

func (*Base) GetStrategyInfo

func (s *Base) GetStrategyInfo() string

GetStrategyInfo

@Description: 查询策略信息
@receiver s
@return string 返回值

func (*Base) GetSubKlineHistory

func (s *Base) GetSubKlineHistory(len int) string

GetSubKlineHistory

@Description: 获取次合约历史k线数据
@receiver s
@param len 数据长度
@return string 返回值

func (*Base) GetSubLongOrderPosition added in v1.2.5

func (s *Base) GetSubLongOrderPosition() int64

GetSubLongOrderPosition

@Description: 获取次交易合约多头报单持仓
@receiver s
@return int64 返回值

func (*Base) GetSubLongRealPosition added in v1.2.7

func (s *Base) GetSubLongRealPosition() int64

GetSubLongRealPosition

@Description: 获取次交易合约多头实际持仓
@receiver s
@return int64 返回值

func (*Base) GetSubShortOrderPosition added in v1.2.5

func (s *Base) GetSubShortOrderPosition() int64

GetSubShortOrderPosition

@Description: 获取次交易合约空头报单持仓
@receiver s
@return int64 返回值

func (*Base) GetSubShortRealPosition added in v1.2.7

func (s *Base) GetSubShortRealPosition() int64

GetSubShortRealPosition

@Description: 获取次交易合约空头实际持仓
@receiver s
@return int64 返回值

func (*Base) GetSubTickBuffer added in v1.2.8

func (s *Base) GetSubTickBuffer() string

GetSubTickBuffer

@Description: 从缓存获取次Tick数据
@receiver s
@return string 返回值

func (*Base) GetSubTickHistory

func (s *Base) GetSubTickHistory(len int) string

GetSubTickHistory

@Description: 获取次Tick历史数据
@receiver s
@param len 数据长度
@return string 返回值

func (*Base) GetSubVirtualPosition

func (s *Base) GetSubVirtualPosition() int64

GetSubVirtualPosition

@Description: 获取次交易合约虚拟持仓
@receiver s
@return int64 返回值

func (*Base) Hello

func (s *Base) Hello() string

Hello

@Description: 检测策略是否存活
@receiver s
@return string 返回值

func (*Base) MakeReplayReport

func (s *Base) MakeReplayReport() string

MakeReplayReport

@Description: 生成回测报告
@receiver s
@return string

func (*Base) ReadLoadingIndicators

func (s *Base) ReadLoadingIndicators() string

ReadLoadingIndicators 读取Loading指标数据

@Description:
@receiver s
@return string

func (*Base) ReadReplayIndicators

func (s *Base) ReadReplayIndicators(startdate int64, enddate int64, length int) string

ReadReplayIndicators 读取指标数据

@Description:
@receiver s
@return string

func (*Base) ReadRunningIndicators

func (s *Base) ReadRunningIndicators() string

ReadRunningIndicators 读取Running指标数据

@Description:
@receiver s
@return string

func (*Base) RegOnInit

func (s *Base) RegOnInit(on OnInitType)

RegOnInit

@Description: 注册初始化回调函数
@receiver s
@param on

func (*Base) RegOnKline

func (s *Base) RegOnKline(on OnKlineType)

RegOnKline

@Description: 注册kline行情回调函数
@receiver s
@param on

func (*Base) RegOnTick

func (s *Base) RegOnTick(on OnTickType)

RegOnTick

@Description: 注册tick行情回调函数
@receiver s
@param on

func (*Base) Replay

func (s *Base) Replay(startdate string, enddate string, length int) string

Replay

@Description: 回测
@receiver s
@param startdate 开始时间
@param enddate 结束时间
@param length 长度(只针对tick级有用)
@return string 返回值

func (*Base) Run

func (s *Base) Run() string

Run

@Description: 运行策略
@receiver s
@return string 返回值

func (*Base) SaveOrderBook added in v1.7.9

func (s *Base) SaveOrderBook()

SaveOrderBook

@Description: 保存orderbook
@receiver s

func (*Base) Sell

func (s *Base) Sell(symbol string, price float64, volume int64, ptype string, offset string) (string, error)

Sell

@Description: 卖单
@receiver s
@param symbol 合约代码
@param price 价格
@param volume 数量
@param ptype 订单价格类型 PRICEMARKET:市价/PRICELIMIT:限价
@param offset 开平仓 TRADEOPEN:开仓/TRADECLOSE:平仓
@return string 订单号
@return error 错误信息

func (*Base) SetConfig

func (s *Base) SetConfig(daemonhost string, daemonwebhost string, daemonchannel string, quoteserverhost string, quotehttphost string, tradeserverhost string, apikey string) string

SetConfig

@Description: 设置配置文件
@receiver s
@param daemonhost 策略守护程序rpc地址
@param daemonwebhost 策略守护程序http地址
@param daemonchannel 策略守护程序消息通道地址
@param quoteserverhost 行情服务器rpc地址
@param quotehttphost 行情http地址
@param tradeserverhost 交易服务器rpc地址
@param apikey 交易key
@return string 返回值

func (*Base) Stop

func (s *Base) Stop() string

Stop

@Description: 停止策略
@receiver s
@return string 返回值

func (*Base) StopReplay

func (s *Base) StopReplay() string

StopReplay

@Description: 停止回测
@receiver s
@return string 返回值

func (*Base) TailOrder

func (s *Base) TailOrder(ordersn string, timeout int64, retry int, addpoints float64, renewpricetype string) error

TailOrder

@Description: 追单
@receiver s
@param ordersn 原订单号
@param timeout 超时时间(毫秒单位)
@param retry 尝试次数
@param addpoints 追单增加点数
@param renewpricetype 追单价格类型
@return error 返回错误

func (*Base) TailOrderNoCancel added in v1.8.0

func (s *Base) TailOrderNoCancel(ordersn string, timeout int64, retry int, addpoints float64, renewpricetype string) error

TailOrderNoCancel

@Description: 追单
@receiver s
@param ordersn 原订单号
@param timeout 超时时间(毫秒单位)
@param retry 尝试次数
@param addpoints 追单增加点数
@param renewpricetype 追单价格类型
@return error 返回错误

type ColumnValue

type ColumnValue struct {
	Name  string
	Value string
}

* * @Description: 列名值键值对参数结构 * @Name: 列名 * @Value: 值

type Contract

type Contract struct {
	MainQuote         string          // 订阅主Quote行情合约
	MainQuoteLen      int             // 订阅主Quote行情缓存长度
	SubQuote          string          // 订阅次Quote行情合约
	SubQuoteLen       int             // 订阅次Quote缓存长度
	MainKline         common.KlineKey // 订阅主K线行情合约Key
	MainKlineLen      int             // 订阅主K线行情缓存长度
	SubKline          common.KlineKey // 订阅次K线行情合约Key
	SubKlineLen       int             // 订阅次K线行情缓存长度
	MainTrade         string          // 主交易合约
	MainTradeExchange string          // 主交易合约交易所
	SubTrade          string          // 次交易合约
	SubTradeExchange  string          // 次交易合约交易所
}

Contract

@Description: 策略相关合约结构

type Indicator

type Indicator struct {
	LoadingFile         *os.File
	LoadingFileName     string
	LoadingColumnNames  []string
	LoadingColumnValues map[string]string
	LoadingCsvWritter   *csv.Writer
	RunningFile         *os.File
	RunningFileName     string
	RunningColumnNames  []string
	RunningColumnValues map[string]string
	RunningCsvWritter   *csv.Writer
	// contains filtered or unexported fields
}

type K

type K struct {
	Time         map[common.KlineKey][]time.Time // 时间戳
	Open         map[common.KlineKey][]float64   // 开盘价
	High         map[common.KlineKey][]float64   // 最高价
	Low          map[common.KlineKey][]float64   // 最低价
	Close        map[common.KlineKey][]float64   // 收盘价
	Volume       map[common.KlineKey][]int64     // 成交量
	OpenInterest map[common.KlineKey][]int64     // 持仓量
}

type KlineBuffer

type KlineBuffer struct {
	Buffer    map[common.KlineKey][]common.Kline
	BufferLen map[common.KlineKey]int
}

KlineBuffer

@Description: K线缓存器

type OnInitType

type OnInitType func()

定义Init回调函数

type OnKlineType

type OnKlineType func(klinekey common.KlineKey, kline common.Kline, offsetflag bool)

定义Kline回调函数

type OnTickType

type OnTickType func(symbol string, quote common.Quote)

定义Tick回调函数

type OrderStatus added in v1.6.0

type OrderStatus struct {
	OrderSN string
	Status  string
}

订单状态结构

type OrderStatusManager added in v1.6.0

type OrderStatusManager struct {
	// contains filtered or unexported fields
}

订单状态管理器

func (*OrderStatusManager) Get added in v1.6.0

func (o *OrderStatusManager) Get(ordersn string) string

Get @Description: 获取订单状态 @receiver o @param ordersn @return string

func (*OrderStatusManager) Update added in v1.6.0

func (o *OrderStatusManager) Update(ordersn string, status string)

Add

@Description: 更新订单状态,没有添加有则更新
@receiver o
@param ordersn
@param status

type Replayer

type Replayer struct {
	DataFile              *os.File
	DataFileName          string
	DataColumnNames       []string
	DataColumnValues      map[string]string
	DataCsvWritter        *csv.Writer
	IndicatorFile         *os.File
	IndicatorFileName     string
	IndicatorColumnNames  []string
	IndicatorColumnValues map[string]string
	IndicatorCsvWritter   *csv.Writer
}

* * @Description: 回测日志结构

type StrategyIf

type StrategyIf interface {
	// 检测插件
	Hello() string

	// 运行策略
	Run() string

	// 停止策略
	Stop() string

	// 回测策略
	Replay(startdate string, enddate string, length int) string

	// 生成回测报告
	MakeReplayReport() string

	// 停止回测
	StopReplay() string

	// 激活插件
	Active() string

	// 设置配置参数
	SetConfig(daemonhost string, daemonwebhost string, daemonchannel string, quoteserverhost string, quotehttphost string, tradeserverhost string, apikey string) string

	// 获取策略信息
	GetStrategyInfo() string

	// 获取主Tick行情缓存
	GetMainTickBuffer() string

	// 获取次Tick行情缓存
	GetSubTickBuffer() string

	// 获取主Tick行情历史
	GetMainTickHistory(len int) string

	// 获取次Tick行情历史
	GetSubTickHistory(len int) string

	// 获取主合约行情历史k线
	GetMainKlineHistory(len int) string

	// 获取次合约行情历史k线
	GetSubKlineHistory(len int) string

	// 读取Loading指标数据
	ReadLoadingIndicators() string

	// 读取Running指标数据
	ReadRunningIndicators() string

	// 读取回测指标数据
	ReadReplayIndicators(startdate int64, enddate int64, length int) string
}

type StrategyPlugin

type StrategyPlugin struct {
	// Impl Injection
	Impl StrategyIf
}

策略插件

func (StrategyPlugin) Client

func (StrategyPlugin) Client(b *plugin.MuxBroker, c *rpc.Client) (interface{}, error)

func (*StrategyPlugin) Server

func (p *StrategyPlugin) Server(*plugin.MuxBroker) (interface{}, error)

type StrategyRPCServer

type StrategyRPCServer struct {
	Impl StrategyIf
}

* * @Description: 策略插件服务端RPC

func (*StrategyRPCServer) Active

func (s *StrategyRPCServer) Active(args interface{}, resp *string) error

func (*StrategyRPCServer) GetMainKlineHistory

func (s *StrategyRPCServer) GetMainKlineHistory(args map[string]interface{}, resp *string) error

func (*StrategyRPCServer) GetMainTickBuffer added in v1.2.8

func (s *StrategyRPCServer) GetMainTickBuffer(args interface{}, resp *string) error

func (*StrategyRPCServer) GetMainTickHistory

func (s *StrategyRPCServer) GetMainTickHistory(args map[string]interface{}, resp *string) error

func (*StrategyRPCServer) GetStrategyInfo

func (s *StrategyRPCServer) GetStrategyInfo(args interface{}, resp *string) error

func (*StrategyRPCServer) GetSubKlineHistory

func (s *StrategyRPCServer) GetSubKlineHistory(args map[string]interface{}, resp *string) error

func (*StrategyRPCServer) GetSubTickBuffer added in v1.2.8

func (s *StrategyRPCServer) GetSubTickBuffer(args interface{}, resp *string) error

func (*StrategyRPCServer) GetSubTickHistory

func (s *StrategyRPCServer) GetSubTickHistory(args map[string]interface{}, resp *string) error

func (*StrategyRPCServer) Hello

func (s *StrategyRPCServer) Hello(args interface{}, resp *string) error

func (*StrategyRPCServer) MakeReplayReport

func (s *StrategyRPCServer) MakeReplayReport(args interface{}, resp *string) error

func (*StrategyRPCServer) ReadLoadingIndicators

func (s *StrategyRPCServer) ReadLoadingIndicators(args interface{}, resp *string) error

func (*StrategyRPCServer) ReadReplayIndicators

func (s *StrategyRPCServer) ReadReplayIndicators(args map[string]interface{}, resp *string) error

func (*StrategyRPCServer) ReadRunningIndicators

func (s *StrategyRPCServer) ReadRunningIndicators(args interface{}, resp *string) error

func (*StrategyRPCServer) Replay

func (s *StrategyRPCServer) Replay(args map[string]interface{}, resp *string) error

func (*StrategyRPCServer) Run

func (s *StrategyRPCServer) Run(args interface{}, resp *string) error

func (*StrategyRPCServer) SetConfig

func (s *StrategyRPCServer) SetConfig(args map[string]interface{}, resp *string) error

func (*StrategyRPCServer) Stop

func (s *StrategyRPCServer) Stop(args interface{}, resp *string) error

func (*StrategyRPCServer) StopReplay

func (s *StrategyRPCServer) StopReplay(args interface{}, resp *string) error

type StrategyRPClient

type StrategyRPClient struct {
	// contains filtered or unexported fields
}

*

  • @Description: 策略插件客户端RPC

func (*StrategyRPClient) Active

func (g *StrategyRPClient) Active() string

func (*StrategyRPClient) GetMainKlineHistory

func (g *StrategyRPClient) GetMainKlineHistory(len int) string

func (*StrategyRPClient) GetMainTickBuffer added in v1.2.8

func (g *StrategyRPClient) GetMainTickBuffer() string

func (*StrategyRPClient) GetMainTickHistory

func (g *StrategyRPClient) GetMainTickHistory(len int) string

func (*StrategyRPClient) GetStrategyInfo

func (g *StrategyRPClient) GetStrategyInfo() string

func (*StrategyRPClient) GetSubKlineHistory

func (g *StrategyRPClient) GetSubKlineHistory(len int) string

func (*StrategyRPClient) GetSubTickBuffer added in v1.2.8

func (g *StrategyRPClient) GetSubTickBuffer() string

func (*StrategyRPClient) GetSubTickHistory

func (g *StrategyRPClient) GetSubTickHistory(len int) string

func (*StrategyRPClient) Hello

func (g *StrategyRPClient) Hello() string

func (*StrategyRPClient) MakeReplayReport

func (g *StrategyRPClient) MakeReplayReport() string

func (*StrategyRPClient) ReadLoadingIndicators

func (g *StrategyRPClient) ReadLoadingIndicators() string

func (*StrategyRPClient) ReadReplayIndicators

func (g *StrategyRPClient) ReadReplayIndicators(startdate int64, enddate int64, length int) string

func (*StrategyRPClient) ReadRunningIndicators

func (g *StrategyRPClient) ReadRunningIndicators() string

func (*StrategyRPClient) Replay

func (g *StrategyRPClient) Replay(startdate string, enddate string, length int) string

func (*StrategyRPClient) Run

func (g *StrategyRPClient) Run() string

func (*StrategyRPClient) SetConfig

func (g *StrategyRPClient) SetConfig(daemonhost string, daemonwebhost string, daemonchannel string, quoteserverhost string, quotehttphost string, tradeserverhost string, apikey string) string

func (*StrategyRPClient) Stop

func (g *StrategyRPClient) Stop() string

func (*StrategyRPClient) StopReplay

func (g *StrategyRPClient) StopReplay() string

type T

type T struct {
	Time         map[string][]time.Time // 时间戳
	LastPrice    map[string][]float64   // 最新价
	AskPrice1    map[string][]float64   // 卖价1
	AskVolume1   map[string][]int64     // 卖量1
	BidPrice1    map[string][]float64   // 买价1
	BidVolume1   map[string][]int64     // 买量1
	Volume       map[string][]int64     // 成交量
	OpenInterest map[string][]int64     // 持仓量
}

Jump to

Keyboard shortcuts

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