// test finised negative

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import com.dukascopy.api.Configurable;
import com.dukascopy.api.Filter;
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.IEngine.OrderCommand;
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;

public class Global_Perpetual implements IStrategy {

    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private int counter = 0;


    @Configurable("Instrument")
    public Instrument instrument = Instrument.EURUSD;

    @Configurable("Slippage")
    public double slippage = 2;

    @Configurable("Amount")
    public double amount = 0.1;

    public Filter filter = Filter.ALL_FLATS;

    @Configurable("Total take profit Money")
    public double tpProfit = 1000;

    @Configurable("Total take loss Money")
    public double ngProfit = 2000;

    @Configurable("Take profit pips")
    public int takeProfitPips = 20;
    @Configurable("Stop loss in pips")
    public int stopLossPips = 0;

    @Configurable("Print all Messages")
    public boolean printmessages = false;

    private double balance;

    @Override
    public void onStart(IContext context) throws JFException {
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.engine = context.getEngine();
        ITick lastTick = history.getLastTick(instrument);

        balance = context.getAccount().getEquity();
        console.getOut().println("initial balance " + (new DecimalFormat("#.#######")).format(balance) + " Date  "
                + getCurrentTime(lastTick.getTime()));

    }

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

    public void onTick(Instrument instrument, ITick tick) throws JFException {
        if (instrument != this.instrument) {
            return;
        }

        if (!engine.getOrders(instrument).isEmpty()) {
            return;
        }
        else if (engine.getOrders(instrument).isEmpty()) {
            submitOrder(OrderCommand.BUY, instrument);
            submitOrder(OrderCommand.SELL, instrument);
        }

    }

    private String getLabel(Instrument instrument) {
        String label = instrument.name();
        label = label + (counter++) + System.currentTimeMillis();
        label = label.toUpperCase();
        return label;
    }

    public void onMessage(IMessage message) throws JFException {
        if (printmessages) {
            print(message);
        }
    }

    public void onStop() throws JFException {
    }

    private void print(Object o) {
        console.getOut().println(o);
    }

    public static String toStr(double[] arr) {
        String str = "";
        for (int r = 0; r < arr.length; r++) {
            str += "[" + r + "] " + (new DecimalFormat("#.#######")).format(arr[r]) + "; ";
        }
        return str;
    }

    public static String toStr(double[][] arr) {
        String str = "";
        if (arr == null) {
            return "null";
        }
        for (int r = 0; r < arr.length; r++) {
            for (int c = 0; c < arr[r].length; c++) {
                str += "[" + r + "][" + c + "] " + (new DecimalFormat("#.#######")).format(arr[r][c]);
            }
            str += "; ";
        }
        return str;
    }

    public String toStr(double d) {
        return (new DecimalFormat("#.#######")).format(d);
    }

    public String toStr(Long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") {

            private static final long serialVersionUID = 1L;

            {
                setTimeZone(TimeZone.getTimeZone("GMT"));
            }
        };
        return sdf.format(time);
    }

    public void onAccount(IAccount account) throws JFException {
        ITick lastTick = history.getLastTick(instrument);

        double pozitivebalance = balance + tpProfit;
        double negativebalance = balance - ngProfit;

        if (tpProfit > 0) {
            if (pozitivebalance < history.getEquity()) {
                for (IOrder order : engine.getOrders()) {
                    if (order.getState() != IOrder.State.CLOSED && order.getState() != IOrder.State.CANCELED
                            && order.getProfitLossInAccountCurrency() > 0 // close positives
                    ) {
                        order.close();
                    }
                }
                balance = account.getEquity();
                console.getOut().println("UP balance " + (new DecimalFormat("#.#######")).format(balance) + " Date  "
                        + getCurrentTime(lastTick.getTime()));
            }
        }
        if (ngProfit > 0) {
            if (negativebalance > history.getEquity()) {
                for (IOrder order : engine.getOrders()) {
                    if (order.getState() != IOrder.State.CLOSED && order.getState() != IOrder.State.CANCELED
                            && order.getProfitLossInAccountCurrency() < 0 // close negatives
                    ) {
                        order.close();
                    }
                }

                balance = account.getEquity();
                console.getOut().println("DOWN balance " + (new DecimalFormat("#.#######")).format(balance) + " Date  "
                        + getCurrentTime(lastTick.getTime()));
            }
        }
    }

    private String getCurrentTime(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat();
        return sdf.format(time);
    }

    private IOrder submitOrder(OrderCommand orderCmd, Instrument instr) throws JFException {

        double stopLossPrice = 0.0, takeProfitPrice = 0.0;

        // Calculating order price, stop loss and take profit prices
        if (orderCmd == OrderCommand.BUY) {
            if (stopLossPips > 0) {
                stopLossPrice = history.getLastTick(instr).getBid() - getPipPrice(stopLossPips);
            }
            if (takeProfitPips > 0) {
                takeProfitPrice = history.getLastTick(instr).getBid() + getPipPrice(takeProfitPips);
            }
        } else {
            if (stopLossPips > 0) {
                stopLossPrice = history.getLastTick(instr).getBid() + getPipPrice(stopLossPips);
            }
            if (takeProfitPips > 0) {
                takeProfitPrice = history.getLastTick(instr).getBid() - getPipPrice(takeProfitPips);
            }
        }

        return engine.submitOrder(getLabel(instr), instr, orderCmd, amount, 0, slippage, stopLossPrice,
                takeProfitPrice);
    }

    private double getPipPrice(double pips) {
        return pips * this.instrument.getPipValue();
    }
}
