比特币通信协议 - 币圈消息

比特币通信协议篇11、二、TURN简介。在典型的情况下,TURN客户端连接到内网中,并且通过一个或者多个NAT到 详细

求改成可用EA

  [复制链接]
197 8
hwq597 发表于 2017-5-25 09:22:34 | 只看该作者 |阅读模式 打印 上一主题 下一主题
//+------------------------------------------------------------------------------------------------------------------+
//|                                                                                M09.D03.Y2011_Expert_Advisor.mq4  |
//|                                                   Copyright ?2008-2011, TradingSyste001.Com - Elite Section  |
//|                                                                              http://www.tradingsystem001.com/  |
//+------------------------------------------------------------------------------------------------------------------+
#property copyright "Copyright ?2008-2011, TradingSystem001x.Com - Elite Section"
#property link "http://www.tradingsystem001.com/"
#include
#include
//+--------------------------------------------------------------------------------+ EXTERNAL INPUTS +---------------+
//|  You can set a closed martingale : martingale=true, maxtrades=1,basketpercent or basketpips=true.
//|  Or an open martingale : martingale=true,tradesperbar=100, basketpercent or basketpips=true, addpositions=true.
//|  Scalping : You can use timeout and target, time filter, set maxtrades=1,changedirection=true to optimize the scalping.
//--- Add "extern " before each parameter to get them in the inputs tab
string comment="EA";                     // comment to display in the order
extern int magic=1234;                   // magic number required if you use different settings on a pair, same tf
bool randommagic=false;                  // use random magic number
bool useprint=true;                     // use print
bool usecomment=false;                   // use comment
bool emailalert=false;                   // use email alert
bool popupalert=false;                   // use popup alert
bool soundalert=false;                   // use sound alert
bool onlybuy=false;                      // only enter buy orders
bool onlysell=false;                     // only enter sell orders
bool closeall=false;                     // close all orders
bool closebuy=false;                     // only close buy orders
bool closesell=false;                    // only close sell orders
bool visualequity=false;                 // draw equity
extern string moneymanagement="Money Management";
extern double lots=0.1;                  // lots size
extern bool mm=false;                    // enable risk management
extern double risk=1;                    // risk in percentage of the account
extern double minlot=0.01;               // minimum lots size
extern double maxlot=100;                // maximum lots size
extern int lotdigits=2;                  // lot digits, 1=0.1, 2=0.01
bool recoverymode=false;                 // we multiply the lots size after closed losses
double recoverymultiplier=2;             // recovery multiplier
bool martingale=false;                   // enable the martingale, set maxtrades to 1
int martingalemode=0;                    // 0=increase after a loss, 1=increase after a profit
double multiplier=2.0;                   // multiplier used for the martingale
double percentrecovering=0;              // enabled if >0, percentage of the last losses to recover
bool fibomartingale=false;               // enable the martingale with fibonacci progression
int fibomartingalemode=0;                // 0=increase after a loss, 1=increase after a profit
bool alembert=false;                     // alembert's martingale
int alembertmode=0;                      // 0=increment/decrement after a loss/profit, 1=decrement/increment after a loss/profit
double increment=1;                      // prev lots+lots*increment
double decrement=1;                      // prev lots-lots*increment
int resetlevel=500;                      // enabled if  target pips
int timeout1=30,target1=7,timeout2=70,target2=5,timeout3=95,target3=4,timeout4=120,
target4=2,timeout5=150,target5=-5,timeout6=180,target6=-8,timeout7=210,target7=-15;
//+----------------------------------------------------------------------------+ INTERNAL PARAMETERS +---------------+
bool closebasket=false,continuebuy=true,continuesell=true,nosleep=true,buy=false,sell=false,buyaddstatus=true,
selladdstatus=true,buyside=true,sellside=true,tradetime=true,newsstatus=true;
int count,global,i,tries=100,lastorder,buyorderprofit,sellorderprofit,lotsize,losses,sleep,recovering=0,ntesthour,
mstop,nstarthour,nnewshour,nendhour,nfridayhour,nmondayhour,number,ticket,gmtshift,expire,tradehour,consloss=0,
consprofit=0,total,oldestlong,oldestshort,handle,csvdatetime,signal1=0,signal2=0,exitsignal1=0,exitsignal2=0,gap1,gap2;
double cb,sl,tp,blots,slots,lastbuylot,lastselllot,lastlot,lastprofit,mlots,win[14],sum[14],totalpips,totalprofit,
percentprofit,percentloss,fibo[20],lastbuyopenprice,lastsellopenprice,lastbuyprofit,lastsellprofit,tradeprofit,
buyorderpips,sellorderpips,lossestorecover=0,globallosses,initialbar=0,initialaccount,baskettrailpipsi,
basketstoppipsi,basketstopdollarsi,baskettraildollarsi,baskettrailpercenti,basketstoppercenti,prevhistoryprofit=0,
historyprofit=0,ilot1,alembertlastprofit;
string istarthour,istartminute,iendhour,iendminute,ifridayhour,ifridayminute,imondayhour,imondayminute,closestatus,
date,csvdate,csvmonth,csvday,csvhour,csvcurrency,cur1,cur2,stat="",fail="",err="",exit="",itesthour,itestminute,
basketpipsflag="",basketdollarsflag="",baskettrailpipsflag="",basketpercentflag="",baskettrailpercentflag="",
baskettraildollarsflag="";
datetime tstart,tend,tfriday,tmonday,lastbuyopentime,tnews,lastsellopentime,time,time2,time3,
time4,time5,time6,time7,lastorderclosetime,alerttime,newstime,daytime,ttesthour;
double dg,pt,mt;
datetime timez;
double prevharsfp1,harsfp1;
double prevharsfp3,harsfp3;
double prevharsfp5,harsfp5;
double prevharsfp7,harsfp7;
double prevharsfp9,harsfp9;
double prevharsfp11,harsfp11;
double prevharsfp13,harsfp13;
double prevharsfp15,harsfp15;
double prevharsfp17,harsfp17;
double prevharsfp19,harsfp19;
double prevharsfp21,harsfp21;
double prevharsfp23,harsfp23;
double prevharsfp25,harsfp25;
double prevharsfp27,harsfp27;
double prevharsfp29,harsfp29;
double prevharsfp31,harsfp31;
double prevharsfp33,harsfp33;
double prevharsfp35,harsfp35;
double prevharsfp40,harsfp40;
double prevharsfp42,harsfp42;
double prevharsfp44,harsfp44;
double prevharsfp46,harsfp46;
double prevharsfp48,harsfp48;
double prevharsfp50,harsfp50;
double prevharsfp52,harsfp52;
double prevharsfp54,harsfp54;
double prevharsfp56,harsfp56;
double prevharsfp58,harsfp58;
double prevharsfp60,harsfp60;
double prevharsfp66,harsfp66;
double prevharsfp2,harsfp2;
double prevharsfp4,harsfp4;
double prevharsfp6,harsfp6;
double prevharsfp8,harsfp8;
double prevharsfp10,harsfp10;
double prevharsfp12,harsfp12;
double prevharsfp14,harsfp14;
double prevharsfp16,harsfp16;
double prevharsfp18,harsfp18;
double prevharsfp20,harsfp20;
double prevharsfp22,harsfp22;
double prevharsfp24,harsfp24;
double prevharsfp26,harsfp26;
double prevharsfp28,harsfp28;
double prevharsfp30,harsfp30;
double prevharsfp32,harsfp32;
double prevharsfp34,harsfp34;
double prevharsfp36,harsfp36;
double prevharsfp41,harsfp41;
double prevharsfp43,harsfp43;
double prevharsfp45,harsfp45;
double prevharsfp47,harsfp47;
double prevharsfp49,harsfp49;
double prevharsfp51,harsfp51;
double prevharsfp53,harsfp53;
double prevharsfp55,harsfp55;
double prevharsfp57,harsfp57;
double prevharsfp59,harsfp59;
double prevharsfp61,harsfp61;
double prevharsfp67,harsfp67;
//+---------------------------------------------------------------------------------+ INITIALIZATION +---------------+
int init(){
   if(timez!=Time[0]){
      harsfp1=ObjectFind("HAR_S_found_pattern_1");
      harsfp3=ObjectFind("HAR_S_found_pattern_3");
      harsfp5=ObjectFind("HAR_S_found_pattern_5");
      harsfp7=ObjectFind("HAR_S_found_pattern_7");
      harsfp9=ObjectFind("HAR_S_found_pattern_9");
      harsfp11=ObjectFind("HAR_S_found_pattern_11");
      harsfp13=ObjectFind("HAR_S_found_pattern_13");
      harsfp15=ObjectFind("HAR_S_found_pattern_15");
      harsfp17=ObjectFind("HAR_S_found_pattern_17");
      harsfp19=ObjectFind("HAR_S_found_pattern_19");
      harsfp21=ObjectFind("HAR_S_found_pattern_21");
      harsfp23=ObjectFind("HAR_S_found_pattern_23");
      harsfp25=ObjectFind("HAR_S_found_pattern_25");
      harsfp27=ObjectFind("HAR_S_found_pattern_27");
      harsfp29=ObjectFind("HAR_S_found_pattern_29");
      harsfp31=ObjectFind("HAR_S_found_pattern_31");
      harsfp33=ObjectFind("HAR_S_found_pattern_33");
      harsfp35=ObjectFind("HAR_S_found_pattern_35");
      harsfp40=ObjectFind("HAR_S_found_pattern_40");
      harsfp42=ObjectFind("HAR_S_found_pattern_42");
      harsfp44=ObjectFind("HAR_S_found_pattern_44");
      harsfp46=ObjectFind("HAR_S_found_pattern_46");
      harsfp48=ObjectFind("HAR_S_found_pattern_48");
      harsfp50=ObjectFind("HAR_S_found_pattern_50");
      harsfp52=ObjectFind("HAR_S_found_pattern_52");
      harsfp54=ObjectFind("HAR_S_found_pattern_54");
      harsfp56=ObjectFind("HAR_S_found_pattern_56");
      harsfp58=ObjectFind("HAR_S_found_pattern_58");
      harsfp60=ObjectFind("HAR_S_found_pattern_60");
      harsfp66=ObjectFind("HAR_S_found_pattern_66");
      
      harsfp2=ObjectFind("HAR_S_found_pattern_2");
      harsfp4=ObjectFind("HAR_S_found_pattern_4");
      harsfp6=ObjectFind("HAR_S_found_pattern_6");
      harsfp8=ObjectFind("HAR_S_found_pattern_8");
      harsfp10=ObjectFind("HAR_S_found_pattern_10");
      harsfp12=ObjectFind("HAR_S_found_pattern_12");
      harsfp14=ObjectFind("HAR_S_found_pattern_14");
      harsfp16=ObjectFind("HAR_S_found_pattern_16");
      harsfp18=ObjectFind("HAR_S_found_pattern_18");
      harsfp20=ObjectFind("HAR_S_found_pattern_20");
      harsfp22=ObjectFind("HAR_S_found_pattern_22");
      harsfp24=ObjectFind("HAR_S_found_pattern_24");
      harsfp26=ObjectFind("HAR_S_found_pattern_26");
      harsfp28=ObjectFind("HAR_S_found_pattern_28");
      harsfp30=ObjectFind("HAR_S_found_pattern_30");
      harsfp32=ObjectFind("HAR_S_found_pattern_32");
      harsfp34=ObjectFind("HAR_S_found_pattern_34");
      harsfp36=ObjectFind("HAR_S_found_pattern_36");
      harsfp41=ObjectFind("HAR_S_found_pattern_41");
      harsfp43=ObjectFind("HAR_S_found_pattern_43");
      harsfp45=ObjectFind("HAR_S_found_pattern_45");
      harsfp47=ObjectFind("HAR_S_found_pattern_47");
      harsfp49=ObjectFind("HAR_S_found_pattern_49");
      harsfp51=ObjectFind("HAR_S_found_pattern_51");
      harsfp53=ObjectFind("HAR_S_found_pattern_53");
      harsfp55=ObjectFind("HAR_S_found_pattern_55");
      harsfp57=ObjectFind("HAR_S_found_pattern_57");
      harsfp59=ObjectFind("HAR_S_found_pattern_59");
      harsfp61=ObjectFind("HAR_S_found_pattern_61");
      harsfp67=ObjectFind("HAR_S_found_pattern_67");
      timez=Time[0];
   }
   dg=Digits;
   if(dg==3 || dg==5){pt=Point*10;mt=10;}
   else{pt=Point;mt=1;}
   gap1=gap;gap2=gap;
//+--------------------------------------------------------------------------------+ INITIALBAR INIT +---------------+
   initinitialbar();
//+-----------------------------------------------------------------------+ FASTER OPTIMIZATION INIT +---------------+
   initoptimization();
//+---------------------------------------------------------------------------+ FIBO MARTINGALE INIT +---------------+
   initfibo();
//+------------------------------------------------------------------------------+ POINT DIGITS INIT +---------------+
   //point();
//+------------------------------------------------------------------------------+ POINT DIGITS INIT +---------------+
   initmm();
   return(0);
}
//+-----------------------------------------------------------------------------------+ ENTRY SIGNAL +---------------+
int signal(int mode,int pos){
   //ma>0 && ma!=EMPTY_VALUE
   //iCustom(NULL,0,"StepMA_v7",maperiod,0,pos);
   double ma=iMA(NULL,timeframe,maperiod1,0,MODE_SMA,PRICE_CLOSE,pos);
   double ma2=iMA(NULL,timeframe,maperiod2,0,MODE_SMA,PRICE_CLOSE,pos);
   double ma3=iMA(NULL,timeframe,maperiod3,0,MODE_SMA,PRICE_CLOSE,pos);
   if(ma>ma2 && ma>ma3 && ma2>ma3)if(mode==0)return(1);
   if(mama2 && ma>ma3 && ma2>ma3)if(mode==0)return(1);
   if(ma0){
         for(i=OrdersHistoryTotal()-1;i>=0;i--){
            OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
            if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic)continue;
            if(OrderOpenTime()[i]=endday)))
   && IsTradeContextBusy()==false){
      signal1=1;
   }
   if(exitsignal2!=2
   && (mafilter==false || (mafilter && signal(OP_SELL,shift)==2))
   && (prevharsfp2==-1 && harsfp2!=-1) || (prevharsfp4==-1 && harsfp4!=-1) || (prevharsfp6==-1 && harsfp6!=-1)
   || (prevharsfp8==-1 && harsfp8!=-1) || (prevharsfp10==-1 && harsfp10!=-1) || (prevharsfp12==-1 && harsfp12!=-1)
   || (prevharsfp14==-1 && harsfp14!=-1)|| (prevharsfp16==-1 && harsfp16!=-1) || (prevharsfp18==-1 && harsfp18!=-1)
   || (prevharsfp20==-1 && harsfp20!=-1) || (prevharsfp22==-1 && harsfp22!=-1) || (prevharsfp24==-1 && harsfp24!=-1)
   || (prevharsfp26==-1 && harsfp26!=-1) || (prevharsfp28==-1 && harsfp28!=-1) || (prevharsfp30==-1 && harsfp30!=-1)
   || (prevharsfp32==-1 && harsfp32!=-1) || (prevharsfp34==-1 && harsfp34!=-1) || (prevharsfp36==-1 && harsfp36!=-1)
   || (prevharsfp41==-1 && harsfp41!=-1) || (prevharsfp43==-1 && harsfp43!=-1) || (prevharsfp45==-1 && harsfp45!=-1)
   || (prevharsfp47==-1 && harsfp47!=-1) || (prevharsfp49==-1 && harsfp49!=-1) || (prevharsfp51==-1 && harsfp51!=-1)
   || (prevharsfp53==-1 && harsfp53!=-1) || (prevharsfp55==-1 && harsfp55!=-1) || (prevharsfp57==-1 && harsfp57!=-1)
   || (prevharsfp59==-1 && harsfp59!=-1) || (prevharsfp61==-1 && harsfp61!=-1) || (prevharsfp67==-1 && harsfp67!=-1)
   && (tradesperday==false || (tradesperday && count[tr]=endday)))
   && IsTradeContextBusy()==false){
      signal2=2;
   }
}
//+-----------------------------------------------------------------------------------------+ START +----------------+
int start(){
//+-------------------------------------------------------------------------------------+ CLOSE CALL +---------------+
   if(closeall || (closetime && Hour()>=closehour)){closeall();return(0);}if(closebuy)close(OP_BUY);if(closesell)close(OP_SELL);
//+--------------------------------------------------------------------------------+ DATA CONDITIONS +---------------+
   if(optimization() || initialbar() || minbars())return(0);
//+-----------------------------------------------------------------------------------+ RANDOM MAGIC +---------------+
   randommagic();
//+---------------------------------------------------------------------------------+ BASKET TRADING +---------------+
   basket();
   if(basketpipsflag=="CLOSE" || basketdollarsflag=="CLOSE" || basketpercentflag=="CLOSE" || baskettrailpipsflag=="CLOSE" || baskettraildollarsflag=="CLOSE" || baskettrailpercentflag=="CLOSE")return(0);
//+----------------------------------------------------------------------------------+ VISUAL EQUITY +---------------+
   visual();
//+------------------------------------------------------------------------------------+ TRADES DATA +---------------+
   data();
//+-------------------------------------------------------------------------------------+ SLEEP CALL +---------------+
   sleep();
//+-------------------------------------------------------------------------------------------+ EXIT +---------------+
   exit();
//+------------------------------------------------------------------------------------------+ ENTRY +---------------+
   entry();
//+--------------------------------------------------------------------------------------+ DIRECTION +---------------+
   //direction();
//+-----------------------------------------------------------------------------------+ INIT TRADING +---------------+
   inittrading();
//+----------------------------------------------------------------------------------+ ADD POSITIONS +---------------+
   addpositions();
//+----------------------------------------------------------------------------------------+ TRADING +---------------+
   trading();
//+------------------------------------------------------------------------------------+ EXIT STATUS +---------------+
   stop();
//+----------------------------------------------------------------------------+ TRADES MODIFICATION +---------------+
   modify();
//+---------------------------------------------------------------------------------------+ MAX AREA +---------------+
   if((!buy && !sell) || max())return(0);
//+---------------------------------------------------------------------------------------+ LOT CALL +---------------+
   lot();
//+--------------------------------------------------------------------------------+ MARTINGALE CALL +---------------+
   martingale();
//+----------------------------------------------------------------------------------+ ALEMBERT CALL +---------------+
   alembert();
//+---------------------------------------------------------------------------+ FIBO MARTINGALE CALL +---------------+
   fibomartingale();
//+----------------------------------------------------------------------------------+ RECOVERY CALL +---------------+
   recovery();
//+------------------------------------------------------------------------------------+ TIME FILTER +---------------+
   time();
//+------------------------------------------------------------------------------------+ NEWS FILTER +---------------+
   news();
//+-------------------------------------------------------------------------------+ TICKET GENERATOR +---------------+
   ticket();
   return(0);
}
//+-------------------------------------------------------------------------------------+ BAR STATUS +---------------+
int bar(int mode){
   int count=0;
   if(OrdersTotal()>0){
      for(i=OrdersTotal()-1;i>=0;i--){
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderCloseTime()==0){
            if(OrderOpenTime()[i]=iTime(NULL,0,0))count++;
            }
            if(OrderType()==OP_SELL || OrderType()==OP_SELLLIMIT || OrderType()==OP_SELLSTOP && mode==OP_SELL){
               if(OrderOpenTime()>=iTime(NULL,0,0))count++;
            }
         }
      }
   }
   if(OrdersHistoryTotal()>0){
      for(i=OrdersHistoryTotal()-1;i>=0;i--){
         OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
         if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic)continue;
         if(OrderOpenTime()[i]=iTime(NULL,0,0))count++;
         if(OrderType()==OP_SELL || OrderType()==OP_SELLLIMIT || OrderType()==OP_SELLSTOP && mode==OP_SELL)if(OrderOpenTime()>=iTime(NULL,0,0))count++;
      }
   }
   return(count);
}
//+----------------------------------------------------------------------------------+ VISUAL EQUITY +---------------+
void visual(){
   if(visualequity){ // You need the indicator vGrafBalance&Equity.mq4
      GlobalVariableSet("vGrafBalance",AccountBalance());
      GlobalVariableSet("vGrafEquity",AccountEquity());
   }
}
//+-----------------------------------------------------------------------------------+ RANDOM MAGIC +---------------+
void randommagic(){
   if(randommagic)if(daytime!=iTime(NULL,1440,0)){
      MathSrand(TimeLocal());
      magic=MathRand();
      daytime=iTime(NULL,1440,0);
   }
}
//+----------------------------------------------------------------------------+ FASTER OPTIMIZATION +---------------+
void initoptimization(){
   initialaccount=AccountBalance();
   if(stop>0){
      stoploss=stop;
      takeprofit=stop;
   }
}
bool optimization(){
   if(firstticks)if(iVolume(NULL,0,0)>ticks)return(true);
   //if(IsTesting())if(AccountEquity()[i]0){
      for(i=0;iOrderOpenTime())oldestlong=OrderOpenTime();
               lastbuyopentime=OrderOpenTime();
               if(addpositions)lastbuyopenprice=OrderOpenPrice();
               if(recovering==1)buyorderprofit=buyorderprofit+OrderProfit();
            }
            if(OrderType()==OP_SELL){
               if(oppositeclose)if(oldestlong==0 || oldestshort>OrderOpenTime())oldestshort=OrderOpenTime();
               lastsellopentime=OrderOpenTime();
               if(addpositions)lastsellopenprice=OrderOpenPrice();
               if(recovering==1)sellorderprofit=sellorderprofit+OrderProfit();
            }
         }
      }
   }
   if(recovering==1)totalprofit=buyorderprofit+sellorderprofit;
}
//+-----------------------------------------------------------------------------------+ TRADING CORE +--------------+
void inittrading(){
   buy=false;
   sell=false;
}
void trading(){
   buyside=true;sellside=true;
   if(onesideatatime){if(count(OP_SELL)>0)if(reversesignals)sellside=false;else buyside=false;if(count(OP_BUY)>0)if(reversesignals)buyside=false;else sellside=false;}
   if(signal1==1 && buyaddstatus && buyside && continuebuy && nosleep
   && (excludedays==false || (excludedays && Day()!=excludeday1 && Day()!=excludeday2))){
      if(reversesignals)sell=true;else buy=true;
      if(changedirection){continuebuy=false;continuesell=true;}
      if(checkclose)if(reversesignals)closestatus="SHORT";else closestatus="LONG";
   }
   if(signal2==2 && selladdstatus && sellside && continuesell && nosleep
   && (excludedays==false || (excludedays && Day()!=excludeday1 && Day()!=excludeday2))){
      if(reversesignals)buy=true;else sell=true;
      if(changedirection){continuebuy=true;continuesell=false;}
      if(checkclose)if(reversesignals)closestatus="LONG";else closestatus="SHORT";
   }
}
//+----------------------------------------------------------------------------------+ OPEN FUNCTION +---------------+
int open(int type,double lots,double price,double stoploss,double takeprofit,int expire,color clr){
   int ticket=0;
   if(lotsmaxlot)lots=maxlot;
   if(type==OP_BUY || type==OP_BUYSTOP || type==OP_BUYLIMIT){
      if(hidesl==false && stoploss>0)sl=price-stoploss*pt;else sl=0;
      if(hidetp==false && takeprofit>0)tp=price+takeprofit*pt;else tp=0;
      if(ecn){sl=0;tp=0;}
   }
   if(type==OP_SELL || type==OP_SELLSTOP || type==OP_SELLLIMIT){
      if(hidesl==false && stoploss>0)sl=price+stoploss*pt;else sl=0;
      if(hidetp==false && takeprofit>0)tp=price-takeprofit*pt;else tp=0;
      if(ecn){sl=0;tp=0;}
   }
   ticket=OrderSend(Symbol(),type,nd(lots,lotdigits),nd(price,dg),slippage*mt,sl,tp,comment+" "+DoubleToStr(magic,0),magic,expire,clr);
   return(ticket);
}
void buy(){
   if(rangesl){stoploss=(Ask-iLow(NULL,rangesltf,iLowest(NULL,rangesltf,MODE_LOW,rangeslperiod,shift)))/pt;if(stoploss0)expire=TimeCurrent()+(expiration*60)-5;
   if(instantorders){
      if(recovering==0){
         while(ticket=0;i--){
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         cnt++;
      }
      return(cnt);
   }
}
//+-------------------------------------------------------------------------+ MAGIC COUNTER FUNCTION +---------------+
int count(int type){
   int cnt=0;
   if(OrdersTotal()>0){
      for(i=OrdersTotal()-1;i>=0;i--){
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Symbol() && OrderType()==type && OrderMagicNumber()==magic)cnt++;
      }
      return(cnt);
   }
}
//+------------------------------------------------------------------------------------+ COUNTER ALL +---------------+
int countall(int mode){
   int cnt=0;
   if(mode==0)cnt=count(OP_BUY)+count(OP_SELL);
   if(mode==1)cnt=count(OP_BUY)+count(OP_SELL)+count(OP_BUYSTOP)+count(OP_SELLSTOP)+count(OP_BUYLIMIT)+count(OP_SELLLIMIT);
   return(cnt);
}
//+---------------------------------------------------------------------------------+ MAX CONDITIONS +---------------+
bool max(){
   if(maxspread!=0)if((Ask-Bid)>maxspread*pt)return(true);
   if(maxtrades=maxtrades)return(true);
   return(false);
}
//+------------------------------------------------------------------------+ LAST HISTORY PROFIT/LOT +---------------+
double history(int mode){
   double lastprofit=0;
   double lastlot=0;
   if(OrdersHistoryTotal()>0){
      for(i=OrdersHistoryTotal()-1;i>=0;i--){
         if(lastprofit!=0)break;
         OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderType()==OP_BUY || OrderType()==OP_SELL){
            lastprofit=OrderProfit();
            lastlot=OrderLots();
         }
      }
   }
   if(mode==0)return(lastlot);
   if(mode==1)return(lastprofit);
   return(0);
}
//+------------------------------------------------------------------------------+ LAST HISTORY TYPE +---------------+
/*int type(){
   int lasttype=2;
   if(OrdersHistoryTotal()>0){
      for(i=OrdersHistoryTotal()-1;i>=0;i--){
         if(lasttype==OP_BUY || lasttype==OP_SELL)break;
         OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderType()==OP_BUY || OrderType()==OP_SELL){
            lasttype=OrderType();
         }
      }
   }
   return(lasttype);
}*/
//+--------------------------------------------------------------------------------------+ DIRECTION +---------------+
/*void direction(){
   if(changedirection){
      if(type()==OP_BUY){continuebuy=false;continuesell=true;}
      if(type()==OP_SELL){continuebuy=true;continuesell=false;}
   }
}*/
//+------------------------------------------------------------------------------+ COUNT PROFIT/LOSS +---------------+
double countprofit(int mode){
   double profit=0;
   if(OrdersTotal()>0){
      for(i=OrdersTotal()-1;i>=0;i--){
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderMagicNumber()==magic){
            if(mode==0)profit=profit+(OrderClosePrice()-OrderOpenPrice())/pt;
            if(mode==1)profit=profit+OrderProfit();
         }
         if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderMagicNumber()==magic){
            if(mode==0)profit=profit+(OrderOpenPrice()-OrderClosePrice())/pt;
            if(mode==1)profit=profit+OrderProfit();
         }
      }
      return(profit);
   }
}
//+----------------------------------------------------------------------+ COUNT HISTORY PROFIT/LOSS +---------------+
double counthistoprofit(int mode){
   double profit=0;
   if(OrdersHistoryTotal()>0){
      for(i=OrdersHistoryTotal()-1;i>=0;i--){
         OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
         if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderMagicNumber()==magic){
            if(mode==2 || mode==3 && TimeDay(OrderOpenTime())!=TimeDay(Time[0]))break;
            if(mode==4 || mode==5 && TimeMonth(OrderOpenTime())!=TimeMonth(Time[0]))break;
            if(mode==0)profit=profit+(OrderClosePrice()-OrderOpenPrice())/pt;
            if(mode==1)profit=profit+OrderProfit();
            if(mode==2 && TimeDay(OrderOpenTime())==TimeDay(Time[0]))profit=profit+(OrderClosePrice()-OrderOpenPrice())/pt;
            if(mode==3 && TimeDay(OrderOpenTime())==TimeDay(Time[0]))profit=profit+OrderProfit();
            if(mode==4 && TimeMonth(OrderOpenTime())==TimeMonth(Time[0]))profit=profit+(OrderClosePrice()-OrderOpenPrice())/pt;
            if(mode==5 && TimeMonth(OrderOpenTime())==TimeMonth(Time[0]))profit=profit+OrderProfit();
         }
         if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderMagicNumber()==magic){
            if(mode==2 || mode==3 && TimeDay(OrderOpenTime())!=TimeDay(Time[0]))break;
            if(mode==4 || mode==5 && TimeMonth(OrderOpenTime())!=TimeMonth(Time[0]))break;
            if(mode==0)profit=profit+(OrderOpenPrice()-OrderClosePrice())/pt;
            if(mode==1)profit=profit+OrderProfit();
            if(mode==2 && TimeDay(OrderOpenTime())==TimeDay(Time[0]))profit=profit+(OrderOpenPrice()-OrderClosePrice())/pt;
            if(mode==3 && TimeDay(OrderOpenTime())==TimeDay(Time[0]))profit=profit+OrderProfit();
            if(mode==4 && TimeMonth(OrderOpenTime())==TimeMonth(Time[0]))profit=profit+(OrderOpenPrice()-OrderClosePrice())/pt;
            if(mode==5 && TimeMonth(OrderOpenTime())==TimeMonth(Time[0]))profit=profit+OrderProfit();
         }
      }
      return(profit);
   }
}
//+------------------------------------------------------------------------+ CONSECUTIVE PROFIT/LOSS +---------------+
int consecutive(int mode){
   int res=0;
   for(i=OrdersHistoryTotal();i>=0;i--){
      OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic){
         if(mode==0){
            if(OrderProfit()>0)break;
            if(OrderProfit()0)res=res+1;
         }
      }
   }
   return(res);
}
//+-----------------------------------------------------------------------------+ GLOBAL PROFIT/LOSS +---------------+
double global(int mode){
   double res=0;
   for(i=OrdersHistoryTotal();i>=0;i--){
      OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic){
         if(mode==0){
            if(OrderProfit()>0)break;
            if(OrderProfit()0)res=res+OrderProfit();
         }
      }
   }
   return(res);
}
//+-----------------------------------------------------------------------------+ ADD POSITIONS CORE +---------------+
void addpositions(){
   buyaddstatus=true;
   selladdstatus=true;
   if(addpositions)checkaddpositions();
}
void checkaddpositions(){
   if(count(OP_BUY)>0){
      buyaddstatus=false;
      if((addposmode==0 && Close[0]=lastbuyopenprice+pipstep*pt*MathPow(pipstepfactor,count(OP_BUY)))
      || (addposmode==2 && Close[0]=lastbuyopenprice+pipstep*pt*MathPow(pipstepfactor,count(OP_BUY)))
      && (signalfilter==false || (signalfilter && (reversesignals==false && signal1==1) || (reversesignals && signal2==2))))buy=true;
   }
   if(count(OP_SELL)>0){
      selladdstatus=false;
      if((addposmode==0 && Close[0]>=lastsellopenprice+pipstep*pt*MathPow(pipstepfactor,count(OP_SELL)))
      || (addposmode==1 && Close[0]=lastsellopenprice+pipstep*pt*MathPow(pipstepfactor,count(OP_SELL))
      || Close[0]=0) || (martingale && addpositions) && (partialtp==false || (partialtp && countglobal()==0)))lots=mm(risk);
   blots=lots;
   slots=lots;
}
//+----------------------------------------------------------------------------------------+ MM CORE +---------------+
void initmm(){
   if(mm){
      if(MarketInfo(Symbol(),MODE_MINLOT)>=1){lotsize=100000;}
      if(MarketInfo(Symbol(),MODE_MINLOT)0)lot=AccountBalance()*(risk/100)/(stoploss*pt/MarketInfo(Symbol(),MODE_TICKSIZE)*MarketInfo(Symbol(),MODE_TICKVALUE));
   else lot=nd((AccountBalance()/lotsize)*0.01*risk,lotdigits);
   return(lot);
}
//+--------------------------------------------------------------------------------+ MARTINGALE CORE +---------------+
void martingale(){
   if(martingale){
      if(!addpositions)checkmartingale();
      if(addpositions)addlots();
   }
}
void checkmartingale(){
   recovering=0;
   globallosses=0;lossestorecover=0;
   lastlot=history(0);
   lastprofit=history(1);
   mlots=0;
   if(martingalemode==0){
      if(lastprofit=resetlevel)mlots=lots;
         }
         if(percentrecovering!=0){
            globallosses=global(0);
            recovering=1;
            lossestorecover=nd(globallosses*0.01*percentrecovering,2);
         }
      }
      else mlots=lots;
   }
   if(martingalemode==1){
      if(lastprofit>0){
         mlots=lastlot*multiplier;
         if(resetlevel=resetlevel)mlots=lots;
         }
      }
      else{
         mlots=lots;
         if(percentrecovering!=0){
            globallosses=global(0);
            recovering=1;
            lossestorecover=nd(globallosses*0.01*percentrecovering,2);
         }
      }
   }
   blots=mlots;
   slots=mlots;
}
//+---------------------------------------------------------------------------+ FIBO MARTINGALE CORE +---------------+
void initfibo(){
   if(fibomartingale){
      fibo[0]=1;fibo[1]=1;fibo[2]=2;fibo[3]=3;fibo[4]=5;fibo[5]=8;fibo[6]=13;fibo[7]=21;
      fibo[8]=34;fibo[9]=55;fibo[10]=89;fibo[11]=144;fibo[12]=233;fibo[13]=377;fibo[14]=610;
      fibo[15]=987;fibo[16]=1597;fibo[17]=2584;fibo[18]=4181;fibo[19]=6765;fibo[20]=10946;
   }
}
void fibomartingale(){
   if(fibomartingale)checkfibomartingale();
}
void checkfibomartingale(){
   if(OrdersHistoryTotal()>0){
      if(fibomartingalemode==0){
         consloss=consecutive(0);
         blots=lots*fibo[consloss];
         slots=lots*fibo[consloss];
         if(resetlevel=resetlevel){
               blots=lots;
               slots=lots;
            }
         }
      }
      if(fibomartingalemode==1){
         consprofit=consecutive(1);
         blots=lots*fibo[consprofit];
         slots=lots*fibo[consprofit];
         if(resetlevel=resetlevel){
               blots=lots;
               slots=lots;
            }
         }
      }
   }
}
//+----------------------------------------------------------------------------------+ ALEMBERT CORE +---------------+
void alembert(){
   if(alembert)checkalembert();
}
void checkalembert(){
   lastlot=history(0);
   alembertlastprofit=history(1);
   if(alembertlastprofit>0){
      if(alembertmode==0){
         blots=lastlot-lots*decrement;
         slots=lastlot-lots*decrement;
         if(resetlevel=resetlevel){
               blots=lots;
               slots=lots;
            }
         }
      }
      else{
         blots=lastlot+lots*increment;
         slots=lastlot+lots*increment;
      }
   }
   if(alembertlastprofit=resetlevel){
               blots=lots;
               slots=lots;
            }
         }
      }
      else{
         blots=lastlot-lots*decrement;
         slots=lastlot-lots*decrement;
      }
   }
}
//+------------------------------------------------------------------------------------+ RECOVERY MODE +-------------+
void recovery(){
   if(recoverymode){
      blots=recoverylots(lots);
      slots=recoverylots(lots);
   }
}
double recoverylots(double lots){
   historyprofit=counthistoprofit(1);
   if(prevhistoryprofithistoryprofit)return(recoverymultiplier*lots);
   return(lots);
}
//+---------------------------------------------------------------------------------+ CLOSE FUNCTION +---------------+
void close(int type){
   if(OrdersTotal()>0){
      for(i=OrdersTotal()-1;i>=0;i--){
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(type==3 || type==OP_BUY && OrderType()==OP_BUY){
            if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && (oppositecloseminloss==0 || (oppositecloseminloss>0 && (OrderClosePrice()-OrderOpenPrice())0 && (OrderOpenPrice()-OrderClosePrice())0){
      for(i=OrdersTotal()-1;i>=0;i--){
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderType()==OP_BUY){
            if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && (hidesl && stoploss>0 && nd(OrderClosePrice()-OrderOpenPrice(),dg)0 && nd(OrderClosePrice()-OrderOpenPrice(),dg)>=takeprofit*pt)){
               RefreshRates();OrderClose(OrderTicket(),OrderLots(),Bid,slippage*mt);
            }
         }
         if(OrderType()==OP_SELL){
            if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && (hidesl && stoploss>0 && nd(OrderOpenPrice()-OrderClosePrice(),dg)0 && nd(OrderOpenPrice()-OrderClosePrice(),dg)>=takeprofit*pt)){
               RefreshRates();OrderClose(OrderTicket(),OrderLots(),Ask,slippage*mt);
            }
         }
      }
   }
}
//+-------------------------------------------------------------------------+ PARTIAL CLOSE FUNCTION +---------------+
void partialtp(int type,int mode,double lot,double takeprofit){
   if(OrdersTotal()>0){
      for(i=OrdersTotal()-1;i>=0;i--){
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(lot=lot && (mode==0 && OrderClosePrice()-OrderOpenPrice()>=takeprofit*pt) || mode==1){
               RefreshRates();OrderClose(OrderTicket(),nd(lot,lotdigits),nd(Bid,dg),slippage*mt);
            }
         }
         if(type==3 || type==OP_SELL && OrderType()==OP_SELL){
            if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderLots()>=lot && (mode==0 && OrderOpenPrice()-OrderClosePrice()>=takeprofit*pt) || mode==1){
               RefreshRates();OrderClose(OrderTicket(),nd(lot,lotdigits),nd(Ask,dg),slippage*mt);
            }
         }
      }
   }
}
//+-----------------------------------------------------------------------+ PARTIAL CLOSE FUNCTION 2 +---------------+
void partialtp2(int type,int mode,double lot1,double lot2,double takeprofit){
   if(OrdersTotal()>0){
      for(i=OrdersTotal()-1;i>=0;i--){
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(lot2=minlot && (mode==0 && OrderClosePrice()-OrderOpenPrice()>=takeprofit*pt) || mode==1){
               RefreshRates();OrderClose(OrderTicket(),nd(lot2,lotdigits),nd(Bid,dg),slippage*mt);
            }
         }
         if(type==3 || type==OP_SELL && OrderType()==OP_SELL){
            if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderLots()==nd(lot1,lotdigits) && OrderLots()>=minlot && (mode==0 && OrderOpenPrice()-OrderClosePrice()>=takeprofit*pt) || mode==1){
               RefreshRates();OrderClose(OrderTicket(),nd(lot2,lotdigits),nd(Ask,dg),slippage*mt);
            }
         }
      }
   }
}
//+----------------------------------------------------------------------------+ CLOSE TIME FUNCTION +---------------+
void closetime(int mode,double target,double timeout1,double timeout2){
   tradeprofit=0;tradehour=0;
   if(OrdersTotal()>0){
      for(i=OrdersTotal()-1;i>=0;i--){
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderType()==OP_BUY){
            if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic){
               tradeprofit=nd(OrderClosePrice()-OrderOpenPrice(),dg);
               tradehour=TimeCurrent()-OrderOpenTime();
               if((mode==0 && tradeprofit>=target*pt && tradehour>timeout1*60 && tradehour=target*pt && tradehour>timeout1*60)){
                  RefreshRates();OrderClose(OrderTicket(),OrderLots(),Bid,slippage*mt);
               }
            }
         }
         if(OrderType()==OP_SELL){
            if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic){
               tradeprofit=nd(OrderOpenPrice()-OrderClosePrice(),dg);
               tradehour=TimeCurrent()-OrderOpenTime();
               if((mode==0 && tradeprofit>=target*pt && tradehour>timeout1*60 && tradehour=target*pt && tradehour>timeout1*60)){
                  RefreshRates();OrderClose(OrderTicket(),OrderLots(),Ask,slippage*mt);
               }
            }
         }
      }
   }
}
//+--------------------------------------------------------------------------------+ DELETE FUNCTION +---------------+
void delete(int type){
   if(OrdersTotal()>0){
      for(i=OrdersTotal()-1;i>=0;i--){
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(type!=6 && type!=7 && type!=8)if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderType()==type)OrderDelete(OrderTicket());
         if(type==6)if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP || OrderType()==OP_BUYLIMIT || OrderType()==OP_SELLLIMIT)OrderDelete(OrderTicket());
         if(type==7)if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT)OrderDelete(OrderTicket());
         if(type==8)if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT)OrderDelete(OrderTicket());
      }
   }
}
//+--------------------------------------------------------------------------------------+ CLOSE ALL +---------------+
void closeall(){
   close(OP_BUY);
   close(OP_SELL);
   delete(7);
   delete(8);
}
//+--------------------------------------------------------------------------------+ STOP CONDITIONS +---------------+
void stop(){
   if(oppositeclose){
      if(buy){if(partialexit)partialtp(OP_SELL,1,lots*tp2percentage*0.01,0);else close(OP_SELL);}
      if(sell){if(partialexit)partialtp(OP_BUY,1,lots*tp2percentage*0.01,0);else close(OP_BUY);}
      if(checkclose){if(closestatus=="LONG")close(OP_BUY);if(closestatus=="SHORT")close(OP_SELL);}
      if(partialexit==false && oldestlong>oldestshort && oldestshort!=0)close(OP_SELL);
      if(partialexit==false && oldestshort>oldestlong && oldestlong!=0)close(OP_BUY);
   }
   if(hidetp || hidesl)hideclose();
   if(onecancelother){if(count(OP_SELL)>0)delete(7);if(count(OP_BUY)>0)delete(8);}
   if(partialtp){
      partialtp2(3,0,blots,blots*tp2percentage*0.01,takeprofit2);
      ilot1=blots-(blots*tp2percentage*0.01);
      if((blots*tp2percentage*0.01)=-1*lossestorecover)close(3);
   if(usetimeout){
      closetime(0,target1,timeout1,timeout2);closetime(0,target2,timeout2,timeout3);
      closetime(0,target3,timeout3,timeout4);closetime(0,target4,timeout4,timeout5);
      closetime(0,target5,timeout5,timeout6);closetime(0,target6,timeout6,timeout7);
      closetime(1,target7,timeout7,0);
   }
   if(exitsignal1==1)if(reversesignals)close(OP_SELL);else close(OP_BUY);
   if(exitsignal2==2)if(reversesignals)close(OP_BUY);else close(OP_SELL);
}
//+-------------------------------------------------------------------------+ TRAILING STOP FUNCTION +---------------+
void movetrailingstop(int mode,double trailingstart,double trailingstop){
   RefreshRates();
   if(OrdersTotal()>0){
      for(i=OrdersTotal()-1;i>=0;i--){
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(mode==0){
            if(OrderType()nd(OrderOpenPrice()+trailingstart*pt,dg) && nd(OrderStopLoss(),dg)(nd(Ask+(trailingstop+trailingstep)*pt,dg))) || (OrderStopLoss()==0)){                 
                     OrderModify(OrderTicket(),OrderOpenPrice(),nd(Ask+trailingstop*pt,dg),OrderTakeProfit(),0,Red);
                     return(0);
                  }
               }
            }
         }
         if(mode==1){
            if(OrderType()=nd(OrderOpenPrice()+trailingstart*pt,dg)){
                     OrderModify(OrderTicket(),OrderOpenPrice(),nd(OrderOpenPrice()+trailingstop*pt,dg),OrderTakeProfit(),0,Blue);
                     return(0);
                  }
               }
               if(OrderType()==OP_BUY && OrderStopLoss()!=0){
                  if(nd(Ask,dg)>=nd(OrderOpenPrice()+trailingstart*pt,dg) && nd(Ask,dg)>=nd(OrderStopLoss()+trailingstart*pt,dg)){
                     OrderModify(OrderTicket(),OrderOpenPrice(),nd(OrderStopLoss()+trailingstop*pt,dg),OrderTakeProfit(),0,Blue);
                     return(0);
                  }
               }
               if(OrderType()==OP_SELL && OrderStopLoss()==0){
                  if(nd(Bid,dg)0){
      for(i=OrdersTotal()-1;i>=0;i--){
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderType()nd(price+(MarketInfo(Symbol(),MODE_STOPLEVEL)+MarketInfo(Symbol(),MODE_SPREAD))*pt,dg)){
                  OrderModify(OrderTicket(),OrderOpenPrice(),nd(price,dg),OrderTakeProfit(),0,Blue);
                  return(0);
               }
               if(nd(OrderStopLoss(),dg)>0 && nd(Bid,dg)>nd(price+(MarketInfo(Symbol(),MODE_STOPLEVEL)+MarketInfo(Symbol(),MODE_SPREAD))*pt,dg) && nd(price,dg)>nd(OrderStopLoss(),dg)){
                  OrderModify(OrderTicket(),OrderOpenPrice(),nd(price,dg),OrderTakeProfit(),0,Blue);
                  return(0);
               }
            }
            if(OrderType()==mode){
               if(nd(OrderStopLoss(),dg)==0 && nd(Ask,dg)0 && nd(Ask,dg)0){
      for(i=OrdersTotal()-1;i>=0;i--){
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
            if(OrderSymbol()==Symbol()&& OrderMagicNumber()==magic){
               if(OrderType()==OP_BUY){
                  if(nd(Bid-OrderOpenPrice(),dg)nd(Bid+(trailingprofit+trailingstep)*pt,dg) || nd(OrderTakeProfit(),dg)==0){
                        OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),nd(Bid+trailingprofit*pt,dg),0,Blue);
                        return(0);
                     }
                  }
               }
               if(OrderType()==OP_SELL){
                  if(nd(OrderOpenPrice()-Ask,dg)0){
      for(i=OrdersTotal()-1;i>=0;i--){
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderType()=nd(breakevengain*pt,dg)){
                  if((nd((OrderStopLoss()-OrderOpenPrice()),dg)=nd(breakevengain*pt,dg)){
                  if((nd((OrderOpenPrice()-OrderStopLoss()),dg)0){
      for(i=OrdersTotal()-1;i>=0;i--){
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic){
            if(OrderType()==OP_BUY){
               if(OrderStopLoss()==0 && stoploss>0 && takeprofit==0){
                  RefreshRates();
                  OrderModify(OrderTicket(),OrderOpenPrice(),nd(Ask-stoploss*pt,dg),OrderTakeProfit(),0,Red);
                  return(0);
               }
               if(OrderTakeProfit()==0 && stoploss==0 && takeprofit>0){
                  RefreshRates();
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),nd(Ask+takeprofit*pt,dg),0,Red);
                  return(0);
               }
               if(OrderTakeProfit()==0 && OrderStopLoss()==0 && stoploss>0 && takeprofit>0){
                  RefreshRates();
                  OrderModify(OrderTicket(),OrderOpenPrice(),nd(Ask-stoploss*pt,dg),nd(Ask+takeprofit*pt,dg),0,Red);
                  return(0);
               }
            }
            if(OrderType()==OP_SELL){
               if(OrderStopLoss()==0 && stoploss>0 && takeprofit==0){
                  RefreshRates();
                  OrderModify(OrderTicket(),OrderOpenPrice(),nd(Bid+stoploss*pt,dg),OrderTakeProfit(),0,Red);
                  return(0);
               }
               if(OrderTakeProfit()==0 && stoploss==0 && takeprofit>0){
                  RefreshRates();
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),nd(Bid-takeprofit*pt,dg),0,Red);
                  return(0);
               }
               if(OrderTakeProfit()==0 && OrderStopLoss()==0 && stoploss>0 && takeprofit>0){
                  RefreshRates();
                  OrderModify(OrderTicket(),OrderOpenPrice(),nd(Bid+stoploss*pt,dg),nd(Bid-takeprofit*pt,dg),0,Red);
                  return(0);
               }
            }
         }
      }
   }
}
//+------------------------------------------------------------------------------+ MODIFICATION CORE +---------------+
void modify(){
   if(highlowts){movetrailingstop2(OP_BUY,Low[shift]-highlowtsmargin*pt);movetrailingstop2(OP_SELL,High[shift]+highlowtsmargin*pt);}
   if(breakevengain>0)movebreakeven(breakevengain,breakeven);
   if(trailingstop>0)movetrailingstop(trailingstopmode,trailingstart,trailingstop);
   if(trailing>0)movetrailingstop(0,trailing,trailing);
   if(trailingprofit>0)movetrailingprofit(trailingstart,trailingprofit);
   if(ecn)ecnmodify(stoploss,takeprofit);
}
//+------------------------------------------------------------------------------------+ BASKET CORE +---------------+
void basket(){
   if(basketpercent || basketpips || basketdollars || baskettrailpercent || baskettrailpips || baskettraildollars)checkbasket();
}
void checkbasket(){
   if(basketpercent){
      if(basketpercentflag=="CLOSE")if(countall(1)==0)basketpercentflag="";
      if(countprofit(1)>=nd(profitpercent*0.01*AccountBalance(),2) || countprofit(1)=profitpips || countprofit(0)=profitdollars || countprofit(1)=baskettrailpercenti+nd(trailstartpercent*0.01*AccountBalance(),2)){
         baskettrailpercenti=countprofit(1);
         basketstoppercenti=baskettrailpercenti-(baskettrailpercenti*(trailstartpercent-trailstoppercent)*0.01);//NormalizeDouble(trailstoppctpercent*0.01*baskettrailpercenti,0);
      }
      if(countprofit(1)=baskettrailpipsi+trailstartpips){
         baskettrailpipsi=countprofit(0);
         basketstoppipsi=baskettrailpipsi-(trailstartpips-trailstoppips);//NormalizeDouble(trailstoppctpips*0.01*baskettrailpipsi,0);
      }
      if(countprofit(0)=baskettraildollarsi+trailstartdollars){
         baskettraildollarsi=countprofit(1);
         basketstopdollarsi=baskettraildollarsi-(trailstartdollars-trailstopdollars);//NormalizeDouble(trailstoppctdollars*0.01*baskettraildollarsi,0);
      }
      if(countprofit(0)0){
      losses=consecutive(0);
      lastorderclosetime=0;
      for(i=OrdersHistoryTotal()-1;i>=0;i--){
         OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
         if(lastorderclosetime!=0)break;
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic){
            lastorderclosetime=OrderCloseTime();
         }
      }
      if(losses>=xlosses)if(TimeCurrent()-lastorderclosetimesum[TimeYear(TimeCurrent())-1999])gmtshift=summergmtshift;
   else gmtshift=wintergmtshift;
   string svrdate=Year()+"."+Month()+"."+Day();
   if(mondayfilter){
      nmondayhour=mondayhour+(gmtshift);if(nmondayhour>23)nmondayhour=nmondayhour-24;
      if(nmondayhour9)imondayhour=nmondayhour;
      if(mondayminute9)imondayminute=mondayminute;
      tmonday=StrToTime(svrdate+" "+imondayhour+":"+imondayminute);
   }
   if(weekfilter){
      nstarthour=starthour+(gmtshift);if(nstarthour>23)nstarthour=nstarthour-24;
      if(nstarthour9)istarthour=nstarthour;
      if(startminute9)istartminute=startminute;
      tstart=StrToTime(svrdate+" "+istarthour+":"+istartminute);
      nendhour=endhour+(gmtshift);if(nendhour>23)nendhour=nendhour-24;
      if(nendhour9)iendhour=nendhour;
      if(endminute9)iendminute=endminute;
      tend=StrToTime(svrdate+" "+iendhour+":"+iendminute);
   }
   if(fridayfilter){
      nfridayhour=fridayhour+(gmtshift);if(nfridayhour>23)nfridayhour=nfridayhour-24;
      if(nfridayhour9)ifridayhour=nfridayhour;
      if(fridayminute9)ifridayminute=fridayminute;
      tfriday=StrToTime(svrdate+" "+ifridayhour+":"+ifridayminute);
   }
   if(testhour!=24){
      ntesthour=testhour+(gmtshift);if(ntesthour>23)ntesthour=ntesthour-24;
      if(ntesthour9)itesthour=ntesthour;
      itestminute="00";
      ttesthour=StrToTime(svrdate+" "+itesthour+":"+itestminute);
   }
   if(weekfilter)if((nstarthourtend) || (nstarthour>nendhour && TimeCurrent()tend))return(true);
   if(tradesunday==false)if(DayOfWeek()==0)return(true);
   if(fridayfilter)if(DayOfWeek()==5 && TimeCurrent()>tfriday)return(true);
   if(mondayfilter)if(DayOfWeek()==1 && TimeCurrent()0){
            while(FileIsEnding(handle)==false){
               if(FileIsEnding(handle))break;
               date=FileReadString(handle);
               csvdate=StringSubstr(date,1,4);
               csvmonth=StringSubstr(date,6,2);
               csvday=StringSubstr(date,9,2);
               csvhour=StringSubstr(date,14,5);
               csvcurrency=StringSubstr(date,24,3);
               csvdatetime=StrToTime(csvdate+"."+csvmonth+"."+csvday+" "+csvhour);
               cur1=StringSubstr(Symbol(),0,3);
               cur2=StringSubstr(Symbol(),3,3);
               if((csvdatetime-Time[0]>=0 && csvdatetime-Time[0]=0 && Time[0]-csvdatetime<minutesafter*60) && cur1==csvcurrency || cur2==csvcurrency)FileClose(handle);return(true);
            }
         }
         FileClose(handle);
         newstime=Time[0];
      }
   }
   if(IsTesting()==false){
      static int prevminute=-1;
      if(Minute()!=prevminute){
         prevminute=Minute();
         int minsincenews=iCustom(NULL,0,"FFCal",true,true,false,true,true,1,0);
         int mintonews=iCustom(NULL,0,"FFCal",true,true,false,true,true,1,1);
         int impactsincenews=iCustom(NULL,0,"FFCal",true,true,false,true,true,2,0);
         int impacttonews=iCustom(NULL,0,"FFCal",true,true,false,true,true,2,1);
         if(impacthigh && (mintonews<=minutesbefore &&  impacttonews==1) || (minsincenews<=minutesafter && impactsincenews==1))return(true);
         if(impactmedium && (mintonews<=minutesbefore &&  impacttonews==2) || (minsincenews<=minutesafter && impactsincenews==2))return(true);
         if(impactlow && (mintonews<=minutesbefore &&  impacttonews==3) || (minsincenews<=minutesafter && impactsincenews==3))return(true);
      }
   }
   return(false);
}
//+-----------------------------------------------------------------------------+ NORMALIZE FUNCTION +---------------+
double nd(double value,double digits){
   double val;
   val=NormalizeDouble(value,digits);
   return(val);
}
收藏
收藏0
转播
转播
分享
分享
分享
淘帖0

精彩评论8

跳转到指定楼层
沙发
小优 发表于 2017-5-25 11:01:26 | 只看该作者
不会写,哈哈,,不过楼主可以看看我的EA
板凳
JoyYueLin 发表于 2017-5-27 19:39:29 | 只看该作者
研究一下。
地板
henaiyun 发表于 2017-5-28 20:50:51 | 只看该作者
改好了。错误没有了
5#
a77887 发表于 2017-6-6 20:51:54 | 只看该作者

改好了。错误没有了     wo xial shishi  
6#
a77887 发表于 2017-6-6 21:00:32 | 只看该作者
改好了。错误没有了     wo xial shishi  
7#
Q617058048 发表于 2017-6-19 14:55:24 | 只看该作者
....................
8#
dongmu 发表于 2017-6-23 23:59:39 | 只看该作者
......
9#
a77887 发表于 2017-6-24 00:06:34 | 只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

发布主题
阅读排行更多+

Powered by 顺水鱼MT4外汇EA网! X3.2© 2001-2017 顺水MT4外汇EA公司.( 陕ICP备17014341号-1