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.

SL and TP for orders of last week
 Post subject: SL and TP for orders of last week Post rating: 1   Post Posted: Thu 21 Mar, 2013, 23:48 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
Consider the following little strategy, which prints out orders from the last week:
package jforex.orders;

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

public class OrderHistoryLastWeek implements IStrategy {
    private IConsole console;
   
    public void onStart(IContext context) throws JFException {
        console = context.getConsole();

        long time = System.currentTimeMillis();
        List<IOrder> orders = context.getHistory().getOrdersHistory(Instrument.EURUSD, time - Period.WEEKLY.getInterval(), time);
        if (orders != null) {
            for (IOrder order : orders) {
                console.getOut().println("history order: " + order + ", SL: " + order.getStopLossPrice() + ", TP: " + order.getTakeProfitPrice() );           
                }
        }
       
        context.stop();
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {}
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}
    public void onMessage(IMessage message) throws JFException {}
    public void onAccount(IAccount account) throws JFException {}
    public void onStop() throws JFException {}
}


The output of this, is something like:
Quote:
22:44:25 history order: [jfw8oss18]-Instrument: EUR/USD, order command: BUY, amount: 0.031, fill time: 2013-03-20 14:27:32:052, fill price 1.29566, close time: 2013-03-20 18:02:06:238, close price 1.29776, SL: 0.0, TP: 1.29764


If the order was closed due to TP, the TP price is proper, but the SP value is 0.0.
If the order was closed due to SL, the TP price is 0.0, and the SP price is proper.

How can I get in both cases the correct TP/SL prices? Every order was filled with SP and TP.


 
 Post subject: Re: SL and TP for orders of last week Post rating: 0   Post Posted: Fri 22 Mar, 2013, 12:17 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
We could not replicate this:
package jforex.orders.history;

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

public class OrderHistoryTPSL implements IStrategy {
    private IConsole console;
    private IEngine engine;
    private IHistory history;

    private Instrument instrument = Instrument.EURUSD;

    public void onStart(IContext context) throws JFException {
        console = context.getConsole();
        history = context.getHistory();
        engine = context.getEngine();

        long startTime = System.currentTimeMillis() - Period.TEN_SECS.getInterval();
        double lastAsk = history.getLastTick(instrument).getAsk();
        IOrder order = engine.submitOrder("orderByTP", instrument, OrderCommand.BUY, 0.1, 0, 0, lastAsk - instrument.getPipValue() * 2, lastAsk
                - instrument.getPipValue());
        order.waitForUpdate(5000, IOrder.State.CLOSED);
        order = engine.submitOrder("orderBySL", instrument, OrderCommand.BUY, 0.1, 0, 0, lastAsk + instrument.getPipValue() * 2, lastAsk
                + instrument.getPipValue());
        order.waitForUpdate(5000, IOrder.State.CLOSED);

        long endTime = System.currentTimeMillis();
        List<IOrder> orders = context.getHistory().getOrdersHistory(instrument, startTime, endTime);
        console.getOut().println(orders);
            for (IOrder o : orders) {
                printOrder(o);
            }

        context.stop();
    }
   
    private void printOrder(IOrder order){
        console.getOut().println(String.format("%s SL: %.5f TP: %.5f", order, order.getStopLossPrice(), order.getTakeProfitPrice()));
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
    }

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

    public void onMessage(IMessage message) throws JFException {
        if(message.getOrder()!= null){
            console.getInfo().println(message);
            if(message.getReasons().contains(IMessage.Reason.ORDER_CLOSED_BY_SL)
                    || message.getReasons().contains(IMessage.Reason.ORDER_CLOSED_BY_TP)){
                console.getOut().println("close reason: " + message.getReasons());
                printOrder(message.getOrder());               
            }
        }
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onStop() throws JFException {
    }
}


Attachments:
OrderHistoryTPSL.java [2.41 KiB]
Downloaded 298 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 

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