Charting in MetaTrader for my indicators written in C++

As you have noticed from my other blogs, I created my ftap algorithm trading system, which is full featured back testing and trading system. I also created a lot of indicators in the system (there are about 31 indicators at the time of writing). Some indicators are “standard”, such as moving average, RSI, MACD, which are simple wrappers of ta-lib. Some indicators are wrote by me, inspired by the ideas on the internet. The indicators are great in back testing but there are some big problem.

The problem

The biggest problem is that it's difficult to see the indicators visually. After a new indicator is created, I use a simple test program in ftap system, to render the indicator on an image file, using a simple charting module in ftap. This method is cumbersome. I can't scroll the chart, I can't verify the values are correct.

With this problem, I even thought one day I may write my own full featured system! But luckily, my mind changed recently

Use MT4 for charting my C++ indicators

I began using MT4 since the day one when I trade Forex. But most time I only use it for charting. Though I can code MQL perfectly, I didn't do a lot of coding on MT4 since I focused on my own C++ trading system.

So I underestimated MT4. I considered MT4 as a charting system with propriatory programming language MQL. Though I know MQL can import DLL, I never thought it has any connection to my large amount of C++ code base.

Recently I created several MT4 indicators for a client using MQL. No C++ involved, but this kind of intensive working on MT4 made me looking closer to MT4 again and see the huge potential of MT4 in helping me on my C++ code. That is, wrap my C++ code in DLL and invoke it from within MT4.

The result

I've finished the wrapper to integrate the RSI indicator in C++ to MT4. After I put the C++ indicator on MT4 chart, the feeling is amazing. Now I can see my C++ indicators visually without I reinventing a new charting system!

Wrapping each C++ indicator to MQL independently is boring and repeated work. I hate repeated work. So I built a tiny framework to automate most of the work. From now on wrapping a C++ indicator is just a piece of cake.


adityajha, 2016/09/20 07:01

+——————————————————————+ | dayHL_Average.mq4 | +——————————————————————+ #property indicator_chart_window #property indicator_buffers 3 #property indicator_color1 Red #property indicator_color3 Red —- input parameters5 extern int show_comment=1; comments on the chart (0 - no, 1 - yes) extern int how_long=1000; bars to be counted (-1 - all the bars) —- indicator buffers double ExtMapBuffer1[]; double ExtMapBuffer2[]; double ExtMapBuffer3[]; +——————————————————————+ | Custom indicator initialization function | +——————————————————————+ int init() { SetIndexBuffer(0, ExtMapBuffer1); SetIndexStyle(0, DRAW_LINE); SetIndexBuffer(1, ExtMapBuffer2); SetIndexStyle(1, DRAW_LINE); SetIndexBuffer(2, ExtMapBuffer3); SetIndexStyle(2, DRAW_LINE); return(0); }

+——————————————————————+ | Custom indicator deinitialization function | +——————————————————————+ int deinit() { Comment(“”); return(0); } int start() { int cnt=0; int begin_bar=0; int prev_day, cur_day; double day_high=0; double day_low=0; double yesterday_high=0; double yesterday_low=0; double yesterday_close=0; double P, S, R; if (Period() >= PERIOD_D1) { Comment(“WARNING: Invalid timeframe! Valid value < D1.”); return(0); } if (how_long == -1) { begin_bar = Bars; } else { begin_bar = how_long; } for (cnt = begin_bar; cnt >= 0; cnt–) { cur_day = TimeDay(Time[cnt]); if (prev_day != cur_day) { yesterday_close = Close[cnt+1]; yesterday_high = day_high; yesterday_low = day_low; P = (yesterday_high + yesterday_low ) / 2; R = yesterday_high; S = yesterday_low; day_high = High[cnt]; day_low = Low[cnt]; prev_day = cur_day; } day_high = MathMax(day_high, High[cnt]); day_low = MathMin(day_low, Low[cnt]); ExtMapBuffer2[cnt] = P; ExtMapBuffer1[cnt] = R; ExtMapBuffer3[cnt] = S; } if (show_comment == 1) { P = (yesterday_high + yesterday_low ) / 2; R = yesterday_high; S = yesterday_low; Comment(“Current H=”, R, “, L=”, S, “, HL/2=”, P, “, H-L=”, (R-S)/Point ); } return(0); } +——————————————————————+

Enter your comment. Wiki syntax is allowed: