//+------------------------------------------------------------------+
//|                                                          aaa.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
extern int magicBuy=12345;
extern int magicSell=54321;
extern double step=35;
extern double proffactor=9;
extern double mult=1.6;
extern double lots=0.3;  
extern double per_K=20;
extern double per_D=6;
extern double slow=6;
extern double zoneBUY=50;
extern double zoneSELL=50;
double openprice,n,lots2,tp;
//+------------------------------------------------------------------+ 
int orderss()
  {int j=0;for (int i=0;i<OrdersTotal();i++)
  {if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
  if((OrderMagicNumber()==magicBuy||OrderMagicNumber()==magicSell))j++;
  }return(j);} 
//+------------------------------------------------------------------+  
void close_buy()
  {for(int cnt =OrdersTotal(); cnt >= 0; cnt--)
  {if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
  if(OrderMagicNumber()==magicBuy)
  {OrderClose(OrderTicket(),OrderLots(),Bid,3,Yellow);lots2=lots;}}}
//+------------------------------------------------------------------+
void close_sell()
  {for(int cnt =OrdersTotal(); cnt >= 0; cnt--)
  {if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
  if(OrderMagicNumber()==magicSell)
  {OrderClose(OrderTicket(),OrderLots(),Ask,3,Yellow);lots2=lots;}}} 
//+------------------------------------------------------------------+
int orders_buy()
  {int j=0;for (int i=0;i<OrdersTotal();i++)
  {if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
  if((OrderType()==OP_BUY)&&(OrderMagicNumber()==magicBuy))j++;
  }return(j);}
//+------------------------------------------------------------------+
int orders_sell()
  {int j=0;for (int i=0;i<OrdersTotal();i++)
  {if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
  if((OrderType()==OP_SELL)&&(OrderMagicNumber()==magicSell))j++;
  }return(j);}
//+------------------------------------------------------------------+
double orders_lots()
  {double j=0;for (int i=0;i<OrdersTotal();i++)
  {if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
  if((OrderMagicNumber()==magicBuy||OrderMagicNumber()==magicSell))j=j+OrderLots();
  }return(j);} 
//+------------------------------------------------------------------+ 
int start()
  {
//----
  double stoch_0 = iStochastic(NULL,0,per_K,per_D,slow,MODE_LWMA,1,0,1);
  double stoch_1 = iStochastic(NULL,0,per_K,per_D,slow,MODE_LWMA,1,1,1);
//----
  if(orderss()>0)tp=orderss()*proffactor;
//----
  if (Ask>=openprice+tp*Point)close_buy();
  if (Bid<=openprice-tp*Point)close_sell();
//----
  if(AccountFreeMargin()<=AccountBalance()/2)return(0);  
//----
    if(orderss()<1)
    {  
    if((stoch_0>stoch_1)&&(stoch_1>zoneBUY))n=OrderSend(Symbol(),OP_BUY,lots,Ask,3,0,0,"",magicBuy,0,Blue);       
    if((stoch_0<stoch_1)&&(stoch_1<zoneSELL))n=OrderSend(Symbol(),OP_SELL,lots,Bid,3,0,0,"",magicSell,0,Red);
    }
//----   
  OrderSelect(n,SELECT_BY_TICKET, MODE_TRADES);openprice=OrderOpenPrice();
//----  
  if(orderss()>0)
  {
   if(orders_buy()>0)
     {
      if (Ask>=openprice+tp*Point)lots2=lots;
      if (Ask>=openprice+tp*Point)n=OrderSend(Symbol(),OP_BUY,lots2,Ask,3,0,0,"",magicBuy,0,Blue);
      if (Ask<=openprice-step*Point)lots2=lots2*mult;
      if (Ask<=openprice-step*Point)n=OrderSend(Symbol(),OP_BUY,NormalizeDouble(lots2,1),Ask,3,0,0,"",magicBuy,0,Blue);
     }
   if(orders_sell()>0)
     {
      if (Bid<=openprice-tp*Point)lots2=lots;
      if (Bid<=openprice-tp*Point)n=OrderSend(Symbol(),OP_SELL,lots2,Bid,3,0,0,"",magicSell,0,Red);
      if (Bid>=openprice+step*Point)lots2=lots2*mult;
      if (Bid>=openprice+step*Point)n=OrderSend(Symbol(),OP_SELL,NormalizeDouble(lots2,1),Bid,3,0,0,"",magicSell,0,Red);
     }
  }
  
  Comment("Total = ",orderss(),"  Lot = ",orders_lots(),"  TakeProfitSell = ",openprice-tp*Point,"  TakeProfitBay = ",openprice+tp*Point,
   "  FreeMargin = ",AccountFreeMargin(),"  Balance = ",AccountBalance(),"  openprice = ",openprice,"  n =",n," lots2 =",lots2);
  }
//+------------------------------------------------------------------+
int init()
{
if((IsTesting()==false)&&(IsOptimization()==false)&&(IsVisualMode()==false))
{
openprice=GlobalVariableGet("openprice");
n=GlobalVariableGet("n");
lots2=GlobalVariableGet("lots2");
}}
int deinit()
{
if((IsTesting()==false)&&(IsOptimization()==false)&&(IsVisualMode()==false))
{
GlobalVariableSet("openprice",openprice);
GlobalVariableSet("n",n);
GlobalVariableSet("lots2",lots2);
}}