package jforex.converted;
import java.awt.Color;
import jforex.mql.MQLBridge;
import com.dukascopy.api.*;
@Library("jftoolbox-1.6.jar")
public class MACD50 extends MQLBridge {
//+------------------------------------------------------------------+
//|                                               Robot_MACD_12.26.9 |
//|                                                     Tokman Yuriy |
//|                                            yuriytokman@gmail.com |
//+------------------------------------------------------------------+
             //External variables 
@Configurable("") public double TakeProfit = 1000;
@Configurable("") public double StopLoss = 30;
@Configurable("") public double TrailingStop  = 0;
@Configurable("") public double Lots = 0.5;


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
protected int start()
  {
   double MacdCurrent, MacdPrevious, SignalCurrent,SignalPrevious;
   int cnt, ticket, total;

   MacdCurrent=iMACD(null,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
   MacdPrevious=iMACD(null,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
   SignalCurrent=iMACD(null,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
   SignalPrevious=iMACD(null,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
   
   double rsi;
        
   rsi=iRSI(null,0,14,PRICE_OPEN,0);
            
        

   total=OrdersTotal();
   if(total<1)//check the number of warrants 
     {
      // Check available funds 
      if(AccountFreeMargin()<(1000*Lots))//number of free funds
        {
         Print("Insufficient funds  = ", AccountFreeMargin());
         return(0);  
        }
      // Opening a long position
      if(MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious
         && MacdCurrent<0 && SignalCurrent<0 && rsi<50 && rsi>45 )
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-StopLoss*Point,Ask+TakeProfit*Point,"-",0,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("open position BUY : ",OrderOpenPrice());
           }
         else Print("Error opening BUY position : ",GetLastError()); 
         return(0);
        }
      // Opening short position
      if(MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious
         && rsi>40 && rsi<60)
        {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+StopLoss*Point,Bid-TakeProfit*Point,"-",0,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("open position SELL : ",OrderOpenPrice());
           }
         else Print("Error opening SELL position : ",GetLastError()); 
         return(0); 
        }
      return(0);
     }
  // Terms of closing orders     
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // availability of open orders 
         OrderSymbol()==Symbol())  // Do the same financial instruments
        {
         if(OrderType()==OP_BUY)   // long position is open
           {
            // Closing condition 
            if(MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious && rsi>48)
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
                 return(0);
                }
           //Trailing Stop
            if(TrailingStop>0)
            {
             if(Bid-OrderOpenPrice()>Point*TrailingStop)
              {
                  if((OrderStopLoss()<Bid-Point*TrailingStop)|| (OrderStopLoss()==0))
                   {
                    OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                    return(0);   
                   }
                 }   
            }
             
           }
         else // short position is open
           {
            // Closing condition
            if( MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious && rsi<50)
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
               return(0);
              }
              
          //Trainling Stop
            if(TrailingStop>0)
             {
                 if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                  {
                      if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                       {
                           OrderModify(OrderTicket(),OrderOpenPrice(),Ask=Point*TrailingStop,OrderTakeProfit(),0,Red);
                           return(0);
                       }
                 }
              } 
           }
        }
     }
   return(0);
  }
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException{}/**/};
