package Indicators;

import com.dukascopy.api.*;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.system.ITesterClient;
import com.dukascopy.api.system.TesterFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Currency;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Starter {
    private static final Logger LOGGER = LoggerFactory.getLogger(Starter.class);
    private static String jnlpUrl = "https://www.dukascopy.com/client/live/jclient/jforex.jnlp";
    private static String userName = "username";
    private static String password = "password";
    private static ITesterClient client;
    private static Future<?> future;
    private static Set<Instrument> instruments;
    Instrument currencyPair = Instrument.EURUSD;            
    private Period myBarPeriod = Period.ONE_MIN;
        
    private void printIndicatorValues() { 
		// Here I would like to access the MACD (or some other indicator) values 
		// unfortunately this doesn't work :(
		long from = ...;
		long to = ...;
		double[][] macdValue = IIndicators.macd(currencyPair, myBarPeriod, OfferSide.BID, AppliedPrice.CLOSE,12,26,9, long from, long to);                
    }

    private static void getData() throws InterruptedException, ExecutionException, ParseException {
    	
    	String dateFromStr = "15.01.2013 9:14:00";
		String dateToStr = "15.01.2013 18:00:00";

		final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
		dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

		Date dateFrom = dateFormat.parse(dateFromStr);
		Date dateTo = dateFormat.parse(dateToStr);

		client.setDataInterval(Period.TEN_MINS, OfferSide.BID, ITesterClient.InterpolationMethod.CLOSE_TICK, dateFrom.getTime(), dateTo.getTime());
                client.setDataInterval(ITesterClient.DataLoadingMethod.DIFFERENT_PRICE_TICKS, dateFrom.getTime(), dateTo.getTime());
		LOGGER.info("Downloading data");
		future = client.downloadData(null);
		future.get();
    }
    
	public static void main(String[] args) throws Exception {
		client = TesterFactory.getDefaultInstance();                
		LOGGER.info("Connecting...");
		client.connect(jnlpUrl, userName, password);
		// wait for it to connect
		int i = 10; // wait max ten seconds
		while (i > 0 && !client.isConnected()) {
			Thread.sleep(1000);
			i--;
		}
		if (!client.isConnected()) {
			LOGGER.error("Failed to connect to Dukascopy servers");
			System.exit(1);
		}

		LOGGER.info("Subscribing instruments...");
                Set<Instrument> instruments = new HashSet<Instrument>();
                instruments.add(Instrument.EURUSD);
                client.setSubscribedInstruments(instruments);
                // wait max 1 second for the instruments to get subscribed
                i = 10;
                while (!client.getSubscribedInstruments().containsAll(instruments) && i>=0) {
                    try {
                        LOGGER.info("Instruments not subscribed yet " + i);
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        LOGGER.info(e.getMessage());
                    }
                    i--;
                }

        getData();                
		printIndicatorValues();                

	} // main() end    
}
