Experts: Source Code Version #3 - page 2

 

just a little improvement on code

#property version   "3.01"
#property strict

enum YesNo{Yes,No};

input string s1       = "";   //--------------------------
input string s2       = "";   //---> General Settings <---
input string s3       = "";   //--------------------------
input int   MaxOrders=1;   //Max Open Orders
input int   MagicNumber=16384;//Magic Number
input int   TP=4000;   //Take Profit (in pips)
input int   SL= 400;   //Stop Loss (in pips)

input string s4       = "";   //--------------------------
input string s5       = "";   //---> Risk Management  <---
input string s6       = "";   //--------------------------
input YesNo UseFixLot= Yes;   //Use Fix Lot
input double Lot=0.1;   //Fixed lot
input double RiskProcent=50.0;   //Risk (in procents)

input string s7       = "";   //--------------------------
input string s8       = "";   //--->  Hour Settings   <---
input string s9       = "";   //--------------------------
input YesNo UseHours= Yes;   //Use Open/Close Hours
input int Hour1       = 8;   //Open Hour
input int Hour2=17;   //Close Hour

double lot=Lot;
double risk=RiskProcent;

void OnInit()
  {
   if(UseFixLot==No)
     {
      if(RiskProcent < 0)risk = 0;
      if(RiskProcent>100)risk = 100;
      ReCalcLot();
     }
  }

void OnTick()
  {
   if(MaxOrders <= CountOrders())return;
   if(!(Hour() > Hour1 && Hour() < Hour2) && UseHours == Yes)return;
   if(UseFixLot==No)ReCalcLot();

   double iMACD1 = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
   double iMACD2 = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
   double iMA1 = iMA(NULL,15,3,0,MODE_EMA,PRICE_MEDIAN,0);
   double iMA2 = iMA(NULL,15,34,0,MODE_EMA,PRICE_MEDIAN,0);
   double iSAR1= iSAR(NULL,15,0.02,0.2,0);

   if(iMACD1>iMACD2)
     {
      double iBearsPower1 = iBearsPower(NULL,15,13,PRICE_CLOSE,0);
      double iBearsPower2 = iBearsPower(NULL,15,13,PRICE_CLOSE,1);

      if((iMA1<iMA2) && (iSAR1>High[0]) && (iBearsPower1<0 && iBearsPower1>iBearsPower2))Order(OP_SELL);
     }
   else if(iMACD1<iMACD2)
     {
      double iBands1 = iBands(NULL,0,20,2,0,PRICE_LOW,MODE_UPPER,1);
      double iBands2 = iBands(NULL,0,20,2,0,PRICE_LOW,MODE_UPPER,2);

      if(High[1]<iBands1 && High[2]<iBands2)
        {
         double iBullsPower1 = iBullsPower(NULL,15,13,PRICE_CLOSE,0);
         double iBullsPower2 = iBullsPower(NULL,15,13,PRICE_CLOSE,1);

         if(iMA1>iMA2 && iSAR1<Low[0] && iBullsPower1>0 && iBullsPower1<iBullsPower2)Order(OP_BUY);
        }
     }

  }

int CountOrders()
  {
   int res=0;

   int total=OrdersTotal();
   for(int pos=0;pos<total;pos++)
     {
      if(OrderSelect(pos,SELECT_BY_POS)==false) continue;
      if(OrderMagicNumber()==MagicNumber)res++;
     }
   return res;
  }

void Order(int BS)
  {
   double price=Bid;
   double tp = SubPips(price,TP);
   double sl = AddPips(price,SL);

   if(BS==OP_BUY)
     {
      price=Ask;
      tp = AddPips(price,TP);
      sl = SubPips(price,SL);
     }

   int ticket=OrderSend(Symbol(),BS,lot,price,3,0,0,"",MagicNumber,0,clrRed);

   if(ticket==-1)
     {
      Print("OrderSend failed. Error #",GetLastError());
     }
   else
     {

      if(OrderSelect(ticket,SELECT_BY_TICKET))
         if(OrderModify(ticket,OrderOpenPrice(),sl,tp,0,clrRed))
            Print("OrderModify failed. Error #",GetLastError());
     }
  }

void ReCalcLot()
  {
   int LotsDigit=1;
   if(MarketInfo(Symbol(),MODE_MINLOT)==0.01) LotsDigit=2;

   double MinLots = NormalizeDouble(MarketInfo(Symbol(),MODE_MINLOT),LotsDigit);
   double MaxLots = NormalizeDouble(MarketInfo(Symbol(),MODE_MAXLOT),LotsDigit);
   double FreeMargin=NormalizeDouble(AccountFreeMargin(),2);

   lot = (FreeMargin*(risk/100))/1000;
   lot = NormalizeDouble(lot,LotsDigit);

   if(lot > MaxLots) lot = MaxLots;
   if(lot < MinLots) lot = MinLots;
  }

double SubPips(double Price, int Pips){return Price - Pips*_Point;}
double AddPips(double Price, int Pips){return Price + Pips*_Point;}