Navigation:Home > Content >

Pendulum_2_05.mq4

Time: 2014-06-18 | Download file:Pendulum_2_05.mq4

// Translated Russian to English - by Capella 2011-11-10

//+------------------------------------------------------------------+
//|                                                Pendulum 2_05.mq4 |
//|                                              Copyright © BFE2006 |
//|                                         http://[email protected] |
//+------------------------------------------------------------------+
#property copyright "Copyright © BFE2006"
#property link      "http://[email protected]"

int      MAGIC=0;         /// In order to manually open MAGIC=0. 
double   Lot;                     //Lot for the installation warrants
extern string   text1="--- Choice of larger lots ---";
extern double   ruka=9;           //Updated by hand the lot with a negative developments.
extern string   text2="--- Select the level of risk ---";
extern string   text3="risk = 0: risk level is lowered, the percentage of profit below";
extern string   text4="range: 1% - 0.1%";
extern string   text5="risk = 1: risk is greater, the percentage of profit above";
extern string   text6="range: 1.7% - 0.5%";
//extern double   risk=1;
extern string   text7="--- The choice of the distance between orders ---";
extern string   text8="--- If 0 is selected automatically (from 70-100 points) ---";
extern double   rastoyanie=0;
extern string   text9="--- The choice of the calculation of profit ---";
extern string   text10="--- If procent = 0, profit - the percentage of the deposit ---";
extern string   text11="--- If procent = 1, use the TRAILING STOP ---";
extern double   PP=100;
extern double   procent=1;
extern string   text12="--- Trailing Stop ---";
extern string   text13="--- If TRS = 0, the minimum ---";
extern double   TRS=0;
extern string   text14="--- Initial deposit ---";
extern double   Start_Depo=3000;
bool     Auto=true;               //If incl. Automatically calculated the lot size, hands and
                                  //global stops.
bool     Ustan_BuyStop=true;      //Can I put Buy-Stop if such a market we have.
bool     Ustan_SellStop=true;     //Can I put Sell-Stop if such a market we have.
int      st_Step;                 //The distance in pixels from the current price level
                                  //unable to install the stop order.
int      st_TrStop;               //Trailing Stop Orders Stop.
                                  //If = 0 then there is no st_TrStep Tarle is not important.
int      st_TrStep;               //Step trail stop orders.
int      PipsProfit;              //Profit at scalping you can put 1, 2, 3, ...
int      Proskalz=5;              //Slippage in points (only when necessary PipsProfit> 0).
double   pipsruka;                //Pipsing for orders from his hand.
bool     UseGlobalLevels=true;    //On / Off. otslezhianiya global levels of profit / loss
                                  //if UseGlobalLevels = false, the value
                                  //Global_TakeProfit and Global_StopLoss not important.
double   Global_TakeProfit;       //Global Take-Profit (given as a percentage).
double   Global_StopLoss;         //Global Stop-loss (defined as a percentage).
// ---------- Прочие переменные --------------------------------------- 
 
double lot,sts,PNT,NewPrice,Price,Balans,Free,pips,glp,klv,prb,sred_profit;
double s_buy,s_sell,s_buystop,s_sellstop,s_buy_lot,s_buystop_lot,s_sell_lot,s_sellstop_lot;   //открытые и отложенные ордера
string SMB,com1,com2,com3,com4,com5;
int    MinLevel;
bool   fm,b,s;
double prc2,prc1,prc08,prc06,prc04,prc02,l1,l2,raznost,t_raznost,L,H,sr,zzz;
double risk,prov,price_buy,price_sell,uroven_stop,izm,buy_sell,lll,mmm,nnn,vvv,min_lot,lot_ruka,MRG,T,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,balanc1,balanc2,balanc3,balanc4,balanc5,balanc6,balanc7,balanc8,balanc9,balanc10;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//------------ Проверяем вкл. советника ------------------------------
  if(!IsExpertEnabled())//если ложь 
   {Alert("Error! Button is clicked * * Advisors");}
   else 
   {Comment("Once the price has changed, Advisor will work.");}
   
