MT4/MQL4入门到精通EA教程第六课-MQL语言常用函数(六)-常用订单功能函数

19 篇文章 13 订阅
19 篇文章 28 订阅

bool OrderClose()

平仓函数,该函数有5个参数


bool  OrderClose( 
   int        ticket,      // 订单号
   double     lots,        // 手数
   double     price,       // 平仓价格
   int        slippage,    // 滑点 
   color      arrow_color  // 箭头颜色
   );

手动开一个订单
在这里插入图片描述
订单号是:198463618

手数是:1

脚本实例

void OnStart() 
  { 
   OrderClose(198463618,1,Bid,3,Red);  
  }

运行一下脚本
在这里插入图片描述
订单平仓成功!

double OrderClosePrice();

返回订单的平仓价

int OrdersHistoryTotal();

返回历史交易订单总单数

bool OrderSelect();

订单识别,有最多三个参数


bool  OrderSelect( 
   int     index,            // 订单号或者自定义排序
   int     select,           // 参数类型
   int     pool=MODE_TRADES  // 排序方式
   );

两个参数形式


OrderSelect(12470,   //订单号
        SELECT_BY_TICKET  //订单号排序
        )

三个参数形式

OrderSelect(i,     //自定义序号
    SELECT_BY_POS, //自定义排序
    MODE_HISTORY   //排序历史订单,持有仓排序代码:MODE_TRADES 
    )

datetime OrderOpenTime();

返回订单开仓时间

datetime OrderCloseTime();

返回订单平仓时间

double OrderOpenPrice();

返回订单开仓价格

double OrderClosePrice();

返回订单平仓价格

double OrderProfit();

返回订单盈亏

double OrderLots();

返回订单开仓手数

int OrderTicket();

返回订单的订单号

脚本实例

void OnStart() 
  { 
   int hstTotal=OrdersHistoryTotal(); 
   Print("hstTotal for the order:", hstTotal); 
   if(OrderSelect(hstTotal-1,SELECT_BY_POS,MODE_HISTORY)==true) 
    { 
     datetime ctm=OrderOpenTime(); 
     if(ctm>0) Print("Open time for the order: ", ctm); 
    
     ctm=OrderCloseTime(); 
     if(ctm>0) Print("Close time for the order: ", ctm); 
     
     double close=OrderClosePrice();
     Print("ClosePrice for the order:", close); 
     
     double open=OrderOpenPrice();
     Print("OpenPrice for the order:", open); 
     
     double profite=OrderProfit(); 
     Print("Profite for the order:", profite);
     
     double lots=OrderLots();  
     Print("Lots for the order:", lots);
         
     int ticket=OrderTicket();
     Print("ticket for the order:", ticket);
     
    } 
  else 
    Print("OrderSelect failed error code is",GetLastError());
  }

实例结果

打印出最后一个交易订单的信息
在这里插入图片描述
在这里插入图片描述
工欲善其事,必先利其器,交易最重要的是遵守规则,严格执行。关注公众号,学习MQL入门到精通EA教程,学习更多EA编程,畅写属于自己的EA,锻造属于自己的神兵利器。

在这里插入图片描述

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!下面是一个简单的MT4 EA编程入门教程: 1. 安装MetaEditor:首先,确保你已经安装了MetaTrader 4(MT4)平台。然后,在MT4中,选择“工具”>“MetaQuotes语言编辑器”(或按下F4键),打开MetaEditor。 2. 创建新的Expert Advisor(EA):在MetaEditor中,选择“文件”>“新建”>“Expert Advisor”,或使用快捷键Ctrl+N。这将打开一个新的源代码窗口。 3. 编写EA代码:在源代码窗口中,你可以编写自己的EA代码。MetaQuotes语言MQL)是一种类似C的编程语言,用于编写MT4 EA。你可以使用各种函数和指令来定义交易逻辑、执行操作等。 以下是一个简单的示例代码,用于在MT4中创建一个基本的移动止损EA: ```c //+------------------------------------------------------------------+ //| MyEA.mq4 | //| Copyright 2021, YourName | //| https://www.yourwebsite.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2021, YourName" #property link "https://www.yourwebsite.com" // 输入参数 extern double StopLoss = 100.0; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int init() { return (0); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { return (0); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { // 获取当前价格 double currentPrice = MarketInfo(Symbol(), MODE_BID); // 设置止损价格 double stopLossPrice = currentPrice - StopLoss * Point; // 设置止损订单 int ticket = OrderSend(Symbol(), OP_SELL, 0.1, currentPrice, 0, stopLossPrice, 0, "MyEA", MagicNumber, 0, Red); if (ticket < 0) { Print("无法设置止损订单!错误代码:", GetLastError()); } } ``` 4. 编译和安装EA:在MetaEditor中,选择“文件”>“编译”(或按下F7键)来编译你的EA代码。如果没有错误,你将在底部的“日志”选项卡中看到“编译成功”的消息。然后,你可以将EA安装到MT4平台中。 5. 在MT4上测试EA:打开MT4平台,选择你要运行EA的图表。然后,从“导航器”窗口中的“专家顾问”部分中拖动你的EA到图表上。确保已启用自动交易,并在需要的时候调整EA的输入参数。 这只是一个入门级的教程,帮助你了解如何编写和安装MT4 EA。如果你想深入学习MT4 EA编程,建议阅读更多相关文档和教程,学习更多高级的概念和技术。祝你编程愉快!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值