Author: Copyright � 2007

This code snippet appears to be part of an automated trading strategy, likely written in MQL4 (MetaQuotes Language 4), used for trading on the MetaTrader platform. Let's break down what it does, section by section, and discuss potential improvements.

Overall Purpose

The code calculates a trading signal based on a combination of several variables (u for "up" and d for "down", representing upward and downward trends, respectively) and then assigns a Signal value based on thresholds. The comments are in Russian, indicating the strategy is likely intended for a Russian-speaking audience.

1. Variable Initialization and Initial Logic (Lines 1-10)

  • Signal=0; Comment("Íå ðåêîìåíäóåòñÿ îòêðûâàòü ïîçèöèè. ÆÄÈÒÅ.");
    • Initializes the Signal variable to 0, which means "no recommendation" or "wait." The comment reinforces this.

2. Trend Calculation (Lines 11-30)

  • This section calculates u1x5v, u1x8v, etc., and d1x5v, d1x8v, etc. The variable names suggest these are upward and downward trend indicators calculated over different timeframes (5, 8, 13, 21, 34 periods, and a "combined" indicator u1acv and d1acv).
  • The logic for setting u1x5v, u1x8v, etc., and d1x5v, d1x8v, etc., is based on comparisons between variables. The code uses if statements to assign values based on these comparisons. The specific logic is not immediately clear without knowing how the original variables are calculated.
  • The code uses || (OR) and && (AND) operators to combine conditions.

3. Signal Assignment (Lines 31-60)

  • This is the core of the signal generation. It checks combinations of the calculated uitog1v, uitog2v, and uitog3v (likely representing the sum of the upward trend variables for each group) and ditog1v, ditog2v, and ditog3v (the sum of the downward trend variables).
  • The thresholds (50, 75) determine the strength of the signal.
  • The comments provide Russian-language explanations of the signals.

Potential Improvements and Considerations

  1. Clarity and Readability:

    • Variable Names: The variable names are cryptic. While u and d are somewhat understandable, the numbers (5, 8, 13, 21, 34) and the acv suffix are not. More descriptive names would significantly improve readability. For example, upwardTrend5, downwardTrend8, combinedUpwardTrend.
    • Comments: While the Russian comments are helpful for the intended audience, adding English comments alongside would make the code more accessible to a wider range of developers.
    • Code Formatting: Consistent indentation and spacing would improve readability.
  2. Logic Simplification:

    • The nested if statements can be complex to follow. Consider refactoring the logic to use more concise expressions or helper functions.
    • The repeated checks for uitog1v, uitog2v, and uitog3v and ditog1v, ditog2v, and ditog3v could be consolidated into a function.
  3. Robustness:

    • Input Parameters: The thresholds (50, 75) are hardcoded. Make them input parameters so they can be easily adjusted without modifying the code.
    • Error Handling: Consider adding checks to ensure that the input variables are valid (e.g., not NaN or infinite).
    • Timeframe Consistency: Ensure that the timeframes used for calculating the trend indicators are consistent.
  4. Strategy Evaluation:

    • Backtesting: Thoroughly backtest the strategy on historical data to evaluate its performance.
    • Optimization: Optimize the input parameters (thresholds, timeframes) to maximize profitability and minimize risk.
    • Risk Management: Implement proper risk management techniques, such as stop-loss orders and position sizing.
  5. Understanding the Underlying Indicators:

    • The most important aspect is to understand how the u1x5v, u1x8v, etc., and d1x5v, d1x8v, etc., are calculated. Without this knowledge, it's difficult to assess the validity of the strategy.

Example of Improved Code (Illustrative)

//+------------------------------------------------------------------+
//|  Trading Strategy                                                |
//+------------------------------------------------------------------+
//|  This strategy calculates a trading signal based on upward and   |
//|  downward trend indicators.                                     |
//+------------------------------------------------------------------+

// Input parameters
input int upwardTrendThreshold = 50;   // Threshold for upward trend signal
input int downwardTrendThreshold = 50; // Threshold for downward trend signal

// Calculate trend indicators (replace with actual calculations)
double upwardTrend5 = CalculateUpwardTrend(5);
double upwardTrend8 = CalculateUpwardTrend(8);
double downwardTrend5 = CalculateDownwardTrend(5);
double downwardTrend8 = CalculateDownwardTrend(8);

// Calculate total scores
double totalUpwardScore = upwardTrend5 + upwardTrend8;
double totalDownwardScore = downwardTrend5 + downwardTrend8;

