🌟

MT4のEAのテンプレート

2023/09/10に公開
//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2018, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property strict


int MagicNumber = 789;



int OnInit(){

return 0;
}


bool is_buy(){return false;}
bool is_sell(){return false;}
void OnTick()
  {

//エントリー
   if(position_count(OP_BUY)==0 && position_count(OP_SELL)==0 && is_buy())
     {
      position_entry(OP_BUY);
     }
   if(position_count(OP_BUY)==0 && position_count(OP_SELL)==0 && is_sell())
     {
      position_entry(OP_SELL);
     }   
     
     }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int position_count(int side)
  {

   int count = 0;
   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderType() == side)
           {
            if(OrderSymbol()==Symbol())
              {
               if(OrderMagicNumber()==MagicNumber)
                 {
                  count++;
                 }
              }
           }
        }
     }
   return count;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void position_entry(int side)
  {

   double qty = 0.1;
   
   
   

   if(side==0)
     {
      bool res= OrderSend(NULL,side,qty,Ask,0,0,0,NULL,MagicNumber,0,clrGreen);
      

     }
   if(side==1)
     {
      bool res= OrderSend(NULL,side,qty,Bid,0,0,0,NULL,MagicNumber,0,clrRed);
     }

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void position_close(int side)
  {
   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderType() == side)
           {
            if(OrderSymbol()==Symbol())
              {
               if(OrderMagicNumber()==MagicNumber)
                 {
                  bool res=  OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, clrBlue);
                 }
              }
           }
        }
     }
  }

Discussion