各位大哥们,请帮小弟写一个自动修改止损价格的EA

 

//+------------------------------------------------------------------+
//| Parabolic.mq4 |
//| Copyright ?2004, MetaQuotes Software Corp. |
//| https://www.metaquotes.net// |
//+------------------------------------------------------------------+
#property copyright "Copyright ?2004, MetaQuotes Software Corp."
#property link "https://www.metaquotes.net//"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Lime
//---- input parameters
extern double Step=0.02;
extern double Maximum=0.2;
//---- buffers
double SarBuffer[];
//----
int save_lastreverse;
bool save_dirlong;
double save_start;
double save_last_high;
double save_last_low;
double save_ep;
double save_sar;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_ARROW);
SetIndexArrow(0,159);
SetIndexBuffer(0,SarBuffer);
//----
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void SaveLastReverse(int last,int dir,double start,double low,double high,double ep,double sar)
{
save_lastreverse=last;
save_dirlong=dir;
save_start=start;
save_last_low=low;
save_last_high=high;
save_ep=ep;
save_sar=sar;
}
//+------------------------------------------------------------------+
//| Parabolic Sell And Reverse system |
//+------------------------------------------------------------------+
int start()
{
static bool first=true;
bool dirlong;
double start,last_high,last_low;
double ep,sar,price_low,price_high,price;
int i,counted_bars=IndicatorCounted();
//----
if(Bars<3) return(0);
//---- initial settings
i=Bars-2;
if(counted_bars==0 || first)
{
first=false;
dirlong=true;
start=Step;
last_high=-10000000.0;
last_low=10000000.0;
while(i>0)
{
save_lastreverse=i;
price_low=Low[i];
if(last_low>price_low) last_low=price_low;
price_high=High[i];
if(last_high<price_high) last_high=price_high;
if(price_high>High[i+1] && price_low>Low[i+1]) break;
if(price_high<High[i+1] && price_low<Low[i+1]) { dirlong=false; break; }
i--;
}
//---- initial zero
int k=i;
while(k<Bars)
{
SarBuffer[k]=0.0;
k++;
}
//---- check further
if(dirlong) { SarBuffer[i]=Low[i+1]; ep=High[i]; }
else { SarBuffer[i]=High[i+1]; ep=Low[i]; }
i--;
}
else
{
i=save_lastreverse;
start=save_start;
dirlong=save_dirlong;
last_high=save_last_high;
last_low=save_last_low;
ep=save_ep;
sar=save_sar;
}
//----
while(i>=0)
{
price_low=Low[i];
price_high=High[i];
//--- check for reverse
if(dirlong && price_low<SarBuffer[i+1])
{
SaveLastReverse(i,true,start,price_low,last_high,ep,sar);
start=Step; dirlong=false;
ep=price_low; last_low=price_low;
SarBuffer[i]=last_high;
i--;
continue;
}
if(!dirlong && price_high>SarBuffer[i+1])
{
SaveLastReverse(i,false,start,last_low,price_high,ep,sar);
start=Step; dirlong=true;
ep=price_high; last_high=price_high;
SarBuffer[i]=last_low;
i--;
continue;
}
//---
price=SarBuffer[i+1];
sar=price+start*(ep-price);
if(dirlong)
{
if(ep<price_high && (start+Step)<=Maximum) start+=Step;
if(price_high<High[i+1] && i==Bars-2) sar=SarBuffer[i+1];

price=Low[i+1];
if(sar>price) sar=price;
price=Low[i+2];
if(sar>price) sar=price;
if(sar>price_low)
{
SaveLastReverse(i,true,start,price_low,last_high,ep,sar);
start=Step; dirlong=false; ep=price_low;
last_low=price_low;
SarBuffer[i]=last_high;
i--;
continue;
}
if(ep<price_high) { last_high=price_high; ep=price_high; }
}
else
{
if(ep>price_low && (start+Step)<=Maximum) start+=Step;
if(price_low<Low[i+1] && i==Bars-2) sar=SarBuffer[i+1];

price=High[i+1];
if(sar<price) sar=price;
price=High[i+2];
if(sar<price) sar=price;
if(sar<price_high)
{
SaveLastReverse(i,false,start,last_low,price_high,ep,sar);
start=Step; dirlong=true; ep=price_high;
last_high=price_high;
SarBuffer[i]=last_low;
i--;
continue;
}
if(ep>price_low) { last_low=price_low; ep=price_low; }
}
SarBuffer[i]=sar;
i--;
}
// sar=SarBuffer[0];
// price=iSAR(NULL,0,Step,Maximum,0);
// if(sar!=price) Print("custom=",sar," SAR=",price," counted=",counted_bars);
// if(sar==price) Print("custom=",sar," SAR=",price," counted=",counted_bars);
//----
return(0);
}
//+------------------------------------------------------------------+

这个是指标的源码,上涨总以第2个指标价格位止损点,下跌总以第2个指标价格为止损点,EA自动检测是付开仓,如果开仓检测是买仓还是卖仓,然后根据买卖仓修改止损价格,详细请看图片。

并提供人工输入当前止损价格的参数默认是0,

以上涨作为例子,

当系统自动检测数止损价格并修改价格是1.4000,可是当前的K线价格是1.4100,我想把止损改成1.4050 所以就用到人数输入的参数了。我把参数输入后止损价格就是我目前的1.4050了。单如果系统检测的止损价格超过了1.4050则系统自动吧止损价格改成先在检测的价格。

请高手帮忙写一个,谢谢。。万分感谢。

 
 

double sar=iSAR(NULL,PERIOD_M15,0.02,0.2,1);

........

//sar跟踪止损
if(OrderType()==OP_BUY || OrderType()==OP_BUYLIMIT || OrderType()==OP_BUYSTOP)
{
if(sar<Close[0])
{
OrderModify(OrderTicket(),OrderOpenPrice(),sar,OrderTakeProfit(),0);
}
}
if(OrderType()==OP_SELL || OrderType()==OP_SELLLIMIT || OrderType()==OP_SELLSTOP)
{
if(sar>Close[0])
{
OrderModify(OrderTicket(),OrderOpenPrice(),sar,OrderTakeProfit(),0);
}
}
......