re-synchronization problem

To add comments, please log in or register
Tsering Dawa Lama
228
Tsering Dawa Lama  
re-synchronization problem. i need more help . here is what i think. After RE-synchronization the open orders on the vps get automatically closed because  Global Variables and orderhistory is used for closing conditions. for my ea to run correctly the global variables and the history orders should NOT be RE-synchronized.  the local terminal has different global variables and history orders than the vps terminal some time  after the last sync. could this be the problem? if it is, then there should be an option during re-synchronization whether to sync Global Variables and/or history orders or not. for my ea i will choose not to sync them. here is the original ea based on which i made my own ea. the basic structure for closing orders are the same. 
//+------------------------------------------------------------------+
//|                                         SWB_SuperTrend_V1.01.mq4 |
//|                                Copyright © 2010, Dennis Hamilton |
//|                                              ramble_32@yahoo.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Dennis Hamilton"
#property link      "ramble_32@yahoo.com"
//----
extern string  Separator_01="----  Input  Settings  ----";
extern double   lot=0.01;
extern double   tp_1=25;
extern double   tp_2=100;
extern int      magic=123;
extern int      bars_closed=0;
extern string  Separator_02="----  Range  &  Offsets  ----";
extern double   range=40;
extern int      max_level=10;
extern int      curb_level=2;
extern double   p1_offset=0;
extern double   p2_offset=0;
extern int      pend_delete=30;
extern double   profit_target=3.0;
extern double   curb_factor=0.5;
extern bool     stealth=false;
extern bool     tier2_sig=false;
extern string  Separator_03="----  Lot  Settings  ----";
extern bool     lot_multiplier=false;
extern double   multiplier=2.0;
extern double   increment=0.02;
extern bool     power_lots=true;
extern string  Separator_04="----  Additional  ----";
extern double   deviation_reset=0;
extern int      reset_level=3;
extern bool     closeall=false;
extern bool     stop_ea=false;
extern bool    hedge=true;
//----
double  pt;
double  p_off;
double  p1_off;
double  p2_off;
double  std=0.1;
double  AE1;
double  p_lot;
double  lot2;
double  bal_2;
double  incr_1;
double  b_price;
double  s_price;
double  d_reset;
int     OT;
bool    deleteall;
bool    b_send;
bool    s_send;
string  f_trade="NULL";
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   pt=Point; if(Digits==3 || Digits==5) pt=10*Point;//Point=0.00001
   tp_1*=pt;
   tp_2*=pt;
   range*=pt;
   if(p1_offset<5.0) p1_offset=0.0;
   if(p2_offset<5.0) p2_offset=0.0;
   p1_off=p1_offset*pt;
   p2_off=p2_offset*pt;
   d_reset=deviation_reset;
   if(curb_level<2) curb_level=0;
//----
   AE1=AccountEquity(); p_lot=lot/AE1;
   if(StringLen(Symbol())>6) std=1.0;
   profit_target*=std;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   int total=0, t_cnt=0, p_cnt=0, b_cnt=0, s_cnt=0, b2_cnt=0, s2_cnt=0;
   int b3_cnt=0, s3_cnt=0;  double FOOP, B_LOOP, S_LOOP;
   for(int i=0; i<OrdersTotal(); i++)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) break;
      if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;
      total++;
//----
      if(cmd()< 2) t_cnt++;
      if(cmd()> 1) p_cnt++;
      if(cmd()==0){ b_cnt++; b3_cnt++; } if(cmd()==4){ b2_cnt++; b3_cnt++; }
      if(cmd()==1){ s_cnt++; s3_cnt++; } if(cmd()==5){ s2_cnt++; s3_cnt++; }
      if(b3_cnt==1 || (b3_cnt>1 && OOP()<B_LOOP)) B_LOOP=OOP();
      if(s3_cnt==1 || (s3_cnt>1 && OOP()>S_LOOP)) S_LOOP=OOP();
      if(b_cnt ==1 || (b_cnt >1 && OOP()>FOOP)) FOOP=OOP();
      if(s_cnt ==1 || (s_cnt >1 && OOP()<FOOP)) FOOP=OOP();
   }
//----
   string H=""; if(stealth) H="_S"; if(hedge) H="_H"; if(stealth && hedge) H="_SH";
   if(total==0) f_trade="Null"+H;
   if(t_cnt==0 && p_cnt==1)  f_trade="Pend"+H;
   if(t_cnt==1 && p_cnt==0){ f_trade="Buy"+H; if(s_cnt==1) f_trade="Sell"+H; }
