mrMartinGale

Author: Fedor Igumnov

Okay, here's a breakdown of what this MetaTrader script appears to do, explained in a way that someone without programming knowledge can understand:

Overall Purpose:

The script is designed to automatically trade on the Forex market (or other markets supported by MetaTrader). It attempts to predict whether the price of an asset will go up or down, and then places buy or sell orders accordingly. It uses several different technical indicators and a "Martingale" strategy, which is a way to try to recover losses by increasing the size of subsequent trades.

Key Components and Logic:

  1. Inputs/Settings:

    • The script has several settings that you can adjust. These are like knobs and dials that control how the script behaves.
    • Martingale: A switch to turn the Martingale strategy on or off.
    • MartingaleDeep: Determines how aggressively the Martingale strategy will increase trade sizes after a loss. It sets a limit to avoid excessively large trades.
    • DefaultVolume: The initial size of each trade (e.g., 0.1 lots). This is the base amount that the script starts with.
    • StopLoss: The amount of loss (in points) that the script will tolerate on a trade before automatically closing it to limit further losses.
    • TakeProfit: The amount of profit (in points) that the script aims to make on a trade before automatically closing it to secure the gain.
  2. Market Analysis & Trade Signals:

    • The script uses a variety of technical indicators to try and "read" the market and predict price movements. These indicators are essentially mathematical formulas that analyze past price data to identify patterns and potential future trends. The indicators used here are:

      • Pattern Recognition: Tries to identify repeating patterns in the price chart. It looks at sequences of price increases or decreases over short periods to identify a pattern and predict future movement.
      • Acceleration/Deceleration Oscillator (AC): Measures the acceleration and deceleration of the current price trend.
      • Accumulation/Distribution Indicator (AD): Relates price and volume to determine if a security is being accumulated (bought) or distributed (sold).
      • Relative Strength Index (RSI): Measures the speed and change of price movements. RSI values are between 0 and 100. Values near 30 are considered oversold and values near 70 are considered overbought.
      • Bulls Power: Measures the ability of buyers to drive prices up.
      • Newton's Laws Metaphors: These are custom indicators defined by the author, likely using price and volume data to assess market momentum and direction.
      • Energy and Impulse Conservation Metaphors: These are custom indicators that could represent momentum conservation.
    • The script sums the output of each indicator to create a final "ordert" output.

  3. Trading Logic:

    • Based on the signals from these indicators, the script decides whether to place a "buy" order (betting the price will go up) or a "sell" order (betting the price will go down).
    • If the "ordert" is a positive number, the script buys. If negative it sells. If it is zero, then it sells.
  4. Martingale Strategy (Optional):

    • If the Martingale setting is turned on, the script will increase the size of the next trade after a losing trade. The idea is that eventually, a winning trade will occur, and the increased size will recover the previous losses.
    • The MartingaleDeep setting limits how many times the script will double the trade size, to prevent the trade sizes from becoming too large and risking too much of the account balance.
  5. Order Placement:

    • OrderSend: This is the command that actually places the trade with the broker. It specifies the asset to trade, whether to buy or sell, the trade volume (size), and the stop-loss and take-profit levels.

Simplified Flow:

  1. The script starts and initializes its settings.
  2. For each new price update:
    • It analyzes the market using the technical indicators.
    • It combines the indicator signals to determine a trading direction (buy or sell).
    • It places a trade based on the signal.
    • If the Martingale strategy is enabled and the previous trade lost, it doubles the trade size for the next trade (up to the MartingaleDeep limit).

Important Considerations:

  • Risk: The Martingale strategy can be very risky. While it can recover losses quickly, a series of losing trades can lead to very large trade sizes and potentially wipe out an account balance.
  • No Guarantees: No trading script can guarantee profits. Market conditions can change, and even the best strategies can have losing streaks.
  • Backtesting and Demo Testing: It is crucial to thoroughly test any trading script on a demo account (with fake money) and backtest it on historical data before using it with real money. This helps to understand its performance and potential risks.
