参考止损下移的方法。
都给你贴上,我也不会这个,你要不懂找人看吧
#include <MTDinc.mqh>
//+------------------------------------------------------------------+
//| 复盘EA演示: 追踪止损和固定止损止盈
//+------------------------------------------------------------------+
#property copyright "MTDriver复盘专家 chaokes.com"
#property link "http://chaokes.com"
//---- input parameters
extern double 止赢=240;
extern double 止损=168;
extern double 追踪止损=360;//移动止损的具体价格偏移值,如果值为0表示不起作用
extern int Magic=0;
extern string 参数说明="单位为点数";
string id_pre="MTD_zzzs_";
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
deleteOBJ_StartsWith(id_pre);
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
deleteOBJ_StartsWith(id_pre);
//----
return(0);
}
int ids=0;
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
double myPoint=Point;
//Alert(DoubleToStr(myPoint,8));
ids=0;
DrawOneMessage("EA -自动追踪止损运行中 , "+Symbol(),DeepPink,12);
//ids++;
//追踪止损
if(追踪止损>0){
_MoveStop(Magic, 追踪止损*myPoint,0*myPoint);//其中追踪止损单位为点数,追踪止损*myPoint=实际的止损值
DrawOneMessage("追踪止损= "+追踪止损+"点="+DoubleToStr(追踪止损*myPoint,Digits),clrYellow,12);
}else{
DrawOneMessage("追踪止损= "+追踪止损+"点(未启用)",clrGray,12);
}
//设置止赢
if(止赢>0){
_TakeProfit(Magic,止赢*myPoint);
DrawOneMessage("首次止赢= "+止赢+"点="+DoubleToStr(止赢*myPoint,Digits),clrYellow,12);
}else{
DrawOneMessage("首次止赢= "+止赢+"点(未启用)",clrGray,12);
}
//设置止损
if(止损>0){
_StopLoss(Magic,止损*myPoint);
DrawOneMessage("首次止损= "+止损+"点="+DoubleToStr(止损*myPoint,Digits),clrYellow,12);
}else{
DrawOneMessage("首次止损= "+止损+"点(未启用)",clrGray,12);
}
DrawOneMessage("Magic = "+Magic,clrYellow,12);
//----
return(0);
}
//+------------------------------------------------------------------+
//| _MoveStop 移动止损函数 function |
//+------------------------------------------------------------------+
int _MoveStop(int MAGIC, double MOVE,double STEP)//_MoveStop(MAGIC, MOVE);
{
//----
//Alert("追踪止损MOVE="+MOVE);
if (MOVE<=0) return(0);
double MoveStopPrice;
for ( int z = MTDOrdersTotal() - 1; z >= 0; z -- )
{
if ( !MTDOrderSelect( z, SELECT_BY_POS ) )
{
Print("MTDOrderSelect(", z, ",SELECT_BY_POS) - Error #",GetLastError() );
continue;
}
if (MTDOrderSymbol()!=Symbol())continue;
if (MTDOrderMagicNumber() != MAGIC )continue;
switch (MTDOrderType())
{
case OP_BUY :
{
MoveStopPrice=NormalizeDouble(MTDBid()-MOVE,Digits);//
//Alert(MoveStopPrice+" , "+MTDOrderTicket());
if (MoveStopPrice>MTDOrderOpenPrice() && (MoveStopPrice>MTDOrderStopLoss() || MTDOrderStopLoss()==0)){ //追踪止损的价格>开仓价 ,并且追踪止损的价格>当前止损价,就移动止损
if(!MTDOrderModify(MTDOrderTicket(),MTDOrderOpenPrice(),MoveStopPrice+STEP,MTDOrderTakeProfit(),MTDOrderExpiration())) { //
Alert("MoveStop_MTDOrderModify Error #",GetLastError());
return(-1);
}
}
continue;
}
case OP_SELL:
{
MoveStopPrice=NormalizeDouble(MTDAsk()+MOVE,Digits);
//Alert(MTDAsk()+","+MOVE+";"+MoveStopPrice+" , "+MTDOrderTicket());
if (MoveStopPrice<MTDOrderOpenPrice() && (MoveStopPrice<MTDOrderStopLoss() || MTDOrderStopLoss()==0))
{
if(!MTDOrderModify(MTDOrderTicket(),MTDOrderOpenPrice(),MoveStopPrice-STEP,MTDOrderTakeProfit(),MTDOrderExpiration())){ //
Alert("MoveStop_MTDOrderModify Error #",GetLastError());
return(-1);
}
}
continue;
}
default: continue;
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
//| 止盈函数 function |
//+------------------------------------------------------------------+
int _TakeProfit(int MAGIC, double TP)//
{
//----
if (TP<=0) return(0);
double TakeProfit;
int OrdersTotal4TakeProfit=MTDOrdersTotal();
for ( int z = OrdersTotal4TakeProfit - 1; z >= 0; z -- )
{
if ( !MTDOrderSelect( z, SELECT_BY_POS ) )
{
Print("MTDOrderSelect(", z, ",SELECT_BY_POS) - Error #",GetLastError() );
continue;
}
if (MTDOrderSymbol()!=Symbol())continue;
if (MTDOrderMagicNumber() != MAGIC )continue;
switch (MTDOrderType())
{
case OP_BUY :
{
TakeProfit=NormalizeDouble(MTDOrderOpenPrice()+TP,Digits);
if (MTDOrderTakeProfit()==0.0)
{
if(!MTDOrderModify(MTDOrderTicket(),MTDOrderOpenPrice(),MTDOrderStopLoss(),TakeProfit,MTDOrderExpiration()))
{
Alert("TakeProfit_MTDOrderModify Error #",GetLastError());
}
}
continue;
}
case OP_SELL:
{
TakeProfit=NormalizeDouble(MTDOrderOpenPrice()-TP,Digits);
if (MTDOrderTakeProfit()==0.0)
{
if(!MTDOrderModify(MTDOrderTicket(),MTDOrderOpenPrice(),MTDOrderStopLoss(),TakeProfit,MTDOrderExpiration()))
{
Alert("TakeProfit_MTDOrderModify Error #",GetLastError());
}
}
continue;
}
default: continue;
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
//| 止损函数 function |
//+------------------------------------------------------------------+
int _StopLoss(int MAGIC, double SL)//_MoveStop(MAGIC, MOVE);
{
//----
if (SL<=0) return(0);//0表示不执行止损
double StopLoss;
int OrdersTotal4StopLoss=MTDOrdersTotal();
for ( int z = OrdersTotal4StopLoss - 1; z >= 0; z -- )
{
if ( !MTDOrderSelect( z, SELECT_BY_POS ) )
{
Print("MTDOrderSelect(", z, ",SELECT_BY_POS) - Error #",GetLastError() );
continue;
}
if (MTDOrderSymbol()!=Symbol())continue;
if (MTDOrderMagicNumber() != MAGIC )continue;
switch (MTDOrderType())
{
case OP_BUY :
{
StopLoss=NormalizeDouble(MTDOrderOpenPrice()-SL,Digits);
//Alert(DoubleToStr(StopLoss,Digits));
if (MTDOrderStopLoss()==0.0)
{
if(!MTDOrderModify(MTDOrderTicket(),MTDOrderOpenPrice(),StopLoss,MTDOrderTakeProfit(),MTDOrderExpiration()))
{
Alert("StopLoss_MTDOrderModify Error #",GetLastError());
}
}
continue;
}
case OP_SELL:
{
StopLoss=NormalizeDouble(MTDOrderOpenPrice()+SL,Digits);
if (MTDOrderStopLoss()==0.0)
{
if(!MTDOrderModify(MTDOrderTicket(),MTDOrderOpenPrice(),StopLoss,MTDOrderTakeProfit(),MTDOrderExpiration()))
{
Alert("StopLoss_MTDOrderModify Error #",GetLastError());
}
}
continue;
}
default: continue;
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
void Drawlabel(int x, int y,string text, color fcolor=Lime,int fsize=12)
{
string id=id_pre+ids;
string font="Arial";
ObjectDelete(id);
ObjectCreate( id, OBJ_LABEL, 0, 0, 0 );
ObjectSetText(id,text,fsize,font,fcolor);
ObjectSet( id, OBJPROP_XDISTANCE, x );
ObjectSet( id, OBJPROP_YDISTANCE, y );
ObjectSet( id, OBJPROP_BACK, true );
//ObjectSet( id, OBJPROP_CORNER, corner );
ObjectSet( id, OBJPROP_CORNER, 0);
}
void DrawOneMessage(string text, color fcolor,int fsize){
int x=365,y=66;
y=y+20*ids;
Drawlabel(x,y,text,fcolor,fsize);
ids=ids+1;
}
//删除xx开头的对象
void deleteOBJ_StartsWith(string findText){
string ObjName;
//int ObjType;
double OBJ_ARROW_PRICE1=0;
int obj_count=ObjectsTotal();
for (int i = 0; i < obj_count; i++)
{
ObjName = ObjectName(i);
int index=StringFind(ObjName, findText, 0);
if(index==0){
if(ObjectDeleteFromName(ObjName)==true){
i=i-1;
obj_count=ObjectsTotal();
}
}
}
//return (0);
}
//删除具体的某个对象
bool ObjectDeleteFromName(string ObjName){
bool result=true;
int try_again=10;//
while(try_again>0){
if(ObjectDelete(ObjName)==true){
try_again=0;
result=true;
}else{
try_again=try_again-1;
result=false;
}
}
return (result);
}
|