-
-
- #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[0];
- double highPrice = High[0];
- double openPrice = Open[0];
- double closePrice = Close[0];
- //符合做多的条件
- if (!isBuyOpened && isCrossBuy && lowPrice < callBackPrice)
- {
- //time[0] 表示当前时间
- if (buytime != Time[0])
- { //解决当前K线重复下单的问题
- if (buy(lots, stop, limit, Symbol() + "buy", magic))
- {
- buytime = Time[0];
- isBuyOpened = true;
- }
-
- }
- }
- //符合做空的条件
- if (!isSellOpened && isCrossSell && highPrice > callBackPrice)
- {
- //time[0] 表示当前时间
- if (selltime != Time[0])
- { //解决当前K线重复下单的问题
- if (sell(lots, stop, limit, Symbol() + "sell", magic))
- {
-
- selltime = Time[0];
- 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);
- }
- }
- }
- }
- }
- }
-
- //+------------------------------------------------------------------+
-
复制代码
|