package newjforex;

import java.io.File;

import com.dukascopy.api.Configurable;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IChart;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IEngine;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IHistory;
import com.dukascopy.api.IIndicators;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IOrder;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;
import com.dukascopy.api.indicators.IIndicator;


public class CustomIndicatorStrategy implements IStrategy {
	//Configurable parameters
		@Configurable("Instrument") public Instrument instrument = Instrument.EURUSD;	
		@Configurable("Amount") public double amount = 0.001;
		@Configurable("Stop loss") public int stopLossPips = 40;
		@Configurable("Take profit") public int takeProfitPips = 40;
	
	private IConsole console;
    private IHistory history;
    private IIndicators indicators;
    private IEngine engine;
    private IIndicator indicator;
    private IChart chart;
    private IOrder order;
	
	public void onStart(IContext context) throws JFException {
		this.console = context.getConsole();
		this.indicators = context.getIndicators();
		this.engine = context.getEngine();		
		this.history = context.getHistory();	
		this.chart = context.getChart(instrument);
		
		//register custom indicator located in ...\JForex\Strategies\files folder
        indicators.registerCustomIndicator(new File(context.getFilesDir() + System.getProperty("file.separator") + "MyIndicator.jfx"));
		//register custom indicator defined inside a strategy
        indicators.registerCustomIndicator(MyIndicator.class);
        
        Object[] firstIndicatorValues =  indicators.calculateIndicator(Instrument.EURUSD, Period.ONE_HOUR, new  OfferSide[] {OfferSide.BID}, "TPIVOT", 
        		new AppliedPrice[]{AppliedPrice.CLOSE}, new Object[]{4}, 0);
        console.getOut().println("first indicator value: " + ((Object[])firstIndicatorValues)[0]);
        
//      //plot only if the chart matches the parameters that are used for indicator calculation
//        if(chart.getSelectedPeriod() != this.period  ){
//            printErr("For proper indicator values please change chart period to "+ this.period);
//        }
//        if(chart.getSelectedOfferSide() != this.side ){
//            printErr("For proper indicator values please change chart side to "+ this.side);
//        int timeIndex = getTimeIndex(inputs[0][index].getTime(), inputs[1]);
        IBar previousBar = null;
        int previousTimeIndex = -1;
        double p = ((previousBar.getHigh() + previousBar.getLow()) / 2);
        double pR1 = p + 0.01;
        double pS1 = p - 0.01;  
//        if (timeIndex > -1 && timeIndex != previousTimeIndex) {
//            previousBar = inputs[1][timeIndex];
//            previousTimeIndex = timeIndex;
//        }
        double aBid = history.getLastTick(instrument).getBid();
        double aAsk = history.getLastTick(instrument).getAsk();
        
//Fetching previous daily bar from history
//IBar prevDailyBar = history.getBar(this.instrument, Period.ONE_HOUR, OfferSide.ASK, 1); 
		       
        //buy
        if(aBid > pR1 ){
            submitOrder(amount, OrderCommand.BUY, stopLossPips);
        }
        //sell 
        if(aAsk < pS1){
            submitOrder(amount, OrderCommand.SELL, stopLossPips);
            }
       }

	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 {
	}
	
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
    private String getLabel(OrderCommand cmd){
    	return cmd.toString() + System.currentTimeMillis();
    }
    
    private IOrder submitOrder(double amount, OrderCommand orderCmd, double takeProfitPips) throws JFException{
    	double stopLossPrice, takeProfitPrice;
    	//Calculating stop loss and take profit prices
    	if (orderCmd == OrderCommand.BUY){
    		stopLossPrice = history.getLastTick(this.instrument).getAsk() - this.stopLossPips * this.instrument.getPipValue(); 
    		takeProfitPrice = history.getLastTick(this.instrument).getAsk() + takeProfitPips * this.instrument.getPipValue(); 
    	}else {
    		stopLossPrice =  history.getLastTick(this.instrument).getBid() + this.stopLossPips * this.instrument.getPipValue();
    		takeProfitPrice = history.getLastTick(this.instrument).getBid() - takeProfitPips * this.instrument.getPipValue();
    	}    	    			
    	//Submitting the order for the specified instrument at the current market price		
    	return engine.submitOrder(getLabel(orderCmd), this.instrument, orderCmd, amount, 0, 20, stopLossPrice, takeProfitPrice);	
    }
    
   
    }
   

    

    
    
