package jforex;

import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IMessage;

public class MPivot implements IStrategy {
	// Configurable parameters
	@Configurable("Instrument")
	public Instrument instrument = Instrument.EURUSD;
	@Configurable("Amount")
	public double amount = 0.001;
	@Configurable("Stop loss")
	public double stopLossPips = 40;
	@Configurable("Take profit")
	public double takeProfitPips = 40;
	@Configurable("Price")
	public double price = 0;
	@Configurable("Slippage")
	public double slippage = 5;
	@Configurable("Slippage")
	public double extraPips = 50;
	@Configurable("Slippage")
	public long goodTillTime = 1;

	// private boolean trailSet = false;
	private IEngine engine;
	private IHistory history;
	private IOrder order;

	public double getBids() {
		return 0;
	}

	public double getAsks() {
		return 0;
	}

	double currentB = getBids();
	double currentA = getAsks();

	public void onStart(IContext context) throws JFException {
		this.engine = context.getEngine();
		this.history = context.getHistory();

		// Fetching previous daily bar from history
		IBar prevBar = history.getBar(this.instrument, Period.ONE_HOUR,
				OfferSide.ASK, 1);
		
		// This is the calculation that the strategy uses (can be changed), the
		// 'extraPips' is added or subtracted to the calculation to give the
		// entry point for a market order.
		
		double calculation = ((prevBar.getHigh() + prevBar.getLow()) / 2);
		
		// Identifying the side of the initial order
		if (currentB >= calculation + extraPips) {
			order = engine.submitOrder("MyOrder", instrument,
					IEngine.OrderCommand.BUY, amount, price, slippage,
					stopLossPips, takeProfitPips, goodTillTime, "MyOrder");
		}
		if (currentA <= calculation - extraPips) {
			order = engine.submitOrder("MyOrder", instrument,
					IEngine.OrderCommand.SELL, amount, price, slippage,
					stopLossPips, takeProfitPips, goodTillTime, "MyOrder");
		}
	}

	private String getLabel(OrderCommand cmd) {
		return cmd.toString() + System.currentTimeMillis();
	}

	private IOrder submitOrder(double amount, OrderCommand orderCmd,
			double takeProfitPips) throws JFException {
		double stopLossPrice, takeProfitPrice;
		// Calculating stop loss and take profit prices
		if (orderCmd == OrderCommand.BUY) {
			stopLossPrice = history.getLastTick(this.instrument).getAsk()
					- this.stopLossPips * this.instrument.getPipValue();
			takeProfitPrice = history.getLastTick(this.instrument).getAsk()
					+ takeProfitPips * this.instrument.getPipValue();
			{
				if (orderCmd == OrderCommand.SELL) {
					stopLossPrice = history.getLastTick(this.instrument)
							.getBid() + this.stopLossPips * this.instrument.getPipValue();
					takeProfitPrice = history.getLastTick(this.instrument)
							.getBid() - takeProfitPips * this.instrument.getPipValue();
				}

				// Submitting the order for the specified instrument at the
				// current market price
				return this.engine.submitOrder(getLabel(orderCmd),
						this.instrument, orderCmd, amount, 0, 0, stopLossPrice,
						takeProfitPrice);
			}
		}
	}

	@Override
	public void onMessage(IMessage message) throws JFException {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTick(Instrument instrument, ITick tick) throws JFException {
		// TODO Auto-generated method stub

	}

	@Override
	public void onBar(Instrument instrument, Period period, IBar askBar,
			IBar bidBar) throws JFException {
		// TODO Auto-generated method stub

	}

	@Override
	public void onAccount(IAccount account) throws JFException {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStop() throws JFException {
		// TODO Auto-generated method stub

	}
}