//------------- Запоминаем начальные значения ------------------------ 
  SMB=Symbol();                           //Symbol of the currency pair
  PNT=MarketInfo(SMB,MODE_POINT);         //The size of the item in the quote currency. 
                                          //For the current symbol is stored in a predefined
                                          //variable Point
  MinLevel=MarketInfo(SMB,MODE_STOPLEVEL);//The minimum acceptable level
                                          //stop loss / take profit in paragraphs
 
//--------------------------------------------------------------------
    return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {

  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   ObjectCreate("label_object1", OBJ_LABEL, 0, 0, 0); 
   ObjectSet("label_object1", OBJPROP_XDISTANCE, 500);  
   ObjectSet("label_object1", OBJPROP_YDISTANCE, 17);
   ObjectSetText("label_object1", "Pendulum™  version 2_05_2!",9, "Verdana", Red);
   
   
   
//---- Check automatic operation
if (Auto==true){Auto();}
MinLevel=MarketInfo(SMB,MODE_STOPLEVEL);//The minimum acceptable level
                                        //stop-lossa/teyk-profita in paragraphs
if (TRS==0){TRS=MinLevel;}
//---- Determine the percentage of profit for the global
if(risk==1)
{
Global_TakeProfit=3;
}

for(int i=1;i<=48;i++)
  {
  L=L+iLow(Symbol(),PERIOD_D1,1);
  H=H+iHigh(Symbol(),PERIOD_D1,1);
  }
//----
sr=((H-L)/i)*10000;
sr=(NormalizeDouble((sr),0));
PipsProfit=sr/10;
PipsProfit=(NormalizeDouble((PipsProfit),0)); if(PipsProfit<1){PipsProfit=1;}
st_Step=(sr-PipsProfit-Proskalz)/2;           if(st_Step<100){st_Step=100;}
                                              //if(st_Step>100){st_Step=100;}
st_TrStop=(sr-PipsProfit-Proskalz)/2;         if(st_TrStop<100){st_TrStop=100;}
                                              //if(st_TrStop>100){st_TrStop=100;}
                                              if(rastoyanie>0){st_TrStop=rastoyanie;st_TrStop=rastoyanie;}
vvv=st_TrStop;

L=0;H=0;

//---- Remember the initial values:
  lot=Lot;           //Lot
  sts=st_TrStop;     //Trail stop orders
  Balans=AccountBalance();
  Free=AccountEquity();
// pips=PipsProfit;
  glp=Balans+(Balans/100*Global_TakeProfit);
  glp=(NormalizeDouble((glp),2));
  
  if(OrdersHistoryTotal()>0){chet();}
//--------------------------------------------------------------------
//----------------- We consider open and pending orders -------------
for (int q=OrdersTotal(); q>=0; q--)
  {
  if (OrderSelect(q,SELECT_BY_POS,MODE_TRADES)==true)
    {
    if(OrderSymbol()!=SMB || OrderMagicNumber()!=MAGIC)continue;
      {          
      if(OrderType()==OP_BUY)      {s_buy++; s_buy_lot=s_buy_lot+OrderLots();}
      if(OrderType()==OP_BUYSTOP)  {s_buystop++; s_buystop_lot=s_buystop_lot+OrderLots();}
      if(OrderType()==OP_SELL)     {s_sell++; s_sell_lot=s_sell_lot+OrderLots();}
      if(OrderType()==OP_SELLSTOP) {s_sellstop++;s_sellstop_lot=s_sellstop_lot+OrderLots();}
      }
    }
  }
 
//------- Check the need for placing orders starting ------
if (s_buy+s_sell==0) {ustorder();}


//------- Check the need for starting Trail orders -----------
if (s_buy+s_sell>0){st_TrStop=0;com1="Trail stop orders - off. (";} //If there is an open order off. Trail stop
if(st_TrStop>0){tr_stop();com1="Trail stop orders - on. (";}//We proceed to Trail

//------------ Check the need for decreasing the shoulder --------------
//- 1
if (s_buy+s_sell==1){risk=1;}
//- 2
if (s_buy+s_sell==2){Global_TakeProfit=1;}
//- 3
if (s_buy+s_sell==3){Global_TakeProfit=1;}
//- 4
if (s_buy+s_sell==4){Global_TakeProfit=1;}

//------------------------------------------
//- 1
if (s_buy+s_sell==1){plecho1();}
//- 2
if (s_buy+s_sell==2){plecho2();}
//- 3
if (s_buy+s_sell==3){plecho3();}
//- 4
if (s_buy+s_sell==4){plecho4();}
//- 5
if (s_buy+s_sell==5){plecho5();}
//- 6
if (s_buy+s_sell==6){plecho6();}

//--- If necessary, the number of oscillations of the pendulum can be increased

//--------------- Check the on / off pipsing ------------------------
if (s_buy+s_sell>1 && UseGlobalLevels==true){PipsProfit=0;com2="Pipsing - off. (";}
if (s_buy+s_sell>1 && UseGlobalLevels==false){pips();com2="Pipsing - on. (";}
if (s_buy+s_sell==1){pips();com2="Pipsing - on. (";}
if (s_buy+s_sell==0){com2="Pipsing - on. (";}
if (PipsProfit==0 || PipsProfit>4)com3=" items";
if (PipsProfit==1)com3=" пункт";
if (PipsProfit>2 && PipsProfit<5)com3=" punkta";

//-------------- Computes foot -----------------------------------
if(s_buy_lot-s_sell_lot>0)
  {
  buy_sell=MathAbs(s_buy_lot-s_sell_lot);
  izm=MarketInfo(SMB,MODE_TICKVALUE)*buy_sell;
  uroven_stop=zzz/izm;
  uroven_stop=(NormalizeDouble((uroven_stop),0));
  }

if(s_buy_lot-s_sell_lot<0)
  {
  buy_sell=MathAbs(s_buy_lot-s_sell_lot);
  izm=MarketInfo(SMB,MODE_TICKVALUE)*buy_sell;
  uroven_stop=zzz/izm;
  uroven_stop=(NormalizeDouble((uroven_stop),0));
  }  
  
//--------------- Check the global levels of ------------------------
if (s_buy+s_sell>0 && s_buy+s_sell<11   && UseGlobalLevels==true && (Free-Balans)>=(Balans/100*Global_TakeProfit)){glob_profit();}
if ((Balans-Free)>=((Balans-Start_Depo)/100*PP)){glob_stop();}

//----------------- derive information -------------------------------
if(Lot>0)
{ 
zzz=glp-AccountEquity(); 
Comment("Open orders for ",SMB,":","\n",
        "buy  - ",s_buy," (",s_buy_lot,")","   sell  - ",s_sell," (",s_sell_lot,")","\n",
        "buystop  - ",s_buystop," (",s_buystop_lot,")","  sellstop  - ",s_sellstop," (",s_sellstop_lot,")","\n",
        com1,vvv,")","\n",
        "--- Global profit ---","\n",glp," ",AccountCurrency()," (",Global_TakeProfit," %)","\n","It remains up to the level  ",zzz," ",AccountCurrency(),
        "  (",uroven_stop,"  item (s)","\n","Number of transactions - ",klv,"\n","profit - ",prb," ",AccountCurrency(),"\n",
        " - ",sred_profit," ",AccountCurrency(),"\n");
}
if(Lot==0)
{
balanc1=(NormalizeDouble((balanc1),2));
Comment(com4,"\n",com5,"\n","minimum - ",balanc1," ",AccountCurrency());
}
        
//----------------- zero out counters --------------------------------
  if (s_buy+s_sell==1) {TRS=15;}
  if (s_buy+s_sell> 1) {TRS=0 ;}
s_buy=0;s_buystop=0;s_sell=0;s_sellstop=0;s_buy_lot=0;s_buystop_lot=0;s_sell_lot=0;s_sellstop_lot=0;
st_TrStop=sts;Lot=lot;PipsProfit=pips;sred_profit=0;
sred_profit=prb/klv;
sred_profit=(NormalizeDouble((sred_profit),2));
klv=0;prb=0;price_buy=0;price_sell=0;       
   return(0);
   
  }
