👏

MQL5でナンピンマーチンでナンピンするかどうかを判断する機能

2023/10/07に公開

input int MagicNumber = 123;


input int     NanpinPoints     = 200;      // ナンピン値幅points

bool is_nanpin(int side)
{
   double lastEntryPrice = get_last_position_entry_price(side);
   double nanpinThreshold = lastEntryPrice + (side == 0 ? -1 : 1) * NanpinPoints * _Point;

   MqlTick tick;
   SymbolInfoTick(_Symbol, tick);
   double currentPrice = (side == 0) ? tick.bid : tick.ask;

   if((side == 0 && currentPrice < nanpinThreshold) || (side == 1 && currentPrice > nanpinThreshold))
   {
      return true;
   }

   return false;
}

double get_last_position_entry_price(int 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