// Determine trading signal
int signal = 0; // No recommendation

if (totalUpwardScore > upwardTrendThreshold) {
    signal = 1; // Buy signal
} else if (totalDownwardScore > downwardTrendThreshold) {
    signal = -1; // Sell signal
} else {
    signal = 0; // No recommendation
}

// Output signal
Print("Signal: ", signal);

// Function to calculate upward trend (replace with actual calculation)
double CalculateUpwardTrend(int period) {
    // Your upward trend calculation logic here
    return 25.0; // Example value
}

// Function to calculate downward trend (replace with actual calculation)
double CalculateDownwardTrend(int period) {
    // Your downward trend calculation logic here
    return 15.0; // Example value
}

Key Takeaways

  • The code is a basic trading strategy that combines trend indicators to generate signals.
  • Improving readability and maintainability is crucial.
  • Thorough backtesting and optimization are essential for evaluating the strategy's performance.
  • Understanding the underlying indicators is key to assessing the strategy's validity.
  • Consider adding input parameters for flexibility and risk management techniques for safety.
  • Translate comments to English for wider understanding.

Remember to replace the placeholder calculations with the actual logic used in your trading strategy. Good luck!

18 Views
0 Downloads
0 Favorites
5matf
//+------------------------------------------------------------------+
//|                                                     5matf.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007"
#property link      ""

extern int StopLoss=0;
extern int TakeProfit=10;
extern int TrailingStop=0;
extern double Lots=0.1;
extern int Slippage=3;

extern int OpenLevel=0;//Óðîâåíü îòêðûòèÿ 0 èëè 1
extern int CloseLevel=1;//Óðîâåíü çàêðûòèÿ 0 èëè 1

extern int TF1 = 15;
extern int TF2 = 60;
extern int TF3 = 240;
extern int maTrendPeriodv_1 = 5;
extern int maTrendPeriodv_2 = 8;
extern int maTrendPeriodv_3 = 13;
extern int maTrendPeriodv_4 = 21;
extern int maTrendPeriodv_5 = 34;

int Signal;
double SL,TP;
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   Comment("");
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
TREND_alexcud();
int Total=0;
for(int cnt=0;cnt<OrdersTotal();cnt++)
   {
   OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
   if(OrderSymbol()==Symbol())
      {
      Total++;
      if(OrderType()==OP_BUY)
         {
         if(Signal<-CloseLevel)
            {
            OrderClose(OrderTicket(),OrderLots(),Bid,Slippage);
            return(0);
            }
         if(TrailingStop>0
         && Bid-OrderOpenPrice()>Point*TrailingStop
         && OrderStopLoss()<Bid-Point*TrailingStop)
            {
            OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0);
            return(0);
            }
         }
      if(OrderType()==OP_SELL)
         {
         if(Signal>CloseLevel)
            {
            OrderClose(OrderTicket(),OrderLots(),Ask,Slippage);
            return(0);
            }
         if(TrailingStop>0
         && OrderOpenPrice()-Ask>Point*TrailingStop
         && (OrderStopLoss()>Ask+Point*TrailingStop || OrderStopLoss()==0))
            {
            OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0);
            return(0);
            }
         }
      }
   }
