In my last article i write about how to save JForex values to files, expecting this month to write about saving this values on database. But i need to find and test a simple database that can be used by the most part of our readers without to much settings, configurations et all and i haven't finished it yet. Because of that, i write in this article about sending emails from inside of JForex in reaction to some values or events we want to get informed about them. The information about this subject provided on Dukascopy Wiki is somewhat incomplete and not works "out of the box", because almost all e-mail providers changed the way we can send e-mails using their servers. The example provided on wiki works if you have a insecure smtp (Simple Mail Transfer Protocol) server, and actually this is not used by almost all e-mail providers to avoid spam and unauthorized access. The possibility of sending e-mails inside JForex with events, values or alerts is a good tool for the ones who can't be always looking the charts but want to be alerted when something occurs and have access to their e-mail. With this article you can build your strategy to send e-mails with text only. At the end of this article i will provide some reference points you can use to extend this strategy to include for example image charts.
Some internal basics about to send an e-mail
To send an e-mail we must obey to some rules or protocols to allow the communication between clients and servers. Some protocols exist for this objective, but the widespread protocol acting as de facto standard is SMTP (Simple Mail Transfer Protocol) and is used by everyone. To use this protocol in the simplest but insecure way, we only need to know the address of the server and port that is listening for connections and the parameters needed to send e-mail and the content of the e-mail. The standard ports used for communication to send e-mails with this protocol are usualy 25, 587 and 465. The parameters can be the "from e-mail address", "send to e-mail address", "subject" of the e-mail and finally the content. But today some security protocols are implemented to avoid in some extent the spam, unauthorized access to smtp servers and to encrypt the messages to avoid message interception as the basic SMTP sends the messages as is. The most common security protocols are SSL (Secure Socket Layer) and TLS (Transport Layer Security) that is considered a evolution of SSL. With this security measures, we need more parameters to send the e-mail. In this case is the type of security used, a username and a password to access the server.
External components needed
The standard Java JRE (Java Runtime Environment) or JDK (Java Development Kit) does not have the components needed from where we can use them to send e-mails without much trouble. For that we need to use a library called "Java Mail" that is provided by the maintainer of Java and can be downloaded here. This is the library used on this article. After the download, you need to decompress the zip file to a directory of your choice. Also you need to get the path of the library "mail.jar" (the zip file contains more content, for example javadoc, changes, etc). To use this library on our strategy we must tell we need to use it. For that we use JForex directive "@Library" before the class strategy declaration that allow us to include external libraries as i show in the code snipet bellow.
Image 1: Snipet of code to include the needed "mail.jar" library.
Setting the parameters for our strategy
As i mentioned before, wee need to define some parameters so we can use them to send e-mails.
This example strategy will allow us to send emails with any of the usual configurations possible for our smtp server. Bellow i show the needed variables that can be set at strategy start.
Image 2: Configurable parameters of the strategy.
At strategy start we get the following window. On first example we have filled the parameters to use Gmail e-mail service. Before you use the gmail smtp server you need to configure your gmail account to allow pop/mapi/smtp connections directly on web interface of gmail on account settings.
Image 3: Window to define parameters at strategy start filled with example for GMail accounts.
On the next image we have the parameters filled for Outlook.com (the new email service from Microsoft that is replacing Live.com and Hotmail.com). You can use the same settings for all this type of accounts from Microsoft just changing the "outlook" by "live" or "hotmail".
Image 4: Parameters filled for Outlook accounts. You can use the same settings for Live and Hotmail accounts just changing the name of the service.
Sending the e-mail
To send e-mails i just created a method to do this called mySendMail() with receive only the parameters "mySubject" and "myMessageBody" because all other needed parameters are set at the start of the strategy. The subject provided is added to the subject prefix and is passed all e-mail content on myMessageBody. On this method i use a switch/case to set the needed parameters to match the needs of each security protocol and adjust for selected security type at beginning. After we initiate the session that allows the connection with the username and password. After we fill the fields of the email and try to send it. We send the information of sent email, or in case of error we send the error to the strategy message tab. Simple as that. You can see the complete code of this method bellow.
Image 5: mySendMail() method where we fill the email and try to send it.
The base procedures to send e-mails from inside JForex is presented at this point. But well, we need to test it. Just for test purposes we use the following code on the onStart() method.
Image 6: Code only for test purposes of the strategy.
Image 7: Examples of "Inbox" and "Message content" on Gmail and Outlook of one e-mail message sent by the strategy with testing string "teste" for subject and message.
Use case scenario examples
At this moment we have the confirmation we can send e-mails from inside JForex, now we can adapt the strategy to match our objectives about the information we want to send to the email. I just will provide three simple and not complete examples but you can adapt them for more complex objectives. In this case i will provide one example for price getting above some value, indicator getting above some value and on close order. I do not filter instruments nor periods on this examples. On the examples i don't use configurable variables, but you can use them to make your strategy more dinamic without the need to recompile each time we want different values or situations on strategy run.
Image 8: Three simple and incomplete "use case scenario" for this strategy. On onTick() method we check if price reach 1.35 level, if so sends alert e-mail and after at each superior +0.05 (500 pips) level reached; On onBar() method we get a indicator value and if it's value is greather than a determined value the strategy send an alert e-mail. On last example, on the onMessage() method the strategy verifies if the received message is about a close of an order and if so, send a alert e-mail informing about it.
Some reference points
To have the possibility to send e-mails in reaction to some events when we can't follow the market on the charts because we are outside at other full time work, when we need to eat, go to the bathroom or have a life this is a way to be always "in" on selected events if we have permanent access to our mail boxes. For ones can be a good tool, for others maybe is nothing... but you decide. Other ideia that came to my mind with this article is a article about how to send sms messages. What you think ? I will try to find a easy way to do that if possible without the need to have to spend too much money on harware or none at all except the value of the sms message.
I hope you like this article and all of it's series. As usual, fell free to comment or suggest anything on comments or on the forum thread dedicated to this article series that you can access here. Also on this thread is the complete strategy presented on this article.