package jforex;

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

public class TestStopLoss implements IStrategy{
    
    private IContext context; 
    private IOrder order;
    
    @Configurable("Stop Loss price")
    public double slPrice;
    
    double entrada;
    boolean compra;
    public void onAccount(IAccount account) throws JFException {
    }

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

    public void onMessage(IMessage message) throws JFException {  
        if (message.getType() == IMessage.Type.ORDER_FILL_OK) {
            
            entrada = message.getOrder().getOpenPrice();
            compra = message.getOrder().isLong();
            
            if (compra == true) {
                message.getOrder().setStopLossPrice(entrada-(slPrice)/10000);                
            }else {
                message.getOrder().setStopLossPrice(entrada+(slPrice)/10000);                
             }        
          
        }
    }

    public void onStart(IContext context) throws JFException {
        this.context = context;
        //submit order when strategy starts
        order = context.getEngine().submitOrder("MyOrder", Instrument.EURUSD, OrderCommand.BUY, 0.1);
    }

    public void onStop() throws JFException {
        //close order 
        if (order.getState() == IOrder.State.FILLED
             || order.getState() == IOrder.State.OPENED) {
                order.close();    
           }
    }

    public void onTick(Instrument arg0, ITick arg1) throws JFException {        
    }
}