
package renkoOnBarTestSimple;


import com.dukascopy.api.Instrument;
import com.dukascopy.api.LoadingProgressListener;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;
import com.dukascopy.api.system.ISystemListener;
import com.dukascopy.api.system.ITesterClient;
import com.dukascopy.api.system.TesterFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.Future;

/**
 * This small program demonstrates how to initialize Dukascopy tester and start a strategy
 */
public class RenkoTesterMain3 {
    private static final Logger LOGGER = LoggerFactory.getLogger(RenkoTesterMain3.class);

    //url of the DEMO jnlp
    private static String jnlpUrl = "https://www.dukascopy.com/client/demo/jclient/jforex.jnlp";
    //user name
    private static String userName = "DEMO3pjxuy";
    //password
    private static String password = "pjxuy";

    public static void main(String[] args) throws Exception 
    {
        //get the instance of the IClient interface
        final ITesterClient client = TesterFactory.getDefaultInstance();
    
        //set the listener that will receive system events
        client.setSystemListener(new ISystemListener() 
        {
            @Override
            public void onStart(long processId) 
            {
                LOGGER.info("Strategy started: " + processId);
            }

            @Override
            public void onStop(long processId) 
            {
                LOGGER.info("Strategy stopped: " + processId);
                File reportFile = new File("C:\\report.html");
                try 
                {
                    client.createReport(processId, reportFile);
                } 
                catch (Exception e) 
                {
                    LOGGER.error(e.getMessage(), e);
                }
                if (client.getStartedStrategies().size() == 0) 
                {
                    System.exit(0);
                }
            }

            @Override
            public void onConnect() {
                LOGGER.info("Connected");
            }

            @Override
            public void onDisconnect() {
                //tester doesn't disconnect
            }
        });

        LOGGER.info("Connecting...");
        //connect to the server using jnlp, user name and password
        //connection is needed for data downloading
        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 Dukascopy servers");
            System.exit(1);
        }

        //set instruments that will be used in testing
        Set<Instrument> instruments = new HashSet<Instrument>();
        instruments.add(Instrument.EURUSD);
        //instruments.add(Instrument.GBPUSD);
        LOGGER.info("Subscribing instruments..." + instruments.toString());
        client.setSubscribedInstruments(instruments);
        
        //setting initial deposit
        LOGGER.info("Setting initial deposit...");
        client.setInitialDeposit(Instrument.EURUSD.getSecondaryCurrency(), 1100);
        //client.setInitialDeposit(Instrument.AUDUSD.getPrimaryCurrency(), 1100);
        
        //set Dukascopy charges
        //client.setLeverage(100);
        //client.setMarginCutLevel(200);

        //set data interval
        LOGGER.info("Setting backtest interval");
        DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dfm.setTimeZone(TimeZone.getTimeZone("GMT"));
        Date startDate = dfm.parse("2011-05-01 00:00:00");
        Date endDate = dfm.parse("2011-06-01 00:00:00");
        client.setDataInterval(Period.TICK, OfferSide.BID, ITesterClient.InterpolationMethod.CLOSE_TICK, 
        					   startDate.getTime(), endDate.getTime());

        //load data
        LOGGER.info("Downloading data");
        Future<?> future = client.downloadData(null);
        //wait for downloading to complete
        future.get();
        
        //start the strategy
        LOGGER.info("Starting strategy");
        client.startStrategy(new renkoTestStrategy3(), 
        new LoadingProgressListener() 
        {
            @Override
            public void dataLoaded(long startTime, long endTime, long currentTime, String information) 
            {
                LOGGER.info(information);
            }

            @Override
            public void loadingFinished(boolean allDataLoaded, long startTime, long endTime, long currentTime) 
            {
            }

            @Override
            public boolean stopJob() 
            {
                return false;
            }
        });
        
        //now it's running
    }
}
