加码策略EA martin.mq4-MT4 EA-峰汇在线
诚信为本
量力而为
当前位置:峰汇在线 > MetaTrader 4 > MT4 EA > 正文

加码策略EA martin.mq4

martin EA属于加码类型策略的自动交易系统。

源码:

//+------------------------------------------------------------------+
//|                                                       martin.mq4 |
//+------------------------------------------------------------------+
#property copyright ""
#property link      "http://www.125808047.com"

extern bool test=false;  
extern int prof=56;
extern int chag=36; 
extern int risk=4; 
extern double lot = 0.01; 
extern double k_lot=1.7;
extern double lot_max=100;
//=====================================================//
int kn=9;  
int magic = 37641; 

int k,c,b_ind,s_ind,objtot,er;
double buy_prs,sel_prs,buy_ind,sel_ind,or_bu,or_sl,prf_b,prf_s,b_pr,s_pr,b2_cls,s2_cls;
string nh;
bool cg = false;

int OnInit()
{ 
if(Digits==2) k=1;
if(Digits==3) k=10;
if(Digits==4) k=1;
if(Digits==5) k=10;
nh=StringSubstr(Symbol(),2,2);
 if(!IsTesting()){
    buy_prs=GlobalVariableGet(nh+"_buy_prs"); buy_ind=GlobalVariableGet(nh+"_buy_ind");
    sel_prs=GlobalVariableGet(nh+"_sel_prs"); sel_ind=GlobalVariableGet(nh+"_sel_ind");}
c=0;
chag=chag*k;
prof=prof*k;
Comment("www.125808047.com");
return(INIT_SUCCEEDED);
}
//============================================//
void OnTick()
{
int i,y,tick_s,tick_b,tick_slm,tick_blm;
double prov,price_b,price_s,prof_b,prof_s,lot_b,lot_s,b_cls,s_cls,abc;
bool mod_b=false,mod_s=false;

   if(Bars-c>1) c=0;
   i=Bars-c; c=Bars;
//==================================//   
 if(!IsTesting()){for(y=0;y<OrdersTotal();y++)
   {cg = OrderSelect(y,0,0); if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=magic||(OrderType()!=0&&OrderType()!=1)) continue;
      prov=prov+OrderProfit()+OrderSwap()+OrderCommission();}
   if(prov<0&&prov<GlobalVariableGet(nh+"_max_prov"))GlobalVariableSet(nh+"_max_prov",prov);}

//=================================================//
    for (y=0;y<OrdersTotal();y++){cg = OrderSelect(y,0);
      if(Symbol()!=OrderSymbol()||OrderMagicNumber()!=magic) continue; 
      if (OrderType() == 0 || OrderType() == 1) abc=abc + OrderProfit() + OrderSwap() + OrderCommission();
     
      if(OrderType()==0){if(prf_b!=OrderTakeProfit() && prf_b!=0) mod_b=true; prf_b=OrderTakeProfit();}
      if(OrderType()==OP_BUY && tick_b<OrderTicket()) {
       tick_b=OrderTicket();
       price_b=OrderOpenPrice();
       lot_b=OrderLots();
       prof_b=OrderTakeProfit();}

      if(OrderType()==1){if(prf_s!=OrderTakeProfit() && prf_s!=0) mod_s=true; prf_s=OrderTakeProfit();}      
      if(OrderType()==OP_SELL && tick_s<OrderTicket()) {
       tick_s=OrderTicket();
       price_s=OrderOpenPrice();
       lot_s=OrderLots();
       prof_s=OrderTakeProfit();} 

      if (OrderType()==OP_BUYLIMIT)  tick_blm = OrderTicket();
      if (OrderType()==OP_SELLLIMIT) tick_slm = OrderTicket();}
