//------------------------------------------------------------------
#property copyright "www.forex-station.com"
#property link      "www.forex-station.com"
#property strict
//------------------------------------------------------------------

enum enPrices
{
   pr_close,      // Close
   pr_open,       // Open
   pr_high,       // High
   pr_low,        // Low
   pr_median,     // Median
   pr_typical,    // Typical
   pr_weighted,   // Weighted
   pr_average,    // Average (high+low+open+close)/4
   pr_medianb,    // Average median body (open+close)/2
   pr_tbiased,    // Trend biased price
   pr_tbiased2,   // Trend biased (extreme) price
   pr_haclose,    // Heiken ashi close
   pr_haopen ,    // Heiken ashi open
   pr_hahigh,     // Heiken ashi high
   pr_halow,      // Heiken ashi low
   pr_hamedian,   // Heiken ashi median
   pr_hatypical,  // Heiken ashi typical
   pr_haweighted, // Heiken ashi weighted
   pr_haaverage,  // Heiken ashi average
   pr_hamedianb,  // Heiken ashi median body
   pr_hatbiased,  // Heiken ashi trend biased price
   pr_hatbiased2  // Heiken ashi trend biased (extreme) price
};
enum enFilterWhat
{
   flt_prc,  // Filter the price
   flt_val,  // Filter the step ma
   flt_both  // Filter both
};
enum enRsiTypes
{
   rsi_rsi,  // Regular RSI
   rsi_slo,  // Slow RSI
   rsi_rap,  // Rapid RSI
   rsi_har,  // Harris RSI
   rsi_rsx,  // RSX
   rsi_cut   // Cuttlers RSI
};


input int                MagicNumber   = 123456;        // Magic number to use for the EA
input bool               EcnBroker     = false;         // Is your broker ECN/STP type of broker?
input double             LotSize       = 0.1;           // Lot size to use for trading
input int                Slippage      = 3;             // Slipage to use when opening new orders
input double             StopLoss      = 100;           // Initial stop loss (in pips)
input double             TakeProfit    = 100;           // Initial take profit (in pips)

input string             dummy1        = "";            // .
input string             dummy2        = "";            // Settings for indicators
input int                BarToUse      = 1;             // Bar to test (0, for still opened, 1 for first closed, and so on)
input enRsiTypes         RsiType       = rsi_rsi;       // RSI calculation method
input int                RsiLength     = 14;            // Rsi length
input enPrices           RsiPrice      = pr_close;      // Rsi price
input double             Sensitivity   = 4;             // Sensivity Factor
input double             StepSize      = 5;             // Atr step divisor
input double             Filter        = 0;             // Filter (<=0, no filtering)
input int                FilterPeriod  = 0;             // Filter period (0<= use rsi period)
input enFilterWhat       FilterOn      = flt_val;       // Apply filter to :
input string             mafilter;                      // Ma filter settings
input bool               MaFilter      = false;         // Use ma filter true/false
input int                TrendPeriod   = 200;           // Trend ma period
input ENUM_MA_METHOD     TrendMaMethod = MODE_EMA;      // Trend ma method
input ENUM_APPLIED_PRICE TrendMaPrice  = PRICE_CLOSE;   // Trend ma price 

input string             dummy3        = "";            // .  
input string             dummy4        = "";            // General settings
input bool               DisplayInfo   = true;          // Dislay info

bool dummyResult;
//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int OnInit()   { return(INIT_SUCCEEDED); }
void OnDeinit(const int reason) { }

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

#define _doNothing 0
#define _doBuy     1
#define _doSell    2
void OnTick()
{
   int doWhat = _doNothing;
      double trend_cur = iCustom(NULL,0,"StepMA of rsi adaptive tema 1.1",PERIOD_CURRENT,RsiType,RsiLength,RsiPrice,Sensitivity,StepSize,Filter,FilterPeriod,FilterOn,10,BarToUse);
      double trend_pre = iCustom(NULL,0,"StepMA of rsi adaptive tema 1.1",PERIOD_CURRENT,RsiType,RsiLength,RsiPrice,Sensitivity,StepSize,Filter,FilterPeriod,FilterOn,10,BarToUse+1);
      if (trend_cur!=trend_pre)
         if (trend_cur == 1)
               doWhat = _doBuy;
         else  doWhat = _doSell;
         if (doWhat==_doNothing && !DisplayInfo) return;
         
         //
         //
         //
         //
         //
         
         bool closeAbove;
         bool closeBelow; 
         if (MaFilter==true)
         {
           closeAbove = (Close[1]>iMA(NULL,0,TrendPeriod,0,TrendMaMethod,TrendMaPrice,1));
           closeBelow = (Close[1]<iMA(NULL,0,TrendPeriod,0,TrendMaMethod,TrendMaPrice,1));
         }
         else      
         {
           closeAbove = true;
           closeBelow = true;
         }
         
   //
   //
   //
   //
   //
   
   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 (DisplayInfo) currentProfit += OrderProfit()+OrderCommission()+OrderSwap();
         
         //
         //
         //
         //
         //
         
         if (OrderType()==OP_BUY)
            if (doWhat==_doSell && closeBelow)
                  { RefreshRates(); if (!OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrNONE)) openedBuys++; }
            else  openedBuys++;
         if (OrderType()==OP_SELL)
            if (doWhat==_doBuy && closeAbove)
                  { RefreshRates(); if (!OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,clrNONE)) openedSells++; }
            else  openedSells++;            
   }
   if (DisplayInfo) Comment("Current profit : "+DoubleToStr(currentProfit,2)+" "+AccountCurrency()); if (doWhat==_doNothing) return;

   //
   //
   //
   //
   //

   if (doWhat==_doBuy && closeAbove && openedBuys==0)
      {
         RefreshRates();
         double stopLossBuy   = 0; if (StopLoss>0)   stopLossBuy   = Ask-StopLoss*_Point*pow(10,_Digits%2);
         double takeProfitBuy = 0; if (TakeProfit>0) takeProfitBuy = Ask+TakeProfit*_Point*pow(10,_Digits%2);
         if (EcnBroker)
         {
            int ticketb = OrderSend(_Symbol,OP_BUY,LotSize,Ask,Slippage,0,0,"",MagicNumber,0,clrNONE);
            if (ticketb>-1)
              dummyResult = OrderModify(ticketb,OrderOpenPrice(),stopLossBuy,takeProfitBuy,0,clrNONE);
         }
         else dummyResult = OrderSend(_Symbol,OP_BUY,LotSize,Ask,Slippage,stopLossBuy,takeProfitBuy,"",MagicNumber,0,clrNONE);
      }
   if (doWhat==_doSell && closeBelow && openedSells==0)
      {
         RefreshRates();
         double stopLossSell   = 0; if (StopLoss>0)   stopLossSell   = Bid+StopLoss*_Point*pow(10,_Digits%2);
         double takeProfitSell = 0; if (TakeProfit>0) takeProfitSell = Bid-TakeProfit*_Point*pow(10,_Digits%2);
         if (EcnBroker)
         {
            int tickets = OrderSend(Symbol(),OP_SELL,LotSize,Bid,Slippage,0,0,"",MagicNumber,0,clrNONE);
            if (tickets>-1)
              dummyResult = OrderModify(tickets,OrderOpenPrice(),stopLossSell,takeProfitSell,0,clrNONE);
         }
         else dummyResult = OrderSend(Symbol(),OP_SELL,LotSize,Bid,Slippage,stopLossSell,takeProfitSell,"",MagicNumber,0,clrNONE);
      }
 return;
}