//+------------------------------------------------------------------+
//| sniper.mq4 |
//| Copyright 2017, MetaQuotes Software Corp. |
//| 史上最好的一次战绩,盈利比7.25。19个月收52万。 |
//| 使用点差、使用百分比、使用点差倍数。
//| ATR震荡指标,反映市场震荡范围,用于确定止损价位。
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link "http://www.mql5.com"
#property version "1.00"
#property strict
#define MAGICMA 2017303
//--- input parameters
input double Lots = 0.1;//最低手数
input int LossRate = 2;//失败翻倍
//input double myLossStop = 0.05;//止损点数
//input double myTakeProfit = 0.06;//止盈点数
//input double mycloseorderfrofit = 0.07;//平仓点数
input double myLossStopB = 5;//止损倍于点差
input double myTakeProfitB = 6;//止盈倍于点差
input double mycloseorderfrofitB = 7;//平仓倍于点差
//double Lots = 0.1;//最小单手数
int myLossStop = 0;//止损点数
int myTakeProfit = 0;//止盈点数
int mycloseorderfrofit = 0;//平仓点数
double mySPREAD = 0;
double myMINLOT = 0;//最小允许标准手数
double myMAXLOT = 0;//最大允许标准手数
double myiatr = 0;//ATR指标
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- create timer
EventSetTimer(60); //决定定时器事件发生频率的秒数
mySPREAD = MarketInfo(Symbol(),MODE_SPREAD);//商品点差
myMINLOT = MarketInfo(Symbol(),MODE_MINLOT);//最小允许标准手数
myMAXLOT = MarketInfo(Symbol(),MODE_MAXLOT);//最大允许标准手数
Print("------------------------------------------------>start");
string strcom = "交易公司名称:["+AccountCompany()+"] 商品名称:["+Symbol()+"]商品点差:["+DoubleToStr(MarketInfo(Symbol(),MODE_SPREAD),5)+"] "+" 平仓点差:["+DoubleToStr(MarketInfo(Symbol(),MODE_STOPLEVEL),3)+"]杠杆比例:["+AccountLeverage()+"] "+" 最";
Print(strcom);
iSetLable("lablecom",strcom,5,40,10,"Verdana",Olive);
string strlot = "杠杆比例:["+AccountLeverage()+"] "+" 最小允许标准手数["+DoubleToStr(myMINLOT,4)+"] "+" 最大允许标准手数:["+DoubleToStr(myMAXLOT,4)+"]";
iSetLable("lablelot",strlot,5,40,10,"Verdana",Olive);
//myLossStop = spread* myLossStopB;
//myTakeProfit = spread*myTakeProfitB;
mycloseorderfrofit = mySPREAD*mycloseorderfrofitB;
if(myMAXLOT/myMINLOT>=1000){
Print(strlot);
}else{
string msg = "手数空间太小,不建议使用本EA。";
string title = "交易提示窗口";
int MsgBoxInfo=MessageBox(msg,title,MB_OK|MB_ICONWARNING);
PlaySound("alert.wav");
printf("message:"+MsgBoxInfo);
return (INIT_FAILED);
}
myiatr = iATR(NULL,0,28,0);
string s = "mySPREAD*Point*10/Close[0]:["+DoubleToStr(mySPREAD*Point/Close[0]*10,4)+"] < iATR/Close[0]:["+DoubleToStr(myiatr/Close[0],4)+"]";
iSetLable("lable20b",s,5,100,10,"Verdana",Olive);
if(myiatr>mySPREAD*Point*10){
Print(s);
}
else{//波幅太小
string str = "波幅太小,不建议使用本EA";
string title = "交易提示窗口";
int MsgBoxInfo=MessageBox(str,title,MB_OK|MB_ICONWARNING);
Print(str);
return (INIT_FAILED);
}
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
double i=0.1;
string style = "Sell";
double oldOrderProfit = 0;
int oldOrderID = -1;
double bmyLossStop = 0; //DoubleToStr(myiatr/Close[0],4)
double bmyTakeProfit = 0;
void OnTick() //每来一个价格运行一次
{
ObjectDelete("Askline");
OrderSelect(OrdersTotal()-1,SELECT_BY_POS);//选当前订单
//显示市场信息
iSetLable("lable0","星期"+DayOfWeek()+" 市场时间:"+Year()+"-"+Month()+"-"+Day()+" "+ Hour()+":"+Minute()+":"+Seconds(),200,0,9,"Verdana",Red);
iSetLable("lable1","当前订单盈亏:"+DoubleToStr(OrderProfit(),2),5,20,10,"Verdana",Olive);
iSetLable("lable2","当前价格:"+DoubleToStr(Close[0],4),5,40,10,"Verdana",Olive);
string s = "当前点差:"+DoubleToStr(mySPREAD,4)+" 价差:"+DoubleToStr(mySPREAD*Point,4)+"小数位数:"+DoubleToStr(MarketInfo(Symbol(),MODE_DIGITS));
iSetLable("lable20",s,5,60,10,"Verdana",Olive);
myiatr = iATR(NULL,0,14,0);
s = "mySPREAD*Point*10/Close[0]:["+DoubleToStr(mySPREAD*Point/Close[0]*10,4)+"] < iATR/Close[0]:["+DoubleToStr(myiatr/Close[0],4)+"]";
iSetLable("lable20b",s,5,100,10,"Verdana",Olive);
Print(s);
//iDrawLine(13,Close[13]+50*Point,6,Close[6]+50*Point);//从第13个蜡烛的收盘价到第6个蜡烛的收盘间画一个绿色虚线
//Print("idrawLine:"+DoubleToStr(Close[0],2)+" Ask:"+DoubleToStr(Ask,2)+" Bid:"+DoubleToStr(Bid,2));
//画条点差线
string HLineBarString="Askline";
ObjectCreate(HLineBarString,OBJ_HLINE,0,Time[0],Ask);//Close[0]+0.050
ObjectSet(HLineBarString,OBJPROP_COLOR,Red);
ObjectSet(HLineBarString,OBJPROP_BACK,false);
if(Bars<100 || IsTradeAllowed()==false)
return;
//---
int myHistoryOrderTotal=OrdersHistoryTotal();//历史订单
int myOrdersTotal = OrdersTotal();//开仓数量
//Print("HistoryOrderTotal:["+DoubleToStr(myHistoryOrderTotal,0)+"] "+"OrdersTotal:["+DoubleToStr(myOrdersTotal,0)+"] "+"OrderProfit:["+DoubleToStr(OrderProfit(),3)+"]");
//Print("OrderProfit()):OrderLots():"+DoubleToStr(OrderProfit(),3)+":"+OrderLots());
//新开仓 ,开仓数为0或者本商品的开仓数量为零。,查找开仓中有没有自己的单。
int myOrders = 0;
double myOldOrderProfit = 0;
double myOldLot = myMINLOT;
//查询自己的单子的个数
if(myOrdersTotal==0) myOrders = 0;
else if(myOrdersTotal>0){
for(int MSLCnt=myOrdersTotal-1;MSLCnt>=0;MSLCnt--){
if (OrderSelect(MSLCnt,SELECT_BY_POS)==true||OrderMagicNumber()==MAGICMA || OrderSymbol()==Symbol()){
//有本产品的单
myOrders++;
}
}
}
if(myOrders==0){
//做多、做空
if(MathRand()%2==0){
style = "Buy";
}else style = "Sell";
//查查历史单中有没有单。
if(myHistoryOrderTotal>0){
for(int MSLCnt=myHistoryOrderTotal-1;MSLCnt>=0;MSLCnt--){
if (OrderSelect(MSLCnt,SELECT_BY_POS,MODE_HISTORY)==true && OrderMagicNumber()==MAGICMA && OrderSymbol()==Symbol()){
//有本产品的单
oldOrderID = MSLCnt;
myOldOrderProfit = OrderProfit();//盈利
myOldLot = OrderLots();//手数
break;
}
}
}
//手数
if (myOldOrderProfit>=0) i=myMINLOT;
else i = myOldLot*LossRate;
//止损
bmyLossStop = DoubleToStr(myiatr,4);
bmyTakeProfit = DoubleToStr(myiatr*1.2,4);
//下单
if(i<myMAXLOT){
iOpenOrdersByATR(style,i,bmyLossStop,bmyTakeProfit);
}
else{
double ii=i;
for(ii;ii>myMAXLOT;ii=ii-myMAXLOT-1){
iOpenOrdersByATR(style,myMAXLOT-1,bmyLossStop,bmyTakeProfit);
}
iOpenOrdersByATR(style,ii,bmyLossStop,bmyTakeProfit);
}
}else if(myOrders>0 && MathAbs(Close[0]-OrderOpenPrice())>mySPREAD*Point*10){
//else if(OrdersTotal()>0&&MathAbs(Close[0]-OrderOpenPrice())>Close[0]*mycloseorderfrofit){
//平仓
iCloseOrders("All");
//追加止损
//iMoveStopLoss(spread);
}
}
/* 函数:在屏幕上显示标签 参数说明:LableName:标签名称;LableDoc:文本内容;LableX:标签X位置;LableY:标签Y位置;DocSize:文本字号;DocStyle:文本字体;DocColor:文本颜色 */
void iSetLable(string LableName,string LableDoc,int LableX,int LableY, int DocSize,string DocStyle,color DocColor) {
ObjectCreate(LableName, OBJ_LABEL, 0, 0, 0);
ObjectSetText(LableName,LableDoc,DocSize,DocStyle,DocColor);
ObjectSet(LableName, OBJPROP_XDISTANCE, LableX);
ObjectSet(LableName, OBJPROP_YDISTANCE, LableY);
}
/* 点数,函数:新单开仓 参数说明: 开仓类型:Buy买入订单、Sell卖出订单、 myLots开仓量、myLossStop止损点数、myTakeProfit止盈点数 */
void iOpenOrders(string myType,double myLots,int myLossStop,int myTakeProfit) {
int mySPREAD=MarketInfo(Symbol(),MODE_SPREAD);//获取市场滑点
double BuyLossStop=Ask-myLossStop*Point;
double BuyTakeProfit=Ask+myTakeProfit*Point;
double SellLossStop=Bid+myLossStop*Point;
double SellTakeProfit=Bid-myTakeProfit*Point;
if (myLossStop<=0){//如果止损参数为0
BuyLossStop=0;
SellLossStop=0;
} if (myTakeProfit<=0){//如果止赢参数为0
BuyTakeProfit=0;
SellTakeProfit=0;
} if (myType=="Buy"){
OrderSend(Symbol(),OP_BUY,myLots,Ask,mySPREAD,BuyLossStop,BuyTakeProfit,NULL,MAGICMA);
//iDrawSign("Buy",Ask);//在第5个蜡烛收盘价上画一个买入箭头
}
if (myType=="Sell"){
OrderSend(Symbol(),OP_SELL,myLots,Bid,mySPREAD,SellLossStop,SellTakeProfit,NULL,MAGICMA);
//iDrawSign("Sell",Bid);
}
}
/* %,函数:新单开仓 参数说明: 开仓类型:Buy买入订单、Sell卖出订单、 myLots开仓量、myLossStop止损点数、myTakeProfit止盈点数 */
void iOpenOrdersByProsion(string myType,double myLots,double myLossStop,double myTakeProfit) {
int mySPREAD=MarketInfo(Symbol(),MODE_SPREAD);//获取市场滑点
double BuyLossStop=Ask*(1-myLossStop);
double BuyTakeProfit=Ask*(1+myTakeProfit);
double SellLossStop=Bid*(1+myLossStop);
double SellTakeProfit=Bid*(1-myTakeProfit);
if (myLossStop<=0){//如果止损参数为0
BuyLossStop=0;
SellLossStop=0;
} if (myTakeProfit<=0){//如果止赢参数为0
BuyTakeProfit=0;
SellTakeProfit=0;
} if (myType=="Buy"){
OrderSend(Symbol(),OP_BUY,myLots,Ask,mySPREAD,BuyLossStop,BuyTakeProfit,NULL,MAGICMA);
}
if (myType=="Sell"){
OrderSend(Symbol(),OP_SELL,myLots,Bid,mySPREAD,SellLossStop,SellTakeProfit,NULL,MAGICMA);
}
}
/* ATR,函数:新单开仓 参数说明: 开仓类型:Buy买入订单、Sell卖出订单、 myLots开仓量、myLossStop止损点数、myTakeProfit止盈点数 */
void iOpenOrdersByATR(string myType,double myLots,double myLossStop,double myTakeProfit){
int mySPREAD=MarketInfo(Symbol(),MODE_SPREAD);//获取市场滑点
double BuyLossStop=Ask-myLossStop;
double BuyTakeProfit=Ask+myTakeProfit;
double SellLossStop=Bid+myLossStop;
double SellTakeProfit=Bid-myTakeProfit;
//Print("set lost :"+DoubleToStr(Ask,4)+"-"+DoubleToStr(myLossStop,4)+"="+DoubleToStr(BuyLossStop,4));
if (myLossStop<=0){//如果止损参数为0
BuyLossStop=0;
SellLossStop=0;
} if (myTakeProfit<=0){//如果止赢参数为0
BuyTakeProfit=0;
SellTakeProfit=0;
} if (myType=="Buy"){
OrderSend(Symbol(),OP_BUY,myLots,Ask,mySPREAD,BuyLossStop,BuyTakeProfit,NULL,MAGICMA);
Print(Symbol()+"myLots:"+myLots+"Ask:"+Ask+" mySPREAD:"+mySPREAD+" BuyLossStop:"+BuyLossStop+" BuyTakeProfit:"+BuyTakeProfit);
}
if (myType=="Sell"){
OrderSend(Symbol(),OP_SELL,myLots,Bid,mySPREAD,SellLossStop,SellTakeProfit,NULL,MAGICMA);
Print(Symbol()+"myLots:"+myLots+"Bid:"+Bid+" mySPREAD:"+mySPREAD+" SellLossStop:"+SellLossStop+" SellTakeProfit:"+SellTakeProfit);
}
ShowError();
}
/* 函数:持仓单平仓 平仓类型:Buy多头订单、Sell空头订单、Profit盈利订单、Loss亏损订单、All全部订单 */
int CO_cnt;//订单计数器
void iCloseOrders(string myType) {
if (OrderSelect(OrdersTotal()-1,SELECT_BY_POS)==false) return;//选择当前持仓订单
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) return;
if (myType=="All") {
for(CO_cnt=OrdersTotal();CO_cnt>=0;CO_cnt--) {
if(OrderSelect(CO_cnt,SELECT_BY_POS)==false) continue;
else OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);
}
oldOrderID = CO_cnt;//保存最后关闭的订单号
}
if (myType=="Buy"){//平掉所有多头订单
for(CO_cnt=OrdersTotal();CO_cnt>=0;CO_cnt--) {
if(OrderSelect(CO_cnt,SELECT_BY_POS)==false) continue;
else if (OrderType()==0) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);
}
}
if (myType=="Sell"){//平掉所有空头订单
for(CO_cnt=OrdersTotal();CO_cnt>=0;CO_cnt--) {
if(OrderSelect(CO_cnt,SELECT_BY_POS)==false) continue;
else if (OrderType()==1) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);
}
}
if (myType=="Profit"){//平掉所有盈利订单
for(CO_cnt=OrdersTotal();CO_cnt>=0;CO_cnt--) {
if(OrderSelect(CO_cnt,SELECT_BY_POS)==false) continue;
else if (OrderProfit()>0) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);
}
}
if (myType=="Loss") {
for(CO_cnt=OrdersTotal();CO_cnt>=0;CO_cnt--) {
if(OrderSelect(CO_cnt,SELECT_BY_POS)==false) continue;
else if (OrderProfit()<0) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);
}
}
}
/* 函数:两点之间画线 参数说明:myFirstTime第一点时间,myFirstPrice第一点价格,mySecondTime第二点时间,mySecondPrice第二点价格 */
int LineNo=0;
void iDrawLine (int myFirstTime,double myFirstPrice,int mySecondTime,double mySecondPrice) {
string myObjectName="Line"+LineNo;
ObjectCreate(myObjectName,OBJ_TREND,0,myFirstTime,myFirstPrice,mySecondTime,mySecondPrice);
ObjectSet(myObjectName,OBJPROP_COLOR,Red);
ObjectSet(myObjectName,OBJPROP_STYLE,STYLE_DOT);
ObjectSet(myObjectName,OBJPROP_WIDTH, 1);
ObjectSet(myObjectName,OBJPROP_BACK,false);
ObjectSet(myObjectName,OBJPROP_RAY,false);
LineNo++;
}
/*
函数:标注符号 红箭头为卖出,绿箭头为买入,红绿圆圈为其他标记
参数说明:
mySignal变量包括Buy-买入箭头
Sell-卖出箭头
GreenMark-绿色圆圈
RedMark-红色圆圈
myPrice当前价格,符号标注位
*/
void iDrawSign(string mySignal,double myPrice) {
if (mySignal=="Buy") {
ObjectCreate("BuyPoint-"+Time[0],OBJ_ARROW,0,Time[0],myPrice);
ObjectSet("BuyPoint-"+Time[0],OBJPROP_COLOR,Green);
ObjectSet("BuyPoint-"+Time[0],OBJPROP_ARROWCODE,241);
} if (mySignal=="Sell") {
ObjectCreate("SellPoint-"+Time[0],OBJ_ARROW,0,Time[0],myPrice);
ObjectSet("SellPoint-"+Time[0],OBJPROP_COLOR,Red);
ObjectSet("SellPoint-"+Time[0],OBJPROP_ARROWCODE,242);
} if (mySignal=="GreenMark") {
ObjectCreate("GreenMark-"+Time[0],OBJ_ARROW,0,Time[0],myPrice);
ObjectSet("GreenMark-"+Time[0],OBJPROP_COLOR,Green);
ObjectSet("GreenMark-"+Time[0],OBJPROP_ARROWCODE,162);
} if (mySignal=="RedMark") {
ObjectCreate("RedMark-"+Time[0],OBJ_ARROW,0,Time[0],myPrice);
ObjectSet("RedMark-"+Time[0],OBJPROP_COLOR,Red);
ObjectSet("RedMark-"+Time[0],OBJPROP_ARROWCODE,162);
}
}
/*
函数:移动止损
参数说明:myStopLoss 预设止损点数
功能说明:遍历所有持仓订单,当持仓单获利达到止损点数时,修改止损价位
iMoveStopLoss(25);
*/
void iMoveStopLoss(int myStopLoss){
int MSLCnt;//订单计数器
if (OrderSelect(OrdersTotal()-1,SELECT_BY_POS)==false) return;//选择当前订单
if (OrdersTotal()>0){
for(MSLCnt=OrdersTotal();MSLCnt>=0;MSLCnt--){
if (OrderSelect(MSLCnt,SELECT_BY_POS)==false) continue;
else{
if (OrderProfit()>0 && OrderType()==0 && ((Close[0]-OrderStopLoss())>((2*myStopLoss)*Point))){
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*myStopLoss,OrderTakeProfit(),0);
Print("===========>");
}
if (OrderProfit()>0 && OrderType()==1 && ((OrderStopLoss()-Close[0])>((2*myStopLoss)*Point))){
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*myStopLoss,OrderTakeProfit(),0);
Print("============>>");
}
}
}
}
}
void ShowError()
{
string description;
int err=GetLastError();
switch(err)
{
case 0: return;
case 1: description="没有错误返回"; break;
case 2: description="没有错误返回,但结果不明"; break;
case 3: description="一般错误"; break;
case 4: description="无效交易参量"; break;
case 5: description="交易服务器繁忙"; break;
case 6: description="客户终端旧版本"; break;
case 7: description="没有连接服务器"; break;
case 8: description="没有权限"; break;
case 9: description="请求国语频繁"; break;
case 64: description="交易运行故障"; break;
case 65: description="账户禁止"; break;
case 128: description="无效超时"; break;
case 129: description="无效价格"; break;
case 130: description="无效停止"; break;
case 131: description="无效交易量"; break;
case 132: description="市场关闭"; break;
case 133: description="交易被禁止"; break;
case 134: description="资金不足"; break;
case 135: description="价格改变"; break;
case 136: description="开价"; break;
case 137: description="经纪繁忙"; break;
case 138: description="重新开价"; break;
case 139: description="订单被锁定"; break;
case 140: description="只允许看涨仓位"; break;
case 141: description="过多请求"; break;
case 145: description="因为过于接近市场,修改被否定"; break;
case 146: description="交易文本已满"; break;
case 147: description="时间周期被经纪否定"; break;
default : description="默认错误"; break;
}
string field=" ";
string msg="错误码:#"+err+" "+description+field;
string title="下单错误"; if (AccountNumber()>0)title=AccountNumber()+": "+title;
MessageBox(msg,title,MB_OK|MB_ICONERROR);
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
}
//+------------------------------------------------------------------+
//| Tester function |
//+------------------------------------------------------------------+
double OnTester()
{
//---
double ret=0.0;
//---
//---
return(ret);
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
//---
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//取消指标后,删除图表的对象
//ObjectsDeleteAll(0);
//--- destroy timer
EventKillTimer();
}