【漆学军】EA编程速成教程(4)修改止损止盈

本课程的目标是给之前下的单子添加止损止盈价。

首选添加外部参数

input int SL=600;      //止损点数
input int TP=200;      //止盈点数

给单子添加止损止盈有两个方法:

一、在下单函数里面带上相应的止损和止盈。

OrderSend函数有11个参数,其中第六个(stoploss)和第七个(takeprofit)分别是止损价和止盈价。

int OrderSend(
string symbol, // symbol
int cmd, // operation
double volume, // volume
double price, // price
int slippage, // slippage
double stoploss, // stop loss
double takeprofit, // take profit
string comment=NULL, // comment
int magic=0, // magic number
datetime expiration=0, // pending order expiration
color arrow_color=clrNONE // color
);

具体使用方法如下:

int ticket=OrderSend(Symbol(),OP_BUY,lots,Ask,3,Ask-SL*Point,Ask+TP*Point,"My order",16384,0,clrGreen);

注意:有些平台下单的时候不允许同时带上止损和止盈,否则会报错,之前的东航金融平台就是,也有的平台要求止损止盈至少要距离当前价格一定的点数,如果设置太小的话,可能造成下单失败。所以,设置止损止盈的方法我们通常使用第二种。

二、下单成功后,通过修改订单设置上止损和止盈。

    修改订单用到的函数是OrderModify,这个函数有6个参数,其中第三个和第四个分别是止损价和止盈价

bool OrderModify(

intticket,// ticket

doubleprice,// price

doublestoploss,// stop loss

doubletakeprofit,// take profit

datetimeexpiration,// expiration

colorarrow_color// color

);

第一个参数ticket是订单编号,订单编号一般是需要通过遍历账户的所有单子来获取,修改止损止盈的全部代码如下:

   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==16384 && OrderType()==OP_BUY)
           {
            if(OrderStopLoss()==0)
              {
               bool res=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-SL*Point,OrderOpenPrice()+TP*Point,0);
               if(res)
                  Print("订单修改成功");
              }
           }
        }
     }

整个EA的全部代码如下:

//+------------------------------------------------------------------+
//|                                                   Test_EA_04.mq4 |
//|                                                             云开 |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "http://www.forexmt4.cn"
#property link      "http://www.forexmt4.cn"

#property description "【漆天编程】 测试EA"
#property description "  "
#property description "这是一款测试EA,作者QQ:80364276"
#property description "  "
#property description "发布时间:2020.04.16"
#property strict
#property icon "//Images//sea.ico"

input double lots=0.1; //交易手数
input int SL=600;      //止损点数
input int TP=200;      //止盈点数

bool isgo=true;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==16384 && OrderType()==OP_BUY)
           {
            if(OrderStopLoss()==0)
              {
               bool res=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-SL*Point,OrderOpenPrice()+TP*Point,0);
               if(res)
                  Print("订单修改成功");
              }
           }
        }
     }
//---
   if(isgo)
     {
      int ticket=OrderSend(Symbol(),OP_BUY,lots,Ask,3,0,0,"My order",16384,0,clrGreen);
      if(ticket<0)
        {
         Print("OrderSend failed with error #",GetLastError());
        }
      else
        {
         isgo=false;
         Print("OrderSend placed successfully");
        }
     }

  }
//+------------------------------------------------------------------+

 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漆学军

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值