Orders Execution
It automatically opens orders when conditions are reached
Indicators Used
Bill Williams Accelerator/Decelerator oscillatorAccumulation/Distribution indicatorRelative strength indexBulls Power indicator
3 Views
0 Downloads
0 Favorites
mrMartinGale
//+------------------------------------------------------------------+
//|                                                mrMartinGale.mq4  |
//|                                                    Fedor Igumnov |
//|                                           igumnovfedor@yandex.ru |
//+------------------------------------------------------------------+
#property copyright "Fedor Igumnov"
#property link      "igumnovfedor@yandex.ru"

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
extern bool      Martingale=true;   //Âêëþ÷èòü âûêëþ÷èòü ìàðòèíãåéë.
extern int       MartingaleDeep=8;  //Ìàñêèìàëüíûé ïîêàçàòåëü ñòåïåíè ìàðòèíãåéëà.
extern double    DefaultVolume=0.1; //Îáúåì îðäåðà ïî óìîë÷àíèþ. Ìàêñèìàëüíûé îáúåì êîòîðûé äàñò ìàðòèíãåéë = DefaultVolume*2^MartingaleDeep;
extern double    StopLoss=100;      //Ñòîï ëîññ 5 çíà÷íûå êîòèðîâêè.
extern double    TakeProfit=1000;   //Òåéê ïðîôèò 5 çíà÷íûå êîòèðîâêè.
                                    

double StartVolume;
int bbars;
int init()
{
StartVolume=DefaultVolume;
bbars=Bars;
   return(0);
  }

