package jforex.strategies.indicators;

import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;

/**
 * The strategy trades according to BWMFI indicator:
 *  - after a green histogram it makes a BUY order,
 *  - after a brown histogram - a SELL order.
 *
 */
public class BWMFIgreenBrown implements IStrategy {

    private IIndicators indicators;
    private IConsole console;
    private IEngine engine;
    
    @Configurable("Instrument")
    public Instrument instrument = Instrument.EURUSD;
    @Configurable("Period")
    public Period period = Period.ONE_MIN;
    @Configurable("Offer side")
    public OfferSide side = OfferSide.BID;
    
    private final int GREEN = 0;
    private final int BROWN = 1;
    private final int BLUE = 2;
    private final int PINK = 3;
    
    private int counter;
    
    @Override
    public void onStart(IContext context) throws JFException {
        console = context.getConsole();
        indicators = context.getIndicators();
        engine = context.getEngine();
    }

    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {}

    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if(instrument != this.instrument || period != this.period){
            return;
        }        
        double[] bwmfi = indicators.bwmfi(instrument, period, side, 1);        
        if(Double.compare(bwmfi[GREEN], 0) != 0){
            engine.submitOrder("greenOrder"+counter++, instrument, OrderCommand.BUY, 0.001);
        } else if(Double.compare(bwmfi[BROWN], 0) != 0){
            engine.submitOrder("brownOrder"+counter++, instrument, OrderCommand.SELL, 0.001);
        }
    }

    @Override
    public void onMessage(IMessage message) throws JFException {}

    @Override
    public void onAccount(IAccount account) throws JFException {}

    @Override
    public void onStop() throws JFException {}

}
