//+------------------------------------------------------------------+
//|                                                     Standard.mq4 |
//|                                             Komgrit Sungkhaphong |
//|                               http://iamforextrader.blogspot.com |
//+------------------------------------------------------------------+
#property copyright "Komgrit Sungkhaphong"
#property link      "http://iamforextrader.blogspot.com"
#include <StandardVariables.mqh>
#include <Func1.mqh>


   double distance;
   int counterticket;
   bool LockDone;
   int LockedBuy=0;
   int LockedSell=0;
   double UpBound,
         DownBound,
         StopBuyStop,
         StopSellStop,
         BuyStopPrice,
         SellStopPrice,
         TargetBuy,
         TargetSell;



//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   ArrayInitialize(Trades,0);

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   PlaySound("tick.wav");
   //Terminal();

   
   
   
   //Print("Pending Buy Stop="+Mas_Tip[4]);
   //----0------------
   //figure out where is the best possible entry for either buy/sell
   double ema144=iMA(NULL,0,144,0,MODE_EMA,PRICE_CLOSE,0);
   double ema169=iMA(NULL,0,169,0,MODE_EMA,PRICE_CLOSE,0);
   double ema12=iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,0);
   double ATR=iATR(NULL,0,ATRPeriod,0);
   

   datetime Exp;      
   if(ema144>ema169)
   {
      UpBound=ema144+BufferPoints*Point;
      DownBound=ema169-BufferPoints*Point;
   }
   else
   {
      UpBound=ema169+BufferPoints*Point;
      DownBound=ema144-BufferPoints*Point;
   }
   


   
   
   Exp=TimeCurrent()+900;
   
   //----0.5----------
   //Lot size control
   
   
   
   double Lot;
   double Gearing;
   /*
   Gearing=(AccountBalance()*(AcceptableLossPercent/100)/(ATR*Multi.Points()));
   Lot=Gearing;
   
   if(Lot<MarketInfo(Symbol(),MODE_MINLOT))  Lot=MarketInfo(Symbol(),MODE_MINLOT);
   if(Lot>MarketInfo(Symbol(),MODE_MAXLOT)) Lot=MarketInfo(Symbol(),MODE_MAXLOT);
   */
   
   Gearing=(AccountBalance()/StartedCapital);
   if(Gearing>1)   Lot=Gearing*StartLotSize;
   else Lot=StartLotSize;
   
   
   //----0.8----------
   //looking previous 5 trades to determine the performance
   //if losing will reduce the lot size
   //if winning would increase lot size
   

   
   
   
   //----1------------
   //issue first order    
   if(CountOrder.Sell()==0 && CountOrder.Buy()==0 && CountOrder.BuyStop()==0)
   {
      LockedBuy=0;      
      if(Ask+StopPoint()<UpBound && Bid-StopPoint()>DownBound)
      {
         BuyStopPrice=UpBound;
         StopBuyStop=UpBound-CoEffStopLoss*ATR;
         TargetBuy=BuyStopPrice+60*Point;
         //StopBuyStop=UpBound-StopLossPoint*Point;
         SendOrder.BuyStop(Lot,BuyStopPrice, StopBuyStop,0,Exp);      
      } 
   }   

   if(CountOrder.Sell()==0 && CountOrder.Buy()==0 && CountOrder.SellStop()==0)
   {
      LockedSell=0;
      if(Ask+StopPoint()<UpBound && Bid-StopPoint()>DownBound)
      {
         SellStopPrice=DownBound;
         StopSellStop=DownBound+CoEffStopLoss*ATR;
         TargetSell=SellStopPrice-60*Point;
         //StopBuyStop=DownBound+StopLossPoint*Point;
         SendOrder.SellStop(Lot,SellStopPrice, StopSellStop,0,Exp);      
      }
   }
 

   
   //----2------------
   //wait until it's filled
   if(Find.OrderBuy()>0 ||Find.OrderSell()>0)
   {
      Delete.PendingAll();
   }   
   
   //----3------------
   //trailing stoploss orders for profit

   if(Find.OrderBuy()>0)
   {
      counterticket=0;
      LockDone=false;
      TrailingBuy(Find.OrderBuy(),TRSL.1,TRSL.2,TRSL.3,TRSL.4);
      
   }  
   if(Find.OrderSell()>0)
   {  
      counterticket=0;
      LockDone=false;
      TrailingSell(Find.OrderSell(),TRSL.1,TRSL.2,TRSL.3,TRSL.4);
      
   }    
   
   
   //----4------------
   //Lock Profit
   
   
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

int FindLastTrades()
{
   int ticket;
   datetime closetime;
   double result;
   for (int i =0; i < OrdersHistoryTotal(); i++)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)
         && OrderSymbol()==Symbol()
         && OrderMagicNumber()==Magic
         && OrderProfit()!=0
         && OrderClosePrice()!=0)
         {
            if(OrderTicket()>ticket)   ticket=OrderTicket();
         }
   }
   return(ticket);
}

int FindNextLastTrades(int _LastTicket)
{
   int ticket;
   datetime closetime;
   double result;
   for (int i =0; i < OrdersHistoryTotal(); i++)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)
         && OrderSymbol()==Symbol()
         && OrderMagicNumber()==Magic
         && OrderClosePrice()!=0)
         {
            if(OrderTicket()>ticket && ticket!=_LastTicket)   ticket=OrderTicket();
         }
   }
   return(ticket);
}