资源加载中... loading...

exchange.SetContractType

exchange.SetContractType()函数用于设置{@var/EXCHANGE exchange}交易所对象当前的合约代码。 期货支持传统商品期货CTP协议,易盛协议。所有操作前需要先使用exchange.SetContractType()函数设置合约代码。

exchange.SetContractType()函数返回一个结构。该结构中记录合约的详细信息,例如:最小下单合约张数、手续费、交割时间等数据。

打印exchange.SetContractType("MA888")函数返回的数据,即合约详细信息如下:

{
    "InstrumentName": "甲醇连续",
    "MinLimitOrderVolume": 1,
    "OpenDate": "20190116",
    "PositionType": 50,
    "LongMarginRatio": 0.06999999999999999,
    "DeliveryYear": 2020,
    "MaxMarketOrderVolume": 1000,
    "ExpireDate": "20200114",
    "PositionDateType": 50,
    "InstLifePhase": 49,
    "UnderlyingMultiple": 1,
    "CombinationType": 48,
    "InstrumentID": "MA001",
    "ExchangeInstID": "MA001",
    "ProductClass": 49,
    "MinMarketOrderVolume": 1,
    "VolumeMultiple": 10,
    "CreateDate": "20190116",
    "ShortMarginRatio": 0.06999999999999999,
    "UnderlyingInstrID": "",
    "ProductID": "MA",
    "PriceTick": 1,
    "StartDelivDate": "20200114",
    "EndDelivDate": "20200114",
    "ExchangeID": "CZCE",
    "MaxLimitOrderVolume": 1000,
    "MaxMarginSideAlgorithm": 48,
    "DeliveryMonth": 1,
    "IsTrading": 1,
    "StrikePrice": 0,
    "OptionsType": 0
}

object

exchange.SetContractType(symbol)

例如:exchange.SetContractType("rb2310"),作用为设置当前操作的合约为螺纹钢2023年10月交割的合约。 主力连续合约的代码为888,例如MA888,连续指数合约为000,例如MA000888000虚拟合约的交易只支持回测,实盘只支持获取行情。

symbol true string

function main(){
    while(true) {
        if (exchange.IO("status")) {
            var ret = exchange.SetContractType("MA888")
            Log("订阅的合约的详细信息:", ret)
            break
        } else {
            LogStatus(_D(), "未连接")
        }
    }
}
def main():
    while True:
        if exchange.IO("status"):
            ret = exchange.SetContractType("MA888")
            Log("订阅的合约的详细信息:", ret)
            break
        else:
            LogStatus(_D(), "未连接")
void main() {
    while(true) {
        if(exchange.IO("status") == 1) {
            auto ret = exchange.SetContractType("MA888");
            Log("订阅的合约的详细信息:", ret);
            break;
        } else {
            LogStatus(_D(), "未连接");
        }
    }
}

使用exchange.SetContractType()函数设置(订阅)合约。

function main(){
    var n = 0
    while(true){
        // 需要在判断exchange.IO("status")函数返回true,即为真值时才可调用行情、交易等函数
        if(exchange.IO("status")){
            // 设置合约为虚拟合约,MA888,即甲醇主力合约
            var ret = _C(exchange.SetContractType, "MA888")
            var ticker = exchange.GetTicker()
            // 当到达交易条件时
            if(n == 100) {
                exchange.SetContractType(ret.InstrumentID)
                Log("设置映射的实际合约:", ret.InstrumentID)
                exchange.SetDirection("buy")
                var id = exchange.Buy(ticker.Buy - 10, 1)
                Log("id:", id)
                Sleep(1000)
                Log(exchange.GetOrder(id))
                Sleep(1000)
                Log(exchange.GetPositions())
                Sleep(1000)
                exchange.CancelOrder(id)
                Sleep(1000)
                Log(exchange.GetOrders())
            }
            n++
            LogStatus(_D(), "已经连接CTP !")
        } else {
            LogStatus(_D(), "未连接CTP !")
        }
    }
}
def main():
    n = 0
    while True:
        if exchange.IO("status"):
            ret = _C(exchange.SetContractType, "MA888")
            ticker = exchange.GetTicker()
            if n == 100:
                exchange.SetContractType(ret["InstrumentID"])
                Log("设置映射的实际合约:", ret["InstrumentID"])
                exchange.SetDirection("buy")
                id = exchange.Buy(ticker["Buy"] - 10, 1)
                Log("id:", id)
                Sleep(1000)
                Log(exchange.GetOrder(id))
                Sleep(1000)
                Log(exchange.GetPositions())
                Sleep(1000)
                exchange.CancelOrder(id)
                Sleep(1000)
                Log(exchange.GetOrders())
            n += 1
            LogStatus(_D(), "已经连接CTP !")
        else:
            LogStatus(_D(), "未连接CTP !")
