package singlejartest;

import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.IIndicators.MaType;

public class BollMaster implements IStrategy {
	private IEngine engine;
	private IHistory history;
	private IIndicators indicators;
	private int counter = 0;
	private int cpt = 0;
	private double sma = 0;

	
	public Instrument instrument = Instrument.EURUSD;
	public Period selectedPeriod = Period.FOUR_HOURS;
	public double amount = 0;
	public int stopLossPips = 100;
	public int takeProfitPips = 250;
	public boolean prebuysig = false;
	public boolean presellsig = false;
	public boolean buyopen = false;
	public boolean sellopen = false;
	

	public void onStart(IContext context) throws JFException {
		this.engine = context.getEngine();
		this.history = context.getHistory();
		this.indicators = context.getIndicators();
	}

	public void onAccount(IAccount account) throws JFException {
		double compte = account.getCreditLine();
		amount = compte/1000000;
	}

	public void onMessage(IMessage message) throws JFException {
	}

	public void onStop() throws JFException {
		//close all orders
		for (IOrder order : engine.getOrders()) {
			engine.getOrder(order.getLabel()).close();
		}
	}

	public void onTick(Instrument instrument, ITick tick) throws JFException {
		double prix = history.getLastTick(instrument).getBid();
		if(sellopen && prix < sma){
			for (IOrder order : engine.getOrders()) {
				engine.getOrder(order.getLabel()).close();
			}
			sellopen = false;
		}
		if(buyopen && prix > sma){
			for (IOrder order : engine.getOrders()) {
				engine.getOrder(order.getLabel()).close();
			}
			buyopen = false;
		}
	}

	public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {

		if (!period.equals(Period.FOUR_HOURS))
			return;

		IBar prevBar = history.getBar(instrument, selectedPeriod, OfferSide.BID, 1);

		int timePeriod = 60;
		MaType maType = MaType.SMA;
		AppliedPrice appliedPrice = AppliedPrice.CLOSE;
		double nbDevDn = 1.65;
		double nbDevUp = 1.65;
		int shift = 0;
		
		double[] bbands = indicators.bbands(instrument, period, OfferSide.BID, appliedPrice, timePeriod, nbDevUp, nbDevDn, maType, shift);
		
		double uppersig = bbands[1] + (bbands[0] - bbands[1])/4;
		double lowersig = bbands[2] + (bbands[1] - bbands[2])/4;
		sma = bbands[1];
		
		if(prevBar.getClose() < bbands[2] && cpt == 0){
			cpt++;
			prebuysig = true;
		}
		if(prevBar.getClose() > bbands[0] && cpt == 0){
			cpt++;
			presellsig = true;
		}
		if(prebuysig && cpt == 1 && prevBar.getClose() > lowersig){
			cpt = 0;
			submitOrder(OrderCommand.BUY);
		}
		if(presellsig && cpt == 1 && prevBar.getClose() < uppersig){
			cpt = 0;
			submitOrder(OrderCommand.SELL);
		}

	}

	private IOrder submitOrder(OrderCommand orderCmd) throws JFException {

		double stopLossPrice, takeProfitPrice;

		// Calculating stop loss and take profit prices
		if (orderCmd == OrderCommand.BUY) {
			
			buyopen = true;
			stopLossPrice = history.getLastTick(this.instrument).getBid() - getPipPrice(this.stopLossPips);
			takeProfitPrice = history.getLastTick(this.instrument).getBid() + getPipPrice(this.takeProfitPips);
		} else {
			sellopen = true;
			stopLossPrice = history.getLastTick(this.instrument).getAsk() + getPipPrice(this.stopLossPips);
			takeProfitPrice = history.getLastTick(this.instrument).getAsk() - getPipPrice(this.takeProfitPips);
		}

		// Submitting an order for the specified instrument at the current market price
		return engine.submitOrder(getLabel(instrument), this.instrument, orderCmd, this.amount, 0, 20, stopLossPrice, takeProfitPrice);
	}

	protected String getLabel(Instrument instrument) {
		String label = instrument.name();
		label = label + (counter++);
		label = label.toUpperCase();
		return label;
	}

	private double getPipPrice(int pips) {
		return pips * this.instrument.getPipValue();
	}

}
