Home System Trading Post

Automate Your Trading with SAR and Advanced Money Management in MetaTrader 4

Attachments
15536.zip (1.59 KB, Download 0 times)

Author: Mohammad Soubra

If you’re diving into automated trading, here’s a straightforward example using the SAR (Stop And Reverse) indicator. Keep in mind, this isn’t a money-making EA; it’s more of a foundational template for coders looking to get their feet wet in the world of algorithmic trading.

Understanding Advanced Money Management:

//+------------------------------------------------------------------+
//    Expert AdvancedMM function
//+------------------------------------------------------------------+
double AdvancedMM()
  {
   int i;
   double AdvancedMMLots=0;
   bool profit1=false;
   int SystemHistoryOrders=0;
  
   for(i=0;i<OrdersHistoryTotal();i++)
     {
      bool ordsel = OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
      if(OrderMagicNumber()==MagicNumber) SystemHistoryOrders++;
     }
    
   bool profit2=false;
   int LO=0;
  
   if(SystemHistoryOrders<2) return(Lots);
   for(i=OrdersHistoryTotal()-1;i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
         if(OrderMagicNumber()==MagicNumber)
           {
            if(OrderProfit()>=0 && profit1) return(Lots);
            if(LO==0)
              {
               if(OrderProfit()>=0) profit1=true;
               if(OrderProfit()<0)  return(OrderLots());
               LO=1;
              }
            if(OrderProfit()>=0 && profit2) return(AdvancedMMLots);
            if(OrderProfit()>=0) profit2=true;
            if(OrderProfit()<0)
              {
               profit1=false;
               profit2=false;
               AdvancedMMLots+=OrderLots();
              }
           }
     }
   return(AdvancedMMLots);
  }


Pro Tip: Always trade responsibly and stay informed about your strategies!

  • Take care and happy trading!

Related Posts

Comments (0)