Previous related articles:

In this article i will talk on the areas of a strategy where all the action happen. But before i want to inform you that in the last article i made some changes in the code on order submit validation and corrected the getLabel method/function because there was a error with charateres on the label string refused as invalid by the platform. This code changes are available at the support thread for this article here. The main areas where all action occurs are "onTick", "onBar", "onMessage" methods/functions. We focus our attention now on them and in some important aspects specially in bar and tick filtering because it's essential we understand this.

Bar and tick filtering

The methods/functions onBar and onTick are executed always for ALL subscribed instruments (onBar and onTick) and ALL bars completion (onBar only) on ALL basic periods - 10 secs, 1 minute,  5 minutes, 10 minutes, 15 minutes, 30 minutes, 1 hour, 4 hours, 1 day, 1 week and 1 month! I have to mention that this time is determined from Dukascopy servers time and for example 4 hours period is determined at every 4 specific hours on the server stream data, not on our 4 hours (00:00, 04:00, 00:8 and so on server time every day). This means that onTick is executed when one tick is generated and received from any pair subscribed from Dukascopy stream data - if we have subscribed EUR/USD and USD/JPY this function is executed at any tick received from this two pairs. Taking as example the two subscribed instruments, the onBar method function is executed at any bar period referred before and for any of this two pairs. When i begin trying to test some made strategies i get some strange results in consequence of not having the correct filters taking in consideration this events. Below i provide the code how i have seen all different bar completion  events and the result of backtesting this strategy.

Image 2: With this code we can see all diferent period bars sent and accepted by the method/function onBar. If we want to see all events just need to uncomment the second line of code of area 5. On bottom of image we see the resulting on historical tester tab.

On next image we see example for tick events with some subscribed instruments.

Image 3: The same, but for tick events. We can see on the image above the resulting events on  method/function onTick with some instruments subscribed on historical tester tab. The method/ function is executed as every tick is receiveid with the identification of the pair responsible for it. We can see this on historical tester tab.

The importance of bar and tick filtering seems obvious. If we want that our strategy to only work with a specific pair or period and act the way we expect we need to do a filter for the currency pair we want to use and to the respective period independently of the currency pairs we have subscribed and the periods in use (because we receive all data from this subscribed pairs). Also on onTick method/function we can filter by any tick object property and in onBar method/function we can filter by askBar and bidBar properties besides the currency pair and period.
This can be achied usualy with "if" condition. There are diferent ways to get the same result with diferent code. In next images i will show to you some ways to achive what you may want. On this next one i show some ways of filtering on the onBar method/function.

Image 4: Some examples of bar filtering on method/function onBar. You must decide what way you want and put it on area 5.

The proposed examples for onBar filtering are only that. You certainly can imagine other instruments individually or a collection of them and also each period individually or a collection of them.  You can also use any of the properties of the objects passed as arguments on this method/function to validate any condition you impose.

Image 5: Some examples of tick filtering on method/function onTick. As before, you must choose a way and put it in this case on area 4.

In the last image we see some examples of tick filtering. The method/function onTick is very useful to do some process more efficient as it's executed on every tick and we don't need to wait for the bar completion for example for a entry or for a exit of a order.


Other useful method/function is the onMessage. I spoke rapidly about this method/function on my first article. Here we are goinf deeper on it. This onMessage is executed on every "message" we receive from the stream data sent by Dukascopy servers and by the platform client itself. What messages, you may ask... No, don't is a free chat interface .  With this received messages from the platform itself and from the Dukascopy servers we can process our connection status with Dukascopy servers, market calendar events, market news, broker messages among others, and the most important for our objective: orders status. Each aspect i spoke before are placed inside the IMessage object and this object have one message type as one of their properties. Here i talk about the types related with orders:
  • SENDING_ORDER - Alerts that a order is to be sent to the server. This  is the first message type this process receive from platform when submiting a order command (?).
  • ORDER_SUBMIT_OK - Sent by server when a order is acepted successfully.
  • ORDER_SUBMIT_REJECTED - Sent by the server when a submited order was refused for any reason.
  •  ORDER_FILL_OK - Sent by the server when the order was sucessfully filled.
  • ORDER_FILL_REJECTED - Sent by the server if for some reason the order fill was not filled.
  • ORDER_CLOSE_OK - Sent by the server when the order was sucessfully closed.
  • ORDER_CLOSE_REJECTED - Sent by the server when the try of close order was failled for any reason.
  • There exist more message types, you can consult them on javadoc documentation here.
On this method/function we can access all message events/notifications where we get a IMessage object passed as parameter of it. In this object we have some useful methods/functions to access the data in IMessage object. They are: 
  • getType() - Useful to determine the type of the message.  Some message types are refered before. They belongs to IMessage.Type object.
  • getContent() - to obtain the textual value of the message as String object.
  • getOrder() - If the message is associated with any order, we can get this order object with it. If the message don't is related with any order this value is null.
  • getCreationTime() - returns the creation time of the message in a long value in miliseconds. You need to process this value to get a more readable date/time.
For example if we want to say to our strategy if it can submit trades when a order is closed and know the value proft/loss on our currency of it we can do this with the following code inside the onMessage method/function (area 6):

if (message.getTye == IMessage.Type.ORDER_CLOSE_OK) {
    // suposing you have this boolean variable to check if strategy can submit any order
    canTrade = true;
    myOrder = message.getOrder();
    // outputs to console tab the value on our currency of profit/loss
    myConsole.getOut().println("Order closed:" + myOrder.getLabel());
    myConsole.getOut().println("Profit or Loss value:" + myOrder.getProfitLossInAccountCurrency());
    myConsole.getOut().println(" Profit or Loss in Pips:" + myOrder.ProfitLossInPips());

As i don't have much more space for this article to give you other examples of this, i provide the code for one strategy to see and analyse all onMessage events on console on the forum thread i created to support this article. Also i provide two more strategies with all the ticks and bars events. You can access this thread here.

Final Words

This articles series is getting too slow as, maybe, nobody wants (me included) in strategy building. But we are trying to build a house not a tent and for this we need to take some more time, building it block after block. I'm presenting concepts that are relevant to know about before we make any strategy as i don't need to explain them later (in my huble opinion). Anyway, i hope you continue to like this articles series. If not, i'm sorry but i'm trying to do my best. As always feel free to comment, suggest anything you consider relevant for this theme or send me a private message if you want. Also you can use the correspondent thread on forum if you don't want to do it here.

Перевести на Английский Показать оригинал