Inside a trade alert system

Cuong Le
5 min readDec 13, 2020

Disclaimer: Trading results in capital loss. Do your own due diligence before taking any action. Trade what you can afford to lose.

Context

If we’re not in Vietnam, I think we all still work from home at the moment due to COVID-19. A few months ago, I watched a video on youtube in which the author emphasizes working home is a rare opportunity to improve ourselves, you need to pick up a skill where it helps you through economic downturn. I’ve started learning how to trade Forex since then. Recently, I found a trading method where we just buy at green dot and sell at red dot on enhanced MACD indicator. It’s simple, isn’t it?

GBPJPY H4

Note: I’m not covering the detail on the trading method in this article.

Problems

That trading strategy is not hard. However, whenever there is either a red dot or a green dot, how do I get notified while I’m doing something? MT-based software will beep beep when there is a signal. It means I have to keep the computer on 24/5 (market time). If I’m away from it, I need to check up on the computer whether I miss anything every time. MT-based platform also provides a push functionality to your mobile. Before we continue, look at the image above again, not all dots are reliable entries. So I need to look at the chart to make a decision. Unfortunately, we don’t have the custom indicator on mobile devices.

I also share my trading journey with my inner circle. They are interested in learning from me. Teaching people learn to read chart, candle sticks seems impossible to me. So how do they can follow me easily at least when to buy or sell? The whole flow is a hassle for me to trade effectively.

Before jumping into the code, I wrote down all the problems to be solved below

1. How do I keep the computer always on?

In forex terms, people use VPS (a remote computer) to run a bot trading. Easily, I can use VPS to monitor my signal. I normally shut it down on Friday night to flush stale memory (recommended by VPS provider).

2. How do I look at a chart on any devices?

The only thing I came up with is just find a way to snapshot a chart and send it as an image. Fortunately, MT-based platform offers what I need

bool ChartScreenShot(long chart_id, string filename, int width, int height, ENUM_ALIGN_MODE );

While I worked on this, I realized the indicator can monitor a higher timeframe than the current timeframe. For example, if MT4 is open with a H4 chart, the indicator can monitor on both H4 and D1 chart at the same time. As the API screenshot provided above, it’s only able to snapshot an opening chart. I came up with a small trick. If there is a signal on a specific symbol at any given timeframe, I programmatically open a new chart window with that timeframe, do the screenshot and then close the window.

long newChartId = ChartOpen(Symbol(), tf);ChartNavigate(newChartId,CHART_END,0);if(ChartScreenShot(newChartId,name,WIDTH,HEIGHT,ALIGN_RIGHT))
{
Print("We've saved the screenshot ",name);
ChartClose(newChartId);
}

3. How do I broadcast my signal?

On my initial implementation, I used WebRequest to interact with backend. It doesn’t work on VPS machine for unknown reasons. Plus, it seems hard for me to upload the chart snapshot in C++ I mentioned above. Eventually, I found out that it’s feasible to launch an external program. I quickly wrote a small jvm program to escape from C++ realm.

#include <WinUser32.mqh>
#import "shell32.dll"
int ShellExecuteW(int hwnd,const string operation,const string file,const string parameters,const string directory,int showCmd);#import...int r = ShellExecuteW(0,"Open", program_path, params,NULL,5);

program_path is location to external program

params is value you will get in args in jvm program

I use Cloudinary as a CDN to host chart snapshot image. When upload is completed with an image url, I make a POST request to backend.

try {
val url = fileUploader.upload(file)
val updateSignal = signal.copy(chartUrl = url)
signalRepository.create(updateSignal)
} catch (e: Exception) {
logger.warn("error while uploading " + e)
}

Backend makes record of the signal in CloudStore then broadcasts the alert to messaging apps. The whole flow is visualized below

4. How do I minimize the maintenance cost?

It’s unavoided to pay the VPS fee. However, I want to minimize the cost for this system as much as possible. There are some candidates such as CloudFunction, Heroku. I go with Heroku because of my expertise in Spring Framework. Free Heroku just gives 550 hours a month. If we add a credit card, we have another 450 hours. See more here. A basic math

30*24 = 720h < 550 + 450 = 1000h

Literally, I have a free backend host. However, it’s not that easy. The free dyno will be in sleep mode every 30 minutes. I simply just ping the server every 25 minutes by another free service which is CloudFunction.

exports.scheduledFunction = functions.pubsub.schedule('every 25 minutes').onRun((context) => {axios
.get('endpoint')
.catch((err: any) => {
if (err !== undefined) {
bot.sendMessage(TELEGRAM_ID, "Server ping error");
}
});
return null;
});

If any error while pinging the Heroku server, a message will be sent to my telegram

Result

It’s always happy to see my code helping me making more money.

Closing thought

The system is still in its in fancy, there is plenty of room for me to improve in the future. It was kind of hackathon project I had to finish during the weekend before the market opens. The project was intriguing to me after a failure of Memoir. Coding is a problem-solving process in which you walk through your thought and connect pieces to a big puzzle. For new people in MT platform, I suggest to read its documentation instead of asking on forums. It saves you more time.

--

--