//====================================================================
//----------------- Put a start pending orders ---------------
void ustorder()
{
  if(s_sellstop==0 && st_Step>=MinLevel)
   {
   NewPrice=Bid-st_Step*Point;
   fm=OrderSend(SMB,OP_SELLSTOP,Lot,NewPrice,3,0,0,NULL,MAGIC,0,Red);
   PlaySound("Wait.wav");
   }
  if(s_buystop==0 && st_Step>=MinLevel)
   {
   NewPrice=Ask+st_Step*Point;
   fm=OrderSend(SMB,OP_BUYSTOP,Lot,NewPrice,3,0,0,NULL,MAGIC,0,Blue);
   PlaySound("Wait.wav");
   }
}
//----------------- Trail stop order ---------------------------
void tr_stop()
{
  for(int w=OrdersTotal();w>=0;w--)
    {
    if (OrderSelect(w,SELECT_BY_POS,MODE_TRADES)==true)
      {
      if(OrderType()==OP_BUYSTOP && OrderOpenPrice()>Ask+(st_TrStop+st_TrStep)*Point)
        {NewPrice=Ask+st_TrStop*Point;fm=OrderModify(OrderTicket(),NewPrice,0,0,0);
        PlaySound("Expert.wav");}
      if(OrderType()==OP_SELLSTOP && OrderOpenPrice()=0;e--)
  {
  if (OrderSelect(e,SELECT_BY_POS,MODE_TRADES)==true)
    {
    if (OrderLots()>lot){OrderDelete(OrderTicket());}
    if(OrderProfit()<0){OrderClose(OrderTicket(),OrderLots(),Bid,5);PlaySound("Error.wav");}
    if(OrderProfit()<0){OrderClose(OrderTicket(),OrderLots(),Bid,5);PlaySound("Error.wav");}
    }
  }
}
//------------------ Working with global levels -------------------
//====================================================================
void glob_profit()
{
if (procent==0)
  {
  for (int i=OrdersTotal();i>=0; i--)
    {
    if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
      {
      if(OrderType()==OP_BUY){OrderClose(OrderTicket(),OrderLots(),Bid,3);PlaySound("Error.wav");}
      if(OrderType()==OP_SELL){OrderClose(OrderTicket(),OrderLots(),Ask,3);PlaySound("Error.wav");}
      }
    }
   zachistka();
  }
if (procent==1)
  {
  for (int it=OrdersTotal();it>=0; it--)
    {
    if (OrderSelect(it,SELECT_BY_POS,MODE_TRADES)==true)
      {
      if(OrderProfit()<0){OrderClose(OrderTicket(),OrderLots(),Bid,7);PlaySound("Error.wav");}
      if(OrderProfit()<0){OrderClose(OrderTicket(),OrderLots(),Ask,7);PlaySound("Error.wav");}
      }
    }
  for (int eit=OrdersTotal();eit>=0; eit--)
    {
    if (OrderSelect(eit,SELECT_BY_POS,MODE_TRADES)==true)
      {
      if(OrderProfit()>0 && OrderType()==OP_BUY && Bid-OrderOpenPrice()>TRS*Point)
        {if(OrderStopLoss()0 && OrderType()==OP_SELL && OrderOpenPrice()-Ask>TRS*Point)
        {if(OrderStopLoss()>Ask+TRS*Point || OrderStopLoss()==0)
          {OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TRS*Point,0,0,CLR_NONE);PlaySound("Error.wav");}
        }
      if(OrderProfit()<0){OrderClose(OrderTicket(),OrderLots(),Bid,3);PlaySound("Error.wav");}
      }
    }
  }
  zachistka();
}
//====================================================================
void glob_stop()
  {
  for (int i=OrdersTotal();i>=0; i--)
    {
    if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
     {
      if(OrderType()==OP_BUY) {OrderClose(OrderTicket(),OrderLots(),Bid,3);}
      if(OrderType()==OP_SELL){OrderClose(OrderTicket(),OrderLots(),Ask,3);}
     }
      
    }
    zachistka();
  }

//-------------------------- shoulder x1 --------------------------------
void plecho1()
{//Determine which way to increase
  for (int e=OrdersTotal();e>=0;e--)
    {
    if (OrderSelect(e,SELECT_BY_POS,MODE_TRADES)==true)
      {
      if (OrderType()==OP_BUYSTOP && OrderLots()=0;e--)
    {
    if (OrderSelect(e,SELECT_BY_POS,MODE_TRADES)==true)
      {
      if(OrderLots()==lot*ruka*2 && OrderType()==OP_BUYSTOP){return(0);}
      if(OrderLots()==lot*ruka*2 && OrderType()==OP_SELLSTOP){return(0);}
      if(OrderLots()==lot && OrderType()==OP_BUY){Price=OrderOpenPrice();Lot=lot*ruka*2;
      fm=OrderSend(SMB,OP_BUYSTOP,Lot,Price,3,0,0,NULL,MAGIC,0,Lime);PlaySound("Ok.wav");}
      if(OrderLots()==lot && OrderType()==OP_SELL){Price=OrderOpenPrice();Lot=lot*ruka*2;
      fm=OrderSend(SMB,OP_SELLSTOP,Lot,Price,3,0,0,NULL,MAGIC,0,Orange);PlaySound("Ok.wav");}
      }
    }
}
//-------------------------- shoulder x3 --------------------------------
void plecho3()
{
    for (int e=OrdersTotal();e>=0;e--)
    {
    if (OrderSelect(e,SELECT_BY_POS,MODE_TRADES)==true)
      {
      if(OrderLots()==lot*ruka*4 && OrderType()==OP_BUYSTOP){return(0);}
      if(OrderLots()==lot*ruka*4 && OrderType()==OP_SELLSTOP){return(0);}
      if(OrderLots()==lot*ruka && OrderType()==OP_BUY){Price=OrderOpenPrice();Lot=lot*ruka*4;
      fm=OrderSend(SMB,OP_BUYSTOP,Lot,Price,3,0,0,NULL,MAGIC,0,Lime);PlaySound("Ok.wav");}
      if(OrderLots()==lot*ruka && OrderType()==OP_SELL){Price=OrderOpenPrice();Lot=lot*ruka*4;
      fm=OrderSend(SMB,OP_SELLSTOP,Lot,Price,3,0,0,NULL,MAGIC,0,Orange);PlaySound("Ok.wav");}
      }
    }
}
//-------------------------- shoulder x4 --------------------------------
void plecho4()
{
    for (int e=OrdersTotal();e>=0;e--)
    {
    if (OrderSelect(e,SELECT_BY_POS,MODE_TRADES)==true)
      {
      if(OrderLots()==lot*ruka*8 && OrderType()==OP_BUYSTOP){return(0);}
      if(OrderLots()==lot*ruka*8 && OrderType()==OP_SELLSTOP){return(0);}
      if(OrderLots()==lot*ruka*2 && OrderType()==OP_BUY){Price=OrderOpenPrice();Lot=lot*ruka*8;
      fm=OrderSend(SMB,OP_BUYSTOP,Lot,Price,3,0,0,NULL,MAGIC,0,Lime);PlaySound("Ok.wav");}
      if(OrderLots()==lot*ruka*2 && OrderType()==OP_SELL){Price=OrderOpenPrice();Lot=lot*ruka*8;
      fm=OrderSend(SMB,OP_SELLSTOP,Lot,Price,3,0,0,NULL,MAGIC,0,Orange);PlaySound("Ok.wav");}
      }
    }
}
//-------------------------- shoulder x5 --------------------------------
void plecho5()
{
    for (int e=OrdersTotal();e>=0;e--)
    {
    if (OrderSelect(e,SELECT_BY_POS,MODE_TRADES)==true)
      {
      if(OrderLots()==lot*ruka*16 && OrderType()==OP_BUYSTOP){return(0);}
      if(OrderLots()==lot*ruka*16 && OrderType()==OP_SELLSTOP){return(0);}
      if(OrderLots()==lot*ruka*4 && OrderType()==OP_BUY){Price=OrderOpenPrice();Lot=lot*ruka*16;
      fm=OrderSend(SMB,OP_BUYSTOP,Lot,Price,3,0,0,NULL,MAGIC,0,Lime);PlaySound("Ok.wav");}
      if(OrderLots()==lot*ruka*4 && OrderType()==OP_SELL){Price=OrderOpenPrice();Lot=lot*ruka*16;
      fm=OrderSend(SMB,OP_SELLSTOP,Lot,Price,3,0,0,NULL,MAGIC,0,Orange);PlaySound("Ok.wav");}
      }
    }
}
//-------------------------- shoulder x6 --------------------------------
void plecho6()
{
    for (int e=OrdersTotal();e>=0;e--)
    {
    if (OrderSelect(e,SELECT_BY_POS,MODE_TRADES)==true)
      {
      if(OrderLots()==lot*ruka*32 && OrderType()==OP_BUYSTOP){return(0);}
      if(OrderLots()==lot*ruka*32 && OrderType()==OP_SELLSTOP){return(0);}
      if(OrderLots()==lot*ruka*8 && OrderType()==OP_BUY){Price=OrderOpenPrice();Lot=lot*ruka*32;
      fm=OrderSend(SMB,OP_BUYSTOP,Lot,Price,3,0,0,NULL,MAGIC,0,Lime);PlaySound("Ok.wav");}
      if(OrderLots()==lot*ruka*8 && OrderType()==OP_SELL){Price=OrderOpenPrice();Lot=lot*ruka*32;
      fm=OrderSend(SMB,OP_SELLSTOP,Lot,Price,3,0,0,NULL,MAGIC,0,Orange);PlaySound("Ok.wav");}
      }
    }
}

//-------------------------- counter ---------------------------------
void chet()
{
for (int r=0; r<=OrdersHistoryTotal(); r++)
   {
   if(OrderSelect(r,SELECT_BY_POS,MODE_HISTORY)==true) 
    if(OrderClosePrice()>0)
     prb=prb+OrderProfit()+OrderSwap();
    if(OrderClosePrice()>0 && (OrderProfit()+OrderSwap())>0)klv=klv+1;
    if(OrderClosePrice()>0 && (OrderProfit()+OrderSwap())<0)klv=klv+1;
   } 
prb=(NormalizeDouble((prb),2));
}
//------------------- Calculation of the lot and hand -----------------------------
//--------------------------------------------------------------------
void Auto()
{
 min_lot=MarketInfo(SMB,MODE_MINLOT);
 //----- 
 
  min_lot=MarketInfo(SMB,MODE_MINLOT);
  double min_prise=MarketInfo(SMB,MODE_TICKVALUE);
  Lot=(AccountFreeMargin()/min_prise)/2800;double l=Lot;
  Lot=NormalizeDouble((Lot),2);
}
//+------------------------------------------------------------------+

Recommend