Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

Close the current position by market price
 Post subject: Close the current position by market price Post rating: 0   New post Posted: Fri 21 May, 2010, 17:36 

User rating: 0
Joined: Fri 21 May, 2010, 17:33
Posts: 1
hi, support:

I am a new guy on java,
I just copy the singal jar example:

import com.dukascopy.api.*;
public class MA_Play implements IStrategy {
    private IEngine engine = null;
    private IIndicators indicators = null;
    private int tagCounter = 0;
    private double[] ma1 = new double[Instrument.values().length];
    private IConsole console;
    public void onStart(IContext context) throws JFException {
        engine = context.getEngine();
        indicators = context.getIndicators();
        this.console = context.getConsole();
        console.getOut().println("Started");
    }
    public void onStop() throws JFException {
        for (IOrder order : engine.getOrders()) {
            order.close();
        }
        console.getOut().println("Stopped");
    }
    public void onTick(Instrument instrument, ITick tick) throws JFException {
        if (ma1[instrument.ordinal()] == -1) {
            ma1[instrument.ordinal()] = indicators.ema(instrument, Period.TEN_SECS, OfferSide.BID, IIndicators.AppliedPrice.MEDIAN_PRICE, 14, 1);
        }
        double ma0 = indicators.ema(instrument, Period.TEN_SECS, OfferSide.BID, IIndicators.AppliedPrice.MEDIAN_PRICE, 14, 0);
        if (ma0 == 0 || ma1[instrument.ordinal()] == 0) {
            ma1[instrument.ordinal()] = ma0;
            return;
        }
        double diff = (ma1[instrument.ordinal()] - ma0) / (instrument.getPipValue());
        if (positionsTotal(instrument) == 0) {
            if (diff > 1) {
                engine.submitOrder(getLabel(instrument), instrument, IEngine.OrderCommand.SELL, 0.1, 0, 0, tick.getAsk()
                        + instrument.getPipValue() * 10, tick.getAsk() - instrument.getPipValue() * 15);
            }
            if (diff < -1) {
                engine.submitOrder(getLabel(instrument), instrument, IEngine.OrderCommand.BUY, 0.1, 0, 0, tick.getBid()
                        - instrument.getPipValue() * 10, tick.getBid() + instrument.getPipValue() * 15);
            }
        }
        ma1[instrument.ordinal()] = ma0;
    }
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) {
    }
    //count open positions
    protected int positionsTotal(Instrument instrument) throws JFException {
        int counter = 0;
        for (IOrder order : engine.getOrders(instrument)) {
            if (order.getState() == IOrder.State.FILLED) {
                counter++;
            }
        }
        return counter;
    }
    protected String getLabel(Instrument instrument) {
        String label = instrument.name();
        label = label.substring(0, 2) + label.substring(3, 5);
        label = label + (tagCounter++);
        label = label.toLowerCase();
        return label;
    }
    public void onMessage(IMessage message) throws JFException {
    }
    public void onAccount(IAccount account) throws JFException {
    }
}


what I want close the current position by market price, not by stop loss or take profit. how can I change :

if (diff < -1) {
                engine.CLOSEOrder()


?

Thank you.


 
 Post subject: Re: Close the current position by market price Post rating: 0   New post Posted: Mon 24 May, 2010, 08:06 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
To close an order, use the IOrder interface function close(). It means that, you need an orders instance to close it. The order instance is returned, when a function submitOrder is called from an IEngine. Or you could try to get it from IEngine using one of the functions getOrder(label) or getOrderById(orderId). For example:
       if (engine.getOrder("MyOrder") != null
             && !order.getState().equals ( IOrder.State.CLOSED )
                  && !order.getState().equals ( IOrder.State.CANCELED ) ) {
          engine.getOrder("MyOrder").close();   
       }


 

Jump to:  

cron
  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com