void main() {
    int n = 0;
    while(true) {
        if(exchange.IO("status") == 1) {
            auto ret = exchange.SetContractType("MA888");
            auto ticker = exchange.GetTicker();
            if(n == 100) {
                exchange.SetContractType(ret["InstrumentID"]);
                Log("设置映射的实际合约:", ret["InstrumentID"]);
                exchange.SetDirection("buy");
                auto id = exchange.Buy(ticker.Buy - 10, 1);
                Log("id:", id);
                Sleep(1000);
                Log(exchange.GetOrder(id));
                Sleep(1000);
                Log(exchange.GetPositions());
                Sleep(1000);
                exchange.CancelOrder(id);
                Sleep(1000);
                Log(exchange.GetOrders());
            }
            n++;
            LogStatus(_D(), "已经连接CTP !");
        } else {
            LogStatus(_D(), "未连接CTP !");
        }
    }
}

商品期货只有回测支持虚拟合约交易,实盘时虚拟合约只支持获取行情,实盘时我们可以用虚拟合约映射的真实合约下单,例如以下代码:

在商品期货策略中调用exchange.SetContractType(ContractType)函数时,实盘或者simnow模拟盘中是可能订阅失败的,例如连接期货公司前置机失败时或者设置了不存在的合约代码时。 订阅虚拟合约成功以后,返回的字段里面的InstrumentID是主力合约(会在订阅同时获取),方便策略实盘下单交易时做映射使用。 对于商品期货合约代码不太熟悉的用户可以使用如下JavaScript语言的代码查询:

function main(){
    while(true){
        if(exchange.IO("status")){         
            var products_CZCE_Tbl = {
                "type" : "table", 
                "title" : "郑商所 CZCE", 
                "cols" : ["商品名称(ProductName)", "合约代码短名(ProductID)" , "一跳价格(PriceTick)", "一手合约乘数(VolumeMultiple)", "交易所代码(ExchangeID)"], 
                "rows" : [] 
            }
            var products_DCE_Tbl = {
                "type" : "table", 
                "title" : "大商所 DCE", 
                "cols" : ["商品名称(ProductName)", "合约代码短名(ProductID)" , "一跳价格(PriceTick)", "一手合约乘数(VolumeMultiple)", "交易所代码(ExchangeID)"], 
                "rows" : [] 
            }
            var products_SHFE_Tbl = {
                "type" : "table", 
                "title" : "上期所 SHFE", 
                "cols" : ["商品名称(ProductName)", "合约代码短名(ProductID)" , "一跳价格(PriceTick)", "一手合约乘数(VolumeMultiple)", "交易所代码(ExchangeID)"], 
                "rows" : [] 
            }
            var products_other_Tbl = {
                "type" : "table", 
                "title" : "其它", 
                "cols" : ["商品名称(ProductName)", "合约代码短名(ProductID)" , "一跳价格(PriceTick)", "一手合约乘数(VolumeMultiple)", "交易所代码(ExchangeID)"], 
                "rows" : [] 
            }
            exchange.IO("products").forEach(function(product) {
                if (product.ExchangeID == "CZCE") {
                    products_CZCE_Tbl.rows.push([product.ProductName, product.ProductID, product.PriceTick, product.VolumeMultiple, product.ExchangeID])
                } else if (product.ExchangeID == "DCE") {
                    products_DCE_Tbl.rows.push([product.ProductName, product.ProductID, product.PriceTick, product.VolumeMultiple, product.ExchangeID])
                } else if (product.ExchangeID == "SHFE") {
                    products_SHFE_Tbl.rows.push([product.ProductName, product.ProductID, product.PriceTick, product.VolumeMultiple, product.ExchangeID])
                } else {
                    products_other_Tbl.rows.push([product.ProductName, product.ProductID, product.PriceTick, product.VolumeMultiple, product.ExchangeID])
                }                
            })
            LogStatus(_D(), "已经连接CTP", "\n`" + JSON.stringify([products_CZCE_Tbl, products_DCE_Tbl, products_SHFE_Tbl, products_other_Tbl]) + "`")  
            Sleep(1000 * 60 * 5)
        } else {
            LogStatus(_D(), "未连接CTP !")
        }
        Sleep(1000)
    }
}

查询结果显示各个交易所的合约信息。

商品期货合约命名规则如下: 前面字母代表品种名(合约代码短名),后面数字代表合约到期日。

交易所 合约命名规则
上期 / 能源所: 小写 + 4个数字
大商所: 小写 + 4个数字
中金所: 大写 + 4个数字
郑商所: 大写 + 3个数字

{@fun/Futures/exchange.GetContractType exchange.GetContractType}

exchange.SetDirection exchange.GetContractType