//------------------------------------------------------------------
#property copyright "www.forex-station.com"
#property link      "www.forex-station.com"
#property strict
//------------------------------------------------------------------

extern int    MagicNumber = 123456;  // Magic number to use for the EA
extern bool   EcnBroker   = false;   // Is your broker ECN/STP type of broker?
extern double LotSize     = 0.1;     // Lot size to use for trading
extern int    Slippage    = 3;       // Slipage to use when opening new orders
extern double StopLoss    = 100;     // Initial stop loss (in pips)
extern double TakeProfit  = 100;     // Initial take profit (in pips)

extern string dummy1      = "";      // .
extern string dummy2      = "";      // Settings for indicators
/*
extern int    BarToUse    = 1;       // Bar to test (0, for still opened, 1 for first closed, and so on)
extern int                Ma1Period   = 12;          // Fast ma period
extern ENUM_APPLIED_PRICE Ma1Price    = PRICE_CLOSE; // Fast ma price
extern ENUM_MA_METHOD     Ma1Method   = MODE_SMA;    // Fast ma method
extern int                Ma2Period   = 26;          // Slow ma period
extern ENUM_APPLIED_PRICE Ma2Price    = PRICE_CLOSE; // Slow ma price
extern ENUM_MA_METHOD     Ma2Method   = MODE_SMA;    // Slow ma method */

input  ENUM_TIMEFRAMES    TimeFrame   = PERIOD_CURRENT;  // Time frame
input  int                MaPeriod    = 1;               // MA period
input  int                MaShift     = 0;               // MA shift
input  ENUM_MA_METHOD     MaMethod    = MODE_EMA;        // MA method
input  ENUM_APPLIED_PRICE UpperPrice  = PRICE_HIGH;      // Upper price
input  ENUM_APPLIED_PRICE LowerPrice  = PRICE_LOW;       // Lower price
input  double             Deviation   = 0.0;             // Deviation
input  int                DwDigits    = 4;               // Data window digits 
input  bool               Interpolate = false;           // Interpolate in MTF mode?

extern string dummy3      = "";      // . 
extern string dummy4      = "";      // General settings
extern bool   DisplayInfo = true;    // Dislay info
extern bool   CloseAfterOneBar=false; // Close order at the next bar?

bool dummyResult;
//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()   { return(0); }
int deinit() { return(0); }

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

