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.

lost onbars
 Post subject: lost onbars Post rating: 0   New post Posted: Tue 31 Jul, 2012, 12:13 

User rating: 0
Joined: Fri 29 Jun, 2012, 12:45
Posts: 10
Dear Support,

I created a simple strategy. It opens 1 new order per opened instruments when bars close on the opened charts.
If I use this on 1 chart, it works perfectly. But after I open more chart, the strategy loses lots of onbar callings randomly.
Can you tell me, what is the problem in my example?

My strategy:
public class TestStrategy 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 {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
    }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        IChart chart = context.getChart(instrument);
        if(chart != null && chart.getSelectedPeriod() == period){
            console.getOut().println("Instrument: " + instrument + " Period: " + period);   
            int numoforders = engine.getOrders().size();
            String s = "LONG_" + numoforders;
            IOrder neworder;
            try{
                neworder = engine.submitOrder(s,instrument,OrderCommand.BUY,0.001,0,100,0,0, 0,s);
                if (neworder != null) {
                    int i = 3;
                    while ((neworder.getState() == State.CREATED || neworder.getState() == State.OPENED) && i > 0) {
                        neworder.waitForUpdate(1000);
                        --i;
                    }
                   
                }
            }catch(JFException jfe){
                console.getOut().println(jfe.getMessage());   
            }
        }
    }
}


Thank you


 
 Post subject: Re: lost onbars Post rating: 0   New post Posted: Tue 31 Jul, 2012, 12:30 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
If you want to check if all charts are available then consider implementing a more simple check in the onTick method:
    public void onTick(Instrument instrument, ITick tick) throws JFException {
        IChart chart = context.getChart(instrument);
        if(chart != null){
            console.getOut().println(chart.getInstrument() + " " + chart.getSelectedPeriod());
        } else {
            console.getOut().println(instrument + " has no chart opened");   
        }
    }   


 
 Post subject: Re: lost onbars Post rating: 0   New post Posted: Tue 31 Jul, 2012, 12:47 

User rating: 0
Joined: Fri 29 Jun, 2012, 12:45
Posts: 10
Thank you for your answer, but this is not why i started this topic.
I don't want to check the available charts.
I want to submit an order on every chart at the closes of the bars on the charts.
I don't open 2 or more charts with the same instrument.
I opened 5 charts (with different instruments), all of charts had the same period(M1).
I wanted to opened a buy order at the ends of the bars, but lots of onbar callings had lost.
At the ends of the bars I could not submit all of the 5 orders, because (1-2-3 or 4) onbar callings lost.

This is, why i started this topic.

Thank you for your help.


 
 Post subject: Re: lost onbars Post rating: 0   New post Posted: Tue 31 Jul, 2012, 17:07 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Please try the strategy with 10 second period and provide:
  1. Strategy log.
  2. Messages tab content.
  3. Java console content, see: viewtopic.php?f=81&t=39073


 
 Post subject: Re: lost onbars Post rating: 0   New post Posted: Wed 01 Aug, 2012, 09:54 

User rating: 0
Joined: Fri 29 Jun, 2012, 12:45
Posts: 10
Hello,

I tested it with 3 M1 charts (CHF/JPY GBP/USD EUR/USD).
I waited for 3 bar closes.
Ther result

