package singlejartest;

import java.util.*;
import java.util.concurrent.TimeUnit;

import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.feed.IFeedDescriptor;
import com.dukascopy.api.feed.IFeedListener;
import com.dukascopy.api.feed.IRenkoBar;
import com.dukascopy.api.feed.util.RenkoFeedDescriptor;

public class RenkoRemote implements IStrategy, IFeedListener {
    private IEngine engine;
    private IConsole console;
    private IHistory history;

    @Configurable("BrickSize")
    public int BrickSize = 1;
    @Configurable("")
    public Set<Instrument> myInstruments = new HashSet<Instrument>(
            Arrays.asList(new Instrument[] {Instrument.EURUSD, Instrument.GBPUSD, Instrument.USDCHF, Instrument.USDCAD, Instrument.EURJPY, Instrument.GBPJPY})
    );

    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();

        context.setSubscribedInstruments(myInstruments, true);
        int i = 60;
        while (!context.getSubscribedInstruments().containsAll(myInstruments) && i >= 0) {
            try {
                console.getOut().println("Instruments not subscribed yet " + i);
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                console.getOut().println(e.getMessage());
            }
            i--;
        }

        for (Instrument instrument : myInstruments) {
            IFeedDescriptor renkoFeedDescriptor = new RenkoFeedDescriptor(instrument, PriceRange.valueOf(BrickSize), OfferSide.BID);
            context.subscribeToFeed(renkoFeedDescriptor, this);
        }

        console.getOut().println("All instruments subscribed!");
    }
    /**************************************************************************************************************************/

    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 {
    }
    /**************************************************************************************************************************/

    @Override
    public void onFeedData(IFeedDescriptor feedDescriptor, ITimedData feedData) {
        console.getOut().println("newbrick on " + feedDescriptor.getInstrument());
    }
    /**************************************************************************************************************************/
}
