模拟盘环境

Author: program, Created: 2022-04-05 17:52:26, Updated: 2022-04-05 17:54:46

介绍:本策略使用python语言创造了一个支持实盘的模拟盘环境

环境配置:

def main():
    IFsign()
    SimSign()
    while True:
        SimGo()

IFsign函数初始化环境,运行时只加载一次,用于创建变量 SimSign函数载入对象 SimGo函数计算模拟账户数值,需要循环运行

数据结构:

Order 订单结构,可由exchange[0].GetOrder()函数返回。

{
    Id          : 123456,        // 交易单唯一标识
    Price       : 1000,          // 下单价格
    Amount      : 10,            // 下单数量
    DealAmount  : 10,            // 成交数量
    AvgPrice    : 1000,          // 成交均价
    Side        : "BUY"          // 订单方向,常量里的订单类型有:BUY,SELL
    Type        : "LONG",        // 订单类型,常量里的订单类型有: LONG,SHORT,NULL
    profit      : 0,             // 订单收益,现货均返回NULL
    feeCcy      : 1,             // 订单手续费
}

Account 账户信息,由exchange[0].GetAccount()函数返回。

{
    Balance         : 1000,      // 可用计价币数量
    FrozenBalance   : 0,         // Balance表示的资产用于挂单的冻结数量
    Stocks          : 1,         // 可用交易币数量
    FrozenStocks    : 0          // Stocks表示的资产用于挂单的冻结数量
}

Position 期货交易中持有的仓位信息,由exchange[0].GetPosition()函数返回此Position结构的数组。

{
    MarginLevel     : 10,        // 持仓杆杠大小
    Amount          : 100,       // 持仓量
    FrozenAmount    : 0,         // 仓位冻结量,用于平仓挂单时的临时冻结仓位数量
    Price           : 10000,     // 持仓均价
    Profit          : 0,         // 持仓浮动盈亏
    Type            : "LONG",    // LONG为多头仓位,SHORT为空头仓位
    Margin          : 1          // 仓位占用的保证金
}

API文档:

以下函数需要通过exchange[交易对序号]对象调用

exchange[0].Buy(price,account)

Buy函数用于下买单,调用后返回一个订单ID。参数值:Price为订单价格,数值类型。Amount为订单数量,数值类型。

def main():
    id = exchange[0].Buy(100, 1)
    Log("id:", id)

exchange[0].Sell(Price, Amount)

Sell函数用于下卖单,调用后返回一个订单ID。参数值:Price为订单价格,数值类型。Amount为订单数量,数值类型。

def main():
    id = exchange[0].Sell(100, 1)
    Log("id:", id)

exchange[0].CancelOrder(Id)

CancelOrder函数用于取消订单,调用后取消某个Id的订单。参数值:Id为订单编号。

def main():
    id = exchange[0].Sell(99999, 1)
    exchange[0].CancelOrder(id)

exchange[0].GetOrder(Id)

GetOrder函数用于获取已成交订单,调用后返回某个Id的订单信息,不填参数返回所有订单信息。参数值:Id为需要获取的订单号,参数Id为整数类型

def main():
    order = exchange[0].GetOrder()

exchange[0].GetOrders(Id)

GetOrders函数用于获取未成交订单,调用后返回某个Id的订单信息,不填参数返回所有订单信息。参数值:Id为需要获取的订单号,参数Id为整数类型

def main():
    orders = exchange[0].GetOrders()

exchange[0].GetAccount()

GetAccount函数用于获取账户信息。返回值:Account结构结构体。

def main():
    account = exchange[0].GetAccount()

exchange[0].GetPosition()

GetPosition函数用于获取当前持仓信息。返回值:position结构体数组。没有持仓则返回空数组,即[]。

def main():
    exchange[0].SetContractType("swap")
    exchange[0].SetMarginLevel(10)
    exchange[0].SetDirection("buy")
    exchange[0].Buy(10000, 2)
    position = exchange[0].GetPosition()

exchange[0].SetMarginLevel(…)

SetMarginLevel函数用于设置杆杠大小。参数值:数值类型。

def main():
    exchange[0].SetMarginLevel(10)

exchange[0].SetDirection(…)

SetDirection函数用于设置exchange[0].Buy或者exchange[0].Sell函数进行期货下单的方向。参数值:字符串类型。

下单函数 SetDirection函数的参数设置的方向 备注
exchange[0].Buy “buy” 买入开多仓
exchange[0].Buy “closesell” 买入平空仓
exchange[0].Sell “sell” 卖出开空仓
exchange[0].Sell “closebuy” 卖出平多仓

参数Direction可以取buy,closebuy,sell,closesell四个参数。

def main():
    exchange[0].SetContractType("swap")
    exchange[0].SetMarginLevel(5)
    exchange[0].SetDirection("buy")
    exchange[0].Buy(10000, 2)
    exchange[0].SetMarginLevel(5)
    exchange[0].SetDirection("closebuy")
    exchange[0].Sell(1000, 2)

exchange[0].SetContractType(…)

SetContractType函数用于设置合约类型。参数值:字符串类型。 参数ContractType可以是任意字符串

def main():
    exchange[0].SetContractType("this_week")

exchange[0].SetServiceCharge()

SetServiceCharge函数用于设置手续费。参数值:数值类型。

def main():
    # 设置0.25%手续费
    exchange[0].SetServiceCharge(0.00025)

exchange[0].SetBalance()

SetBalance函数用于设置余额。参数值:数值类型。

def main():
    # 设置余额为10000
    exchange[0].SetBalance(10000)

exchange[0].SetSpread()

SetSpread函数用于设置点差。参数值:数值类型。

def main():
    # 设置点差为0.005%
    exchange[0].SetSpread(0.005)

策略地址


More