/* MyJFMail - Sending emails from JForex * Not a functional strategy, just for learn purposes * If any questions please read the related article at Article Contest * * Developed by Community member JLongo */ package myJForex.strategies; import com.dukascopy.api.Configurable; import com.dukascopy.api.IAccount; import com.dukascopy.api.IBar; import com.dukascopy.api.IConsole; import com.dukascopy.api.IContext; import com.dukascopy.api.IHistory; import com.dukascopy.api.IIndicators; import com.dukascopy.api.IMessage; import com.dukascopy.api.IStrategy; import com.dukascopy.api.ITick; import com.dukascopy.api.Instrument; import com.dukascopy.api.JFException; import com.dukascopy.api.Library; import com.dukascopy.api.OfferSide; import com.dukascopy.api.Period; import com.dukascopy.api.RequiresFullAccess; import java.util.Calendar; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; @RequiresFullAccess // we need full access to access mail library // if you don't have the java mail library, you can download it from // http://www.oracle.com/technetwork/java/javamail/index-138643.html // extract the contents of downloaded file to a directory of your choice // but don't forget to change the path below to match the place of mail.jar // on your system @Library("P:/Recursos/javamail/mail.jar") public class MyJFMail implements IStrategy{ // Base objects creation private IContext myContext; private IHistory myHistory; private IConsole myConsole; private IIndicators myIndicators; private IAccount myAccount; // email settings public enum ServerSecurity { TLS, SSL, NONE } @Configurable("SMTP Server: ") public String mySMTPServer = "smtpserver"; @Configurable("Server security type:") public ServerSecurity myServerSecurity = ServerSecurity.TLS; // smtp server standard ports @Configurable("Smtp server port (587, 465, 25):") public String mySmtpPort = "587"; @Configurable("Username: ") public String myUsername = "username"; @Configurable("Password: ") public String myPassword = "password"; @Configurable("Send to email:") public String mySendToEmail = "to email"; @Configurable("Sent from email:") public String mySentFromEmail = "from email"; @Configurable("Email subject default prefix:") public String myEmailSubjectPrefix = "E-mail from Strategy - "; @Override public void onStart(IContext context) throws JFException { // Base objects initialization myContext = context; myHistory = myContext.getHistory(); myConsole = myContext.getConsole(); myIndicators = myContext.getIndicators(); myAccount = myContext.getAccount(); // just to test if everything is working mySendMail("test email", "Just testing if everything is working"); } //private double myValue = 1.35; @Override public void onTick(Instrument instrument, ITick tick) throws JFException { // level reached example // if (tick.getBid() > myValue){ // String myMessage = "The pair " + instrument + "has reached the" // + "level " + tick.getBid() + " at" + myGetDateTime(); // mySendMail("Price level reached!", myMessage); // myValue += 0.05; // } } @Override public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { // indicator value reached // double myRSIValue = myIndicators.rsi(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.OPEN, 10, 0); // if (myRSIValue > 80){ // mySendMail("Overbought level reached", "Overbought level reached at " + myGetDateTime()); // } } @Override public void onMessage(IMessage message) throws JFException { // order close example // if (message.getType() == IMessage.Type.ORDER_CLOSE_OK){ // String myMessage = "Order " + message.getOrder().getId() + " Closed!" + // "\n Closed at " + message.getOrder().getCloseTime() + "\n with Profit/Loss: " // + message.getOrder().getProfitLossInAccountCurrency(); // mySendMail("Order closed", myMessage); // } } @Override public void onAccount(IAccount account) throws JFException { } @Override public void onStop() throws JFException { } public void mySendMail(String mySubject, String myMessageBody){ // settings to make the connection to the server Properties myProperties = new Properties(); myProperties.put("mail.smtp.host", mySMTPServer); myProperties.put("mail.smtp.port", mySmtpPort); switch(myServerSecurity){ case TLS: // settings for smtp TLS security myProperties.put("mail.smtp.auth", "true"); myProperties.put("mail.smtp.starttls.enable", "true"); break; case SSL: // settings for smtp SSL security myProperties.put("mail.smtp.socketFactory.port", mySmtpPort); myProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); myProperties.put("mail.smtp.auth", "true"); break; default: break; } // initiate the session Session mySession = Session.getDefaultInstance(myProperties, new javax.mail.Authenticator() { @Override protected javax.mail.PasswordAuthentication getPasswordAuthentication() { return new javax.mail.PasswordAuthentication(myUsername, myPassword); } }); // try to send try { Message myMessage = new MimeMessage(mySession); myMessage.setFrom(new InternetAddress(mySentFromEmail)); myMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mySendToEmail)); myMessage.setSubject(myEmailSubjectPrefix + mySubject); myMessage.setText(myMessageBody); Transport.send(myMessage); myConsole.getOut().println("Strategy has sent one e-mail with this " + "subject '" + myEmailSubjectPrefix + mySubject + "' on " + myGetDateTime()); } catch (Exception e) { myConsole.getOut().println("ERROR SENDING MAIL! : Strategy has tried " + "to send one e-mail with this " + "subject '" + myEmailSubjectPrefix + mySubject + "' on " + myGetDateTime() + " with error!"); } } private String myGetDateTime() { String dateTime; Calendar myCalendar = Calendar.getInstance(); int day = myCalendar.get(Calendar.DAY_OF_MONTH); int month = myCalendar.get(Calendar.MONTH) + 1; int year = myCalendar.get(Calendar.YEAR); int hour = myCalendar.get(Calendar.HOUR_OF_DAY); int minute = myCalendar.get(Calendar.MINUTE); dateTime = "" + year + "-" + month + "-" + day + " " + hour + ":" + minute; return dateTime; } }