In last article i presented a strategy called “MyOfflineTrades” that allow us to do manual trading on Historical Tester. I have described all steps needed to use it and how to use it. Now i will try to explain how i have developed it and you can use this knowledge to extend or to use it on other projects. I tried to make it in the simplest way i can, maybe some code can be developed in some more efficient ways.


One of first dilemas i faced was to decide how i integrate the form of the strategy on the strategy itself. We have two solutions for that: one as external java source file and import the corresponding class or jar file by calling it as external file. But this approach can create some problems to some users using it. Other solution is to use inner classes and develop the form class inside the strategy itself. This project is a simple one, just a few classes and not too large in code. This way i decided by this second approach and this way i don't need to provide more files than the strategy file itself. The only disavantage is working all the code on only one java file (not advisable to use on large projects). Bellow a snipet code how to do that.

Image 3: Snipet example of the utilization of inner classes

After decinding this i have to think about how to structurate the strategy and the form where we can interact with the “Historical Tester” to place the orders. To design the form window i used the Netbeans IDE using their visual editor to make the base objects and make my life easiest. After this i ajusted the code to be inserted on the strategy with some tunning and changes. Later i need to do some changes but i have done then just coding them in the strategy itself.

Image 4: Using the visual editor on Netbeans IDE.

I need a way to update the top labels showing us the actual price. The method by excellence to get the bid and ask price in real time is the “onTick()” method. In this method i decided to update the form labels that inform us about bid and ask price and update the created/filled orders values on the orders table. This is done with the next code snipet.

Image 5: The code on “onTick()" method.

On this method we update the top labels with the last bid and ask price calling the method “myForm.updatePrices(tick)” passing the “tick” object, verify if we have any order created/filled and update the table where we have the information about them passing the “order” object on the “myForm.updateTableOrder(order)”. If we don't have any order we clear the table using the method “myForm.synchronize()”. This method was created to do a “workarround” with some orders not removed correctly from the table when closed.

Image 6: The “onMessage()” method where we add/delete/update order status and set “Stop Loss and Take Profit”.

Other problem i get into is how to update the orders (insert/delete) on the table. Also i have several choices to do that: maintaining a array or list with the orders and update them on the “onTick()” method is one way among others but i came to the “onMessage()” method where we receive all events related with the creation/fill and close of the orders and to me this way seems the easiest way to do this. Also i took this method to set the “Stop Loss” and “Take Profit” at order creation. And this are the two inherithed methods of the strategy with significant code. All other process are done inside of the inner class “OfflineForm”.

The inner class “OfflineForm”

This inner class is where allmost all “active” code resides. It's started with:

class OfflineForm extends JFrame implements ActionListener{
… class code …
[end code]

This class is initiated inside the strategy class and that's why we call it a “inner class”. It extends “Jframe” java class to allow us to build the form window and implements “ActionListener” to allow us to capture or react to some events that occurs inside the form window. I don't have the space to discuss all the code and my objective is only to discuss some aspects that gives me more work to pass them. The complete code is well commented (i think) and is understandable.

The text fields where the user input the values accept any character and to only accept numbers and one dot i need to implement a key listener to filter what the user writes at any of this fields. We can achieve this with the following code after the initialization of the text field.

Image 7: Text field example of key filtering. We only allow numbers or one dot, all other characters are discarded.

Also i validate all the inputs because the user can past anything on this fields and can past anything “strange”. The radio buttons are grouped to allow us to have the choices for order type and the value type filled on “Stop Loss” and “Take Profit”, nothing special here, we use what the user selected to proced according with this choices.

The Table

The table was the area that had gave me more work to implement. To the form window not get too large i have to split all the values i want to show in three rows. Also i have created two hidden collumns to store some needed values to help us on the update process of the orders on the table. Centering the values and the background/foreground colors of the cells are other “problematic” area. To to this i have to override the table model to change the default one and for that i created two additional classes.

Image 8: The two aditional inner classes to override the default table model. One to center the content on the cell and the other takes the task of setting the foreground and the background of the cells according it's value or position.

Other thing i have to decide is how to close the trades manually… Also here we have several choices, with buttons is one example but i don't like the aspect of the buttons on the table. So, i decided to create a table mouse event listener to get the cells where the user have clicked. The following snipet do that task.

Image 9: The snipet code that takes the task of getting the mouse click events over the table allowing us to close the orders and also trigger a window to reset the “Stop Loss” or “Take Profit” when we click on the corresponding cell.

Other aspect that the code above does is also to trigger a “Inputbox” to allow us to change the value of “Stop Loss” or “Take Profit” of the corresponding cell clicked at order row. I can use different approach, allowing the user edit the corresponding cell, but i have some troubles validating the value to not allow invalid values. Because of this i developed in this simple way. One user at the forum suggested to allow the change of values of “Stop Loss” or “Take Profit” directly at the charts changing the position of the corresponding line, but at this moment i have to explore if exists some way to do that on the Historical Tester. Bellow the snipet code that shows us the "InputBox".

Image 10: Snipet code that shows us a input box to change the values of “Stop Loss” or “Take Profit” and validates the user input.

Final words

There are much more i can say about "How it's made" in this strategy, but to not bore you i only wrote the essencial aspects i have considered on it. As is, this strategy do everything i want and need. It meets all my requirements as i described in the initial article about it. I don't have received much more requests from readers/users. The only two i have received, one is about "LIMIT" orders and is fully integrated already on this code. The other, is the one i talked before about the change of "Stop Loss" or "Take Profit" directly on the chart. I don't know if i will do some efforts trying to developing this, but if i find a solution on my learning path about JForex API, surely i consider to implement it. Also, if you have any suggestion, feel free to say me here or in the forum and i will try to do my best if i consider that a useful thing. The code of this strategy is published on the forum on the thread related with this articles here and on the thread about this strategy here.

As always i hope you like this article and find this strategy useful.

Trade well and prospers in your way.


翻译为 英语 显示原始