package jforex;

import com.dukascopy.api.*;
import java.util.*;
import com.dukascopy.api.IIndicators.MaType;
import com.dukascopy.api.IIndicators;

public class koenig implements IStrategy {
    
    public Instrument myInstrument = Instrument.EURUSD;
  
    private IContext context = null;
    private IEngine engine = null;
    private IChart chart = null;
    private IHistory history = null;
    private IIndicators indicators = null;
    private IConsole console = null;
    private double volume = 0.02;
    private int profitLimit;
    private int lossLimit;
    private double bidPrice;
    private double askPrice;
    private double accountEquity;
    public Period period = Period.FIVE_MINS; 
   private Filter indicatorFilter = Filter.NO_FILTER;
   
    public void onStart(IContext context) throws JFException {
         Set subscribedInstruments = new HashSet();
         subscribedInstruments.add(Instrument.EURUSD);
         context.setSubscribedInstruments(subscribedInstruments);
        this.context = context;  
        engine = context.getEngine();
        indicators = context.getIndicators();
        history = context.getHistory();
        console = context.getConsole();
        indicators = context.getIndicators();
      
    }

    public void onStop() throws JFException {
    }
       
    public void onTick(Instrument instrument, ITick tick) throws JFException {
    if (!instrument.equals(Instrument.EURUSD) || !period.equals(Period.FIVE_MINS)) return; //filter FIVE_MINS EUR/USD ticks
      }
      
      protected int positionsTotal(Instrument instrument) throws JFException {
        int counter = 0;
        for (IOrder order : engine.getOrders(instrument)) {
            if (order.getState() == IOrder.State.FILLED) {
                counter++;
            }
        }
        return counter;
    }

    protected String getLabel(Instrument instrument) {
        String label = instrument.name();
        
        long time = new java.util.Date().getTime();

        label = label.substring(0, 2) + label.substring(3, 5);
        label = label + time;
        label = label.toLowerCase();
        return label;
    }
  
   public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar)throws JFException
   if (!instrument.equals(Instrument.EURUSD) || !period.equals(Period.FIVE_MINS)) return; //filter FIVE_MINS EUR/USD bars 
         } 
    
    IBar prevBar = history.getBar(instrument, period, OfferSide.BID, 1);
  if(period == Period.FIVE_MINS) { 
      
        profitLimit = 20;
        lossLimit = 80;
             
                      
      if (askbar.getVolume() == 0) return;
      

             double openPrice = bidbar.getOpen();
             
             askPrice = askbar.getClose();
             bidPrice = bidbar.getClose();

double ema = this.indicators.ema(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 34, 0);                                
double tvs = this.indicators.tvs(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 24, 0);
double tvs1 = this.indicators.tvs(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 24, 1);
               
       if (positionsTotal(instrument) == 0) {
           
            if  (bidPrice > ema && tvs > tvs1 && tvs > 0 && tvs1 < 0) {
                     
                 buy (instrument, engine, profitLimit, lossLimit, volume);
                  
                 }
                
                else  if (bidPrice < ema && tvs < tvs1 && tvs < 0 && tvs1 > 0) { 
                    
                sell (instrument, engine, profitLimit, lossLimit, volume);   
                    
                  }   
          }      
         }
   }
    

  public void onMessage(IMessage message) throws JFException {
        
        if (message != null && message.getType() == IMessage.Type.ORDER_CLOSE_OK) {
            
            IOrder lastOne = message.getOrder();
            
            double profitsLoss = lastOne.getProfitLossInPips();
            
            console.getOut().println("Order : "+lastOne.getLabel()+ " "+ lastOne.getOrderCommand()+ " Pips: "+profitsLoss);
            
           
        }
}
     





  @Override
   public void onAccount(IAccount account) throws JFException {
      
      accountEquity = account.getEquity();
       
        

   }
    public void sell(Instrument instrument, IEngine engine, int takeProfitPipLevel, int endLossPipLevel, double volumeParam)  throws JFException {
        
        engine.submitOrder(getLabel(instrument), instrument, IEngine.OrderCommand.SELL, volumeParam, 0, 3, bidPrice
                        + instrument.getPipValue() *endLossPipLevel, bidPrice - instrument.getPipValue() * takeProfitPipLevel);
     } 
     
      public void buy(Instrument instrument, IEngine engine, int takeProfitPipLevel, int endLossPipLevel, double volumeParam)  throws JFException {
        
         engine.submitOrder(getLabel(instrument), instrument, IEngine.OrderCommand.BUY, volumeParam, 0, 3, askPrice
                        - instrument.getPipValue() * endLossPipLevel, askPrice + instrument.getPipValue() * takeProfitPipLevel);
     } 
}