dimanche 14 août 2016

Automating trades from Opentrade Stars

Hi,

Opentrade stars are churning out some serious returns and that too on consistent basis. Nice stuff and I really appreciate for their skills and the level they are reached to.

I have followed many stars for many months and it is highly impossible to understand their multi leg strategies based on what data is provided. No way to know if its iron condor or single trade or they are hedging it against some previous other instrument etc. Sorry to say But I really never understood/learned a single thing from observing those trades so I think only one way remaining is to follow then without understanding their strategy for similar returns. If you don't understand something then follow it, one day you will definitely understand something. ;)

Basically I never understood how they get such awesome results. Even I can not imaging of 100-300% returns in 90 days time frame, and they are doing it consistently. And as we 'the common people' do not have such magic wind, lets follow them.

We get email notifications for their trades and we can follow the same path if we want. This is designed for the same. But I was never able to follow them manually from mails I was receiving, So I thought of automating it.

So if you have auto trading setup(If you are able to fire orders from amibroker etc) then you can easily automate it.

A small utility created in java will keep on monitoring emails and will create a new file if it receives any mail(You can filter for opentrade mails if you want). So I will suggest to create a new mail account preferably gmail as I have included gmail settings in following code.

//************

import java.io.IOException;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.event.MessageCountAdapter;
import javax.mail.event.MessageCountEvent;

import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPStore;

public class EmailMonitor {

private static final String username = "abc@gmail.com";
private static final String password = "xyz";

public static void main(String[] args) {

Properties properties = new Properties();
// properties.put("mail.debug", "true");
properties.put("mail.store.protocol", "imaps");
properties.put("mail.imaps.host", "imap.gmail.com");
properties.put("mail.imaps.port", "993");
// properties.put("mail.imaps.timeout", "10000");

Session session = Session.getInstance(properties); // not
// getDefaultInstance
IMAPStore store = null;
Folder inbox = null;

try {
store = (IMAPStore) session.getStore("imaps");
store.connect(username, password);

if (!store.hasCapability("IDLE")) {
throw new RuntimeException("IDLE not supported");
}

inbox = (IMAPFolder) store.getFolder("INBOX");
inbox.addMessageCountListener(new MessageCountAdapter() {

@Override
public void messagesAdded(MessageCountEvent event) {
Message[] messages = event.getMessages();

for (Message message : messages) {
try {

Multipart mp = (Multipart) message.getContent();
BodyPart bp = mp.getBodyPart(0);

System.out.println("Mail Subject:- " + message.getSubject());
System.out.println("Mail body:- " + bp.getContent());
} catch (MessagingException | IOException e) {
e.printStackTrace();
}
}
}
});

IdleThread idleThread = new IdleThread(inbox);
idleThread.setDaemon(false);
idleThread.start();

idleThread.join();
// idleThread.kill(); //to terminate from another thread

} catch (Exception e) {
e.printStackTrace();
} finally {

close(inbox);
close(store);
}
}

private static class IdleThread extends Thread {
private final Folder folder;
private volatile boolean running = true;

public IdleThread(Folder folder) {
super();
this.folder = folder;
}

public synchronized void kill() {

if (!running)
return;
this.running = false;
}

@Override
public void run() {
while (running) {

try {
ensureOpen(folder);
System.out.println("enter idle");
((IMAPFolder) folder).idle();
} catch (Exception e) {
// something went wrong
// wait and try again
e.printStackTrace();
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
// ignore
}
}

}
}
}

public static void close(final Folder folder) {
try {
if (folder != null && folder.isOpen()) {
folder.close(false);
}
} catch (final Exception e) {
// ignore
}

}

public static void close(final Store store) {
try {
if (store != null && store.isConnected()) {
store.close();
}
} catch (final Exception e) {
// ignore
}

}

public static void ensureOpen(final Folder folder) throws MessagingException {

if (folder != null) {
Store store = folder.getStore();
if (store != null && !store.isConnected()) {
store.connect(username, password);
}
} else {
throw new MessagingException("Unable to open a null folder");
}

if (folder.exists() && !folder.isOpen() && (folder.getType() & Folder.HOLDS_MESSAGES) != 0) {
System.out.println("open folder " + folder.getFullName());
folder.open(Folder.READ_ONLY);
if (!folder.isOpen())
throw new MessagingException("Unable to open folder " + folder.getFullName());
}

}
}


// *******************

Beauty of this code is that it is very light weight on CPU, as there are no loops to check on frequent intervals. Also its very fast, almost realtime. So there will not be more than 1 second delay when mails receives and your order hits.

Now you can filter content of the mail from this code, add some logic and save to a file on local folder.

Amibroker can read flawlessly from files. So just read from the file, fire order, delete the file. or you can take other approach if you want.

This is just a headstart which should be sufficient to get you started and you SHOULD be able to automate opentrade star trades from this mechanism. easy and simple.


Automating trades from Opentrade Stars

Aucun commentaire:

Enregistrer un commentaire