FT_BBmMA_v2

Author: Copyright � 2009, FORTRADER.RU. ��������� �� ������ �����������
Price Data Components
Series array that contains close prices for each bar
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt Closes Orders by itself It can change open orders parameters, due to possible stepping strategy
Indicators Used
Bollinger bands indicatorMoving average indicator
0 Views
0 Downloads
0 Favorites
FT_BBmMA_v2
//+------------------------------------------------------------------+
//| FT_BB+MA.mq4.mq4
//| Copyright © 2009, FORTRADER.RU. Ñòðàòåãèÿ íà ïðîáîé áîëëåíäæåðà
//| http://www.fortrader.ru | http://forexsystems.ru/sistemy-ozhidayuschie-realizacii/11095-ochen-nuzhen-sovetnik-po-bollindzheru.html | Èññëåäîâàíèå äëÿ 56 âûïóñêà æóðíàëà FORTRADER.RU
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, FORTRADER.RU. Ñòðàòåãèÿ íà ïðîáîé áîëëåíäæåðà"
#property link      "http://www.fortrader.ru | http://forexsystems.ru/sistemy-ozhidayuschie-realizacii/11095-ochen-nuzhen-sovetnik-po-bollindzheru.html | Èññëåäîâàíèå äëÿ 56 âûïóñêà æóðíàëà FORTRADER.RU"

extern int periodH1=5;
extern int periodD=7;
int bars;

int start()
  {
  if(Bars!=bars)
  {bars=Bars;
  if(OpenPattern1()!=0){Print("Îñòàíîâëåí ïî îøèáêå"); return(0);}
  PosManager();
  }
   return(0);
  }
   
extern int       bb=20;
extern int       otc=1;
extern int       sma=20;
extern int SL=50;
extern int TP=80;

int MG=564651;
double Lots=0.1;
int mn=1;

extern int startpipsclose=30;

   double startup,startdw;int err;

   int OpenPattern1()
   {
      int perh1=GetPeriod(periodH1);
      int perd=GetPeriod(periodD);
   
      if(perd<perh1){return(0);}

      double bb1= iBands(NULL,perh1,bb,otc,0,PRICE_CLOSE,MODE_UPPER,1);
      double bb2= iBands(NULL,perh1,bb,otc,0,PRICE_CLOSE,MODE_LOWER,1);
      double ma = iMA(NULL,perh1,sma,0,MODE_SMA,PRICE_CLOSE,1);
      
      /*ïîêóïêà*/
      if(iClose(NULL,perh1,1)<bb2){startup=1;}
      if(iClose(NULL,perh1,1)>bb2 && startup==1 && iClose(NULL,perd,1)>ma && ChPos(1)==0)
      {
         if(OpenOrderOnMarket(0,SL,TP)!=0) return(-1);
      }
      if(iClose(NULL,perh1,1)>bb2){startup=0;}
      
      /*ïðîäàæà*/   
      if(iClose(NULL,perh1,1)>bb1){startdw=1;}
      if(iClose(NULL,perh1,1)<bb1 && startdw==1 && iClose(NULL,perd,1)<ma && ChPos(0)==0)
      {
         if(OpenOrderOnMarket(1,SL,TP)!=0) return(-1);
      }
      if(iClose(NULL,perh1,1)<bb1){startdw=0;}
   
   return(0);
   }

 //ïðîâåðÿåò åñòü ëè îòêðûòûå îðäåðà
int ChPos(int type) 
{//ïîäðîáíîå îïèñàíèå: http://fxnow.ru/blog.php?user=Yuriy&blogentry_id=100

   int i;int col;
   for( i=1; i<=OrdersTotal(); i++)         
   {
      if(OrderSelect(i-1,SELECT_BY_POS)==true) 
       {                                   
           if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && type==1&& OrderMagicNumber()==MG){col=1;}
           if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && type==0&& OrderMagicNumber()==MG){col=1;}
       }
    }   
return(col);
}

//äëÿ îïòèìèçàöèè ïî âñåì ïåðèîäàì ïî âñåì ïåðèîäàì
int GetPeriod(int period)
{int periodres;
 switch(period)
  {
   case 1: periodres=1;break;
   case 2: periodres=5;break;
   case 3: periodres=15;break;
   case 4: periodres=30;break;
   case 5: periodres=60;break;
   case 6: periodres=240;break;
   case 7: periodres=1440;break;
   default: periodres=1;break;
  }
return(periodres);
}