if(Total==0)
   {
   if(Signal>OpenLevel)
      {
      SL=0;TP=0;
      if(StopLoss>0)   SL=Ask-Point*StopLoss;
      if(TakeProfit>0) TP=Ask+Point*TakeProfit;
      OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,SL,TP,NULL,0,0);
      return(0);
      }
   if(Signal<OpenLevel)
      {
      SL=0;TP=0;
      if(StopLoss>0)   SL=Bid+Point*StopLoss;
      if(TakeProfit>0) TP=Bid-Point*TakeProfit;
      OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,SL,TP,NULL,0,0);
      return(0);
      }
   }
  return(0);
}
//+------------------------------------------------------------------+
void TREND_alexcud()
   {
double MaH11v,  MaH41v, MaD11v, MaH1pr1v, MaH4pr1v, MaD1pr1v;
double MaH12v,  MaH42v, MaD12v, MaH1pr2v, MaH4pr2v, MaD1pr2v;
double MaH13v,  MaH43v, MaD13v, MaH1pr3v, MaH4pr3v, MaD1pr3v;
double MaH14v,  MaH44v, MaD14v, MaH1pr4v, MaH4pr4v, MaD1pr4v;
double MaH15v,  MaH45v, MaD15v, MaH1pr5v, MaH4pr5v, MaD1pr5v;

double u1x5v, u1x8v, u1x13v, u1x21v, u1x34v;
double u2x5v, u2x8v, u2x13v, u2x21v, u2x34v;
double u3x5v, u3x8v, u3x13v, u3x21v, u3x34v;
double u1acv, u2acv, u3acv;

double d1x5v, d1x8v, d1x13v, d1x21v, d1x34v;
double d2x5v, d2x8v, d2x13v, d2x21v, d2x34v;
double d3x5v, d3x8v, d3x13v, d3x21v, d3x34v;
double d1acv, d2acv, d3acv;

MaH11v=iMA(NULL,TF1,maTrendPeriodv_1,0,MODE_SMA,PRICE_CLOSE,0);   MaH1pr1v=iMA(NULL,TF1,maTrendPeriodv_1,0,MODE_SMA,PRICE_CLOSE,1);
MaH12v=iMA(NULL,TF1,maTrendPeriodv_2,0,MODE_SMA,PRICE_CLOSE,0);   MaH1pr2v=iMA(NULL,TF1,maTrendPeriodv_2,0,MODE_SMA,PRICE_CLOSE,1);
MaH13v=iMA(NULL,TF1,maTrendPeriodv_3,0,MODE_SMA,PRICE_CLOSE,0);   MaH1pr3v=iMA(NULL,TF1,maTrendPeriodv_3,0,MODE_SMA,PRICE_CLOSE,1);
MaH14v=iMA(NULL,TF1,maTrendPeriodv_4,0,MODE_SMA,PRICE_CLOSE,0);   MaH1pr4v=iMA(NULL,TF1,maTrendPeriodv_4,0,MODE_SMA,PRICE_CLOSE,1);
MaH15v=iMA(NULL,TF1,maTrendPeriodv_5,0,MODE_SMA,PRICE_CLOSE,0);   MaH1pr5v=iMA(NULL,TF1,maTrendPeriodv_5,0,MODE_SMA,PRICE_CLOSE,1);
   
MaH41v=iMA(NULL,TF2,maTrendPeriodv_1,0,MODE_SMA,PRICE_CLOSE,0);   MaH4pr1v=iMA(NULL,TF2,maTrendPeriodv_1,0,MODE_SMA,PRICE_CLOSE,1);
MaH42v=iMA(NULL,TF2,maTrendPeriodv_2,0,MODE_SMA,PRICE_CLOSE,0);   MaH4pr2v=iMA(NULL,TF2,maTrendPeriodv_2,0,MODE_SMA,PRICE_CLOSE,1);
MaH43v=iMA(NULL,TF2,maTrendPeriodv_3,0,MODE_SMA,PRICE_CLOSE,0);   MaH4pr3v=iMA(NULL,TF2,maTrendPeriodv_3,0,MODE_SMA,PRICE_CLOSE,1);
MaH44v=iMA(NULL,TF2,maTrendPeriodv_4,0,MODE_SMA,PRICE_CLOSE,0);   MaH4pr4v=iMA(NULL,TF2,maTrendPeriodv_4,0,MODE_SMA,PRICE_CLOSE,1);
MaH45v=iMA(NULL,TF2,maTrendPeriodv_5,0,MODE_SMA,PRICE_CLOSE,0);   MaH4pr5v=iMA(NULL,TF2,maTrendPeriodv_5,0,MODE_SMA,PRICE_CLOSE,1);
     
MaD11v=iMA(NULL,TF3,maTrendPeriodv_1,0,MODE_SMA,PRICE_CLOSE,0);   MaD1pr1v=iMA(NULL,TF3,maTrendPeriodv_1,0,MODE_SMA,PRICE_CLOSE,1);
MaD12v=iMA(NULL,TF3,maTrendPeriodv_2,0,MODE_SMA,PRICE_CLOSE,0);   MaD1pr2v=iMA(NULL,TF3,maTrendPeriodv_2,0,MODE_SMA,PRICE_CLOSE,1);
MaD13v=iMA(NULL,TF3,maTrendPeriodv_3,0,MODE_SMA,PRICE_CLOSE,0);   MaD1pr3v=iMA(NULL,TF3,maTrendPeriodv_3,0,MODE_SMA,PRICE_CLOSE,1);
MaD14v=iMA(NULL,TF3,maTrendPeriodv_4,0,MODE_SMA,PRICE_CLOSE,0);   MaD1pr4v=iMA(NULL,TF3,maTrendPeriodv_4,0,MODE_SMA,PRICE_CLOSE,1);
MaD15v=iMA(NULL,TF3,maTrendPeriodv_5,0,MODE_SMA,PRICE_CLOSE,0);   MaD1pr5v=iMA(NULL,TF3,maTrendPeriodv_5,0,MODE_SMA,PRICE_CLOSE,1);
     
     if (MaH11v < MaH1pr1v) {u1x5v = 0; d1x5v = 1;}
     if (MaH11v > MaH1pr1v) {u1x5v = 1; d1x5v = 0;}
     if (MaH11v == MaH1pr1v){u1x5v = 0; d1x5v = 0;}           
     if (MaH41v < MaH4pr1v) {u2x5v = 0; d2x5v = 1;}           
     if (MaH41v > MaH4pr1v) {u2x5v = 1; d2x5v = 0;}
     if (MaH41v == MaH4pr1v){u2x5v = 0; d2x5v = 0;}           
     if (MaD11v < MaD1pr1v) {u3x5v = 0; d3x5v = 1;}           
     if (MaD11v > MaD1pr1v) {u3x5v = 1; d3x5v = 0;}
     if (MaD11v == MaD1pr1v){u3x5v = 0; d3x5v = 0;} 
     
     if (MaH12v < MaH1pr2v) {u1x8v = 0; d1x8v = 1;}
     if (MaH12v > MaH1pr2v) {u1x8v = 1; d1x8v = 0;}
     if (MaH12v == MaH1pr2v){u1x8v = 0; d1x8v = 0;}           
     if (MaH42v < MaH4pr2v) {u2x8v = 0; d2x8v = 1;}           
     if (MaH42v > MaH4pr2v) {u2x8v = 1; d2x8v = 0;}
     if (MaH42v == MaH4pr2v){u2x8v = 0; d2x8v = 0;}           
     if (MaD12v < MaD1pr2v) {u3x8v = 0; d3x8v = 1;}             
     if (MaD12v > MaD1pr2v) {u3x8v = 1; d3x8v = 0;}
     if (MaD12v == MaD1pr2v){u3x8v = 0; d3x8v = 0;}
     
     if (MaH13v < MaH1pr3v) {u1x13v = 0; d1x13v = 1;}
     if (MaH13v > MaH1pr3v) {u1x13v = 1; d1x13v = 0;}
     if (MaH13v == MaH1pr3v){u1x13v = 0; d1x13v = 0;}             
     if (MaH43v < MaH4pr3v) {u2x13v = 0; d2x13v = 1;}           
     if (MaH43v > MaH4pr3v) {u2x13v = 1; d2x13v = 0;}
     if (MaH43v == MaH4pr3v){u2x13v = 0; d2x13v = 0;}           
     if (MaD13v < MaD1pr3v) {u3x13v = 0; d3x13v = 1;}           
     if (MaD13v > MaD1pr3v) {u3x13v = 1; d3x13v = 0;}
     if (MaD13v == MaD1pr3v){u3x13v = 0; d3x13v = 0;}
     
     if (MaH14v < MaH1pr4v) {u1x21v = 0; d1x21v = 1;}
     if (MaH14v > MaH1pr4v) {u1x21v = 1; d1x21v = 0;}
     if (MaH14v == MaH1pr4v){u1x21v = 0; d1x21v = 0;}             
     if (MaH44v < MaH4pr4v) {u2x21v = 0; d2x21v = 1;}           
     if (MaH44v > MaH4pr4v) {u2x21v = 1; d2x21v = 0;}
     if (MaH44v == MaH4pr4v){u2x21v = 0; d2x21v = 0;}           
     if (MaD14v < MaD1pr4v) {u3x21v = 0; d3x21v = 1;}           
     if (MaD14v > MaD1pr4v) {u3x21v = 1; d3x21v = 0;}
     if (MaD14v == MaD1pr4v){u3x21v = 0; d3x21v = 0;} 
     
     if (MaH15v < MaH1pr5v) {u1x34v = 0; d1x34v = 1;}
     if (MaH15v > MaH1pr5v) {u1x34v = 1; d1x34v = 0;}
     if (MaH15v == MaH1pr5v){u1x34v = 0; d1x34v = 0;}             
     if (MaH45v < MaH4pr5v) {u2x34v = 0; d2x34v = 1;}           
     if (MaH45v > MaH4pr5v) {u2x34v = 1; d2x34v = 0;}
     if (MaH45v == MaH4pr5v){u2x34v = 0; d2x34v = 0;}           
     if (MaD15v < MaD1pr5v) {u3x34v = 0; d3x34v = 1;}           
     if (MaD15v > MaD1pr5v) {u3x34v = 1; d3x34v = 0;}
     if (MaD15v == MaD1pr5v){u3x34v = 0; d3x34v = 0;}
     
double  acv  = iAC(NULL, TF1, 0);
double  ac1v = iAC(NULL, TF1, 1);
double  ac2v = iAC(NULL, TF1, 2);
double  ac3v = iAC(NULL, TF1, 3);

if((ac1v>ac2v && ac2v>ac3v && acv<0 && acv>ac1v)||(acv>ac1v && ac1v>ac2v && acv>0)) {u1acv = 3; d1acv = 0;}
if((ac1v<ac2v && ac2v<ac3v && acv>0 && acv<ac1v)||(acv<ac1v && ac1v<ac2v && acv<0)) {u1acv = 0; d1acv = 3;}
if((((ac1v<ac2v || ac2v<ac3v) && acv<0 && acv>ac1v) || (acv>ac1v && ac1v<ac2v && acv>0))
|| (((ac1v>ac2v || ac2v>ac3v) && acv>0 && acv<ac1v) || (acv<ac1v && ac1v>ac2v && acv<0)))
{u1acv = 0; d1acv = 0;}
 
double  ac03v = iAC(NULL, TF3, 0);
double  ac13v = iAC(NULL, TF3, 1);
double  ac23v = iAC(NULL, TF3, 2);
double  ac33v = iAC(NULL, TF3, 3);

if((ac13v>ac23v && ac23v>ac33v && ac03v<0 && ac03v>ac13v)||(ac03v>ac13v && ac13v>ac23v && ac03v>0)) {u3acv = 3; d3acv = 0;}     
if((ac13v<ac23v && ac23v<ac33v && ac03v>0 && ac03v<ac13v)||(ac03v<ac13v && ac13v<ac23v && ac03v<0)) {u3acv = 0; d3acv = 3;}     
if((((ac13v<ac23v || ac23v<ac33v) && ac03v<0 && ac03v>ac13v) || (ac03v>ac13v && ac13v<ac23v && ac03v>0))
|| (((ac13v>ac23v || ac23v>ac33v) && ac03v>0 && ac03v<ac13v) || (ac03v<ac13v && ac13v>ac23v && ac03v<0)))
{u3acv = 0; d3acv = 0;}

   double uitog1v = (u1x5v + u1x8v + u1x13v + u1x21v + u1x34v + u1acv) * 12.5;
   double uitog2v = (u2x5v + u2x8v + u2x13v + u2x21v + u2x34v + u2acv) * 12.5;
   double uitog3v = (u3x5v + u3x8v + u3x13v + u3x21v + u3x34v + u3acv) * 12.5;
 
   double ditog1v = (d1x5v + d1x8v + d1x13v + d1x21v + d1x34v + d1acv) * 12.5;
   double ditog2v = (d2x5v + d2x8v + d2x13v + d2x21v + d2x34v + d2acv) * 12.5;
   double ditog3v = (d3x5v + d3x8v + d3x13v + d3x21v + d3x34v + d3acv) * 12.5;

   Signal=0; Comment("Íå ðåêîìåíäóåòñÿ îòêðûâàòü ïîçèöèè. ÆÄÈÒÅ.");
   if (uitog1v>50  && uitog2v>50  && uitog3v>50)  {Signal=1; Comment("Íåïëîõîé ìîìåíò äëÿ îòêðûòèÿ ïîçèöèè BUY");}
   if (ditog1v>50  && ditog2v>50  && ditog3v>50)  {Signal=-1;Comment("Íåïëîõîé ìîìåíò äëÿ îòêðûòèÿ ïîçèöèè SELL");}
   if (uitog1v>=75 && uitog2v>=75 && uitog3v>=75) {Signal=2; Comment("ÓÄÀ×ÍÛÉ ìîìåíò äëÿ îòêðûòèÿ ïîçèöèè BUY");}
   if (ditog1v>=75 && ditog2v>=75 && ditog3v>=75) {Signal=-2;Comment("ÓÄÀ×ÍÛÉ ìîìåíò äëÿ îòêðûòèÿ ïîçèöèè SELL");}
   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 ---