😎

MQL5で最後にエントリーしたポジションの価格を調べる

2023/10/07に公開
double get_last_position_entry_price(ENUM_ORDER_TYPE side)
  {
   double lastEntryPrice = 0;
   long lastEntryTime = 0;
   long entryTime = 0;

   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      if("" != PositionGetSymbol(i))
        {
         if(PositionGetInteger(POSITION_TYPE) == side)
           {
            if(Symbol() == PositionGetString(POSITION_SYMBOL))
              {
               if(PositionGetInteger(POSITION_MAGIC) == MagicNumber)
                 {
                  entryTime = PositionGetInteger(POSITION_TIME);
                  if(entryTime > lastEntryTime)
                    {
                     lastEntryTime = entryTime;
                     lastEntryPrice = PositionGetDouble(POSITION_PRICE_OPEN);
                    }
                 }
              }
           }
        }
     }

   return lastEntryPrice;
  }

Discussion