test data

Daily IT department for "lack of memory" brain CEO

Admin

by Admin

Hello, everyone! I hope you're doing well. Today, by the end of the day, we will be able to print our first custom indicator on the chart. Our main focus here is on the structure of the code and how to create the script, rather than the logic or some fancy functions themselves. So, let's get started.


Create a Custom Indicator Script in MT5


From day 1 up to now, we've only created a blank script without any pre-built functions. However, MT5 provides us with other advanced options, such as pre-built Expert Advisors or pre-built indicators (which we will use today). Here are the step-by-step instructions to achieve this:


In MetaEditor, go to New > Custom Indicator > Next




Input your file name > Next > Check the box for OnCalculate(...,open,high,low,close) > Next

  • When you enter the file name, you all so can set up some parameters, which allow you to change some input settings without touching the code later. We'll skip this for now.
  • You might wonder what OnCalculate, OnTimer, and OnChartEvent are. They're called Event Handling. All Event Handlers of MT5 are listed here. If you have time, you can read about them. For now, we only care about OnCalculate - this Event Handler will do something for us every time the price changes.




Click Add > Enter your Indicator label and color > Finish

  • We have an option to display our indicator in a separate window called \"Separate window\". Don't tick it (if you do, you can change it later).




Review of the Script Structure


After following the above steps, you will get a pre-script like this:



The First path is all the setting being displayed as #property.

  • Copyright and version: These are important if you ever want to sell your tool. When discussing selling a tool, you don't provide the source code; you only sell the compiled version of your code with the .ex5 extension to protect the algorithms behind it.
  • The chart indicator_chart_window: This determines where the indicator will be shown. You can change this setting. The color and label of the indicator are also listed here.
  • The buffer: This is the array that holds the price information and then displays it on the chart. We mainly add logic to manipulate this property.

The second and third parts are where we implement the logic.

  • Since we're on day 6, we won't delve into this part too much. We should focus on understanding the overall structure first.


Adding Code and Running the Custom Script


Now let's add the script below to your file and run it by pressing F5 on your keyboard. I've set up a simple Moving Average with a period of 15 candles:


#property indicator_chart_window\r\n#property indicator_buffers 1\r\n#property indicator_plots   1\r\n//---- plot MA\r\n#property indicator_label1  \"MA\"\r\n#property indicator_type1   DRAW_LINE\r\n#property indicator_color1  clrRed\r\n#property indicator_style1  STYLE_SOLID\r\n#property indicator_width1  1\r\n//--- input parameters\r\ninput bool               AsSeries=true;\r\ninput int                period=15;\r\ninput ENUM_MA_METHOD     smootMode=MODE_EMA;\r\ninput ENUM_APPLIED_PRICE price=PRICE_CLOSE;\r\ninput int                shift=0;\r\n//--- indicator buffers\r\ndouble                   MABuffer[];\r\nint                      ma_handle;\r\n//+------------------------------------------------------------------+\r\n//| Custom indicator initialization function                         |\r\n//+------------------------------------------------------------------+\r\nint OnInit()\r\n  {\r\n//--- indicator buffers mapping\r\n   SetIndexBuffer(0,MABuffer,INDICATOR_DATA);\r\n   Print(\"Parameter AsSeries = \",AsSeries);\r\n   Print(\"Indicator buffer after SetIndexBuffer() is a timeseries = \",\r\n         ArrayGetAsSeries(MABuffer));\r\n//--- set short indicator name\r\n   IndicatorSetString(INDICATOR_SHORTNAME,\"MA(\"+period+\")\"+AsSeries);\r\n//--- set AsSeries (depends on input parameter)\r\n   ArraySetAsSeries(MABuffer,AsSeries);\r\n   Print(\"Indicator buffer after ArraySetAsSeries(MABuffer,true); is a timeseries = \",\r\n         ArrayGetAsSeries(MABuffer));\r\n//---\r\n   ma_handle=iMA(Symbol(),0,period,shift,smootMode,price);\r\n   return(INIT_SUCCEEDED);\r\n  }\r\n//+------------------------------------------------------------------+\r\n//| Custom indicator iteration function                              |\r\n//+------------------------------------------------------------------+\r\nint OnCalculate(const int rates_total,\r\n                const int prev_calculated,\r\n                const datetime &time[],\r\n                const double &open[],\r\n                const double &high[],\r\n                const double &low[],\r\n                const double &close[],\r\n                const long &tick_volume[],\r\n                const long &volume[],\r\n                const int &spread[])\r\n  {\r\n//--- check if all data calculated\r\n   if(BarsCalculated(ma_handle)<rates_total) return(0);\r\n//--- we can copy not all data\r\n   int to_copy;\r\n   if(prev_calculated>rates_total || prev_calculated<=0) to_copy=rates_total;\r\n   else\r\n     {\r\n      to_copy=rates_total-prev_calculated;\r\n      //--- last value is always copied\r\n      to_copy++;\r\n     }\r\n//--- try to copy\r\n   if(CopyBuffer(ma_handle,0,0,to_copy,MABuffer)<=0) return(0);\r\n//--- return value of prev_calculated for next call\r\n   return(rates_total);\r\n  }\r\n//+------------------------------------------------------------------+\r\n


We will get a red line on our chart as bellow.



Conclusion


Custom scripts open the door to a world of possibilities in MT5. They empower traders to automate routine tasks, conduct in-depth technical analysis, and execute complex trading strategies with precision. As you delve deeper into the realm of custom scripts, remember that practice and experimentation are your allies. Don't hesitate to explore more complex scripts and seek out additional resources to expand your scripting skills.


On the 6th day of the challege, we have archive something. Remember every castle begun with a small brick. Let's keep learning.

#ahihi

※ Related Blogs