int OpenOrderOnMarket(int type,int slpips,int tppips)
{double op,sl,tp;
   
   if(type==0)
   {
   op=Ask;if(slpips>0){sl=op-slpips*Point*mn;}if(tppips>0){tp=op+tppips*Point*mn;}
   err=OrderSend(Symbol(),OP_BUY,Lots,NormalizeDouble(op,Digits),3,NormalizeDouble(sl,Digits),NormalizeDouble(tp,Digits),"56 FORTRADER.RU",MG,0,Red);
   if(err<0){Print("OrderSend()-  Îøèáêà OP_BUY.  op "+op+" sl "+sl+" tp "+tp+" "+GetLastError());return(-1);}
   }
   
   if(type==1)
   {
    op=Bid;if(slpips>0){sl=op+slpips*Point*mn;}if(tppips>0){tp=op-tppips*Point*mn;}
    err=OrderSend(Symbol(),OP_SELL,Lots,NormalizeDouble(op,Digits),3,NormalizeDouble(sl,Digits),NormalizeDouble(tp,Digits),"56 FORTRADER.RU",MG,0,Red);
    if(err<0){Print("OrderSend()-  Îøèáêà OP_SELL.  op "+op+" sl "+sl+" tp "+tp+" "+GetLastError());return(-1);}
   }
return(0);
}

int PosManager()
{

   if(ProfitPos(1)>startpipsclose*Point*mn)
   {
   _OrderCloseBy(1,Lots,Lots/2);
   }
   
   if(ProfitPos(0)>startpipsclose*Point*mn)
   {
   _OrderCloseBy(0,Lots,Lots/2);
   }

return(0);
}


//Ðåçóëüòàò ïîçèöèè ïðèáûëè â ïóíêòàõ
double ProfitPos(int type) 
{int i;
   for( i=1; i<=OrdersTotal(); i++)         
   {
      if(OrderSelect(i-1,SELECT_BY_POS)==true) 
       {                                   
           if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && type==1 && OrderMagicNumber()==MG) {return(Ask-OrderOpenPrice());}
           if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && type==0 && OrderMagicNumber()==MG){return(OrderOpenPrice()-Ask);}
       }
    }   
return(0);
}

//Çàêðûâàåò ÷àñòü ïîçèöèè 
int _OrderCloseBy(int type, double curlot, double closelot)
   {//http://fxnow.ru/blog.php?user=Yuriy&blogentry_id=99
   
   int err;
   for(int i=1; i<=OrdersTotal(); i++)          
     {
      if (OrderSelect(i-1,SELECT_BY_POS)==true) 
        {
         if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && type==1 && OrderMagicNumber()==MG && OrderLots()>closelot)
         {
          err=OrderClose(OrderTicket(),closelot,Bid,3,Violet); 
          if(err<0){Print("OrderClose()-  Îøèáêà çàêðûòèÿ OP_BUY.  OrderTicket "+OrderTicket()+" OrderLots() "+OrderLots()+" Bid "+Bid+" "+GetLastError());return(-1);}
          _OrderModifyBy(1,Lots,Lots/2);
         }
         if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && type==0 && OrderMagicNumber()==MG && OrderLots()>closelot)
         {
          err=OrderClose(OrderTicket(),closelot,Ask,3,Violet); 
          if(err<0){Print("OrderClose()-  Îøèáêà çàêðûòèÿ OP_SELL.  OrderTicket "+OrderTicket()+" OrderLots() "+OrderLots()+" Ask "+Ask+" "+GetLastError());return(-1);}
         _OrderModifyBy(0,Lots,Lots/2);
         }
        }
       }
   return(0);
   }
   


extern int mewstoploss=20;
 int  _OrderModifyBy(int type, double curlot, double closelot)
{int i;bool err;


      for( i=1; i<=OrdersTotal(); i++)        
      {  
        if(type==1)
       {
         if(OrderSelect(i-1,SELECT_BY_POS)==true)
          {  
           if(OrderType()==OP_BUY && OrderLots()==closelot && OrderStopLoss()!=(OrderOpenPrice()-mewstoploss*Point*mn))  
           {                
              err=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-mewstoploss*Point*mn,OrderTakeProfit(),0,Red);
              if(err==false){return(-1);}
               return(0);
            }//if(BBUSize>0
          }//if(OrderSelect(i
       }//if(type==1)
           
       if(type==0)
       {
         if(OrderSelect(i-1,SELECT_BY_POS)==true)
         {
          if(OrderType()==OP_SELL && OrderLots()==closelot && OrderStopLoss()!=(OrderOpenPrice()+mewstoploss*Point*mn))  
          {        
             err=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+mewstoploss*Point*mn,OrderTakeProfit(),0,Red);
             if(err==false){return(-1);}
             return(0);
           }//if(BBUSize>0
         }// if(OrderSelect
      }
    }// for( i=1;
return(0);
}

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---