//+------------------------------------------------------------------+
//
//+------------------------------------------------------------------+
#property link      "http://www.forexfactory.com/" 
#property version   "1.00"
#property description "Programmer Voldemar227"
#property strict
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
enum Trade
  {
   Only_BUY,   // Only BUY
   Only_SELL,  // Only SELL
   Only_All    // BUY and SELL
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
enum ma
  {
   BUY_MA  = 0,
   SELL_MA = 1,
   OFF     = 2
  };
input Trade  TypeOrders  = Only_SELL;
input double Lot         = 0.00;  // Lot
input double Risk        = 1.01;  // Risk percent if Lot=0;
input int    MinStep     = 400;   // Step order
input int    MinStepPlus = 0;     // Add minimal step
input double MinProfit   = 10.00; // Minimal Profit Close
input int    Magic       = 227;
input int    Slippage    = 30;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   int b=0,s=0;
   double
   BuyProfit  = 0,
   SellProfit = 0,
   BuyMin     = 0,
   SellMax    = 0,
   BuyMinLot  = 0,
   SellMaxLot = 0,
   BuyLot     = 0,
   SellLot    = 0,
   BuyMax     = 0,
   SellMin    = 0,
   SellTicProfit = 0,
   BuyTicProfit  = 0;
   int
   BuyMaxTic  = 0,
   SellMinTic = 0;

   for(int i=OrdersTotal()-1; i>=0; i--)
      if(OrderSelect(i,SELECT_BY_POS))
         if(OrderSymbol()==Symbol())
            if(OrderMagicNumber()==Magic)
              {
               if(OrderType()==OP_BUY)
                 {
                  b++;
                  if(OrderOpenPrice()<BuyMin || BuyMin==0)
                    {
                     BuyMin=OrderOpenPrice();
                     BuyMinLot=OrderLots();
                    }
                  if(OrderOpenPrice()>BuyMax || BuyMin==0)
                    {
                     BuyMax=OrderOpenPrice();
                     BuyMaxTic=OrderTicket();
                     BuyTicProfit=OrderProfit();
                    }
                  if(OrderProfit()>0)BuyProfit+=OrderProfit()+OrderCommission()+OrderSwap();
                 }
               if(OrderType()==OP_SELL)
                 {
                  s++;
                  if(OrderOpenPrice()>SellMax || SellMax==0)
                    {
                     SellMax=OrderOpenPrice();
                     SellMaxLot=OrderLots();
                    }
                  if(OrderOpenPrice()<SellMin || SellMin==0)
                    {
                     SellMin=OrderOpenPrice();
                     SellMinTic=OrderTicket();
                     SellTicProfit=OrderProfit();
                    }
                  if(OrderProfit()>0)SellProfit+=OrderProfit()+OrderCommission()+OrderSwap();
                 }
              }
// Summ profit final
   double ProfitBuy=BuyTicProfit+BuyProfit;
   double ProfitSel=SellTicProfit+SellProfit;

   double  lot=NormalizeDouble(AccountBalance()*Risk/100/MarketInfo(Symbol(),MODE_MARGINREQUIRED),2);

   if(b==0)
      BuyLot=(Lot>0)?Lot:lot;
   else
      BuyLot=BuyMinLot+Lot;
   if(s==0)
      SellLot=(Lot>0)?Lot:lot;
   else
      SellLot=SellMaxLot+Lot;

   double BuyDist  = NormalizeDouble(MinStep*Point()+MinStepPlus*Point()*b,Digits());
   double SellDist = NormalizeDouble(MinStep*Point()+MinStepPlus*Point()*s,Digits());


   if(TypeOrders==Only_BUY || TypeOrders==Only_All)
      if((b==0) || (b>0 && (BuyMin-Ask)>=BuyDist))
         if(OrderSend(Symbol(),OP_BUY,NormalizeDouble(BuyLot,2),NormalizeDouble(Ask,Digits()),Slippage,0,0,"My order",Magic,0,clrGreen)<0)
            Print(" Error open Buy N ",GetLastError());

   if(TypeOrders==Only_SELL || TypeOrders==Only_All)
      if((s==0) || (s>0 && (Bid-SellMax)>=SellDist))
         if(OrderSend(Symbol(),OP_SELL,NormalizeDouble(SellLot,2),NormalizeDouble(Bid,Digits()),Slippage,0,0,"My order",Magic,0,clrGreen)<0)
            Print(" Error open Sell N ",GetLastError());

   if(ProfitBuy>=MinProfit && b>=2)
      CloseAll(OP_BUY,BuyMaxTic);

   if(ProfitSel>=MinProfit && s>=2)
      CloseAll(OP_SELL,SellMinTic);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
void CloseAll(int aType,int ticket)
  {
   for(int i=OrdersTotal()-1; i>=0; i--)
      if(OrderSelect(i,SELECT_BY_POS))
         if(OrderSymbol()==Symbol())
            if(OrderMagicNumber()==Magic)
              {
               if(OrderType()==aType && OrderType()==OP_BUY)
                  if(OrderProfit()>0 || OrderTicket()==ticket)
                     if(!OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits()),Slippage,clrRed))
                        Print(" OrderClose OP_BUY Error N",GetLastError());

               if(OrderType()==aType && OrderType()==OP_SELL)
                  if(OrderProfit()>0 || OrderTicket()==ticket)
                     if(!OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits()),Slippage,clrRed))
                        Print(" OrderClose OP_SELL Error N",GetLastError());

              }
  }
//+------------------------------------------------------------------+
