//+------------------------------------------------------------------+
//|                                                        by Maloma |
//+------------------------------------------------------------------+

#include <stdlib.mqh>
#include <stderror.mqh>
#include <b-Lots.mqh>

//extern double Lots=0.1;
extern int    LMA=5;//4
extern int    MMA=21;//22
extern int    MAType=2;//0..3/
extern int    PriceType=5;//0..6/
extern int    TP=80;//80
extern int    SL=50;//50
       bool   DoBuy=false, DoSell=false;
       int    magic=2938723,i,j;
       double LMA0,LMA1,LMA2,MMA0,MMA1,MMA2,WMA0,WMA1,WMA2;
      
int start() 
{
  LMA0=iMA(NULL,0,LMA,0, MAType,PriceType,0);
  LMA1=iMA(NULL,0,LMA,0, MAType,PriceType,1);
  LMA2=iMA(NULL,0,LMA,0, MAType,PriceType,2);
  MMA0=iMA(NULL,0,MMA,0, MAType,PriceType,0);
  MMA1=iMA(NULL,0,MMA,0, MAType,PriceType,1);
  MMA2=iMA(NULL,0,MMA,0, MAType,PriceType,2);
  
  if ((LMA2>MMA2) && (MMA1>LMA1) && (LMA0<LMA1) && !DoSell)
    {
     j=OrdersTotal()-1;
     for(i=j;i>=j;i--)
       {
        OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
        if ((OrderType()==OP_BUY)&&(OrderMagicNumber()==magic))
          {
           OrderClose(OrderTicket(),OrderLots(),Bid,3,CLR_NONE);
          }
       }
     DoSell=true;
     DoBuy=false;
     OrderSend(Symbol(),OP_SELL,GetSizeLot(),Bid,3,Ask+SL*Point,Ask-TP*Point,"",magic,0,CLR_NONE);
    }
  if ((LMA2<MMA2) && (MMA1<LMA1) && (LMA0>LMA1) && !DoBuy)
    {
     j=OrdersTotal()-1;
     for(i=j;i>=j;i--)
       {
        OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
        if ((OrderType()==OP_SELL)&&(OrderMagicNumber()==magic))
          {
           OrderClose(OrderTicket(),OrderLots(),Ask,3,CLR_NONE);
          }
       }
     DoBuy=true; 
     DoSell=false;
     OrderSend(Symbol(),OP_BUY,GetSizeLot(),Ask,3,Bid-SL*Point,Bid+TP*Point,"",magic,0,CLR_NONE);
    }
  return(0);
}