mrMartinGale_Update1

Author: Fedor Igumnov

The given code appears to be a trading algorithm written in MQL4 for MetaTrader 4. This type of script is designed to automate trading decisions based on certain criteria and signals generated by various functions (Signal1, Signal2, etc.). Below, I'll provide an overview of how this script works and highlight some key aspects:

Key Components

  1. Global Variables: The script defines several global variables such as DefaultVolume, Counter1, LastAccountBalance, and others that are used to maintain state across different executions.

  2. Signals Functions (Signal1 to Signal8): These functions presumably implement various trading indicators or logic to generate signals for buying or selling. Each function returns an integer value, which contributes to the overall trading decision.

  3. Martingale Strategy: This script includes a Martingale approach that adjusts trade volume based on past performance and defined strategies (MartingaleType).

  4. Kelly Criterion: When KellyOn is true, it applies the Kelly criterion to adjust the trade size dynamically based on calculated probabilities of winning/losing.

  5. Decision Making: The function decision(int signal) determines whether to place a buy or sell order based on the comparison between long and short statistics (LongStats, ShortStats) for the given signal.

  6. Order Placement: Orders are placed using OrderSend() with parameters that depend on current market prices (Ask, Bid), stop loss, take profit values, and whether an inversion strategy is used.

Execution Flow

  • The script executes within a loop in the start() function (although not explicitly named here, it's common for such scripts to have a start function).

  • It updates counters and checks account balance changes to adjust strategies (MartingaleType, volume adjustments).

  • Combines signals from multiple functions into a single decision point (Result).

  • Calls decision(Result) to potentially place orders based on calculated statistics.

Important Considerations

  1. Risk Management: Ensure that stop loss and take profit values are set appropriately for your risk tolerance.

  2. Volume Adjustments: The script dynamically adjusts trade volumes, which can lead to significant risks if not carefully monitored.

  3. Testing: Before deploying this script in a live environment, extensive backtesting should be conducted to evaluate its performance under various market conditions.

  4. Error Handling: Consider adding error handling for order placement failures or network issues.

  5. Market Conditions: The effectiveness of signals depends on current and historical market data; ensure your indicators are well-calibrated.

  6. Regulatory Compliance: Make sure that automated trading aligns with the regulatory requirements in your jurisdiction.

This script provides a framework for implementing a trading strategy, but its success will largely depend on how well it is tuned to specific markets and conditions.

Orders Execution
It automatically opens orders when conditions are reached
Indicators Used
Bill Williams Accelerator/Decelerator oscillatorAccumulation/Distribution indicatorRelative strength indexBulls Power indicator
4 Views
0 Downloads
0 Favorites
mrMartinGale_Update1
//+------------------------------------------------------------------+
//|                                                mrMartinGale.mq4  |
//|                                                    Fedor Igumnov |
//|                                           igumnovfedor@yandex.ru |
//+------------------------------------------------------------------+
#property copyright "Fedor Igumnov"
#property link      "igumnovfedor@yandex.ru"

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
   
extern int       MartingaleDeep=8;  //Ìàñêèìàëüíûé ïîêàçàòåëü ñòåïåíè ìàðòèíãåéëà.
extern double    DefaultVolume=0.1; //Îáúåì îðäåðà ïî óìîë÷àíèþ. Ìàêñèìàëüíûé îáúåì êîòîðûé äàñò ìàðòèíãåéë = DefaultVolume*2^MartingaleDeep;
extern double    StopLoss=100;      //Ñòîï ëîññ 5 çíà÷íûå êîòèðîâêè.
extern double    TakeProfit=1000;   //Òåéê ïðîôèò 5 çíà÷íûå êîòèðîâêè.
extern bool Inversion=false;        //Âêëþ÷èòü âûêëþ÷èòü èíâåðñèþ îðäåðîâ.
extern bool KellyOn=true;           //Âêëþ÷èòü âûêëþ÷èòü êîððåêöèþ îáúåìîâ ïî ôîðìóëå Êåëëè. 
int LongStats[16];                                  
int ShortStats[16];
double MaxVolume;
double MinVolume;
double StartVolume;
double LastAccountBalance;
double KellyCorrection=1;

double TotalWinCounter=1;
double TotalLooseCounter=1;
int MartingaleType=0;
int bbars;
int Counter1=0;
int Counter2=0;
int Result=0;
int init()
{
LastAccountBalance=AccountBalance();
for (int i=0;i<17;i++)
{
LongStats[i]=2;
ShortStats[i]=1;
}
MinVolume=DefaultVolume;
MaxVolume=DefaultVolume*MathPow(2,MartingaleDeep-1);
bbars=Bars;
   return(0);
  }

int deinit()
  {
//----

//----
   return(0);
  }
 
        int Signal1()
        {
            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 Signal2()
        {
            
           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 Signal3()
        {
            
           if (Close[1]<Open[1])
                 return (1);
           if (Close[1]>Open[1])
              return (-1);
         
        }
       int Signal4()
        {
            
           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 Signal5()
        {   
           if (Close[1]*Volume[1]<Open[1]*Volume[2])
              return (1);
           if (Close[1]*Volume[1]>Open[1]*Volume[2])
              return (-1);        
        }  
        int Signal6()
        {
        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 Signal7()
        {
        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 Signal8()
        {
             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 buff,LastResult;
  double P,Q,A,B;
   if ((bbars!=Bars)&&(Time[0]-TimeCurrent()==0))
   {
  //OrderSelect(0,SELECT_BY_POS,MODE_TRADES);  
  // OrderClose(OrderTicket(),OrderLots(),Bid,20);
  //OrderClose(OrderTicket(),OrderLots(),Ask,20);
   Counter2++;
   bbars=Bars; 
   if (AccountBalance()>LastAccountBalance)
   TotalWinCounter++;
   if (AccountBalance()<LastAccountBalance)
   TotalLooseCounter++;  
   P=TotalWinCounter/(TotalWinCounter+TotalLooseCounter);
   Q=1-P;
   A=(TakeProfit +StopLoss)/TakeProfit;
   B=(TakeProfit +StopLoss)/StopLoss;
   KellyCorrection=MathAbs((P/B)-(Q/A));

    if (MartingaleType ==0)
    {
      if (Counter1==1)
         DefaultVolume=MinVolume;      
      if (AccountBalance()>LastAccountBalance)
      {
         DefaultVolume=DefaultVolume*2;
       }
    }
    if (MartingaleType ==1)
    {
        if (Counter1==1)
            DefaultVolume=MaxVolume;   
        if (AccountBalance()>LastAccountBalance)
        {
            DefaultVolume=DefaultVolume/2;
        }
    }
    if (MartingaleType ==2)
    {
        if (Counter1==1)
            DefaultVolume=MinVolume;
        if (AccountBalance()<LastAccountBalance)
        {
         DefaultVolume=DefaultVolume*2;
        }
       
    }
    if (MartingaleType ==3)
    {
        if (Counter1==1)
            DefaultVolume=MaxVolume;
        if (AccountBalance()<LastAccountBalance)
        {
            DefaultVolume=DefaultVolume/2;
      
        }
    }
    if (MartingaleType ==4)
    {
      if (Counter1==1)
         DefaultVolume=MinVolume;
      if (AccountBalance()>LastAccountBalance)
      {
         DefaultVolume=DefaultVolume*2;
     
      }
      if (AccountBalance()<LastAccountBalance)
      {
         DefaultVolume=DefaultVolume/2;  
      }
      
    }
    if (MartingaleType ==5)
   {
      if (Counter1==1)
         DefaultVolume=MinVolume;
      if (AccountBalance()>LastAccountBalance)
      {
         DefaultVolume=DefaultVolume/2;
      
      }
      if (AccountBalance()<LastAccountBalance)
      {
         DefaultVolume=DefaultVolume*2; 
      }
      
    }
    if (MartingaleType ==6)
    {
         if (Counter1==1)
         DefaultVolume=MinVolume;
         DefaultVolume=DefaultVolume*2;
    }
    if (MartingaleType ==7)
    {
         if (Counter1==1)
         DefaultVolume=MaxVolume;
         DefaultVolume=DefaultVolume/2;   
    }
    LastResult=Result;
    if ((Close[1]-Open[1])>0)
    {
    buff=LastResult+8;
    LongStats[buff]++;
    }
    if ((Close[1]-Open[1])<0)
    {
    buff=LastResult+8;
    ShortStats[buff]++;
    }
    if (KellyOn==true)
    {
    if ((Counter2>100)&&(KellyCorrection<0.9)&&(KellyCorrection>0.1))
    DefaultVolume=DefaultVolume*KellyCorrection;
    }
    Result=Signal1()+Signal2()+Signal3()+Signal4()+Signal5()+Signal6()+Signal7()+Signal8();
    
    decision(Result);
    LastAccountBalance=AccountBalance();
    if ((MartingaleType==7)&&(Counter1==MartingaleDeep))
    MartingaleType=0;
    if (Counter1==MartingaleDeep)
    {
    MartingaleType++;
    Counter1=0;
    }
   Counter1++;
   }

    
    
 
  
                                                                        
//----
   return(0);
  }
int decision(int signal)
{
if (LongStats[signal+8]>ShortStats[signal+8])
{
    RefreshRates();
    if (Inversion==false)
    OrderSend(Symbol(),OP_BUY,DefaultVolume,Ask,20,Bid-StopLoss*Point,Ask+TakeProfit*Point);
     if (Inversion==true)
     OrderSend(Symbol(),OP_SELL,DefaultVolume,Bid,20,Bid+StopLoss*Point,Ask-TakeProfit*Point);
}
if (LongStats[signal+8]<ShortStats[signal+8])
{
    RefreshRates();
    if (Inversion==false)
    OrderSend(Symbol(),OP_SELL,DefaultVolume,Bid,20,Bid+StopLoss*Point,Ask-TakeProfit*Point);
      if (Inversion==true)
    OrderSend(Symbol(),OP_BUY,DefaultVolume,Ask,20,Bid-StopLoss*Point,Ask+TakeProfit*Point);   
}
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 ---