#define _doNothing 0
#define _doBuy     1
#define _doSell    2
int start()
{
   int doWhat = _doNothing;
  /*    double diffc = iMA(NULL,0,Ma1Period,0,Ma1Method,Ma1Price,BarToUse)  -iMA(NULL,0,Ma2Period,0,Ma2Method,Ma2Price,BarToUse);
      double diffp = iMA(NULL,0,Ma1Period,0,Ma1Method,Ma1Price,BarToUse+1)-iMA(NULL,0,Ma2Period,0,Ma2Method,Ma2Price,BarToUse+1);
      if ((diffc*diffp)<0)
         if (diffc>0)*/

         
         double diffc = iCustom(NULL,TimeFrame,"Trend envelopes (new)",0,MaPeriod,MaShift,MaMethod,UpperPrice,LowerPrice,Deviation,0,0);
         double diffp = iCustom(NULL,TimeFrame,"Trend envelopes (new)",0,MaPeriod,MaShift,MaMethod,UpperPrice,LowerPrice,Deviation,1,0);
         
         if (diffc<Ask)
               doWhat = _doBuy;
         else  
         if (diffp>Bid)       
               doWhat = _doSell;
               
         if (doWhat==_doNothing && !DisplayInfo) return(0);
         
   //
   //
   //
   //
   //
   
   int    openedBuys    = 0;
   int    openedSells   = 0;
   double currentProfit = 0;
   for (int i = OrdersTotal()-1; i>=0; i--)
   {
      if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
      if (OrderSymbol()      != Symbol())            continue;
      if (OrderMagicNumber() != MagicNumber)         continue;

      //
      //
      //
      //
      //
      
      if (CloseAfterOneBar)
      {
         int barOpenedAt = iBarShift(NULL,0,OrderOpenTime());
         int barCurrent  = iBarShift(NULL,0,Time[0]);
         if (barOpenedAt!=barCurrent)
         {
            RefreshRates();
               if (OrderType() == OP_BUY)  if (OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,CLR_NONE)) continue;
               if (OrderType() == OP_SELL) if (OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,CLR_NONE)) continue;
         }               
      }
      if (DisplayInfo) currentProfit += OrderProfit()+OrderCommission()+OrderSwap();
         
         if (OrderType()==OP_BUY)
            if (doWhat==_doSell)
                  { RefreshRates(); if (!OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,CLR_NONE)) openedBuys++; }
            else  openedBuys++;
         if (OrderType()==OP_SELL)
            if (doWhat==_doBuy)
                  { RefreshRates(); if (!OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,CLR_NONE)) openedSells++; }
            else  openedSells++;            
   }
   if (DisplayInfo) Comment("Current profit : "+DoubleToStr(currentProfit,2)+" "+AccountCurrency()); if (doWhat==_doNothing) return(0);

   //
   //
   //
   //
   //

   if (doWhat==_doBuy && openedBuys==0)
      {
         RefreshRates();
         double stopLossBuy   = 0; if (StopLoss>0)   stopLossBuy   = Ask-StopLoss*Point*MathPow(10,Digits%2);
         double takeProfitBuy = 0; if (TakeProfit>0) takeProfitBuy = Ask+TakeProfit*Point*MathPow(10,Digits%2);
         if (EcnBroker)
         {
            int ticketb = OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage,0,0,"",MagicNumber,0,CLR_NONE);
            if (ticketb>-1)
              dummyResult = OrderModify(ticketb,OrderOpenPrice(),stopLossBuy,takeProfitBuy,0,CLR_NONE);
         }
         else dummyResult = OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage,stopLossBuy,takeProfitBuy,"",MagicNumber,0,CLR_NONE);
      }
   if (doWhat==_doSell && openedSells==0)
      {
         RefreshRates();
         double stopLossSell   = 0; if (StopLoss>0)   stopLossSell   = Bid+StopLoss*Point*MathPow(10,Digits%2);
         double takeProfitSell = 0; if (TakeProfit>0) takeProfitSell = Bid-TakeProfit*Point*MathPow(10,Digits%2);
         if (EcnBroker)
         {
            int tickets = OrderSend(Symbol(),OP_SELL,LotSize,Bid,Slippage,0,0,"",MagicNumber,0,CLR_NONE);
            if (tickets>-1)
              dummyResult = OrderModify(tickets,OrderOpenPrice(),stopLossSell,takeProfitSell,0,CLR_NONE);
         }
         else dummyResult = OrderSend(Symbol(),OP_SELL,LotSize,Bid,Slippage,stopLossSell,takeProfitSell,"",MagicNumber,0,CLR_NONE);
      }
      
   
            
   DrawLabel("Profit",5,15,"Profit",8,"Arial",clrDodgerBlue,"");
   DrawLabel("Balance",5,30,"Balance",8,"Arial",clrDodgerBlue,"");
   DrawLabel("Equity",5,45,"Equity",8,"Arial",clrDodgerBlue,"");
   
   if(DoubleToStr(AccountProfit(),2)> 0 ) DrawLabel("Profitx",85,15,DoubleToStr(AccountProfit(),2) + " $",8,"Arial",clrLime,DoubleToStr(AccountProfit(),2));
      else
        if(DoubleToStr(AccountProfit(),2) < 0 ) DrawLabel("Profitx",85,15,DoubleToStr(AccountProfit(),2) + " $",8,"Arial",clrRed,DoubleToStr(AccountProfit(),2));
   
   DrawLabel("Balancex",85,30,DoubleToStr(AccountBalance(),2) + " $",8,"Arial",clrYellow,DoubleToStr(AccountBalance(),2));
   DrawLabel("Equityx",85,45,DoubleToStr(AccountEquity(),2) + " $",8,"Arial",clrYellow,DoubleToStr(AccountEquity(),2));  
      
   return(0);
}

//+------------------------------------------------------------------+
//| Draw Label function                                              |
//+------------------------------------------------------------------+
void DrawLabel(string name,int x,int y,string label,int size=9,string font="Arial",color clr=DimGray,string tooltip="")
  {
//--- 
   if (ObjectFind(name)) ObjectDelete(name);
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,label,size,font,clr);
   ObjectSet(name,OBJPROP_CORNER,1);
   ObjectSet(name,OBJPROP_XDISTANCE,x);
   ObjectSet(name,OBJPROP_YDISTANCE,y);
   ObjectSetString(0,name,OBJPROP_TOOLTIP,tooltip);
//---
  }