/*     
 * This is a test programm only, do not use for trading! 
 * It checks submitOrder
 * method of IEngine
 * At current JForex version it produces a strategy crash
 *
 * (c) Stash GmbH Muenchen / Germany, December 5th 2017
 *     www.stash.de
 *     Author: Bernhard Schicht
 *     Email: exp@stash.de
 */



package jforex;

import java.util.*;
import com.dukascopy.api.IEngine.*;
import com.dukascopy.api.*;

public class PlaceBidOfferBug implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
    
    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.context = context;
        this.indicators = context.getIndicators();
        this.userInterface = context.getUserInterface();
    }

    public void onAccount(IAccount account) throws JFException {
        Set<Instrument> subscribe = new HashSet<Instrument>();    
        subscribe.add(Instrument.USDTRY);
        context.setSubscribedInstruments(subscribe, true);
    
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
    }
    private boolean runOnce = false;
    private ITick tick = null;
    public void onTick(Instrument instrument, ITick tick) throws JFException {
        this.tick = tick;
    }
    
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if (instrument != Instrument.USDTRY) return;
        if (period != Period.ONE_MIN) return;
        if (tick == null) return;
        if (runOnce) return;
                    
        runOnce = true;
        // place bid order 1 pip above current tick when bar starts
        IOrder longOrder = engine.submitOrder(
                "Test_"+System.currentTimeMillis(),instrument,
                OrderCommand.PLACE_BID, 
                0.01, tick.getBid()+1D*instrument.getPipValue(), 
                20, askBar.getLow()-200D*instrument.getPipValue(), 
                askBar.getHigh()+5D*instrument.getPipValue(), 0L); 
    }
}