cm-MA close

Profit factor:
0.00
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt Closes Orders by itself
Indicators Used
Moving average indicator
Miscellaneous
It issuies visual alerts to the screen
2 Views
0 Downloads
0 Favorites
cm-MA close
ÿþ//+------------------------------------------------------------------+

#property copyright "Copyright © 2016, http://cmillion.ru"

#property link      "cmillion@narod.ru"

#property strict

#property description "!>25B=8: 70:@K205B ?>78F88 =0 ?5@5A5G5=88 ."

#property description "!>25B=8: @01>B05B B>;L:> ?> >@45@0< B>9 20;NB=>9 ?0@K =0 :>B>@>9 CAB0=>2;5=."

#property description "A;8 C:070= Magik = -1 B> A>25B=8: 2848B 8 70:@K205B >@45@0 A ;N1K< <038:><. =0G5 B>;L:> A B5, :>B>@K9 C:070= 2 40==>< ?0@0<5B@5"

//--------------------------------------------------------------------*/

extern int     period_1             = 5,           //5@8>4 CA@54=5=8O 4;O 2KG8A;5=8O ?5@2>9 MA.

               ma_shift_1           = 0,           //!4283 8=48:0B>@0 >B=>A8B5;L=> F5=>2>3> 3@0D8:0.

               ma_method_1          = MODE_EMA,    //5B>4 CA@54=5=8O. >65B 1KBL ;N1K< 87 7=0G5=89 <5B>4>2 A:>;L7OI53> A@54=53> (Moving Average).

                                                   //MODE_SMA 0 @>AB>5 A:>;L7OI55 A@54=55 

                                                   //MODE_EMA 1 -:A?>=5=F80;L=>5 A:>;L7OI55 A@54=55 

                                                   //MODE_SMMA 2 !3;065==>5 A:>;L7OI55 A@54=55 

                                                   //MODE_LWMA 3 8=59=>-2725H5==>5 A:>;L7OI55 A@54=55 



               applied_price_1      = PRICE_OPEN;  //A?>;L7C5<0O F5=0. >65B 1KBL ;N1>9 87 F5=>2KE :>=AB0=B.

                                                   //PRICE_CLOSE 0 &5=0 70:@KB8O 

                                                   //PRICE_OPEN 1 &5=0 >B:@KB8O 

                                                   //PRICE_HIGH 2 0:A8<0;L=0O F5=0 

                                                   //PRICE_LOW 3 8=8<0;L=0O F5=0 

                                                   //PRICE_MEDIAN 4 !@54=OO F5=0, (high+low)/2 

                                                   //PRICE_TYPICAL 5 "8?8G=0O F5=0, (high+low+close)/3 

                                                   //PRICE_WEIGHTED 6 725H5==0O F5=0 70:@KB8O, (high+low+close+close)/4 



extern int     period_2             = 30,          //5@8>4 CA@54=5=8O 4;O 2KG8A;5=8O 2B>@>9 MA.

               ma_shift_2           = 0,           //!4283 8=48:0B>@0 >B=>A8B5;L=> F5=>2>3> 3@0D8:0.

               ma_method_2          = MODE_LWMA,   //5B>4 CA@54=5=8O. >65B 1KBL ;N1K< 87 7=0G5=89 <5B>4>2 A:>;L7OI53> A@54=53> (Moving Average).

               applied_price_2      = PRICE_OPEN,  //A?>;L7C5<0O F5=0. >65B 1KBL ;N1>9 87 F5=>2KE :>=AB0=B.

               timeframe_2          = 0;           //5@8>4. >65B 1KBL >4=8< 87 ?5@8>4>2 3@0D8:0. 0 >7=0G05B ?5@8>4 B5:CI53> 3@0D8:0.



extern int     barOpen              = 1;           //10@ =0 :>B>@>< 645< A83=0; (0- B5:CI89  1 - ?@>H;K9)



extern int     Magic          = -1;  //C=8:0;L=K9 =><5@ >@45@>2 :>;B>@K5 2848B A>25B=8: (-1 2A5 <038:8)

