MT4 often-used function

ok,

void enterAskPosition(double lots)
{
   if(0 > OrderSend(Symbol(),OP_SELL,lots,Bid,slippoint,stoploss,takeprofit,"My Order #kay",13603,0,Red))
   {
      writeLog("sell open failure");
   }
}

//---write log to another file for convienience to debug
void writeLog(string s)
{ 
    int handle = FileOpen("log.txt",FILE_READ | FILE_WRITE,';'); 
    if( handle == -1) 
    { 
        Print("------------ XXXXXXXXXXXXXXXXX ----------------");
    } 
    else 
    { 
        FileSeek(handle, 0, SEEK_END); 
        FileWrite(handle,s); 
        FileClose(handle); 
    }
}
//---close all the bid position include limit or stop order
void closeAllBidPosition()
{
   writeLog("---- enter function close all bid position");
   for(int i = OrdersTotal()-1;i >= 0;i--)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
         if(OrderType() == OP_BUY)
         {
            OrderClose(OrderTicket(),OrderLots(),Bid,0,White);
         }
         else if(OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP)
         {
            OrderDelete(OrderTicket());
         }
      }
   }
}

//---close all the ask position include limit or stop order
void closeAllAskPosition()
{
   writeLog("---- enter function close all ask position");
   for(int i = OrdersTotal()-1;i >= 0;i--)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
         if(OrderType() == OP_SELL)
         {
            OrderClose(OrderTicket(),OrderLots(),Ask,0,White);
         }
         else if(OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP)
         {
            OrderDelete(OrderTicket());
         }
      }
   }
}


void enterBidPosition(double lots)
{
   if(0 > OrderSend(Symbol(),OP_BUY,lots,Ask,slippoint,stoploss,takeprofit,"My Order #kay",13602,0,Blue))
   {
      writeLog("buy open failure");
   }
}


//---calculate the number of all the bid ticket
double calculate_totalCount_bid()
{
   double tb = 0;
   for(int i = OrdersTotal() - 1;i >= 0;i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderSymbol() == Symbol())
         {
            if(OrderType() == OP_BUY)
            {
               tb++;
            }
         }
      }
   }
   totalCount_bid = tb;
   return (tb); 
}

//---calculate the number of all the ask ticket
double calculate_totalCount_ask()
{
   double ta = 0;
   for(int i = OrdersTotal() - 1;i >= 0;i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderSymbol() == Symbol())
         {
            if(OrderType() == OP_SELL)
            {
               ta++;
            }
         }
      }
   }
   totalCount_ask = ta;
   return (ta); 
}

//---calculate the number of all position
double calculate_totalCount_pos()
{
   double tp = 0;
   for(int i = OrdersTotal() - 1;i >= 0;i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderSymbol() == Symbol())
         {
            if(OrderType() == OP_SELL || OrderType() == OP_BUY)
            {
               tp++;
            }
         }
      }
   }
   totalCount_pos = tp;
   return (tp); 
}

//---calculate all the cost price besides bid and ask
void calculate_cost_price()
{
   if(!ifChangedOfTotalCount_pos())
   {
      return;
   }
   for(int i = OrdersTotal() - 1;i >= 0;i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderSymbol() == Symbol())
         {
            if(OrderType() == OP_BUY)
            {
               Print("-------- hi hi");
               totalLots_bid += OrderLots();
               totalValue_bid += OrderLots() * OrderOpenPrice();
               Print("-----------totalValue_bid = ",totalValue_bid);
            }
            else if(OrderType() == OP_SELL)
            {
               totalLots_ask += OrderLots();
               totalValue_ask += OrderLots() * OrderOpenPrice();
               Print("----------totalValue_ask = ",totalValue_ask);
            }
         }
      }
   } 
   if(totalLots_bid > 0)
   {
      costPrice_bid = totalValue_bid / totalLots_bid;
   }
   if(totalLots_ask > 0)
   {
      costPrice_ask = totalValue_ask / totalLots_ask;
   } 
   if(costPrice_bid > 0 && costPrice_ask > 0)
   {
      Print("------------ costPrice_bid = ",costPrice_bid);
      Print("------------ costPrice_ask = ",costPrice_ask);
   }
}

//---check for whether there is a ask position
bool ifExistAskPosition()
{
   bool flag = false;
   for(int i = OrdersTotal();i > 0;i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderSymbol() == Symbol())
         {
            if(OrderType() == OP_SELL)
            {
               flag = true;
            }
         }
      }
   }
   return (flag);
}

//---check for whether there is a bid position
bool ifExistBidPosition()
{
   bool flag = false;
   for(int i = OrdersTotal();i > 0;i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderSymbol() == Symbol())
         {
            if(OrderType() == OP_BUY)
            {
               flag = true;
            }
         }
      }
   }
   return (flag);
}

//---calculate all the profit in the current symbol
double totalProfit()
{
   double totalProfit = 0;
   for(int i = OrdersTotal();i > 0;i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderSymbol() == Symbol())
         {
            if(OrderType() == OP_BUY || OrderType() == OP_SELL)
            {
               totalProfit += OrderProfit();
            }
         }
      }
   }
   return (totalProfit);
}

//---calculate all the bid position profit in the current Symbol
double totalBidProfit()
{
   double totalBidProfit = 0;
   for(int i = OrdersTotal();i > 0;i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderSymbol() == Symbol())
         {
            if(OrderType() == OP_BUY)
            {
               totalBidProfit += OrderProfit();
            }
         }
      }
   }
   return (totalBidProfit);
}

//---calculate all the ask position profit in the current symbol
double totalAskProfit()
{
   double totalAskProfit = 0;
   for(int i = OrdersTotal();i > 0;i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderSymbol() == Symbol())
         {
            if(OrderType() == OP_SELL)
            {
               totalAskProfit += OrderProfit();
            }
         }
      }
   }
   return (totalAskProfit);
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值