package jforex.requests;

import java.text.SimpleDateFormat;
import java.util.*;

import com.dukascopy.api.*;

public class CheckOpenPrice2 implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IContext context;
    private Map<IOrder, Double> openPrices = new HashMap<IOrder, Double>();
    
    @SuppressWarnings("serial")
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") {
        {
            setTimeZone(TimeZone.getTimeZone("GMT"));
        }
    };

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

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
        IOrder o = message.getOrder();
        if (o == null) {
            return;
        }

        if (message.getType() == IMessage.Type.ORDER_FILL_OK) {
            openPrices.put(o, o.getOpenPrice());
        } else if (message.getType() == IMessage.Type.ORDER_CLOSE_OK) {
            double origOpenPrice = openPrices.get(o);
            if (Double.compare(origOpenPrice, o.getOpenPrice()) != 0) {
                console.getErr().println(
                        String.format("%s original open price=%.5f, open price on close=%.5f. Fill time=%s close time=%s", 
                                o.getLabel(), origOpenPrice, o.getOpenPrice(), sdf.format(o.getFillTime()), sdf.format(o.getCloseTime())
                                ));
            } else {
                console.getOut().println(
                        String.format("%s open price=%.5f, on close matches the original one. Fill time=%s close time=%s", 
                                o.getLabel(), o.getOpenPrice(), sdf.format(o.getFillTime()), sdf.format(o.getCloseTime())
                                ));
            }
        }

    }

    public void onStop() throws JFException {
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
        List<IOrder> orders = engine.getOrders(Instrument.GBPJPY);

        // set SL to BE if getProfitLossInPips() >= 25
        for (int i = 0; i < orders.size(); i++) {
            if (orders.get(i).getProfitLossInPips() >= 25 && orders.get(i).getStopLossPrice() != orders.get(i).getOpenPrice())
                orders.get(i).setStopLossPrice(orders.get(i).getOpenPrice());
        }
    }

    // Open a new Order for each H1-Bar (SHORT on BEAR-BAR / LONG on BULL-BAR)
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if (period.equals(Period.ONE_HOUR) && instrument.equals(Instrument.GBPJPY)) {
            String label = "order" + bidBar.getTime();
            double sl = bidBar.getClose() + 0.70;
            IEngine.OrderCommand cmd = IEngine.OrderCommand.SELL;

            if (bidBar.getClose() > bidBar.getOpen()) {
                cmd = IEngine.OrderCommand.BUY;
                sl = askBar.getClose() - 0.70;
            }

            engine.submitOrder(label, instrument, cmd, 0.001, 0, 0, sl, 0);
        }
    }

}