Author: Ron Thompson
3MA02EXP

This automated trading script is designed to manage trades by identifying specific momentum shifts in the market using moving averages. Instead of manually watching charts, the system monitors price trends and executes actions when two different speed indicators cross paths.

Core Trading Logic

The script operates based on the relationship between two primary technical indicators:

  • Fast Moving Average: A very quick reaction to recent price changes (5-period).
  • Slow Moving Average: A broader view of the market trend (20-period).

The system looks for a "Crossover Event." This happens when the fast indicator moves from one side of the slow indicator to the other. This movement typically suggests a shift in market momentum—for example, moving from a cooling period into a growth phase, or vice versa.

Operational Workflow

To ensure stable and organized trading, the script follows a specific sequence of checks:

  1. Pre-Flight Safety Checks: Before performing any actions, the system verifies that there is enough historical data on the chart to calculate accurate averages and ensures that the account has sufficient available funds to support the required trade size.
  2. Trade Restriction: The logic is designed to manage only one position at a time per currency pair. It scans your active trades to ensure it doesn't "double up" on the same symbol unnecessarily.
  3. Crossover Detection: The script constantly calculates whether the fast moving average has just crossed over the slow moving average.
  4. Smart Order Management: If a crossover is detected, the script evaluates your current open position:
    • If you have an active Sell position and the indicators signal a move toward a "Buy" trend, the system will automatically close the Sell order to protect your capital.
    • If you have an active Buy position and the indicators signal a move toward a "Sell" trend, the system will automatically close the Buy order.

Summary for the User

This script acts as an automated "exit manager." It watches for changes in market sentiment by tracking how quickly prices are moving relative to their recent history. When those movements cross a specific threshold, it identifies that the current trade direction may be ending and handles the closing of the position automatically to align with the new trend.

Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
Indicators Used
Moving average indicator
10 Views
0 Downloads
0 Favorites
3MA02EXP
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//| 3MA Bunny Cross Expert                               |
//+------------------------------------------------------------------+
#property copyright "Ron Thompson"
#property link      "http://www.lightpatch.com/forex"

// User Input
extern double Lots = 0.1;



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|

int init()
  {
   return(0);
  }


//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }


//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
  {
   double   cMAfst=0, pMAfst=0;
   double   cMAslo=0, pMAslo=0;
   double    MA100=0;

      
   int    total=0;
   bool   found=false;
   int    otype=0;

   int      cnt=0;
   
   string cmmt="";
   
   //
   // Error checking
   //
   
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);
     }
   
   if(AccountFreeMargin()<(1000*Lots))
     {
      Print("We have no money");
      return(0);
     }

   //
   // only one order at a time/per symbol 
   // so see if our symbol has an order open
   //


   
   cMAfst=iMA(Symbol(),0,5 ,0,MODE_LWMA,PRICE_CLOSE, 1);
   pMAfst=iMA(Symbol(),0,5 ,0,MODE_LWMA,PRICE_CLOSE, 2);
      
   cMAslo=iMA(Symbol(),0,20,0,MODE_LWMA,PRICE_CLOSE, 1);
   pMAslo=iMA(Symbol(),0,20,0,MODE_LWMA,PRICE_CLOSE, 2);


   found=false;


   // rising or falling
   if ((pMAfst<=cMAslo && cMAfst>=cMAslo) || (pMAfst>=cMAslo && cMAfst<=cMAslo))
     {
      // check for existing order
      cmmt=cmmt+"crossover ";
      Comment(cmmt);
      
      total=OrdersTotal();
      for(cnt=0;cnt<OrdersTotal();cnt++)
        {
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
         if(OrderSymbol()==Symbol())
           {
            cmmt=cmmt+"found symbol";
            Comment(cmmt);
            //ensure existing order, if any, is older than bar 2
            if(OrderOpenTime()<Time[2])
              { 
               found=true;
               otype=OrderType();
               cmmt=cmmt+" 2 bars old";
               Comment(cmmt);
               break;
              }
           }
        }
     }

   if (found==true)
     {
      cmmt=cmmt+" infound";
      Comment(cmmt);
      
      if (pMAfst<=cMAslo && cMAfst>=cMAslo && otype==1)  //exist sell
        {
         cmmt=cmmt+" CLOSE sell BUY buy";
         Comment(cmmt);
         OrderClose(OrderTicket(),Lots,Bid,0,Red);
         //Print("BUY  Order started  ",Ask);
         //OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"BC Buy ",16123,0,White);
         //if(GetLastError()==0)Comment("BC_BUY  Order opened : ",Ask);
        }
      if (pMAfst>=cMAslo && cMAfst<=cMAslo && otype==0)  // exist buy
        {
         cmmt=cmmt+" CLOSE buy BUY sell";
         Comment(cmmt); 
         OrderClose(OrderTicket(),Lots,Ask,0,White);
         //Print("SELL Order started  ",Bid);
         //OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"BC Sell",16321,0,Red);
         //if(GetLastError()==0)Comment("BC_SELL Order opened : ",Bid );
        }
     }


   return(0);
  }

Comments