HomeAdvanced MT4 and cTrader FeaturesCustom Indicators and Scripts: How to use and create them

    Custom Indicators and Scripts: How to use and create them

    Level:

    Course language:

    English

    Custom Indicators and Scripts: How to Use and Create Them

    Custom indicators and scripts can enhance your trading experience by adding unique functionalities to your trading platforms, such as MT4 (MetaTrader 4) and cTrader. These tools allow traders to tailor the platforms to their specific needs, implement complex strategies, and automate trading tasks. Here’s a detailed guide on how to use and create custom indicators and scripts for both MT4 and cTrader: 

    1. Introduction to Custom Indicators and Scripts

    1.1. What are Custom Indicators?

    • Definition: Custom indicators are user-defined tools that analyze price data and provide visual or statistical information on a trading chart. 
    • Purpose: They extend the capabilities of the standard indicators provided by MT4 and cTrader, allowing for advanced technical analysis and strategy development. 

    1.2. What are Scripts?

    • Definition: Scripts are programs written to perform a specific function or series of functions on the trading platform. Unlike Expert Advisors (EAs), they execute their tasks once and then stop. 
    • Purpose: Scripts can automate repetitive tasks, such as placing orders or managing trades, and can enhance trading efficiency. 

     2. Creating Custom Indicators in MT4

    2.1. Overview of MetaEditor

    • MetaEditor: The integrated development environment for creating and editing indicators, scripts, and Expert Advisors in MT4. 
    • Access: Open MetaEditor from the MT4 platform by clicking on Tools > MetaQuotes Language Editor or pressing F4. 

    2.2. Basic Structure of an MT4 Indicator

    • Indicator Code Structure: 

    mql4 

    //+——————————————————————+
    //| Custom Indicator                                                 |
    //+——————————————————————+
    #property indicator_separate_window
    #property indicator_buffers 1
    #property indicator_color1 Blue

    double IndicatorBuffer[];

    int OnInit()
    {
        IndicatorBuffers(1);
        SetIndexBuffer(0, IndicatorBuffer);
        return(INIT_SUCCEEDED);
    }

    void OnTick()
    {
        // Indicator calculations
        IndicatorBuffer[0] = SomeCalculation();
    }
     

    2.3. Creating a Simple Moving Average Indicator

    • Code Example: 

    mql4 

    //+——————————————————————+
    //| Custom Moving Average Indicator                                  |
    //+——————————————————————+
    #property indicator_separate_window
    #property indicator_buffers 1
    #property indicator_color1 Red

    extern int MovingAveragePeriod = 14;
    double MovingAverageBuffer[];

    int OnInit()
    {
        IndicatorBuffers(1);
        SetIndexBuffer(0, MovingAverageBuffer);
        return(INIT_SUCCEEDED);
    }

    void OnTick()
    {
        int limit = Bars – MovingAveragePeriod;
        for (int i = 0; i < limit; i++)
        {
            MovingAverageBuffer[i] = iMA(NULL, 0, MovingAveragePeriod, 0, MODE_SMA, PRICE_CLOSE, i);
        }
    }
     

    2.4. Compiling and Testing

    • Compile: Save your code and click on the Compile button in MetaEditor to compile the indicator. 
    • Test: Apply the custom indicator to a chart in MT4 by selecting Insert > Indicators > Custom and choosing your indicator. 

     

    3. Creating Custom Indicators in cTrader

    3.1. Overview of cAlgo

    • cAlgo: The integrated development environment for creating and editing cTrader indicators, robots, and scripts. 
    • Access: Open cAlgo from cTrader by selecting the cAlgo tab. 

    3.2. Basic Structure of a cTrader Indicator

    • Indicator Code Structure: 

    csharp 

    // Define a custom indicator
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class CustomIndicator : Indicator
    {
        [Parameter(“Period”, DefaultValue = 14)]
        public int Period { get; set; }

        [Output(“Custom Indicator”, Color = Colors.Blue)]
        public IndicatorDataSeries OutputSeries { get; set; }

        protected override void Initialize()
        {
            // Initialization code
        }

        public override void Calculate(int index)
        {
            // Indicator calculations
            OutputSeries[index] = SomeCalculation(index);
        }
    }
     

    3.3. Creating a Simple Moving Average Indicator

    • Code Example: 

    csharp 

    // Define a custom moving average indicator
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MovingAverageIndicator : Indicator
    {
        [Parameter(“Period”, DefaultValue = 14)]
        public int Period { get; set; }

        [Output(“Moving Average”, Color = Colors.Red)]
        public IndicatorDataSeries MovingAverageSeries { get; set; }

        protected override void Initialize()
        {
            // Initialization code
        }

        public override void Calculate(int index)
        {
            if (index >= Period)
            {
                double sum = 0;
                for (int i = 0; i < Period; i++)
                {
                    sum += MarketSeries.Close[index – i];
                }
                MovingAverageSeries[index] = sum / Period;
            }
        }
    }
     

    3.4. Compiling and Testing

    • Compile: Save and compile your code in cAlgo using the Compile button. 
    • Test: Apply the custom indicator to a chart in cTrader by selecting Indicators from the cTrader platform and choosing your indicator. 

    4. Creating Custom Scripts

    4.1. MT4 Script Example

    • Script Code: 

    mql4 

    //+——————————————————————+
    //| Custom Script to Place a Buy Order                               |
    //+——————————————————————+
    void OnStart()
    {
        double lotSize = 0.1;
        double price = Ask;
        double stopLoss = price – 100 * Point;
        double takeProfit = price + 100 * Point;
        int slippage = 3;

        int ticket = OrderSend(Symbol(), OP_BUY, lotSize, price, slippage, stopLoss, takeProfit, “Buy Order”, 0, 0, Blue);
        if (ticket < 0)
        {
            Print(“Error opening order: “, GetLastError());
        }
    }
     

    4.2. cTrader Script Example

    • Script Code: 

    csharp 

    // Define a script to place a buy order
    [Script]
    public class PlaceBuyOrder : Script
    {
        [Parameter(“Lot Size”, DefaultValue = 1)]
        public double LotSize { get; set; }

        [Parameter(“Stop Loss (pips)”, DefaultValue = 10)]
        public int StopLossPips { get; set; }

        [Parameter(“Take Profit (pips)”, DefaultValue = 10)]
        public int TakeProfitPips { get; set; }

        public override void Run()
        {
            double price = Symbol.Bid;
            double stopLoss = price – StopLossPips * Symbol.PipSize;
            double takeProfit = price + TakeProfitPips * Symbol.PipSize;

            Trade.CreateMarketOrder(TradeType.Buy, Symbol, LotSize, “Buy Order”, stopLoss, takeProfit);
        }
    }
     

    5. Best Practices

    5.1. Testing and Debugging

    • Backtesting: Use historical data to test your custom indicators and scripts before applying them in live trading environments. 
    • Debugging: Use debugging tools and logging functions to identify and correct errors in your code. 

    5.2. Documentation and Updates

    • Documentation: Keep detailed documentation of your custom indicators and scripts to facilitate updates and maintenance. 
    • Updates: Regularly review and update your code to adapt to changing market conditions and platform updates. 

     

    6. Resources for Learning

    6.1. Official Documentation

    • MT4: Refer to the MetaTrader 4 documentation for detailed information on MQL4 programming. 
    • cTrader: Explore the cTrader Developer documentation for cAlgo programming resources. 

    6.2. Online Communities

    • Forums: Participate in forums and communities such as MQL5.com and cTrader forums to exchange ideas and seek advice. 

     

    Conclusion 

    Custom indicators and scripts are powerful tools for enhancing your trading strategies on MT4 and cTrader. By understanding how to create and use these tools, you can tailor your trading platform to meet your specific needs, automate repetitive tasks, and implement advanced analysis techniques. Leveraging these capabilities effectively can lead to improved trading performance and efficiency. 

    Book a 1-on-1
    Call Session

    Want Nordine's full attention? Nothing compares with a live one on one strategy call! You can express all your concerns and get the best and most straight forward learning experience.

    Related courses