package singlejartest;

import com.dukascopy.api.*;

public class MRtest implements IStrategy {
    private IEngine engine = null;
    private int tagCounter = 0;
    private IConsole console;

    public void onStart(IContext context) throws JFException {
        engine = context.getEngine();
        this.console = context.getConsole();
        console.getOut().println("Started");
    }

    public void onStop() throws JFException {
        for (IOrder order : engine.getOrders()) {
            order.close();
        }
        console.getOut().println("Stopped");
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
        

        if (positionsTotal(instrument) == 0) {
                
                console.getOut().println("Try Open an Order!");
                engine.submitOrder(getLabel(instrument), instrument, IEngine.OrderCommand.BUY, 0.006);
                console.getOut().println("Tried Open an Order!");            
        }

    }

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) {
    }

    //count open positions
    protected int positionsTotal(Instrument instrument) throws JFException {
        int counter = 0;
        for (IOrder order : engine.getOrders(instrument)) {
            if (order.getState() == IOrder.State.FILLED) {
                counter++;
            }
        }
        return counter;
    }

    protected String getLabel(Instrument instrument) {
        String label = instrument.name();
        label = label.substring(0, 2) + label.substring(3, 5);
        label = label + (tagCounter++);
        label = label.toLowerCase();
        return label;
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onAccount(IAccount account) throws JFException {
    }
}