int deinit()
  {
//----

//----
   return(0);
  }
 
        int Pattern()
        {
            RefreshRates();
            int chastota[16];
            int PrivedQuoteGL[168];
            int three, two, one;
            int i;
            for (i=0;i<168;i++)
            {
            if (Open[i]>Open[i+1])
                PrivedQuoteGL[i]=1;
            if (Open[i]<Open[i+1])
                PrivedQuoteGL[i]=-1;
            }

            three = PrivedQuoteGL[2];
            two = PrivedQuoteGL[1];
            one = PrivedQuoteGL[0];
            for (i = 3; i < 168; i=i+4)
            {
                if ((PrivedQuoteGL[i - 3] == -1) && (PrivedQuoteGL[i - 2] == -1) && (PrivedQuoteGL[i - 1] == -1) && (PrivedQuoteGL[i] == -1))
                    chastota[0]++;
                if ((PrivedQuoteGL[i - 3] == -1) && (PrivedQuoteGL[i - 2] == -1) && (PrivedQuoteGL[i - 1] == -1) && (PrivedQuoteGL[i] == 1))
                    chastota[1]++;
                if ((PrivedQuoteGL[i - 3] == -1) && (PrivedQuoteGL[i - 2] == -1) && (PrivedQuoteGL[i - 1] == 1) && (PrivedQuoteGL[i] == -1))
                    chastota[2]++;
                if ((PrivedQuoteGL[i - 3] == -1) && (PrivedQuoteGL[i - 2] == -1) && (PrivedQuoteGL[i - 1] == 1) && (PrivedQuoteGL[i] == 1))
                    chastota[3]++;
                if ((PrivedQuoteGL[i - 3] == -1) && (PrivedQuoteGL[i - 2] == 1) && (PrivedQuoteGL[i - 1] == -1) && (PrivedQuoteGL[i] == -1))
                    chastota[4]++;
                if ((PrivedQuoteGL[i - 3] == -1) && (PrivedQuoteGL[i - 2] == 1) && (PrivedQuoteGL[i - 1] == -1) && (PrivedQuoteGL[i] == 1))
                    chastota[5]++;
                if ((PrivedQuoteGL[i - 3] == -1) && (PrivedQuoteGL[i - 2] == 1) && (PrivedQuoteGL[i - 1] == 1) && (PrivedQuoteGL[i] == -1))
                    chastota[6]++;
                if ((PrivedQuoteGL[i - 3] == -1) && (PrivedQuoteGL[i - 2] == 1) && (PrivedQuoteGL[i - 1] == 1) && (PrivedQuoteGL[i] == 1))
                    chastota[7]++;
                if ((PrivedQuoteGL[i - 3] == 1) && (PrivedQuoteGL[i - 2] == -1) && (PrivedQuoteGL[i - 1] == -1) && (PrivedQuoteGL[i] == -1))
                    chastota[8]++;
                if ((PrivedQuoteGL[i - 3] == 1) && (PrivedQuoteGL[i - 2] == -1) && (PrivedQuoteGL[i - 1] == -1) && (PrivedQuoteGL[i] == 1))
                    chastota[9]++;
                if ((PrivedQuoteGL[i - 3] == 1) && (PrivedQuoteGL[i - 2] == -1) && (PrivedQuoteGL[i - 1] == 1) && (PrivedQuoteGL[i] == -1))
                    chastota[10]++;
                if ((PrivedQuoteGL[i - 3] == 1) && (PrivedQuoteGL[i - 2] == -1) && (PrivedQuoteGL[i - 1] == 1) && (PrivedQuoteGL[i] == 1))
                    chastota[11]++;
                if ((PrivedQuoteGL[i - 3] == 1) && (PrivedQuoteGL[i - 2] == 1) && (PrivedQuoteGL[i - 1] == -1) && (PrivedQuoteGL[i] == -1))
                    chastota[12]++;
                if ((PrivedQuoteGL[i - 3] == 1) && (PrivedQuoteGL[i - 2] == 1) && (PrivedQuoteGL[i - 1] == -1) && (PrivedQuoteGL[i] == 1))
                    chastota[13]++;
                if ((PrivedQuoteGL[i - 3] == 1) && (PrivedQuoteGL[i - 2] == 1) && (PrivedQuoteGL[i - 1] == 1) && (PrivedQuoteGL[i] == -1))
                    chastota[14]++;
                if ((PrivedQuoteGL[i - 3] == 1) && (PrivedQuoteGL[i - 2] == -1) && (PrivedQuoteGL[i - 1] == 1) && (PrivedQuoteGL[i] == 1))
                    chastota[15]++;
            }

            if ((one==1) && (two==1) && (three==1))
            {
                if (chastota[14]>chastota[15])
                    return (-1);
                if (chastota[14]<chastota[15])
                    return (1);
            }
                        if ((one==1) && (two==1) && (three==-1))
            {
                if (chastota[12]>chastota[13])
                    return (-1);
                if (chastota[12]<chastota[13])
                    return (1);
            }
                        if ((one==1) && (two==-1) && (three==1))
            {
                if (chastota[10]>chastota[11])
                    return (-1);
                if (chastota[10]<chastota[11])
                    return (1);
            }
                        if ((one==1) && (two==-1) && (three==-1))
            {
                if (chastota[8]>chastota[9])
                    return (-1);
                if (chastota[8]<chastota[9])
                    return (1);
            }
                        if ((one==-1) && (two==1) && (three==1))
            {
                if (chastota[6]>chastota[7])
                    return (-1);
                if (chastota[6]<chastota[7])
                    return (1);
            }
                        if ((one==-1) && (two==1) && (three==-1))
            {
                if (chastota[4]>chastota[5])
                    return (-1);
                if (chastota[4]<chastota[5])
                    return (1);
            }
                        if ((one==-1) && (two==-1) && (three==1))
            {
                if (chastota[2]>chastota[3])
                    return (-1);
                if (chastota[2]<chastota[3])
                   return (1);
            }
                        if ((one==-1) && (two==-1) && (three==-1))
            {
                if (chastota[0]>chastota[1])
                   return (-1);
                if (chastota[0]<chastota[1])
                    return (1);
            }

         
        }
      int NewtonLawAC()
        {
            
           if (iAC(Symbol(),0,0)*Volume[1]<iAC(Symbol(),0,0)*Volume[2])
                 return (1);
           if (iAC(Symbol(),0,0)*Volume[1]>iAC(Symbol(),0,0)*Volume[2])
              return (-1);
         
        }
      int Trendy()
        {
            
           if (Close[1]<Open[1])
                 return (1);
           if (Close[1]>Open[1])
              return (-1);
         
        }
       int NewtonLawAD()
        {
            
           if (iAD(Symbol(),0,0)*Volume[1]<iAD(Symbol(),0,0)*Volume[2])
              return (1);
           if (iAD(Symbol(),0,0)*Volume[1]>iAD(Symbol(),0,0)*Volume[2])
              return (-1);
        }      
        int NewtonLaw()
        {   
           if (Close[1]*Volume[1]<Open[1]*Volume[2])
              return (1);
           if (Close[1]*Volume[1]>Open[1]*Volume[2])
              return (-1);        
        }  
        int EnergyConservation()
        {
        double H1,V1,g1,H2,V2,g2;
        V1=iRSI(NULL,0,14,PRICE_CLOSE,1);
        g1=iAC(Symbol(),0,1);
        H1=V1*V1/2*g1;
        V2=iRSI(NULL,0,14,PRICE_CLOSE,2);
        g2=iAC(Symbol(),0,2);       
        H2=V2*V2/2*g2;
        if (H1>H2)
        return(1);
          
        if (H1<H2)
        return(-1);      
        }
        int ImpulseConservation()
        {
        double m1,m2,V1,V2,U,m;
        m=Volume[0]+Volume[1];
        m1=Volume[1];
        m2=Volume[0];
        V1=iBullsPower(Symbol(),0,14,PRICE_CLOSE,0);
        U=(m1*V1+m2*V2)/m;
        if (U>0)
        return(1);
        if (U<0)
        return(-1);
        }
        int RSIm()
        {
             double r1; 
            
             double r2; 
              r1= iRSI(Symbol(),0,14,PRICE_CLOSE,1);
              r2= iRSI(Symbol(),0,14,PRICE_CLOSE,2);
             if (r2<29 && r1>29)
             return (1);
             if (r2>71 && r1<71) 
             return (-1);
        } 
           
        
