bihaibaiy 发表于 2024-1-21 23:59:20

均线趋势交易,带移动止损功能

               
                                       
#property copyright "dzsq"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

using System.ComponentModel;
using System.Drawing;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System;

extern int longMa = 20;   //长均线
extern int shortMa = 10;//短均线
extern int callBachMa = 30;//回踩线

extern int stop = 300; //止损
extern int limit = 900; //止盈
extern double lots = 1;//手数
extern double movestop = 500; //移动止损
extern int magic = 12345;

bool isCrossBuy = false; //是否交叉
bool isCrossSell = false;
//是否会调到支撑线
bool isSellCallback = false;
bool isBuyCallback = false;
//是否开仓
bool isBuyOpened = false;
bool isSellOpened = false;

datetime buytime = 0;
datetime selltime = 0;
//+------------------------------------------------------------------+
//| Expert initialization function                                 |
//+------------------------------------------------------------------+
int OnInit()
{
    //---

    //---
    return (INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    //---

}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    //向下取整
    //lots = MathFloor(AccountBalance()/10000);
    //获取所有的均线价格,长期,短期,回调
    double longmaPrice1 = iMA(NULL, 0, longMa, 0, MODE_SMA, PRICE_CLOSE, 1);
    double longmaPrice2 = iMA(NULL, 0, longMa, 0, MODE_SMA, PRICE_CLOSE, 2);
    double shortmaPrice1 = iMA(NULL, 0, shortMa, 0, MODE_SMA, PRICE_CLOSE, 1);
    double shortmaPrice2 = iMA(NULL, 0, shortMa, 0, MODE_SMA, PRICE_CLOSE, 2);
    double callBackPrice = iMA(NULL, 0, callBachMa, 0, MODE_SMA, PRICE_CLOSE, 0);

    //均线交叉多头
    if (shortmaPrice1 > longmaPrice1 && shortmaPrice2 < longmaPrice2)
    {
      isCrossBuy = true;
      isCrossSell = false;
      isSellOpened = false;
      isSellCallback = false;
      closesell(Symbol() + "sell", magic);
    }

    //均线交叉空头
    if (shortmaPrice1 < longmaPrice1 && shortmaPrice2 > longmaPrice2)
    {
      isCrossBuy = false;
      isCrossSell = true;
      isBuyOpened = false;
      isBuyCallback = false;
      closebuy(Symbol() + "buy", magic);
    }

    double lowPrice = Low;
    double highPrice = High;
    double openPrice = Open;
    double closePrice = Close;
    //符合做多的条件
    if (!isBuyOpened && isCrossBuy && lowPrice < callBackPrice)
    {
      //time表示当前时间
      if (buytime != Time)
      {   //解决当前K线重复下单的问题
            if (buy(lots, stop, limit, Symbol() + "buy", magic))
            {
                buytime = Time;
                isBuyOpened = true;
            }

      }
    }
    //符合做空的条件
    if (!isSellOpened && isCrossSell && highPrice > callBackPrice)
    {
      //time表示当前时间
      if (selltime != Time)
      {   //解决当前K线重复下单的问题
            if (sell(lots, stop, limit, Symbol() + "sell", magic))
            {

                selltime = Time;
                isSellOpened = true;

            }

      }
    }
    yidongzhisun();
}

//下单函数 针对一个单子
int buy(double lots, double stop, double limit, string comm, int buymagic)
{//手数,止损,止盈,注释,magic
    int ticket = 0;   //订单号
    bool zhaodan = false;//判断有没有重复下单,其实上面已经写过了,这里不写也没关系
    for (int i = 0; i < OrdersTotal(); i++)
    {

      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true)
      {
            string zhushi = OrderComment();
            int magicNumber = OrderMagicNumber();

            if (OrderSymbol() == Symbol() && OrderType() == OP_BUY && zhushi == comm && magicNumber == buymagic)
            {
                zhaodan = true;
                break;
            }
      }
    }
    if (zhaodan == false)
    {
      if (stop != 0 && limit == 0)
      {
            ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, 50, Ask - stop * Point, 0, comm, buymagic, 0, White);
      }
      if (stop == 0 && limit != 0)
            ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, 50, 0, Ask + limit * Point, comm, buymagic, 0, White);
      if (stop == 0 && limit == 0)
            ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, 50, 0, 0, comm, buymagic, 0, White);
      if (stop != 0 && limit != 0)
            ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, 50, Ask - stop * Point, Ask + limit * Point, comm, buymagic, 0, White);
    }
    return (ticket);

}