//+------------------------------------------------------------------+
   for(i=0; i<OrdersTotal(); i++)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) break;
      if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;
      double OTP=OrderTakeProfit(), tp_3=tp_1;
      if(cmd()<2 && OTP==0)
      {
         if(t_cnt>1) tp_3=tp_2; if(cmd()==1) tp_3*=-1;
         if(!stealth || (stealth && OrderComment()=="L1")) OTP=OOP()+tp_3;
         if(OTP!=0){ bool res = OrderModify(OrderTicket(),0,0,OTP,0,CLR_NONE); deleteall=true; }
      }
//----
      if(t_cnt==0 && pend_delete>0)
      {
         datetime OOT=OrderOpenTime(), get_time=TimeCurrent();
         if(cmd()>1 && get_time-OOT>pend_delete*60) bool resdel = OrderDelete(OrderTicket());
      }
   }
//+------------------------------------------------------------------+
   double o_charts=GlobalVariableGet("open_charts"); o_charts=1; // Global currently by-passed
   double AB=AccountBalance(), pl_factor=1/o_charts; lot2=lot/o_charts;
//----
   if(power_lots)
   {
      if(AB>AE1) lot2=NormalizeDouble((p_lot*AB)/o_charts,Digits);
      pl_factor=(lot2/lot)/o_charts; double lot3=lot2;
   }
//----
   double p_targ=profit_target*pl_factor, b_lot=lot2, s_lot=lot2; incr_1=increment*pl_factor;
//+------------------------------------------------------------------+
   bool t_buy=false, t_sell=false, t2_buy=false, t2_sell=false;
   if((p1_off==0 && t_cnt==0) || (p1_off>0 && b3_cnt==0 && (s_cnt==0 || f_trade=="Buy"+H)))  t_buy=true;
   if((p1_off==0 && t_cnt==0) || (p1_off>0 && s3_cnt==0 && (b_cnt==0 || f_trade=="Sell"+H))) t_sell=true;
   if(f_trade=="Buy"+H  && b3_cnt>0 && b_cnt<max_level) t2_buy=true;
   if(f_trade=="Sell"+H && s3_cnt>0 && s_cnt<max_level) t2_sell=true;
//----
   if(total==0){ closeall=false; b_send=false; s_send=false; }
   if(b2_cnt==0 && s2_cnt==0) deleteall=false;
   if(!stop_ea && !closeall && !deleteall)
   {
      if(t_buy || (t2_buy && Ask<=B_LOOP-range-p2_off)) // BUY
      {
         if(signal()==0 || (b_cnt>0 && !tier2_sig))
         {
            if(lot_multiplier)  b_lot=lot2*MathPow(multiplier,b_cnt);
            if(!lot_multiplier) b_lot=lot2+(incr_1*b_cnt);
            OT=0; p_off=p1_off;
            if(b_cnt>0) p_off=p2_off; if(p_off>0) OT=4;
            //----
            resdel = OrderSend(Symbol(),OT,b_lot,Ask+p_off,3,0,0,"L"+(b_cnt+1),magic,0,Blue);
         }
      }
//----
      if(t_sell || (t2_sell && Bid>=S_LOOP+range+p2_off)) // SELL
      {
         if(signal()==1 || (s_cnt>0 && !tier2_sig))
         {
            if(lot_multiplier)  s_lot=lot2*MathPow(multiplier,s_cnt);
            if(!lot_multiplier) s_lot=lot2+(incr_1*s_cnt);
            OT=1; p_off=p1_off;
            if(s_cnt>0) p_off=p2_off; if(p_off>0) OT=5;
            //----
            resdel = OrderSend(Symbol(),OT,s_lot,Bid-p_off,3,0,0,"L"+(s_cnt+1),magic,0,Red);
         }
      }
   }
//+------------------------------------------------------------------+
   double profit=0, profit_1=0, profit_2=0, profit_3=0, t_lot=0, b2_lot=0, s2_lot=0;
   for(i=0; i<OrdersTotal(); i++)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) break;
      if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;
      profit+=OrderProfit();
      if(t_cnt>2 && OT2()=="L")
      {
         if(OrderComment()=="L"+t_cnt)     profit_1=OrderProfit();
         if(OrderComment()=="L"+(t_cnt-1)) profit_2=OrderProfit();
         profit_3=profit_1+profit_2;
      }
      if(cmd()< 2) t_lot+=OrderLots();
      if(cmd()==0){ b2_lot+=OrderLots(); if(OOP()==FOOP) b_price=OOP(); }
      if(cmd()==1){ s2_lot+=OrderLots(); if(OOP()==FOOP) s_price=OOP(); }
   }
