package jforex;

import com.dukascopy.api.*;

public class Strategy implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;

    private String LABEL = "mylabel";
    private Instrument INSTRUMENT = Instrument.EURUSD;
    String id = null;
    
    IOrder order = null;
    IOrder longOrder = null;
    IOrder shortOrder = null;

    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.context = context;
        this.indicators = context.getIndicators();
        this.userInterface = context.getUserInterface();
        
        ITick tick = history.getLastTick(INSTRUMENT);
        double price = tick.getAsk() + 1 * INSTRUMENT.getPipValue();
        double tp = price + 1 * INSTRUMENT.getPipValue();
        double sl = price - 1 * INSTRUMENT.getPipValue();
        
        longOrder = Order(0.05, price, 0, sl, tp, "", "1");
              
        double price2 = tick.getBid() - 1 * INSTRUMENT.getPipValue();
        double tp2 = price2 - 1 * INSTRUMENT.getPipValue();
        double sl2 = price2 + 1 * INSTRUMENT.getPipValue();
        
        shortOrder = Order(-0.05, price2, 0, sl2, tp2, "", "2");
    }

    private IOrder Order(double lot, double price, double slippage, double stoploss, double takeprofit, String comment, String number) throws JFException {
        String id = null;

        ITick tick = history.getLastTick(INSTRUMENT);
        IEngine.OrderCommand command = null;

        if (lot > 0) {
            if (tick.getAsk() >= price)
                command = IEngine.OrderCommand.BUYLIMIT;
            else
                command = IEngine.OrderCommand.BUYSTOP;
        }

        if (lot < 0) {
            if (tick.getBid() <= price)
                command = IEngine.OrderCommand.SELLLIMIT;
            else
                command = IEngine.OrderCommand.SELLSTOP;
        }

        IOrder order = engine.submitOrder(LABEL + "_" + number, INSTRUMENT, command, Math.abs(lot), price, slippage, stoploss, takeprofit, 0, comment);        
        return order;
    }

    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 {
        if(!instrument.equals(INSTRUMENT)) {
            return;
        }
        
        console.getOut().println("order "+order);
        if(order == null) {
            if(shortOrder.getState() == IOrder.State.FILLED) {
                console.getOut().println("shortOrder");
                order = shortOrder;
                
                console.getOut().println("longOrder.close();");
                longOrder.close();
            }
            if(longOrder.getState() == IOrder.State.FILLED) {
                console.getOut().println("longOrder");
                order = longOrder;
                
                console.getOut().println("shortOrder.close();");
                shortOrder.close();            
            }            
        } else {
            if(order.getState() == IOrder.State.CLOSED) {
                                
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                }
                
                IOrder order2 = history.getHistoricalOrderById(order.getId());        
                console.getOut().println("history order .getOriginalAmount() "+ order2.getOriginalAmount());
                console.getOut().println("order .getOriginalAmount() "+ order.getOriginalAmount());
                
                context.stop();
            }                  
        }
        
    }

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
}