//下单函数 针对一个单子
int sell(double lots, double stop, double limit, string comm, int sellmagic)
{//手数,止损,止盈,注释,magic
    int ticket = 0;   //订单号
    bool zhaodan = false;//判断有没有重复下单,其实上面已经写过了,这里不写也没关系
    for (int i = 0; i < OrdersTotal(); i++)
    {

      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true)
      {
            string zhushi = OrderComment();
            int magicNumber = OrderMagicNumber();

            if (OrderSymbol() == Symbol() && OrderType() == OP_SELL && zhushi == comm && magicNumber == sellmagic)
            {
                zhaodan = true;
                break;
            }
      }
    }
    if (zhaodan == false)
    {
      if (stop != 0 && limit == 0)
      {
            ticket = OrderSend(Symbol(), OP_SELL, lots, Bid, 50, Bid + stop * Point, 0, comm, sellmagic, 0, White);
      }
      if (stop == 0 && limit != 0)
            ticket = OrderSend(Symbol(), OP_SELL, lots, Bid, 50, 0, Bid - limit * Point, comm, sellmagic, 0, White);
      if (stop == 0 && limit == 0)
            ticket = OrderSend(Symbol(), OP_SELL, lots, Bid, 50, 0, 0, comm, sellmagic, 0, White);
      if (stop != 0 && limit != 0)
            ticket = OrderSend(Symbol(), OP_SELL, lots, Bid, 50, Bid + stop * Point, Bid - limit * Point, comm, sellmagic, 0, White);
    }
    return (ticket);
}
// 平货币
void closebuy(string comm, int magic)
{
    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true)
      {

            if (OrderSymbol() == Symbol() && OrderType() == OP_BUY && OrderComment() == comm && OrderMagicNumber() == magic)
            {
                OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 300, Green);
            }
      }
    }

}
// 平货币
void closesell(string comm, int magic)
{
    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true)
      {

            if (OrderSymbol() == Symbol() && OrderType() == OP_SELL && OrderComment() == comm && OrderMagicNumber() == magic)
            {
                OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 300, Green);
            }
      }
    }

}
//设置移动止损
void yidongzhisun()
{
    for (int i = 0; i < OrdersTotal(); i++)
    {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true)
      {

            if (OrderSymbol() == Symbol() && OrderType() == OP_BUY && OrderMagicNumber() == magic)
            {

                if ((Bid - OrderOpenPrice()) >= Point * movestop)
                {

                  if (OrderStopLoss() < (Bid - Point * movestop) || OrderStopLoss() == 0)
                  {
                        OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * movestop, OrderTakeProfit(), 0, Green);
                  }
                }
            }
            if (OrderSymbol() == Symbol() && OrderType() == OP_SELL && OrderMagicNumber() == magic)
            {

                if ((OrderOpenPrice() - Ask) >= Point * movestop)
                {

                  if (OrderStopLoss() < (Ask + Point * movestop) || OrderStopLoss() == 0)
                  {
                        OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * movestop, OrderTakeProfit(), 0, Green);
                  }
                }
            }
      }
    }
}

//+------------------------------------------------------------------+

ken138888 发表于 2024-1-23 20:10:05

慢慢学习

ken138888 发表于 2024-1-25 13:10:42

Mq5的应该怎样写

hwensen 发表于 2024-1-30 08:54:22

看不懂1111

石头1968 发表于 2024-1-30 13:41:56

均线趋势交易,带移动止损功能,。很不错的源代码。感谢分享!

静观明 发表于 2024-2-2 14:38:59

很不错的源代码。感谢分享

甘草糖 发表于 2024-2-8 22:53:19

很不错的源码,谢谢楼主的慷慨分享。

shllwk 发表于 2024-2-9 22:35:16

很不错的源代码。感谢分享!

ken138888 发表于 2024-2-10 13:03:02

有空我也来写一下

andourson 发表于 2024-2-15 17:27:29

想学,但看不懂

wasa 发表于 2024-2-17 09:50:54

感谢分享,虽然不是很懂这种交易

xinhua123 发表于 2024-2-18 00:07:19

谢谢分享代码

xuxiaobai 发表于 2024-2-24 14:13:35

看到代码就头疼,哈哈

ken138888 发表于 2024-3-7 06:11:34

带移动止损啊

13564043947 发表于 2024-3-24 01:45:30

策略不错

t5530407 发表于 2024-3-30 10:47:59

感谢提供

liubing1986 发表于 2024-4-8 22:38:30

非常仔细

631311675 发表于 2024-4-14 11:22:46

移动止损与市价500点的间距,[微笑],但是它每次tick修改一次,是一小时,还是一秒?

ken138888 发表于 2024-4-14 13:48:46

就是每个TICK啊

wasesdq2 发表于 2024-4-23 10:42:20

这个怎么弄?
页: [1] 2 3 4
查看完整版本: 均线趋势交易,带移动止损功能