我们写一个自动交易程序,逻辑如下 · 下一个多单 · 检查当前盈利情况 · 盈利大于1美金止盈平仓,或者亏损大于1美金止损平仓 · 退出程序 //+------------------------------------------------------------------+ //| QuantSample.mq4 | //| Copyright 2020, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property strict //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- int ticket = 0; if(OrdersTotal() == 0) {//检查当前是否有订单,没有则下单 //send order ticket = OrderSend(Symbol(), OP_BUY, 0.01, MarketInfo(Symbol(), MODE_ASK), 100, 0.0, 0.0, "comment", 1024, 0,0xFFFFFFFF); }
double profit = 0; for(int i = 0; i < OrdersTotal(); i++){ //计算当前盈亏 if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) break; profit = OrderProfit(); }
if(profit > 1.0 || profit < -1.0) { //当前盈亏满足条件则平仓退出 //close order for (int index = OrdersTotal() - 1 ; index >= 0 ; index = index - 1) { if(!OrderSelect(index, SELECT_BY_POS, MODE_TRADES)){ //选择订单 PrintFormat(">>> select failed closing trade <%s> at index %d", Symbol(), index); continue; }
bool close_result = true ; switch(OrderType()) { case OP_BUY : close_result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(), MODE_BID),100,0xFFFFFFFF) ; //关闭订单 break; case OP_SELL : close_result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),100,0xFFFFFFFF) ; break; } } ExpertRemove();//退出EA } } //+------------------------------------------------------------------+ |