Appearance
报单&挂单信息类方法
类与对象
报单类定义在MT5CTP命名空间,需要显性调用,定义报单类的操作对象:
MQL5
// 类可理解为自定义类型,使用自定义类型定义了一个变量(对象) this_order
MT5CTP::CHistoryOrderInfo this_order;
// 调用对象方法 : 使用序号选定持仓,持仓序号从1开始
for(int i = this_order.Total(); i > 0; i--) {
if(!this_order.SelectByIndex(i))
continue;
// do...
}MQL支持动态指针,但需要自己管理指针的生命周期,如果不是必须,不建议使用:
MQL5
// 类可理解为自定义类型,使用自定义类型定义了一个变量(对象) this_order
MT5CTP::CHistoryOrderInfo *this_order= new MT5CTP::CHistoryOrderInfo();
if(CheckPointer(this_order)==POINTER_DYNAMIC) {
// 调用对象方法 : 使用序号选定持仓,持仓序号从1开始
for(int i = this_order.Total(); i > 0; i--) {
if(!this_order.SelectByIndex(i))
continue;
// do...
}
// 销毁动态指针
delete(this_order);
}与报单类一样,挂单类定义在MT5CTP命名空间,需要显性调用,定义挂单类的操作对象:
MQL5
// 类可理解为自定义类型,使用自定义类型定义了一个变量(对象) this_order
MT5CTP::COrderInfo this_order;
// 调用对象方法 : 使用序号选定持仓,持仓序号从1开始
for(int i = this_order.Total(); i > 0; i--) {
if(!this_order.SelectByIndex(i))
continue;
// do...
}MQL支持动态指针,但需要自己管理指针的生命周期,如果不是必须,不建议使用:
MQL5
// 类可理解为自定义类型,使用自定义类型定义了一个变量(对象) this_order
MT5CTP::COrderInfo *this_order= new MT5CTP::COrderInfo();
if(CheckPointer(this_order)==POINTER_DYNAMIC) {
// 调用对象方法 : 使用序号选定持仓,持仓序号从1开始
for(int i = this_order.Total(); i > 0; i--) {
if(!this_order.SelectByIndex(i))
continue;
// do...
}
// 销毁动态指针
delete(this_order);
}扩展类功能
如果类的方法无法满足应用场景,可以使用继承的方式扩展类功能,在新的类中定义新方法,实现新功能(注意继承中使用的命名空间)。
MQL5
// 定义名为'NewOrder'的新类,继承自 MT5CTP::CHistoryOrderInfo
class NewOrder:public MT5CTP::CHistoryOrderInfo {
// do...
}继承和扩展挂单类功能:
MQL5
// 定义名为'NewOrder'的新类,继承自 MT5CTP::COrderInfo
class NewOrder:public MT5CTP::COrderInfo {
// do...
}完整的例子 | 报单
三步走完成输出报单数据到日志的脚本代码:
MQL5
#property copyright "Copyright 2026, MT5CTP项目组"
#property link "mt5ctp@qq.com"
#property version "1.00"
#property description "MT5CTP功能Demo|仅用于量化投资爱好者测试、研究"
#property description "基于MT5CTP类库[API 2.00 build 0030]"
//+------------------------------------------------------------------+
//| Include & Define |
//+------------------------------------------------------------------+
// 第一步:导入MT5CTP类库
#include <mt5ctp\HistoryOrderInfo.mqh>
// 第二步:定义报单对象 _self_order,注意调用命名空间 MT5CTP
MT5CTP::CHistoryOrderInfo _self_order;
// 自定义结构体
struct OuterOrder {
string symbol; // 合约代码
string direction; // 买卖
string type; // 开平
long volume_init; // 初始数量
long volume; // 剩余数量
long volume_td; // 成交数量
double price; // 持仓价格
datetime time; // 报单时间
string status; // 报单状态
};
// 定义结构体数组
OuterOrder outer[];
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart() {
// 输出信息准备,结构体数组初始化
int total = _self_order.Total();
ArrayResize(outer,total);
// 第三步:报单对象循环遍历,提取数据
// 务必注意报单序号是从1开始,不是从0开始
int outer_index = WRONG_VALUE;
for(int i = 1; i <= total; i++) {
if(!_self_order.SelectByIndex(i))
continue;
// 提取报单数据,填充输出结构体数组
::ZeroMemory(outer[++outer_index]);
outer[outer_index].symbol = _self_order.Symbol();
outer[outer_index].direction = _self_order.DirectionDescription();
long open_close = _self_order.OpenClose();
if(open_close==ORDER_TYPE_ENTRY)
outer[outer_index].type = "开";
else if(open_close==ORDER_TYPE_EXIT)
outer[outer_index].type = "平";
else if(open_close==ORDER_TYPE_EXITTODAY)
my_order[outer_index].type = "平今";
outer[outer_index].volume_init = _self_order.VolumeTotal();
outer[outer_index].volume = _self_order.Volume();
outer[outer_index].volume_td = _self_order.VolumeTraded();
outer[outer_index].price = _self_order.LimitPrice();
outer[outer_index].time = _self_order.Time();
outer[outer_index].status = _self_order.OrderStatusDescription();
}
// 输出报单信息
ArrayPrint(outer);
}
//+------------------------------------------------------------------+完整的例子 | 挂单
三步走完成输出挂单数据到日志的脚本代码:
MQL5
#property copyright "Copyright 2026, MT5CTP项目组"
#property link "mt5ctp@qq.com"
#property version "1.00"
#property description "MT5CTP功能Demo|仅用于量化投资爱好者测试、研究"
#property description "基于MT5CTP类库[API 2.00 build 0030]"
//+------------------------------------------------------------------+
//| Include & Define |
//+------------------------------------------------------------------+
// 第一步:导入MT5CTP类库
#include <mt5ctp\OrderInfo.mqh>
// 第二步:定义挂单对象 _self_order,注意调用命名空间 MT5CTP
MT5CTP::COrderInfo _self_order;
// 自定义结构体
struct OuterOrder {
string symbol; // 合约代码
string direction; // 买卖
string type; // 开平
long volume_init; // 初始数量
long volume; // 剩余数量
long volume_td; // 成交数量
double price; // 持仓价格
datetime time; // 报单时间
string status; // 报单状态
};
// 定义结构体数组
OuterOrder outer[];
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart() {
// 输出信息准备,结构体数组初始化
int total = _self_order.Total();
ArrayResize(outer,total);
// 第三步:挂单对象循环遍历,提取数据
// 务必注意挂单序号是从1开始,不是从0开始
int outer_index = WRONG_VALUE;
for(int i = 1; i <= total; i++) {
if(!_self_order.SelectByIndex(i))
continue;
// 提取挂单数据,填充输出结构体数组
::ZeroMemory(outer[++outer_index]);
outer[outer_index].symbol = _self_order.Symbol();
outer[outer_index].direction = _self_order.DirectionDescription();
long open_close = _self_order.OpenClose();
if(open_close==ORDER_TYPE_ENTRY)
outer[outer_index].type = "开";
else if(open_close==ORDER_TYPE_EXIT)
outer[outer_index].type = "平";
else if(open_close==ORDER_TYPE_EXITTODAY)
my_order[outer_index].type = "平今";
outer[outer_index].volume_init = _self_order.VolumeTotal();
outer[outer_index].volume = _self_order.Volume();
outer[outer_index].volume_td = _self_order.VolumeTraded();
outer[outer_index].price = _self_order.LimitPrice();
outer[outer_index].time = _self_order.Time();
outer[outer_index].status = _self_order.OrderStatusDescription();
}
// 输出报单信息
ArrayPrint(outer);
}
//+------------------------------------------------------------------+