package jforex.strategies;

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.util.TimePeriodAggregationFeedDescriptor;

public class ExampleStrategy implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IContext context;
    private Instrument instrument = Instrument.EURUSD;
    private FeedListener feedListener;
    
    //private DateFormat dfm = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
    
    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.context = context;
        this.feedListener = new FeedListener(this.context);
        //IFeedDescriptor fd = new TicksFeedDescriptor(instrument);
        IFeedDescriptor fd = new TimePeriodAggregationFeedDescriptor(instrument, Period.ONE_HOUR, OfferSide.BID, Filter.WEEKENDS);
        this.context.subscribeToFeed(fd, feedListener);
    }

    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 {
        if (instrument.equals(this.instrument) && period.equals(Period.ONE_HOUR) && this.engine.getOrders(instrument).size()==0) {
            this.console.getOut().println("Period: "+period+", Instrument: "+instrument+" Orders: "+this.engine.getOrders(instrument).size());
            this.engine.submitOrder("test",instrument, OrderCommand.BUY, 0.1);
            this.context.unsubscribeFromFeed(feedListener);
        }
    }
    
    
    private class FeedListener implements IFeedListener {
    	private IContext context;
    	
    	public FeedListener(IContext c) {
    		this.context = c;
    	}

		@Override
		public void onFeedData(IFeedDescriptor arg0, ITimedData arg1) {
			context.getConsole().getOut().println("Feed: "+arg0);
		}
        
    }
}