package jforex;

import java.util.*;

import com.dukascopy.api.*;

public class CheckOpenPrice implements IStrategy
{
    private IEngine engine;
    private IConsole console;
    private IContext context;
    private ArrayList <OrderOpenPrice> openPrices = new ArrayList<OrderOpenPrice>();

    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
    {
        switch(message.getType())
        {
            case ORDER_FILL_OK:        // Sent after successful order filling
            {
                IOrder order = message.getOrder();

                if(order.getState().equals(IOrder.State.FILLED))
                {
                    //console.getOut().println("  -> ORDER_FILL_OK  @  " + order.getId() + "   OpenPrice: " + order.getOpenPrice());

                    openPrices.add(new OrderOpenPrice(order.getId(), order.getOpenPrice()));
                }

                break;
            }

            case ORDER_CLOSE_OK:        // Sent after successful order closing
            {
                IOrder order = message.getOrder();

                for(int i = 0; i < openPrices.size(); i++)
                    if(openPrices.get(i).getId().equals(order.getId()))
                    {
                        if(openPrices.get(i).getOpenPrice() != order.getOpenPrice())
                            console.getErr().println("  -> Different OpenPrice @ Order: " + order.getId() + "   OpenPrice@ORDER_FILL_OK: " + openPrices.get(i).getOpenPrice() + "  OpenPrice@ORDER_CLOSE_OK: " + order.getOpenPrice());

                        openPrices.remove(i);
                        break;
                    }

                break;
            }

            default: return;
        }
    }

    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);
        }
    }

    class OrderOpenPrice
    {
        private String id;
        private double openPrice;

        OrderOpenPrice(final String id, final double openPrice)
        {
            this.id = id;
            this.openPrice = openPrice;
        }

        public String getId()            { return id; }

        public double getOpenPrice()     { return openPrice; }
    }
}