//+------------------------------------------------------------------+
//|                                                      NLMAExp.mq4 |
//|                                             http://liteforex.net |
//|                                                              Alf |
//+------------------------------------------------------------------+
#property copyright "http://liteforex.net"
#property link      "Alf"

//---- input parameters
extern int       ST=50;
extern int       TP=100;
extern int       STEP=30; // шаг для трелингстопа
double    Lot=0.1;
extern int     Price          = 0;
extern int     Length         = 9;
extern int     Displace       = 0;
extern int     Filter         = 0;
extern double  Deviation      = 0; 
extern double    MinLot=0.1;
extern double    MaxLot=5;
extern double    K=1.68;// коэфицмент изменения лота, 1 лот неизменяется 2 удвоение 3 утроение и т.д.

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   lot();
   Comment("Lot=",Lot);
   
   if(OrdersTotal()==1)
   {
      OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()==OP_BUY && sell()) {OrderClose(OrderTicket(),OrderLots(),Bid,3);OrderSend(Symbol(),OP_SELL,Lot,Bid,3,Ask+ST*Point,Bid-TP*Point);Alert("Sell");return(0);}
      if(OrderType()==OP_SELL && buy()) {OrderClose(OrderTicket(),OrderLots(),Ask,3);OrderSend(Symbol(),OP_BUY,Lot,Ask,3,Bid-ST*Point,Ask+TP*Point);Alert("Buy");return(0);}
      if(OrderType()==OP_BUY)
         if(OrderStopLoss()+ST*Point+STEP*Point<Bid) OrderModify(OrderTicket(),OrderOpenPrice(),Bid-ST*Point,OrderTakeProfit(),0);
      if(OrderType()==OP_SELL)
         if(OrderStopLoss()-ST*Point-STEP*Point>Ask)OrderModify(OrderTicket(),OrderOpenPrice(),Ask+ST*Point,OrderTakeProfit(),0);
   }
   
   if(OrdersTotal()==0)
   {
      
      if(buy()){OrderSend(Symbol(),OP_BUY,Lot,Ask,3,Bid-ST*Point,Ask+TP*Point);Alert("Buy");return(0);}
      if(sell()){OrderSend(Symbol(),OP_SELL,Lot,Bid,3,Ask+ST*Point,Bid-TP*Point);Alert("Sell");}
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+

bool buy()
{
   if(iCustom(Symbol(),0,"NonLagMA",Price,Length,Displace,Filter,1,1,Deviation,false,0,0)>iCustom(Symbol(),0,"NonLagMA",Price,Length,Displace,Filter,1,1,Deviation,false,0,1) && iCustom(Symbol(),0,"NonLagMA",Price,Length,Displace,Filter,1,1,Deviation,false,0,1)<iCustom(Symbol(),0,"NonLagMA",Price,Length,Displace,Filter,1,1,Deviation,false,0,2))return(true);
   return(false); 
}

bool sell()
{
      if(iCustom(Symbol(),0,"NonLagMA",Price,Length,Displace,Filter,1,1,Deviation,false,0,0)<iCustom(Symbol(),0,"NonLagMA",Price,Length,Displace,Filter,1,1,Deviation,false,0,1) && iCustom(Symbol(),0,"NonLagMA",Price,Length,Displace,Filter,1,1,Deviation,false,0,1)>iCustom(Symbol(),0,"NonLagMA",Price,Length,Displace,Filter,1,1,Deviation,false,0,2))return(true);
   return(false); 
}

void lot()
{
   if(OrdersHistoryTotal()==0) {Lot=MinLot;return;}
   OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY);
   if(OrderProfit()>0) {Lot=MinLot;return;}
   Lot=OrderLots()*K;
   if(MinLot<1) Lot=MathRound(Lot*10)/10;
   else Lot=MathRound(Lot);
   if(Lot<MinLot)Lot=MinLot;
   if(Lot>MaxLot)Lot=MaxLot;
}