//----
   double dev_b, dev_s, deviate=0;
   if(b_cnt>0){ dev_b=b_price-Ask; if(dev_b>0) deviate=dev_b/pt; }
   if(s_cnt>0){ dev_s=Bid-s_price; if(dev_s>0) deviate=dev_s/pt; }
//+------------------------------------------------------------------+
   for(i=OrdersTotal()-1; i>=0; i--)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) break;
      if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;
      if(cmd()<2)
      {
         if(deviate>=d_reset && d_reset>0 && t_cnt>=reset_level)
         {
            if(OOP()==FOOP)
            bool closeorder = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,CLR_NONE);
         }
         if(t_cnt>=curb_level && profit_3>=p_targ*curb_factor+3*MathAbs(OrderCommission()+OrderSwap()))
         {
            if(OCM()=="L"+t_cnt || OCM()=="L"+(t_cnt-1))
            bool closeorder2 = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,CLR_NONE);
         }
      }
   }
//+------------------------------------------------------------------+
 double balance=0;
   for(i=0; i<OrdersHistoryTotal(); i++)
   {
     if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) break ;
      if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;
      balance+=OrderProfit();
   }
//----
   if(t_cnt==0) GlobalVariableSet("bal_2"+Symbol()+magic,balance);
   bal_2=GlobalVariableGet("bal_2"+Symbol()+magic);
//+------------------------------------------------------------------+
   if(balance+profit>=bal_2+p_targ) closeall=true;
   if(closeall || deleteall)
   {
      for(i=OrdersTotal()-1; i>=0; i--)
      {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) break;
         if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;
         if(closeall) deleteall=true;
         if(cmd()>1 && deleteall) closeorder2 = OrderDelete(OrderTicket());
         if(cmd()<2 && closeall)  closeorder = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,CLR_NONE);
      }
   }
//+------------------------------------------------------------------+
   string multi="Multiplier = "+DoubleToStr(multiplier,2), tier2="Yes", dev_r="";
   if(!lot_multiplier) multi="Increment = "+DoubleToStr(incr_1,2);
   if(!tier2_sig) tier2="No"; if(d_reset>0) dev_r=" / "+DoubleToStr(d_reset,0)+" / "+t_cnt+" / "+reset_level;
//----
   string line_1="Balance = "+DoubleToStr(bal_2,2)+"  |  Profit = "+DoubleToStr(bal_2+profit,2)+"  |  Equity = "+DoubleToStr(AccountEquity(),2)+"  |  Target = "+DoubleToStr(profit_target,2)+" / "+DoubleToStr(p_targ,2)+" / "+DoubleToStr(profit,2)+"\n";
   string line_2="Start Lot = "+DoubleToStr(lot,2)+"  |  Power lots = "+DoubleToStr(lot3,2)+"  |  Open = "+b_cnt+" / "+s_cnt+"  |  Pending = "+b2_cnt+" / "+s2_cnt+"  |  Deviate = "+DoubleToStr(deviate,0)+dev_r+"\n";
   string line_3="OPT = "+f_trade+"  |  "+multi+"  |  Spread = "+DoubleToStr((Ask-Bid)/pt,2)+"  |  Total Lots = "+DoubleToStr(t_lot,2)+"  |  Tier 2 = "+tier2+"\n";
//----
   Comment(line_1, line_2, line_3);
//----
   return(0);
  }
//+------------------------------------------------------------------+
int cmd()
  { int cmd=OrderType();
    return(cmd); }
//----
double OOP()
  { double OOP=OrderOpenPrice();
    return(OOP); }
//----
string OCM()
  { string OCM=OrderComment();
    return(OCM); }
//----
string OT2()
  { string OT2=StringSubstr(OrderComment(),0,1);
    return(OT2); }
//+------------------------------------------------------------------+
int signal()
  {
   int signal=2;
   if(iCustom(NULL,0,"SuperTrend",10,0.3,0,0)!=EMPTY_VALUE) signal=0;
   if(iCustom(NULL,0,"SuperTrend",10,0.3,1,0)!=EMPTY_VALUE) signal=1;
   return(signal);
  }
//+------------------------------------------------------------------+


To add comments, please log in or register