//=====================================================//
   if(Ask>buy_prs && or_bu<=0) {buy_prs=Ask; buy_ind=Ask;}
   if((Ask<buy_prs && Ask<buy_ind) || buy_ind==0) buy_ind=Ask;
   b_cls=buy_ind+MathFloor(prof+chag/2)*Point;
   or_bu=MathFloor((buy_prs-buy_ind)/Point/chag); 
   if(Bid>=b_cls && or_bu>0){buy_prs=Ask; buy_ind=Ask; or_bu=0;}
   if(tick_b==0 && b_ind>0){buy_prs=Ask; buy_ind=Ask; or_bu=0;}

   if((Bid<sel_prs||sel_prs==0) && or_sl<=0) {sel_prs=Bid; sel_ind=Bid;}
   if(Bid>sel_prs && Bid>sel_ind) sel_ind=Bid;
   s_cls=sel_ind-MathFloor(prof+chag/2)*Point;
   or_sl=MathFloor((sel_ind-sel_prs)/Point/chag);
   if(Ask<=s_cls && or_sl>0){sel_prs=Bid; sel_ind=Bid; or_sl=0;}
   if(tick_s==0 && s_ind>0) {sel_prs=Bid; sel_ind=Bid; or_sl=0;}

 if(!IsTesting()){
   GlobalVariableSet(nh+"_buy_prs",buy_prs); GlobalVariableSet(nh+"_buy_ind",buy_ind);
   GlobalVariableSet(nh+"_sel_prs",sel_prs); GlobalVariableSet(nh+"_sel_ind",sel_ind);} 
//===============================
   if(tick_b==0 && tick_blm!=0) cg = OrderDelete(tick_blm);
   if(tick_s==0 && tick_slm!=0) cg = OrderDelete(tick_slm);

   if(or_bu==risk && tick_b==0) open(0); 
   if(or_sl==risk && tick_s==0) open(1);

   if(tick_b>0 && tick_blm==0 && lot_b*k_lot<lot_max && Ask>price_b-chag*Point) open_lim(2,price_b,lot_b);
   if(tick_s>0 && tick_slm==0 && lot_s*k_lot<lot_max && Bid<price_s+chag*Point) open_lim(3,price_s,lot_s);

   if(mod_b) modif(0,prof_b);
   if(mod_s) modif(1,prof_s);
//===============================
  b_ind=tick_b;   s_ind=tick_s; 
//=================================================//
 if(!test){if(b_pr!=buy_prs || s_pr!=sel_prs || i>=1 || objtot!=ObjectsTotal())
         object(buy_prs,buy_prs-chag*kn*Point, sel_prs,sel_prs+chag*kn*Point);
//======= 
 if(b_cls>buy_prs && ObjectFind("clsbuy")==0) ObjectDelete("clsbuy"); 
 if(s_cls<sel_prs && ObjectFind("clsel")==0)  ObjectDelete("clsel");

 if(b_cls<buy_prs && (b2_cls!=b_cls || i>=1 || objtot!=ObjectsTotal())) cls(0,b_cls);
 if(s_cls>sel_prs && (s2_cls!=s_cls || i>=1 || objtot!=ObjectsTotal())) cls(1,s_cls);

   b2_cls=b_cls; s2_cls=s_cls;
   b_pr=buy_prs; s_pr=sel_prs;
   objtot=ObjectsTotal();}
  }
//==================================//
int open(int tip){int tk;
   if(tip==0){tk=OrderSend(Symbol(),OP_BUY,lot,Ask,3,0,0,"martin",magic); er=GetLastError();
   if(tk>0){cg = OrderSelect(tk,SELECT_BY_TICKET); cg = OrderModify(OrderTicket(),OrderOpenPrice(),0,OrderOpenPrice()+prof*Point,0);
       er=GetLastError(); }}
             
   if(tip==1){tk=OrderSend(Symbol(),OP_SELL,lot,Bid,3,0,0,"martin",magic); er=GetLastError();    
   if(tk>0){cg = OrderSelect(tk,SELECT_BY_TICKET); cg = OrderModify(OrderTicket(),OrderOpenPrice(),0,OrderOpenPrice()-prof*Point,0);
       er=GetLastError();}}return(0);}
//==================================//
int open_lim(int tip,double prc,double lota){double lott=NormalizeDouble(lota*k_lot,2); if(lota==0.01 && k_lot>1.3)lott=0.02;
   if(tip==2){cg = OrderSend(Symbol(),OP_BUYLIMIT,lott,prc-chag*Point,3,0,prc-(chag-prof)*Point,"martin",magic,0);
               er=GetLastError();}
   if(tip==3){cg = OrderSend(Symbol(),OP_SELLLIMIT,lott,prc+chag*Point,3,0,prc+(chag-prof)*Point,"martin",magic,0);}
               er=GetLastError(); return(0);}
