1. 全平仓函数
2.设置按钮 给按钮添加功能的函数
//---------------------初始化函数 创建按钮
int OnInit()
{
//---Print("EA需求请联系QQ601681702");
Print("价格童叟无欺");
ObjectCreate(0,"BUY",OBJ_BUTTON,0,0,0);
ObjectSetInteger(0,"BUY",OBJPROP_XDISTANCE,ChartGetInteger(0,CHART_WIDTH_IN_PIXELS)-100);
ObjectSetInteger(0,"BUY",OBJPROP_YDISTANCE,50);
ObjectSetString(0,"BUY",OBJPROP_TEXT,"Buy");
ObjectCreate(0,"SELL",OBJ_BUTTON,0,0,0);
ObjectSetInteger(0,"SELL",OBJPROP_XDISTANCE,ChartGetInteger(0,CHART_WIDTH_IN_PIXELS)-100);
ObjectSetInteger(0,"SELL",OBJPROP_YDISTANCE,80);
ObjectSetString(0,"SELL",OBJPROP_TEXT,"Sell");
//---
ObjectSetInteger(0,"BUY",OBJPROP_XDISTANCE,X_SIZE);
ObjectSetInteger(0,"BUY",OBJPROP_YDISTANCE,Y_SIZE);
ObjectSetInteger(0,"SELL",OBJPROP_XDISTANCE,X_SIZE);
ObjectSetInteger(0,"SELL",OBJPROP_YDISTANCE,Y_SIZE+30);
return(0);
}
//----给图表上的元素添加功能-------------------------------------------
void OnChartEvent(const int id,
const long &lparam, //长整形的参数
const double &dparam, //double类型的参数
const string &sparam) //字符串类型的参数
{
//--- Check the event by pressing a mouse button
double ticket1=0;
//--- If you click on the object with the name buttonID 点一下BUY按钮就会对当前货币现价买入,
if(id==CHARTEVENT_OBJECT_CLICK&&sparam=="BUY"&&ObjectGetInteger(0,"BUY",OBJPROP_STATE))
{
//--- State of the button - pressed or not
bool selected1=ObjectGetInteger(0,"BUY",OBJPROP_STATE);
//--- log a debug message
Print("BUY Button pressed = ",selected1);
ticket1=OrderSend(Symbol(),OP_BUY,Lots,NormalizeDouble(Ask,Digits),3,NormalizeDouble(Ask-StopLoss1*Point,Digits),
0,"My EA",Magic,0,Red);//NormalizeDouble(Ask+TakeProfit1*Point,Digits)
ObjectSetInteger(0,"BUY",OBJPROP_STATE,0);
}
if(id==CHARTEVENT_OBJECT_CLICK&&sparam=="SELL"&&ObjectGetInteger(0,"SELL",OBJPROP_STATE))
{
//--- State of the button - pressed or not
bool selected2=ObjectGetInteger(0,"SELL",OBJPROP_STATE);
//--- log a debug message
Print("SELL Button pressed = ",selected2);
ticket1=OrderSend(Symbol(),OP_SELL,Lots,NormalizeDouble(Bid,Digits),3,NormalizeDouble(Bid+StopLoss1*Point,Digits),
0,"My EA",Magic,0,Green);//NormalizeDouble(Ask+TakeProfit1*Point,Digits)
ObjectSetInteger(0,"SELL",OBJPROP_STATE,0);
}
}
3. 计算服务器和GMT时间平移
//+------------------------------------------------------------------+
//| 计算服务器和GMT时间平移.mq4 |
//| Copyright 2013 By Xiyu QQ:19646 |
//| www.520FX.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013 By Xiyu QQ:19646"
#property link "www.520FX.com"
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
iGMTDifference();
return(0);
}
//+------------------------------------------------------------------+
/*
函 数
输出参量:
输出参数:
算 法:
*/
double iGMTDifference()
{
int GMT=StrToDouble(iWorldTime(0,0)); //获得格林威治时间
int ServerTime=StrToDouble(TimeToStr(TimeCurrent(),TIME_MINUTES)); //获得MT4服务器时间
iDisplayInfo("Author", "作者:熙羽 520FX",1,10,15,8,"Times New Roman",SlateGray);
iDisplayInfo("wing",CharToStr(175),1,20,25,30,"Wingdings", Gold);
Alert("历史数据中心平移时差为: "+(ServerTime-GMT));
}
/*
函 数:计算当地时间
输入参数:int myTimeZone 本地时区 string myLocalSummerTime 夏令时
输出参数:指定时区时间,格式:hh:mm:ss
算 法:获取本地计算机时间,北京时间,计算GMT时间
根据夏令时调整
*/
string iWorldTime(int myTimeZone, string myLocalSummerTime)
{
string myTime; //函数返回参数
//计算夏令时起止
int mySummerStartTime = StrToTime(StringSubstr(myLocalSummerTime, 0, 16));
int mySummerEndTime = StrToTime(StringSubstr(myLocalSummerTime, 17, 16));
//
string myHH,myMM,mySS;
int GMT = TimeHour(Time[0])-2; //计算GMT时间
int myLocalTime = GMT+myTimeZone;
if (myLocalTime < 0) myLocalTime = myLocalTime+24;
if (myLocalTime >= 24) myLocalTime = myLocalTime-24;
myHH = DoubleToStr(myLocalTime,0);
if (StringLen(myHH) == 1) myHH = "0"+myHH;
myMM = DoubleToStr(TimeMinute(TimeLocal()),0);
if (StringLen(myMM) == 1) myMM = "0"+myMM;
mySS = DoubleToStr(TimeSeconds(TimeLocal()),0);
if (StringLen(mySS) == 1) mySS = "0"+mySS;
myTime = myHH+":"+myMM;
if (StrToTime(myTime) > mySummerStartTime && StrToTime(myTime) < mySummerEndTime)
{
myHH = DoubleToStr(StrToDouble(myHH)+1,0);
if (StringLen(myHH) == 1) myHH = "0"+myHH;
myTime = myHH+":"+myMM+"*";
}
return (myTime);
}
/*
4.函 数:在屏幕上显示文字标签
输入参数:标签名称 文本内容 文本显示角 标签X位置坐标 标签Y位置坐标 文本字号 文本字体 文本颜色
输出参数:
算 法:
*/
void iDisplayInfo(string LableName,string LableDoc,int Corner,int LableX,int LableY,int DocSize,string DocStyle,color DocColor)
{
if (Corner == -1) return(0);
ObjectCreate(LableName, OBJ_LABEL, 0, 0, 0);
ObjectSetText(LableName, LableDoc, DocSize, DocStyle,DocColor);
ObjectSet(LableName, OBJPROP_CORNER, Corner);
ObjectSet(LableName, OBJPROP_XDISTANCE, LableX);
ObjectSet(LableName, OBJPROP_YDISTANCE, LableY);
}
5. 获取最近的订单
//---get the last order's open price. the order latest, the cnt bigger.
if (LastPrice==0)
{
for(cnt=0;cnt
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);// cnt=1 corresponding the first order,cnt=2 corresponding the second order,
mode=OrderType();
if (OrderSymbol()==Symbol())
{
LastPrice=OrderOpenPrice();
if (mode==OP_BUY) { myOrderType=2; }
if (mode==OP_SELL) { myOrderType=1; }
}
}
}
6. 马丁模块,
//--- 如果之前开的是卖单(myOrderType==1),允许继续开单,且当前订单比之前订单的开单价高了Pips*Point个点,那么继续做空mylotsi,
//---通过continueOpening控制开单的总量,Pips是马丁间隔
if (myOrderType==1 && ContinueOpening)
{
if ((Bid-LastPrice)>=Pips*Point || OpenOrders<1)
{
SellPrice=Bid;
LastPrice=0;
// 如果TakeProfit!=0,则将tp定义为bid-takeProfit
if (TakeProfit==0) { tp=0; }
else { tp=SellPrice-TakeProfit*Point; }
if (InitialStop==0) { sl=0; }
else { sl=SellPrice+InitialStop*Point; }
if (OpenOrders!=0)
{
mylotsi=lotsi;
for(cnt=1;cnt<=OpenOrders;cnt++)
{
// 如果允许的总订单数大于12,则下一次开仓量是前一次的1.5倍
if (MaxTrades>12) { mylotsi=NormalizeDouble(mylotsi*1.5,2); }
// 如果允许开的总订单数小于等于12,则下一次开单量是前一次的两倍
else { mylotsi=NormalizeDouble(mylotsi*2,2); }
}
} else { mylotsi=lotsi; }
if (mylotsi>100) { mylotsi=100; }
OrderSend(Symbol(),OP_SELL,mylotsi,SellPrice,slippage,sl,tp,NULL,0,0,Red);
return(0);
}
}
if (myOrderType==2 && ContinueOpening)
{
if ((LastPrice-Ask)>=Pips*Point || OpenOrders<1)
{
BuyPrice=Ask;
LastPrice=0;
if (TakeProfit==0) { tp=0; }
else { tp=BuyPrice+TakeProfit*Point; }
if (InitialStop==0) { sl=0; }
else { sl=BuyPrice-InitialStop*Point; }
if (OpenOrders!=0) {
mylotsi=lotsi;
for(cnt=1;cnt<=OpenOrders;cnt++)
{
if (MaxTrades>12) { mylotsi=NormalizeDouble(mylotsi*1.5,2); }
else { mylotsi=NormalizeDouble(mylotsi*2,2); }
}
} else { mylotsi=lotsi; }
if (mylotsi>100) { mylotsi=100; }
OrderSend(Symbol(),OP_BUY,mylotsi,BuyPrice,slippage,sl,tp,NULL,0,0,Blue);
return(0);
}
}
7. profit 计算模块
//赋值给变量profit,盈利计算模块
for(cnt=0;cnt
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol()==Symbol())
{
LastTicket=OrderTicket();
if (OrderType()==OP_BUY) { LastType=OP_BUY; }
if (OrderType()==OP_SELL) { LastType=OP_SELL; }
LastClosePrice=OrderClosePrice();
LastLots=OrderLots();
if (LastType==OP_BUY)
{
//Profit=Profit+(Ord(cnt,VAL_CLOSEPRICE)-Ord(cnt,VAL_OPENPRICE))*PipValue*Ord(cnt,VAL_LOTS);
if (OrderClosePrice()
if (OrderClosePrice()>OrderOpenPrice())
{ Profit=Profit+(OrderClosePrice()-OrderOpenPrice())*OrderLots()/Point; }
}
if (LastType==OP_SELL)
{
//Profit=Profit+(Ord(cnt,VAL_OPENPRICE)-Ord(cnt,VAL_CLOSEPRICE))*PipValue*Ord(cnt,VAL_LOTS);
if (OrderClosePrice()>OrderOpenPrice())
{ Profit=Profit-(OrderClosePrice()-OrderOpenPrice())*OrderLots()/Point; }
if (OrderClosePrice()
}
//Print(Symbol,":",Profit,",",LastLots);
}
}
Profit=Profit*PipValue;
text2="Profit: $"+DoubleToStr(Profit,2)+" +/-";
用comment可以显示text2