FIFO Workaround Added to MPTM

Post Reply
User avatar
remix919
Trader
Posts: 91
Joined: Wed Jan 23, 2013 2:42 am
Location: Some Road in the USA

FIFO Workaround Added to MPTM

Post by remix919 »

Hi there all, I did a search and didn't find any solutions to what I was looking for. Does anyone have a version of Steve's MPTM that has a FIFO workaround built into it for closing baskets at TP or SL? The following code should work for this:

Code: Select all

void closeAllPoisitions(int type)
{
   while(orderCount(type) > 0)
   {
      datetime oldest        = Time[0]+Period()*60+1;
      int      ticketToClose = -1;
      double   closePrice;
     
      //
      //
      //
      //
      //
     
      for(int cnt = OrdersTotal()-1; cnt>=0; cnt--)
      {
         if (!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)) continue;
         if (OrderSymbol() != Symbol())                     continue;
         if (OrderMagicNumber() != getMagic())              continue;
         
         if ((OrderType() == OP_BUY  && type == OP_BUY) || (OrderType() == OP_SELL && type == OP_SELL))
         if (OrderOpenTime()<oldest)
         {
            oldest        = OrderOpenTime();
            ticketToClose = OrderTicket();
            RefreshRates();
               if (OrderType() == OP_BUY)
                     closePrice = Bid;
               else  closePrice = Ask;    
         }
      }
     
      //
      //
      //
      //
      //
     
      if (ticketToClose!=-1)
      if (OrderSelect(ticketToClose,SELECT_BY_TICKET,MODE_TRADES)) OrderClose(ticketToClose,OrderLots(),closePrice,0,CLR_NONE);
   }
}  
OR from JL:

Code: Select all

//void LookForTradingOpportunities()





bool CloseTrade(int ticket)
{   
   bool result;
   //if (OpenTrades = 1)
   //result = OrderClose(ticket, OrderLots(), OrderClosePrice(), 1000, CLR_NONE);
   
   //else 
   {
      while (OpenTrades > 0)
      {
       int Closed,CloseCount;
       int CloseTrades[,2];
       double OPrice;

         for(int y=OrdersTotal()-1;y>=0;y--)
         {   if(!OrderSelect(y,SELECT_BY_POS,MODE_TRADES))continue;
   
   if(OrderSymbol()!=Symbol() && OrderMagicNumber()!=Magic)continue; //This is the sentece added
   ArrayResize      (CloseTrades,CloseCount+1);
          CloseTrades[CloseCount,0]=OrderOpenTime();
          CloseTrades[CloseCount,1]=OrderTicket();
          CloseCount++;

         }

        if(CloseCount>0)
       {   if(!UseFIFO)ArraySort(CloseTrades,WHOLE_ARRAY,0,MODE_DESCEND);
          else if(CloseCount!=ArraySort(CloseTrades))Print("Error sorting CloseTrades Array");
          for(y=0;y<CloseCount;y++)
          {   if(!OrderSelect(CloseTrades[y,1],SELECT_BY_TICKET))continue;
             while(IsTradeContextBusy())Sleep(100);
             if(IsStopped())return(-1);
             //if(OrderType()>OP_SELL)result=OrderDelete(OrderTicket(),CLR_NONE);
             else
             {   if(OrderType()==OP_BUY)OPrice=NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID),MarketInfo(OrderSymbol(),MODE_DIGITS));
                else OPrice=NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),MarketInfo(OrderSymbol(),MODE_DIGITS));
                result=OrderClose(OrderTicket(),OrderLots(),OPrice,1000,CLR_NONE);
             }
             if(result)Closed++;
          }
         if(Closed==CloseCount) return (true);
       }

      }
      }
   //Actions when trade send succeeds
Just what I found from looking around. I'm not an advanced coder, so am unfortunately unable to do this task myself, but I think there are quite a few US traders that would definitely benefit from being able to use MPTM basket closing without having FIFO errors. Would greatly appreciate feedback or help =)

Thanks!
EA's are the best way to trade. Why? Because they take the emotion out of trading.
Post Reply

Return to “Coders Hangout”