package jforex.requests;

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

@RequiresFullAccess
public class ExampleStrat implements IStrategy {
    
    @Configurable("instrument")
    public Instrument instrument = Instrument.EURUSD;
    
    private IHistory history;
    private IEngine engine;
    int counter;

    @Override
    public void onStart(IContext context) throws JFException {
        history = context.getHistory();
        engine = context.getEngine();
        
    }

    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {
        if (instrument != this.instrument) {
            return;
        }
        IBar prev = history.getBar(Instrument.EURUSD, Period.ONE_MIN, OfferSide.BID, 1);

        double price = tick.getBid();
        double high = prev.getHigh();
        double low = prev.getLow();

        if (engine.getOrders(instrument).size() == 0) {
            if (price < low) {
                engine.submitOrder("order" + ++counter, instrument, OrderCommand.SELL, 0.001);
            } else if (price > high) {
                engine.submitOrder("order" + ++counter, instrument, OrderCommand.BUY, 0.001);
            }
        }

    }

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

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

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

    @Override
    public void onStop() throws JFException {    }

}
