package jforex.orders;

import com.dukascopy.api.*;

/**
 * The strategy creates a market order on its start with TP at 5 pips.
 * Every 10 seconds it increases the TP by 1 pip
 *
 */
public class TpModify implements IStrategy {

    private IOrder order;
    private Instrument instrument = Instrument.EURUSD;
    @Override
    public void onStart(IContext context) throws JFException {
        double lastBid = context.getHistory().getLastTick(instrument).getBid();
        order = context.getEngine().submitOrder(
                "order", instrument, IEngine.OrderCommand.BUY, 0.1, 0, 10, 0, lastBid + instrument.getPipValue() * 5);
    }
    
    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if(this.instrument == instrument && period == Period.TEN_SECS && order.getState() == IOrder.State.FILLED){
            order.setTakeProfitPrice(order.getTakeProfitPrice() + instrument.getPipValue());
        }
    }

    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {}
    @Override
    public void onMessage(IMessage message) throws JFException {}
    @Override
    public void onAccount(IAccount account) throws JFException {}
    @Override
    public void onStop() throws JFException {}
}
