import java.text.SimpleDateFormat;
import java.util.*;

import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IIndicators.AppliedPrice;
//import com.dukascopy.calculator.OObject;
import com.dukascopy.api.IOrder.State;

public class TwentyPipsADay implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
    public IOrder oOrder; 
    
    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.context = context;
        this.indicators = context.getIndicators();
        this.userInterface = context.getUserInterface();
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
    }
    
    boolean lock;
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    	  if ((period.getInterval() != 1000 * 30 * 60) && (instrument == Instrument.EURUSD)) {
              // console.getOut().println("Incorrect bar period, p = " +
              // period.getInterval());
              return; // играем только на 10 мин барах
          }
    	
    	  if (oOrder == null) {
			  
			  if (getSMA(instrument, period, askBar) == 1 && getMomentum(instrument, period) == 1) {
    			  String tmpstr = getLabel(instrument);
    			  oOrder = engine.submitOrder(tmpstr, instrument, OrderCommand.BUY, 0.5, 0, 0, askBar.getClose() - instrument.getPipValue() *20 ,askBar.getClose() + instrument.getPipValue() *20 );
    			  lock = true;
    			  return;
    		  }
    		  if (getSMA(instrument, period,bidBar) == -1 && getMomentum(instrument, period) == -1) {
    			  String tmpstr = getLabel(instrument);
    			  oOrder = engine.submitOrder(tmpstr, instrument, OrderCommand.SELL, 0.5, 0, 0, bidBar.getClose() + instrument.getPipValue() *20 ,bidBar.getClose() - instrument.getPipValue() *20 );
    			  lock = true;
    			  return;
    		  }  
		  }
	  
    	  
    	 if (/*allowTradeByTime(13, 23, instrument) && !lock*/ oOrder.getState() != State.FILLED && oOrder.getState() != State.CREATED) {
    		  console.getOut().println("getsma=" + getSMA(instrument, period, bidBar) + " MOM=" +  getMomentum(instrument, period));
    		  if (getSMA(instrument, period, askBar) == 1 && getMomentum(instrument, period) == 1) {
    			  String tmpstr = getLabel(instrument);
    			  oOrder = engine.submitOrder(tmpstr, instrument, OrderCommand.BUY, 0.5, 0, 0, askBar.getClose() - instrument.getPipValue() *20 ,askBar.getClose() + instrument.getPipValue() *20 );
    			  lock = true;
    		  }
    		  if (getSMA(instrument, period,bidBar) == -1 && getMomentum(instrument, period) == -1) {
    			  String tmpstr = getLabel(instrument);
    			  oOrder = engine.submitOrder(tmpstr, instrument, OrderCommand.SELL, 0.5, 0, 0, bidBar.getClose() + instrument.getPipValue() *20 ,bidBar.getClose() - instrument.getPipValue() *20 );
    			  lock = true;
    		  }
    	 }
    		  
    	  } 
    private int tagCounter = 0;
    protected String getLabel(Instrument instrument) {
        String label = instrument.name();
        label = label.substring(0, 2) + label.substring(3, 5);
        label = label + (tagCounter++);
        label = label.toLowerCase();
        return label;
    }
    
  
    
    private double getSMA(Instrument instrument, Period period, IBar bar) throws JFException {
    	
    	double sma = indicators.sma(instrument, period, OfferSide.BID, AppliedPrice.CLOSE, 20, 1);
    	sma = indicators.ma(instrument,period,OfferSide.BID,IIndicators.AppliedPrice.CLOSE,20,IIndicators.MaType.SMA,0);

    	
    	if (bar.getLow() > sma) {
    		return 1;
    	} else if (bar.getHigh() < sma) {
    		return -1;
    	} else {
    		return 0;
    	}
    }
    
    private double getMomentum(Instrument instrument, Period period) throws JFException {
    	double momentum = indicators.mom(instrument, period, OfferSide.BID, AppliedPrice.CLOSE, 5, 1);
    	 console.getOut().println("MOM=" +  momentum);
    	double s = 0;
    	for (int i = 2; i < 73; i++) {
    		s += indicators.mom(instrument, period, OfferSide.BID, AppliedPrice.CLOSE, 5, i);
    	}
    	s/=72;
    	if (momentum > s) {
    		return 1;
    	} else {
    		return -1;
    	}
    }
}