Скачать MetaTrader 5
Авторизуйтесь или зарегистрируйтесь, чтобы добавить комментарий
Миллиард задач уже выполнено в MQL5 Cloud Network. Тестируй роботов еще быстрее!
Alexander Cherkeev
6166
Alexander Cherkeev 2016.05.26 18:07 
//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict

extern double       Lots         = 0.01; // 
extern double       MaxLots      = 0.5;  // 
extern double       Multiplier   = 2;    // 
extern double       StepSAR=0.2; // 
extern int          Slippage=5;    //
int          Magic=5832; // 

int ticket;
double SL,Lot;
bool mdf,cls;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(CountTrades(OP_BUY)>0 && SignalSell()) CloseOrders(OP_BUY);
   if(CountTrades(OP_SELL)>0 && SignalBuy()) CloseOrders(OP_SELL);

   if(CountTrades()==0)
     {
      if(SignalBuy())
        {
         Lot=Lots; if(FindProfitLastOrder()<0) Lot=NormalizeDouble(FindLastLots()*Multiplier,2); if(Lot>MaxLots) Lot=MaxLots;
         ticket=OrderSend(Symbol(),OP_BUY,Lot,Ask,Slippage,0,0,"",Magic);
        }
      if(SignalSell())
        {
         Lot=Lots; if(FindProfitLastOrder()<0) Lot=NormalizeDouble(FindLastLots()*Multiplier,2); if(Lot>MaxLots) Lot=MaxLots;
         ticket=OrderSend(Symbol(),OP_SELL,Lot,Bid,Slippage,0,0,"",Magic);
        }
     }

//   ModificationSL();
  }
//+------------------------------------------------------------------+
int CountTrades(int type=-1)
  {
   int cnt=0;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && (OrderType()==type || type==-1))
            cnt++;
        }
     }
   return(cnt);
  }
//+------------------------------------------------------------------+
bool SignalBuy()
  {
   double Par0 = iSAR(Symbol(), 0, StepSAR, 0.2, 0);
   double Par1 = iSAR(Symbol(), 0, StepSAR, 0.2, 1);

   if(Par0 > Low[0]+(High[0]-Low[0])/2 && Par1 < High[1]-(High[1]-Low[1])/2) return(true);
   return(false);
  }
//+------------------------------------------------------------------+
bool SignalSell()
  {
   double Par0 = iSAR(Symbol(), 0, StepSAR, 0.2, 0);
   double Par1 = iSAR(Symbol(), 0, StepSAR, 0.2, 1);

   if(Par0 < High[0]-(High[0]-Low[0])/2 && Par1 > Low[1]+(High[1]-Low[1])/2) return(true);
   return(false);
  }
//+------------------------------------------------------------------+
double FindProfitLastOrder(int type=-1)
  {
   datetime lasttime = 0;
   double lastprofit = 0;

   for(int i=OrdersHistoryTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && (OrderType()==type || type==-1))
           {
            if(OrderCloseTime()>lasttime)
              {
               lasttime=OrderCloseTime();
               lastprofit=OrderProfit()+OrderCommission()+OrderSwap();
              }
           }
        }
     }
   return(lastprofit);
  }
//+------------------------------------------------------------------+
double FindLastLots(int type=-1)
  {
   double lastlots    = 0;
   datetime lasttime  = 0;

   for(int i=OrdersHistoryTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && (OrderType()==type || type==-1))
           {
            if(OrderCloseTime()>lasttime)
              {
               lasttime = OrderCloseTime();
               lastlots = OrderLots();
              }
           }
        }
     }
   return(lastlots);
  }
//+------------------------------------------------------------------+
//void ModificationSL()
//{
//   double stoplevel = MarketInfo(Symbol(), MODE_STOPLEVEL)*Point;
//   double Par0 = iSAR(Symbol(), 0, StepSAR, 0.2, 0);

//   for (int i=OrdersTotal()-1; i>=0; i--)
//   {
//      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
//      {
//         if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
//         {
//            if (OrderType() == OP_BUY)
//            {
//               if (Par0 < High[0]-(High[0]-Low[0])/2)
//               {
//                  SL = Par0;
//                  if (Bid-SL >= stoplevel && OrderStopLoss() != SL)
//                     mdf = OrderModify(OrderTicket(), OrderOpenPrice(), SL, OrderTakeProfit(), 0);
//               }
//            }
//            if (OrderType() == OP_SELL)
//            {
//               if (Par0 > Low[0]+(High[0]-Low[0])/2)
//               {
//                  SL = Par0;
//                  if (SL-Ask >= stoplevel && OrderStopLoss() != SL)
//                     mdf = OrderModify(OrderTicket(), OrderOpenPrice(), SL, OrderTakeProfit(), 0);
//               }
//            }
//         }
//      }
//   }
//}
//+------------------------------------------------------------------+
void CloseOrders(int type=-1)
  {
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()<2 && (OrderType()==type || type==-1))
           {
            double price=0;
            int try=0;

            if(OrderType() == OP_BUY)  price = Bid;
            if(OrderType() == OP_SELL) price = Ask;

            while(try<5)
              {
               cls=OrderClose(OrderTicket(),OrderLots(),price,Slippage);
               if(cls) break;
               Sleep(1000*2);
               try++;
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
Помогите пожалуйста вставить проверку на наличие средств (свободной маржи)?
Vitaly Muzichenko
2847
Vitaly Muzichenko 2016.05.26 20:30  
Alexander Cherkeev:
Помогите пожалуйста вставить проверку на наличие средств (свободной маржи)?

Простейшее:

void OnTick()
  {
   if(CountTrades(OP_BUY)>0 && SignalSell()) CloseOrders(OP_BUY);
   if(CountTrades(OP_SELL)>0 && SignalBuy()) CloseOrders(OP_SELL);

   if(CountTrades()==0)
     {
      if(SignalBuy())
        {
         Lot=Lots; if(FindProfitLastOrder()<0) Lot=NormalizeDouble(FindLastLots()*Multiplier,2); if(Lot>MaxLots) Lot=MaxLots;
         if(AccountFreeMarginCheck(Symbol(),OP_BUY,Lot)>0) {
           ticket=OrderSend(Symbol(),OP_BUY,Lot,Ask,Slippage,0,0,"",Magic);
         }
        }
      if(SignalSell())
        {
         Lot=Lots; if(FindProfitLastOrder()<0) Lot=NormalizeDouble(FindLastLots()*Multiplier,2); if(Lot>MaxLots) Lot=MaxLots;
         if(AccountFreeMarginCheck(Symbol(),OP_SELL,Lot)>0) {
           ticket=OrderSend(Symbol(),OP_SELL,Lot,Bid,Slippage,0,0,"",Magic);
         }
        }
     }

//   ModificationSL();
  }

 Почитайте о AccountFreeMarginCheck

Alexander Cherkeev
6166
Alexander Cherkeev 2016.05.26 21:01  
Vitaly Muzichenko:

Простейшее:

 Почитайте о AccountFreeMarginCheck

Спасибо)))
/
Авторизуйтесь или зарегистрируйтесь, чтобы добавить комментарий