//==================================//
bool modif(int tp,double pf){int y;
   if(tp==0){ for(y=0;y<OrdersTotal();y++){cg = OrderSelect(y,0);
      if(Symbol()!=OrderSymbol() || OrderMagicNumber()!=magic || OrderType()!=0) continue;
       cg = OrderModify(OrderTicket(),OrderOpenPrice(),0,pf,0); er=GetLastError();}}
//=======   
   if(tp==1){ for(y=0;y<OrdersTotal();y++){cg = OrderSelect(y,0);
      if(Symbol()!=OrderSymbol() || OrderMagicNumber()!=magic || OrderType()!=1) continue;   
       cg = OrderModify(OrderTicket(),OrderOpenPrice(),0,pf,0); er=GetLastError(); }}return(0);}      
//======================================//
bool object(double up_b,double dw_b,double up_s,double dw_s){
   if(ObjectFind("lin_b")==-1)
      {ObjectCreate("lin_b",2,0,Time[0]+Period()*240,up_b,Time[0]+Period()*240,dw_b);
       ObjectSet("lin_b",10,0);ObjectSet("lin_b",6,DeepSkyBlue);
       ObjectSet("lin_b",8,2);}   
   else{ObjectSet("lin_b",1,up_b); ObjectSet("lin_b",0,Time[0]+Period()*240);
        ObjectSet("lin_b",3,dw_b); ObjectSet("lin_b",2,Time[0]+Period()*240);}      
//-------------
   if(ObjectFind("lin_s")==-1)
      {ObjectCreate("lin_s",2,0,Time[0]+Period()*480,up_s,Time[0]+Period()*480,dw_s);
       ObjectSet("lin_s",10,0);ObjectSet("lin_s",6,Red);
       ObjectSet("lin_s",8,2);}   
   else{ObjectSet("lin_s",1,up_s);ObjectSet("lin_s",0,Time[0]+Period()*480);
        ObjectSet("lin_s",3,dw_s);ObjectSet("lin_s",2,Time[0]+Period()*480);}
//-------------
 string ln; int y,tm,tm2,col,shn; double kln;

   for(y=0;y<=kn*2+1;y++){ln="kr_"+DoubleToStr(y,0);    
      if(y<=kn){kln=up_b-y*chag*Point; tm=120; tm2=240;}
      if(y>kn && y<=kn*2+1){kln=up_s+(y-kn-1)*chag*Point; tm=360; tm2=480;}
      if(y==risk || y==kn+1+risk) {col=Lime; shn=3;} else {col=DarkGoldenrod; shn=0;}

   if(ObjectFind(ln)==-1)   
       {ObjectCreate(ln,2,0,Time[0]+Period()*tm,kln,Time[0]+Period()*tm2,kln);
        ObjectSet(ln,10,0);ObjectSet(ln,6,col); ObjectSet(ln,8,shn);}
   else{ObjectSet(ln,1,kln);ObjectSet(ln,3,kln);ObjectSet(ln,6,col); ObjectSet(ln,8,shn);
        ObjectSet(ln,0,Time[0]+Period()*tm);ObjectSet(ln,2,Time[0]+Period()*tm2);}}return(0);}
//==============//
 bool cls(int tp,double clos){
   if(tp==0){if(ObjectFind("clsbuy")==-1) 
       {ObjectCreate("clsbuy",2,0,Time[0]+Period()*120,clos,Time[0]+Period()*240,clos);
        ObjectSet("clsbuy",10,0);ObjectSet("clsbuy",6,Magenta); ObjectSet("clsbuy",8,3);}
   else{ObjectSet("clsbuy",1,clos); ObjectSet("clsbuy",3,clos);   
        ObjectSet("clsbuy",0,Time[0]+Period()*120); ObjectSet("clsbuy",2,Time[0]+Period()*240);}}

   if(tp==1){if(ObjectFind("clsel")==-1)      
       {ObjectCreate("clsel",2,0,Time[0]+Period()*360,clos,Time[0]+Period()*480,clos);
        ObjectSet("clsel",10,0); ObjectSet("clsel",6,Magenta); ObjectSet("clsel",8,3);}
   else{ObjectSet("clsel",1,clos); ObjectSet("clsel",3,clos);   
        ObjectSet("clsel",0,Time[0]+Period()*360); ObjectSet("clsel",2,Time[0]+Period()*480);}}
       return(0);}
打赏
版权所有转载请注明标题及链接:峰汇在线 » 加码策略EA martin.mq4
上一篇:
下一篇:
分享到: 更多 (0)

如您觉得此文不错请扫码打赏!

支付宝扫一扫打赏

微信扫一扫打赏