int start()
  {
//----
   int ordert=666;
   if (bbars!=Bars)
   {
     bbars=Bars; 
    ordert=NewtonLaw()+Trendy()+Pattern()+RSIm()+NewtonLawAD()+NewtonLawAC()+EnergyConservation()+ImpulseConservation();
    
    }
    if (ordert==0)
    {  
    if (Martingale==true)
    {
    DefaultVolume=DefaultVolume*2;
    if (DefaultVolume>StartVolume*MathPow(2,MartingaleDeep))
    DefaultVolume=StartVolume;
    }  
    RefreshRates();
    OrderSend(Symbol(),OP_SELL,TakeProfit,Bid,20,Bid+StopLoss*Point,Ask-TakeProfit*Point);
    //OrderSend(Symbol(),OP_BUY,DefaultVolume,Ask,20,Bid-StopLoss*Point,Ask+TakeProfit*Point);
      ordert=666;   
    }
        if (ordert==1)
    {  
    if (Martingale==true)
    {
    DefaultVolume=DefaultVolume*2;
    if (DefaultVolume>StartVolume*MathPow(2,MartingaleDeep))
    DefaultVolume=StartVolume;
    }  
    RefreshRates();
   // OrderSend(Symbol(),OP_SELL,DefaultVolume,Bid,20,Bid+StopLoss*Point,Ask-TakeProfit*Point);
    OrderSend(Symbol(),OP_BUY,DefaultVolume,Ask,20,Bid-StopLoss*Point,Ask+TakeProfit*Point);
      ordert=666;   
    }
        
    if (ordert==2)
    {  
    if (Martingale==true)
    {
    DefaultVolume=DefaultVolume*2;
    if (DefaultVolume>StartVolume*MathPow(2,MartingaleDeep))
    DefaultVolume=StartVolume;
    }  
    RefreshRates();
   // OrderSend(Symbol(),OP_SELL,DefaultVolume,Bid,20,Bid+StopLoss*Point,Ask-TakeProfit*Point);
    OrderSend(Symbol(),OP_BUY,DefaultVolume,Ask,20,Bid-StopLoss*Point,Ask+TakeProfit*Point);
      ordert=666;   
    }
    if (ordert==3)
    {  
    if (Martingale==true)
    {
    DefaultVolume=DefaultVolume*2;
    if (DefaultVolume>StartVolume*MathPow(2,MartingaleDeep))
    DefaultVolume=StartVolume;
    }  
    RefreshRates();
    //OrderSend(Symbol(),OP_SELL,DefaultVolume,Bid,20,Bid+StopLoss*Point,Ask-TakeProfit*Point);
     OrderSend(Symbol(),OP_BUY,DefaultVolume,Ask,20,Bid-StopLoss*Point,Ask+TakeProfit*Point);
      ordert=666;   
    }
        if (ordert==4)
    {  
    if (Martingale==true)
    {
    DefaultVolume=DefaultVolume*2;
    if (DefaultVolume>StartVolume*MathPow(2,MartingaleDeep))
    DefaultVolume=StartVolume;
    }  
    RefreshRates();
    OrderSend(Symbol(),OP_BUY,DefaultVolume,Ask,20,Bid-StopLoss*Point,Ask+TakeProfit*Point);
   //OrderSend(Symbol(),OP_SELL,DefaultVolume,Bid,20,Bid+StopLoss*Point,Ask-TakeProfit*Point);
      ordert=666;   
    }
        if (ordert==5)
    {  
    if (Martingale==true)
    {
    DefaultVolume=DefaultVolume*2;
    if (DefaultVolume>StartVolume*MathPow(2,MartingaleDeep))
    DefaultVolume=StartVolume;
    }  
    RefreshRates();
    OrderSend(Symbol(),OP_BUY,DefaultVolume,Ask,20,Bid-StopLoss*Point,Ask+TakeProfit*Point);
   // OrderSend(Symbol(),OP_SELL,DefaultVolume,Bid,20,Bid+StopLoss*Point,Ask-TakeProfit*Point);
      ordert=666;   
    }
        if (ordert==6)
    {  
    if (Martingale==true)
    {
    DefaultVolume=DefaultVolume*2;
    if (DefaultVolume>StartVolume*MathPow(2,MartingaleDeep))
    DefaultVolume=StartVolume;
    }  
    RefreshRates();
    OrderSend(Symbol(),OP_BUY,DefaultVolume,Ask,20,Bid-StopLoss*Point,Ask+TakeProfit*Point);
    //OrderSend(Symbol(),OP_SELL,DefaultVolume,Bid,20,Bid+StopLoss*Point,Ask-TakeProfit*Point);
      ordert=666;   
    }
        if (ordert==7)
    {  
    if (Martingale==true)
    {
    DefaultVolume=DefaultVolume*2;
    if (DefaultVolume>StartVolume*MathPow(2,MartingaleDeep))
    DefaultVolume=StartVolume;
    }  
    RefreshRates();
    OrderSend(Symbol(),OP_BUY,DefaultVolume,Ask,20,Bid-StopLoss*Point,Ask+TakeProfit*Point);
    //OrderSend(Symbol(),OP_SELL,DefaultVolume,Bid,20,Bid+StopLoss*Point,Ask-TakeProfit*Point);
      ordert=666;   
    }
        if (ordert==8)
    {  
    if (Martingale==true)
    {
    DefaultVolume=DefaultVolume*2;
    if (DefaultVolume>StartVolume*MathPow(2,MartingaleDeep))
    DefaultVolume=StartVolume;
    }  
    RefreshRates();
    OrderSend(Symbol(),OP_BUY,DefaultVolume,Ask,20,Bid-StopLoss*Point,Ask+TakeProfit*Point);
   // OrderSend(Symbol(),OP_SELL,DefaultVolume,Bid,20,Bid+StopLoss*Point,Ask-TakeProfit*Point);
      ordert=666;   
    }
        if (ordert==-1)
    {  
    if (Martingale==true)
    {
    DefaultVolume=DefaultVolume*2;
    if (DefaultVolume>StartVolume*MathPow(2,MartingaleDeep))
    DefaultVolume=StartVolume;
    }  
    RefreshRates();
    //OrderSend(Symbol(),OP_BUY,DefaultVolume,Ask,20,Bid-StopLoss*Point,Ask+TakeProfit*Point);
   OrderSend(Symbol(),OP_SELL,DefaultVolume,Bid,20,Bid+StopLoss*Point,Ask-TakeProfit*Point);
      ordert=666;   
    }
        if (ordert==-2)
    {  
    if (Martingale==true)
    {
    DefaultVolume=DefaultVolume*2;
    if (DefaultVolume>StartVolume*MathPow(2,MartingaleDeep))
    DefaultVolume=StartVolume;
    }  
    RefreshRates();
   // OrderSend(Symbol(),OP_BUY,DefaultVolume,Ask,20,Bid-StopLoss*Point,Ask+TakeProfit*Point);
    OrderSend(Symbol(),OP_SELL,DefaultVolume,Bid,20,Bid+StopLoss*Point,Ask-TakeProfit*Point);
      ordert=666;   
    }
        if (ordert==-3)
    {  
    if (Martingale==true)
    {
    DefaultVolume=DefaultVolume*2;
    if (DefaultVolume>StartVolume*MathPow(2,MartingaleDeep))
    DefaultVolume=StartVolume;
    }  
    RefreshRates();
    //OrderSend(Symbol(),OP_BUY,DefaultVolume,Ask,20,Bid-StopLoss*Point,Ask+TakeProfit*Point);
    OrderSend(Symbol(),OP_SELL,DefaultVolume,Bid,20,Bid+StopLoss*Point,Ask-TakeProfit*Point);
      ordert=666;   
    }
        if (ordert==-4)
    {  
    if (Martingale==true)
    {
    DefaultVolume=DefaultVolume*2;
    if (DefaultVolume>StartVolume*MathPow(2,MartingaleDeep))
    DefaultVolume=StartVolume;
    }  
    RefreshRates();
   // OrderSend(Symbol(),OP_BUY,DefaultVolume,Ask,20,Bid-StopLoss*Point,Ask+TakeProfit*Point);
    OrderSend(Symbol(),OP_SELL,DefaultVolume,Bid,20,Bid+StopLoss*Point,Ask-TakeProfit*Point);
      ordert=666;   
    }
        if (ordert==-5)
    {  
    if (Martingale==true)
    {
    DefaultVolume=DefaultVolume*2;
    if (DefaultVolume>StartVolume*MathPow(2,MartingaleDeep))
    DefaultVolume=StartVolume;
    }  
    RefreshRates();
    //OrderSend(Symbol(),OP_BUY,DefaultVolume,Ask,20,Bid-StopLoss*Point,Ask+TakeProfit*Point);
    OrderSend(Symbol(),OP_SELL,DefaultVolume,Bid,20,Bid+StopLoss*Point,Ask-TakeProfit*Point);
      ordert=666;   
      
    }
        if (ordert==-6)
    {  
    if (Martingale==true)
    {
    DefaultVolume=DefaultVolume*2;
    if (DefaultVolume>StartVolume*MathPow(2,MartingaleDeep))
    DefaultVolume=StartVolume;
    }  
    RefreshRates();
   // OrderSend(Symbol(),OP_BUY,DefaultVolume,Ask,20,Bid-StopLoss*Point,Ask+TakeProfit*Point);
    OrderSend(Symbol(),OP_SELL,DefaultVolume,Bid,20,Bid+StopLoss*Point,Ask-TakeProfit*Point);
      ordert=666;   
    }
        if (ordert==-7)
    {  
    if (Martingale==true)
    {
    DefaultVolume=DefaultVolume*2;
    if (DefaultVolume>StartVolume*MathPow(2,MartingaleDeep))
    DefaultVolume=StartVolume;
    }  
    RefreshRates();
   // OrderSend(Symbol(),OP_BUY,DefaultVolume,Ask,20,Bid-StopLoss*Point,Ask+TakeProfit*Point);
    OrderSend(Symbol(),OP_SELL,DefaultVolume,Bid,20,Bid+StopLoss*Point,Ask-TakeProfit*Point);
      ordert=666;   
    }
        if (ordert==-8)
    {  
    if (Martingale==true)
    {
    DefaultVolume=DefaultVolume*2;
    if (DefaultVolume>StartVolume*MathPow(2,MartingaleDeep))
    DefaultVolume=StartVolume;
    }  
    RefreshRates();
    //OrderSend(Symbol(),OP_BUY,DefaultVolume,Ask,20,Bid-StopLoss*Point,Ask+TakeProfit*Point);
    OrderSend(Symbol(),OP_SELL,DefaultVolume,Bid,20,Bid+StopLoss*Point,Ask-TakeProfit*Point);
      ordert=666;   
    }
                                                                        
//----
   return(0);
  }

//+---------

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---