/*[[
Name := Expert Advisor: 50-50_ExpertAdvisor
Author := iExpertAdvisor
Link := http://www.iExpertAdvisor.com
Notes := Free MetaTrader ExpertAdvisor, UserFunction. BUY or SELL on a Moving Average crossover, used for testing market behavior.
]]*/


// Expert Advisor Input Parameters - these variables persist across EA invocations


Defines:

OrderFlag(0);          // Input parameter used to determine order type of Buy or Sell
Xbars(4);                // Input parameter used to determine when to exit

// Temp variables - these variables are reset with each EA invocation

Vars
:

FastMa0(0),                   // Current period's Fast Moving Average
FastMa1(0),                   // Last period's Fast Moving Average
SlowMa0(0),                  // Current period's Slow Moving Average
SlowMa1(0),                  // Last period's Fast Slow Average
SetBuyOrder(0),            // Flag used to control Buy Orders
SetSellOrder(0),            // Flag used to control Sell Orders
status(0);                       // Used to save the status of a UserFunction


// Set the indicator values
FastMa0=iMA(8,MODE_EMA,0);      // Current period's 8-period EMA
FastMa1=iMA(8,MODE_EMA,1);     // Last period's 8-period EMA
SlowMa0=iMA(13,MODE_EMA,0);   // Current period's 13-period EMA
SlowMa1=iMA(13,MODE_EMA,1);   // Last period's 13-period EMA

// Only open 1 trade at a time, exit if there is already an open trade
If (TotalTrades > 0) then
{
     // If the input parameter Xbars in non-zero then call the fnExitBar UserFunction
    
If( xBars > 0 ) then
     {
        status = UserFunction( "fnExitBar", 4 );
     };
     Exit;
};


// Test if the Fast MA (8-period) has crossed ABOVE the Slow MA (13-period)
If( (FastMa1<SlowMa1) and (FastMa0>SlowMa0) )then
{
     // The OrderFlag controls the type of order to enter: Buy or Sell
    
If( OrderFlag == 0 ) then
     {
        SetBuyOrder=1;   // Set the flag to enter a Buy order
    
   SetSellOrder=0;   // Reset the flag for a Sell order
    
}
     else
     {
        SetSellOrder=1; // Set the flag to enter a Sell order
    
   SetBuyOrder=0; // Reset the flag for a Buy order
    
}
};

// Test if the Fast MA (8-period) has crossed BELOW the Slow MA (13-period)
If( (FastMa1>SlowMa1) and (FastMa0<SlowMa0) )then
{
   // The OrderFlag controls the type of order to enter: Buy or Sell
   If( OrderFlag == 0 ) then
   {
      SetSellOrder=1; ; // Set the flag to enter a Sell order
      SetBuyOrder=0; // Reset the flag for a Buy order
   }
   else
   {
      SetBuyOrder=1; // Set the flag to enter a Buy order
      SetSellOrder=0; // Reset the flag for a Sell order
   }

};


// Enter an order if the flag is set

If ( SetBuyOrder == 1 ) then
{
   SetOrder(OP_BUY, Lots, Ask, 3, Ask-StopLoss*Point, Ask+TakeProfit*Point, BLUE);
   Exit; // Always exit after an order entry or modification
};

If ( SetSellOrder == 1 ) then
{
   SetOrder(OP_SELL, Lots, Bid, 3, Bid+StopLoss*Point, Bid-TakeProfit*Point, RED);
   Exit; // Always exit after an order entry or modification
};

// End of EA