Message:
[*]08:36:42 Strategy "TestStrategy" is stopped at 2012-08-01 08:36:42.704 GMT on the local computer with no parameters
08:36:42 Stopping "TestStrategy" strategy at 2012-08-01 08:36:42.678 GMT on the local computer
08:40:00 Order FILLED at 1.56343 USD (#156052367 BUY 0.001 mil. GBP/USD @ MKT MAX SLIPPAGE 0.01) - Position #40305783
08:36:29 Order BUY 1000 GBP/USD @ MKT is sent at 2012-08-01 08:36:29.947 GMT by the strategy "TestStrategy": from the local computer
08:39:00 Order FILLED at 1.56353 USD (#156052240 BUY 0.001 mil. GBP/USD @ MKT MAX SLIPPAGE 0.01) - Position #40305719
08:35:33 Order BUY 1000 GBP/USD @ MKT is sent at 2012-08-01 08:35:33.681 GMT by the strategy "TestStrategy": from the local computer
08:39:00 Order FILLED at 1.2303 USD (#156052238 BUY 0.001 mil. EUR/USD @ MKT MAX SLIPPAGE 0.01) - Position #40305718
08:35:33 Order BUY 1000 EUR/USD @ MKT is sent at 2012-08-01 08:35:33.565 GMT by the strategy "TestStrategy": from the local computer
08:38:01 Order FILLED at 1.22987 USD (#156052002 BUY 0.001 mil. EUR/USD @ MKT MAX SLIPPAGE 0.01) - Position #40304132
08:34:37 Order BUY 1000 EUR/USD @ MKT is sent at 2012-08-01 08:34:37.495 GMT by the strategy "TestStrategy": from the local computer
08:38:00 Order FILLED at 1.56316 USD (#156052000 BUY 0.001 mil. GBP/USD @ MKT MAX SLIPPAGE 0.01) - Position #40304131
08:34:37 Order BUY 1000 GBP/USD @ MKT is sent at 2012-08-01 08:34:37.304 GMT by the strategy "TestStrategy": from the local computer
08:38:00 Order FILLED at 79.981 JPY (#156051998 BUY 0.001 mil. CHF/JPY @ MKT MAX SLIPPAGE 1) - Position #40304130
08:34:37 Order BUY 1000 CHF/JPY @ MKT is sent at 2012-08-01 08:34:37.067 GMT by the strategy "TestStrategy": from the local computer
08:33:57 Strategy "TestStrategy" is started at 2012-08-01 08:33:57.223 GMT on the local computer with no parameters
08:33:57 Starting "TestStrategy" strategy at 2012-08-01 08:33:57.201 GMT on the local computer
[*]

java console:
wrote nothing

trade log:
[*]
01.08.2012 08:40:00 Order #156052367, Position #40305783 BUY 1 000.00 GBP/USD Average 1.56343 FILLED
08:40:00.575 Trade #107840830 1 000.00 1.56343

01.08.2012 08:39:00 Order #156052240, Position #40305719 BUY 1 000.00 GBP/USD Average 1.56353 FILLED
08:39:00.335 Trade #107840768 1 000.00 1.56353

01.08.2012 08:39:00 Order #156052238, Position #40305718 BUY 1 000.00 EUR/USD Average 1.2303 FILLED
08:39:00.181 Trade #107840766 1 000.00 1.23030

01.08.2012 08:38:01 Order #156052002, Position #40304132 BUY 1 000.00 EUR/USD Average 1.22987 FILLED
08:38:00.789 Trade #107839124 1 000.00 1.22987

01.08.2012 08:38:00 Order #156052000, Position #40304131 BUY 1 000.00 GBP/USD Average 1.56316 FILLED
08:38:00.504 Trade #107839122 1 000.00 1.56316

01.08.2012 08:38:00 Order #156051998, Position #40304130 BUY 1 000.00 CHF/JPY Average 79.981 FILLED
08:38:00.169 Trade #107839120 1 000.00 79.981
[*]

Strategy Log loads nothing.

So as you can see at the 1st closes it opened 3 postioins, at the 2nd just 2, finally at the 3rd just 1.
So I needed 9 positions, but I got just 6, 3 onbar callind missed.

Thank you for your answer


 
 Post subject: Re: lost onbars Post rating: 0   New post Posted: Thu 02 Aug, 2012, 09:33 

User rating: 0
Joined: Fri 29 Jun, 2012, 12:45
Posts: 10
Do you have any idea or solution to fix this problem?


 
 Post subject: Re: lost onbars Post rating: 0   New post Posted: Thu 02 Aug, 2012, 10:25 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
From your example and logs it is hard to tell where the problem lies. could you please launch the following strategy and post strategy log in case if the orders don't open:
package jforex.orders;

import java.text.SimpleDateFormat;
import java.util.TimeZone;

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

public class OrderPerChartBar implements IStrategy {

    @Configurable("")
    public Period period = Period.TEN_SECS;
   
    private IConsole console;
    private IContext context;
    private IEngine engine;
   
    @SuppressWarnings("serial")
    public static SimpleDateFormat sdfTimeOnly = new SimpleDateFormat("HH_mm_ss") {
        {
            setTimeZone(TimeZone.getTimeZone("GMT"));
        }
    };
   
    @Override
    public void onStart(IContext context) throws JFException {
        this.context = context;
        this.console = context.getConsole();
        this.engine = context.getEngine();

    }

    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if(this.period != period){
            return;
        }
        IChart chart = context.getChart(instrument);
        print("%s %s bar arrived: %s",instrument, period, bidBar);
        if(chart == null){
            console.getWarn().println(instrument +" chart is null");
            return;
        }
        if(chart.getSelectedPeriod() != period){
            console.getWarn().println(instrument +" chart period "+chart.getSelectedPeriod()+" != " + period);
            return;
        }
       
        String label = String.format("%s_%s",instrument.getPrimaryCurrency(), sdfTimeOnly.format( bidBar.getTime()));
        IOrder order = engine.submitOrder(label, instrument, OrderCommand.BUY, 0.001);
        print("chart period is OK, submitted order "+ order);
    }
   
    private void print(String format, Object...args){
        print(String.format(format,args));
    }
   
    private void print(Object o){
        console.getOut().println(o);
    }

    @Override
    public void onMessage(IMessage message) throws JFException {
        if(message.getOrder() != null){ //only print order related messages
            console.getInfo().println(message);
        }
    }

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

    @Override
    public void onAccount(IAccount account) throws JFException {}

    @Override
    public void onStop() throws JFException {}

}


Attachments:
OrderPerChartBar.java [2.29 KiB]
Downloaded 287 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.
 
 Post subject: Re: lost onbars Post rating: 0   New post Posted: Thu 02 Aug, 2012, 12:17 

User rating: 0
Joined: Fri 29 Jun, 2012, 12:45
Posts: 10
Thank you for your example code.

I tested it, it worked well.
But I completed your code with a waitForUpdate (order.waitForUpdate(2000);) calling and the problem came forward again.

I need to wait for the filled state in my strategy, but I can't do this, because waitForUpdate function hides other onbar callings.

Can you help me to solve this problem?

Thank you


 
 Post subject: Re: lost onbars Post rating: 0   New post Posted: Thu 02 Aug, 2012, 12:41 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Quote:
I need to wait for the filled state in my strategy, but I can't do this, because waitForUpdate function hides other onbar callings.
Consider processing your orders in onMessage, see:
https://www.dukascopy.com/wiki/#Manage_Order_State/Follow_market_order_fill


 
 Post subject: Re: lost onbars Post rating: 0   New post Posted: Tue 14 Aug, 2012, 13:06 

User rating: 0
Joined: Fri 29 Jun, 2012, 12:45
Posts: 10
Thank you, I've tried it in onMessage, but I can't decide which order got in filled state. Right after onsubmit is there a way to get the order's ID or any unique identifier (not the comment or label as they aren't reliable to identify) to decide which order get filled?

We have more strategies running on one instrument in different timeframes, and they open on onbars. So the instrument or the opentime isn't work as an identifier.

We send for example three submitorder at the same time, then waiting on the onmessage function. If we get that an order just got filled than how to decide it's for which strategy?

Thanks!


 
 Post subject: Re: lost onbars Post rating: 0   New post Posted: Tue 14 Aug, 2012, 16:39 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
see:
https://www.dukascopy.com/wiki/#Order_Management/Maintain_order_list


 

Jump to:  

  © 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