package jforex.strategies;
 
import java.text.SimpleDateFormat;
import java.util.TimeZone;
 
import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
 
public class TradeOnDirection implements IStrategy {
 
   @Configurable("Instrument")
   public Instrument instrument = Instrument.EURUSD;
   @Configurable("Instrument")
   public Period period = Period.TEN_SECS;   
   @Configurable("Amount")
   public double amount = 0.001;
   @Configurable("Stop loss")
   public int stopLossPips = 3;
   @Configurable("Take profit")
   public int takeProfitPips = 5;
    
   private IEngine engine;
   private IConsole console;
   private IHistory history;
    
   private int orderCount;
    
   @Override
   public void onStart(IContext context) throws JFException {
      this.engine = context.getEngine();
      this.console = context.getConsole();
      this.history = context.getHistory();
   }
 
   @Override
   public void onTick(Instrument instrument, ITick tick) throws JFException {
       
   }
 
   @Override
   public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
       
      if( !this.instrument.equals(instrument) || !this.period.equals(period) ){
         return;      
      }

      //take previous bar from historical data
      IBar prevBar = history.getBar(instrument, period, OfferSide.BID, 2);
      
      console.getOut().println(bidBar.getClose() + " " + prevBar.getClose());
 
      OrderCommand cmd = bidBar.getClose() > prevBar.getClose()
         ? OrderCommand.BUY
         : OrderCommand.SELL;
       
      submitOrder(cmd);
   }
    
   private IOrder submitOrder(OrderCommand orderCmd) throws JFException {
 
      double stopLossPrice, takeProfitPrice;
 
      // Calculating stop loss and take profit prices
      if (orderCmd == OrderCommand.BUY) {
         stopLossPrice = history.getLastTick(this.instrument).getBid() - getPipPrice(this.stopLossPips);
         takeProfitPrice = history.getLastTick(this.instrument).getBid() + getPipPrice(this.takeProfitPips);
      } else {
         stopLossPrice = history.getLastTick(this.instrument).getAsk() + getPipPrice(this.stopLossPips);
         takeProfitPrice = history.getLastTick(this.instrument).getAsk() - getPipPrice(this.takeProfitPips);
      }
 
      // Submitting an order for the specified instrument at the current market price
      return engine.submitOrder("order" + ++orderCount, this.instrument, orderCmd, this.amount, 0, 5, stopLossPrice, takeProfitPrice);
   }
    
   private double getPipPrice(int pips) {
      return pips * this.instrument.getPipValue();
   }
 
   @Override
   public void onMessage(IMessage message) throws JFException {
      //if the message is related to order print its content
      if (message.getOrder() != null){
         console.getOut().println(message.getOrder().getLabel() + " " + message.getType() + " " + message.getContent());
      }
   }
 
   @Override
   public void onAccount(IAccount account) throws JFException {
 
   }
 
   @Override
   public void onStop() throws JFException {
      //close all orders on strategy stop
      for(IOrder o : engine.getOrders()){
         o.close();
      }
   }
 
}