//--------------------------------------------------------------------

int OnInit()

{

   return(INIT_SUCCEEDED);

}

//--------------------------------------------------------------------

void OnTick()

{

   if (!IsTradeAllowed()) return;

   double MAf0 = iMA(NULL,0,period_1,ma_shift_1,ma_method_1,applied_price_1,barOpen);

   double MAs0 = iMA(NULL,0,period_2,ma_shift_2,ma_method_2,applied_price_2,barOpen);

   double MAf1 = iMA(NULL,0,period_1,ma_shift_1,ma_method_1,applied_price_1,barOpen+1);

   double MAs1 = iMA(NULL,0,period_2,ma_shift_2,ma_method_2,applied_price_2,barOpen+1);

   if (MAf1 <= MAs1 && MAf0 > MAs0) CloseAll(OP_SELL);

   if (MAf1 >= MAs1 && MAf0 < MAs0) CloseAll(OP_BUY);

   if (!IsTesting()) return;

   if (OrdersTotal()>0) return;

   double lots = CheckVolumeValue(0.01);

   if (AccountFreeMarginCheck(Symbol(),OP_BUY,lots)<1) return;

   if (AccountFreeMarginCheck(Symbol(),OP_SELL,lots)<1) return;

   if (OrderSend(Symbol(),OP_BUY, lots,Ask,100,0,0,NULL,Magic,0,Blue)==-1) Print("Error OrderSend BUY");

   if (OrderSend(Symbol(),OP_SELL,lots,Bid,100,0,0,NULL,Magic,0,Red)==-1) Print("Error OrderSend SELL");

}

//--------------------------------------------------------------------

bool CloseAll(int tip)

{

   bool error=true;

   int j,err,nn=0,OT;

   while(!IsStopped())

   {

      for (j = OrdersTotal()-1; j >= 0; j--)

      {

         if (OrderSelect(j, SELECT_BY_POS))

         {

            if (OrderSymbol() == Symbol() && (OrderMagicNumber() == Magic || Magic==-1))

            {

               OT = OrderType();

               if (tip!=OT) continue;

               if (OT==OP_BUY) 

               {

                  error=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),30,Blue);

               }

               if (OT==OP_SELL) 

               {

                  error=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),30,Red);

               }

               if (!error) 

               {

                  err = GetLastError();

                  if (err<2) continue;

                  if (err==129) 

                  {

                     RefreshRates();

                     continue;

                  }

                  if (err==146) 

                  {

                     if (IsTradeContextBusy()) Sleep(2000);

                     continue;

                  }

                  Print("H81:0 ",err," 70:@KB8O >@45@0 N ",OrderTicket(),"     ",TimeToStr(TimeCurrent(),TIME_SECONDS));

               }

            }

         }

      }

      int n=0;

      for (j = 0; j < OrdersTotal(); j++)

      {

         if (OrderSelect(j, SELECT_BY_POS))

         {

            if (OrderSymbol() == Symbol() && (OrderMagicNumber() == Magic || Magic==-1))

            {

               OT = OrderType();

               if (tip!=OT) continue;

               if (OT==OP_BUY || OT==OP_SELL) n++;

            }

         }  

      }

      if (n==0) break;

      nn++;

      if (nn>10) 

      {

         Alert(Symbol()," 5 C40;>AL 70:@KBL 2A5 A45;:8, >AB0;>AL 5I5 ",n);

         return(false);

      }

      Sleep(1000);

      RefreshRates();

   }

   return(true);

}

//+------------------------------------------------------------------+

double CheckVolumeValue(double volume)

{

   double min_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);

   if(volume<min_volume) return(min_volume);

   double max_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);

   if(volume>max_volume) return(max_volume);

   double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);//--- 3@040F8O >1J5<0

   int ratio=(int)MathRound(volume/volume_step);

   if(MathAbs(ratio*volume_step-volume)>0.0000001) return(ratio*volume_step);

   return(volume);

}

//+------------------------------------------------------------------+

Comments