//+------------------------------------------------------------------+
//|                                                  tutorial_ea.mq4 |
//|                                   Copyright © 2009, Thomas Liles |
//|                                       http://www.some_site.com   |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Thomas Liles"
#property link      "http://www.some_site.com"
extern double lots=0.01;
int blah;
int buys,sells;
double buy_lots,sell_lots,profit;
string signal;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   if (!GlobalVariableCheck(AccountNumber()+WindowExpertName()+Symbol()+IsTesting()))
   GlobalVariableSet(AccountNumber()+WindowExpertName()+Symbol()+IsTesting(),iBarShift(NULL,0,1));
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   GlobalVariableDel(AccountNumber()+WindowExpertName()+Symbol()+1);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
orders();
check_signal();
trade();
//----
   return(0);
  }
//+------------------------------------------------------------------+
void check_signal()
{
if (GlobalVariableCheck("buy"))signal="buy";
if (GlobalVariableCheck("sell"))signal="sell";
}
void trade()
{
int total_orders=buys+sells;
if (signal=="buy")OrderSend(Symbol(),OP_BUY,lots,Ask,3,NULL,NULL,NULL,NULL,0,Blue);
if (signal=="sell")OrderSend(Symbol(),OP_SELL,lots,Bid,3,NULL,NULL,NULL,NULL,0,Red);
orders();
if ((buys+sells)!=total_orders)
   {
   signal="none";
   GlobalVariableDel("buy");
   GlobalVariableDel("sell");
   }
}
void orders()
{
buys=0;
sells=0;
buy_lots=0;
sell_lots=0;
profit=0;
  for(int i=OrdersTotal();i>=0;i--)
 {
    OrderSelect(i, SELECT_BY_POS);
    if ( OrderType() == OP_BUY)  
        {
        buys=buys+1;
        buy_lots=buy_lots+OrderLots();
        profit=(profit+OrderProfit())+OrderSwap();
        }
    if ( OrderType() == OP_SELL )
        {
        sells=sells+1;
        sell_lots=sell_lots+OrderLots();
        profit=(profit+OrderProfit())+OrderSwap();
        }
 }
}