均线趋势交易,带移动止损功能
#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);
}
}
}
}
}
}
//+------------------------------------------------------------------+
慢慢学习 Mq5的应该怎样写 看不懂1111 均线趋势交易,带移动止损功能,。很不错的源代码。感谢分享! 很不错的源代码。感谢分享 很不错的源码,谢谢楼主的慷慨分享。 很不错的源代码。感谢分享! 有空我也来写一下 想学,但看不懂 感谢分享,虽然不是很懂这种交易 谢谢分享代码 看到代码就头疼,哈哈 带移动止损啊 策略不错 感谢提供 非常仔细 移动止损与市价500点的间距,[微笑],但是它每次tick修改一次,是一小时,还是一秒? 就是每个TICK啊 这个怎么弄?