package test;

import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IEngine;
import com.dukascopy.api.IHistory;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IOrder;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.Period;
import java.util.Collections;

public class PartialCloseComment implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;

    @Override
    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.context = context;
        context.setSubscribedInstruments(Collections.singleton(Instrument.EURUSD), true);
        IOrder order = engine.submitOrder("test123", Instrument.EURUSD, IEngine.OrderCommand.BUY, 0.01, 0, 5.0, 0, 0, 0, "testcomment");
        order.waitForUpdate(IOrder.State.FILLED);
        if (order.getComment() != null) {
            console.getOut().println("Order comment is " + order.getComment());
        } else {
            console.getOut().println("Order comment is null");
        }
    }

    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {
    }

    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if (instrument == Instrument.EURUSD && period == Period.ONE_MIN) {
            IOrder order = engine.getOrder("test123");
            if (order != null) {
                order.close(0.001);
                if (order.getComment() != null) {
                    console.getOut().println("Order comment is " + order.getComment());
                } else {
                    console.getOut().println("Order comment is null");
                }
            }
        }
    }

    @Override
    public void onMessage(IMessage message) throws JFException {
    }

    @Override
    public void onAccount(IAccount account) throws JFException {
    }

    @Override
    public void onStop() throws JFException {
    }
}
