#Graal-CROSSmuvingi-tral

Author: Rosh
Profit factor:
0.86

Okay, here's a breakdown of what this MetaTrader script does, explained in plain language for someone who isn't a programmer:

This script is designed to automatically trade on the Forex market. It analyzes price charts and tries to identify potential buying and selling opportunities based on certain indicators and rules.

Here's a breakdown of the key steps:

  1. Setup and Configuration:

    • The script starts by defining a set of adjustable settings (called "extern inputs"). These settings act like controls that a user can tweak to customize the trading strategy. Think of them like dials on a machine. Some examples are:
      • FastPeriod and SlowPeriod: These control the sensitivity of two moving averages, which are used to smooth out price fluctuations.
      • MomPeriod: This affects how the script measures the strength of price trends.
      • MomFilter: A threshold for the momentum indicator, determining how strong the trend needs to be before the script considers a trade.
      • PercentCapital: A percentage of your account balance to risk on each trade.
      • Lots: The initial trade size.
      • Slippage: This accounts for the potential difference between the requested trade price and the actual price executed by the broker.
      • StopLoss: The amount of money you're willing to lose on a trade.
      • TakeProfit: The amount of profit you're aiming to make on a trade.
      • Several variables dealing with a trailing stop strategy.
  2. Calculating Existing Trades:

    • The script first checks if there are already any open trades associated with this particular script (identified by its "magic number"). It counts the number of buy and sell orders that are currently active.
  3. Optimizing Trade Size (Lot Size):

    • The script then calculates the optimal trade size ("lot size") for new trades. It aims to adjust the size of the trades based on the risk you're willing to take and the performance of previous trades. The script calculates the lot size based on the free margin in your account and the maximum risk you've set. It also tries to reduce the lot size if there have been a series of losing trades.
  4. Checking for New Trade Opportunities (Opening Trades):

    • The script constantly monitors the price data and calculates the values of some indicators.
    • Moving Averages: It uses two moving averages to identify potential trend changes. If the faster moving average crosses above the slower moving average, it might signal an uptrend (a potential buy). If it crosses below, it might signal a downtrend (a potential sell).
    • Momentum: It also uses a momentum indicator to measure the strength of the trend.
    • Buy Condition: The script will open a buy order if the fast moving average is above the slow moving average, the fast moving average just crossed above the slow moving average, the momentum is above a certain threshold, and the momentum is increasing.
    • Sell Condition: The script will open a sell order if the fast moving average is below the slow moving average, the fast moving average just crossed below the slow moving average, the momentum is below a certain threshold, and the momentum is decreasing.
    • When the script identifies a trading opportunity based on these indicators, it sends an order to buy or sell. The order includes the calculated lot size, slippage, and take profit levels. Stop loss is handled at the next stage, when a trade is opened.
  5. Checking for Trade Closure (Closing Trades):

    • If there are existing open trades, the script will instead check for conditions to close them.
    • ATR (Average True Range): A measure of how much an asset typically moves in a given time period.
    • Trailing Stop: The script implements a trailing stop loss strategy. A trailing stop loss automatically adjusts the stop loss price as the trade becomes more profitable, helping to lock in gains.
    • The script checks if the price has moved far enough to trigger the trailing stop loss and adjusts the stop loss accordingly.
  6. The start() Function:

    • The start() function is the main function that runs repeatedly. It first checks if the market data is sufficient (enough bars on the chart) and if trading is allowed. Then, based on whether there are any open trades, it calls either the CheckForOpen() function (to look for new trade opportunities) or the CheckForClose() function (to manage existing trades).

In Summary:

The script is an automated Forex trading system that uses moving averages and momentum to identify potential buy and sell signals. It manages trade sizes based on risk and account balance and implements a trailing stop loss strategy to protect profits. The user can customize the strategy by adjusting the settings at the beginning of the script.

Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt can change open orders parameters, due to possible stepping strategy
Indicators Used
Moving average indicatorMomentum indicator
Miscellaneous
It plays sound alerts
14 Views
1 Downloads
0 Favorites
#Graal-CROSSmuvingi-tral
//+------------------------------------------------------------------+
//|                                            Graal-FxProg_team.mq4 |
//|               //+-                                              Rosh |
//|               http://www.investo.ru/forum/viewtopic.php?t=124777 |
//+------------------------------------------------------------------+
#property copyright "Rosh"

#define MAGIC  256
extern int       FastPeriod=13;
extern int       SlowPeriod=34;
extern int       MomPeriod=14;
extern double    MomFilter=0.1;
extern double    PercentCapital=10.0;
extern double    Lots=0.1;
extern int       Slippage=3;
extern int       StopLoss=20;
extern int       TakeProfit=200;
int myBars;
extern double Patr=9;
extern double Prange=5;
extern double Kstop=1.5;
extern double kts=2;
extern double Vts=2;
extern double MaximumRisk        = 0.05;
extern double DecreaseFactor     = 10;
//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double LotsOptimized()
  {
   double lot=Lots;
   int    orders=HistoryTotal();     // history orders total
   int    losses=0;                  // number of losses orders without a break
//---- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//---- calcuulate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      for(int i=orders-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
         //----
         if(OrderProfit()>0) break;
         if(OrderProfit()<0) losses++;
        }
      if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
     }
//---- return lot size
   if(lot<0.1) lot=0.1;
   return(lot);
  }
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
  int cnt;
  double curFastMA=iMA(NULL,0,FastPeriod,0,MODE_EMA,PRICE_CLOSE,1);
  double curSlowMA=iMA(NULL,0,SlowPeriod,0,MODE_EMA,PRICE_OPEN,1);
  double prevFastMA=iMA(NULL,0,FastPeriod,0,MODE_EMA,PRICE_CLOSE,2);
  double prevSlowMA=iMA(NULL,0,SlowPeriod,0,MODE_EMA,PRICE_OPEN,2);
  double curMom=iMomentum(NULL,0,MomPeriod,PRICE_CLOSE,1)-100.0;
  double prevMom=iMomentum(NULL,0,MomPeriod,PRICE_CLOSE,2)-100.0;
//----
   if (Bars!=myBars)
      {
      myBars=Bars;
      if (curFastMA>curSlowMA && prevFastMA<prevSlowMA && curMom>MomFilter && curMom>prevMom)
         {
         
         OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,Slippage,0,Ask+TakeProfit*Point,"buy",MAGIC,0,Blue);
         }
      if (curFastMA<curSlowMA && prevFastMA>prevSlowMA && curMom<-MomFilter && curMom<prevMom)
         {
         
         OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,Slippage,0,Bid-TakeProfit*Point,"sell",MAGIC,0,Red);
         }
      }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {
  int i,mode,ticket,total;
  double cnt=0,ValATR=0,hi=0,lo=0,SL=0,TS=0,prevBars=0;
  if (prevBars!=Bars) 
  {
    ValATR=0;
    for(i=1; i<=Patr; i++) { if(i<=Patr) { ValATR+=High[i]-Low[i]; } }
    ValATR=ValATR/Patr;
     
    hi=High[Highest(NULL,0,MODE_HIGH,Prange,Prange)]; 
    lo=Low[Lowest(NULL,0,MODE_LOW,Prange,Prange)]; 
 
    if (Vts==1)  {TS=kts*ValATR; SL=Kstop*ValATR;}
    if (Vts==2)  {TS=(hi-lo);    SL=Kstop*(hi-lo);}
    prevBars = Bars;
  }

  if (Vts<1 || Vts>2)  return(0);

//-------------------------------------------------------

  for (cnt=0; cnt<=OrdersTotal(); cnt++)
  {
    OrderSelect(cnt,SELECT_BY_POS);
    mode=OrderType();
    if(OrderSymbol()==Symbol())
    {
//First Stop---------------------------------
      if (mode==OP_BUY && OrderStopLoss()== 0)
      {
        OrderModify(OrderTicket(),OrderOpenPrice(),Low[0]-SL,OrderTakeProfit(),0,CLR_NONE);
        PlaySound("expert.wav");
        return(0);
      }

      if (mode==OP_SELL && OrderStopLoss()==0)
      {
        OrderModify(OrderTicket(),OrderOpenPrice(),High[0]+SL,OrderTakeProfit(),0,CLR_NONE);
        PlaySound("expert.wav");
        return(0);
      }
//Main Trailing-------------------------------
      if ((mode==OP_BUY && High[0]-OrderOpenPrice()>TS && OrderStopLoss()<High[0]-TS) || OrderStopLoss()==0)
      {
        OrderModify(OrderTicket(),OrderOpenPrice(),High[0]-TS,OrderTakeProfit(),0,CLR_NONE);
        PlaySound("expert.wav");
        return(0);
      }

      if ((mode==OP_SELL && OrderOpenPrice()-Low[0]>TS && OrderStopLoss()>Low[0]+TS) || OrderStopLoss()==0)
      {
        OrderModify(OrderTicket(),OrderOpenPrice(),Low[0]+TS,OrderTakeProfit(),0,CLR_NONE);
        PlaySound("expert.wav");
        return(0);
      }
    }
  }

  Comment("Versia: ",Vts,"\n",
          "Per_ATR: ",Patr,"\n",
          "Per_Range: ",Prange,"\n",
          "Range: ",(High[0]-Low[0]),"\n",
          "ATR: ",ValATR,"\n",
          "SL: ",SL,"\n",
          "TS: ",TS);

  return(0);
}

//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();
//----
  }
//+------------------------------------------------------------------+

Profitability Reports

USD/CHF Jul 2025 - Sep 2025
1.67
Total Trades 26
Won Trades 19
Lost trades 7
Win Rate 73.08 %
Expected payoff 35.83
Gross Profit 2332.25
Gross Loss -1400.68
Total Net Profit 931.57
-100%
-50%
0%
50%
100%
USD/CAD Jul 2025 - Sep 2025
0.62
Total Trades 20
Won Trades 14
Lost trades 6
Win Rate 70.00 %
Expected payoff -21.28
Gross Profit 707.11
Gross Loss -1132.75
Total Net Profit -425.64
-100%
-50%
0%
50%
100%
NZD/USD Jul 2025 - Sep 2025
0.89
Total Trades 26
Won Trades 16
Lost trades 10
Win Rate 61.54 %
Expected payoff -5.60
Gross Profit 1236.40
Gross Loss -1382.00
Total Net Profit -145.60
-100%
-50%
0%
50%
100%
GBP/USD Jul 2025 - Sep 2025
1.09
Total Trades 24
Won Trades 17
Lost trades 7
Win Rate 70.83 %
Expected payoff 5.40
Gross Profit 1573.00
Gross Loss -1443.30
Total Net Profit 129.70
-100%
-50%
0%
50%
100%
GBP/CAD Jul 2025 - Sep 2025
1.69
Total Trades 24
Won Trades 19
Lost trades 5
Win Rate 79.17 %
Expected payoff 22.89
Gross Profit 1345.45
Gross Loss -796.07
Total Net Profit 549.38
-100%
-50%
0%
50%
100%
GBP/AUD Jul 2025 - Sep 2025
0.58
Total Trades 16
Won Trades 10
Lost trades 6
Win Rate 62.50 %
Expected payoff -29.53
Gross Profit 646.60
Gross Loss -1119.14
Total Net Profit -472.54
-100%
-50%
0%
50%
100%
EUR/USD Jul 2025 - Sep 2025
1.30
Total Trades 23
Won Trades 17
Lost trades 6
Win Rate 73.91 %
Expected payoff 17.13
Gross Profit 1700.00
Gross Loss -1306.00
Total Net Profit 394.00
-100%
-50%
0%
50%
100%
AUD/USD Jul 2025 - Sep 2025
0.23
Total Trades 16
Won Trades 5
Lost trades 11
Win Rate 31.25 %
Expected payoff -75.56
Gross Profit 351.70
Gross Loss -1560.70
Total Net Profit -1209.00
-100%
-50%
0%
50%
100%
USD/JPY Jan 2025 - Jul 2025
0.60
Total Trades 54
Won Trades 39
Lost trades 15
Win Rate 72.22 %
Expected payoff -29.89
Gross Profit 2434.06
Gross Loss -4048.25
Total Net Profit -1614.19
-100%
-50%
0%
50%
100%
USD/CHF Jan 2025 - Jul 2025
0.96
Total Trades 46
Won Trades 34
Lost trades 12
Win Rate 73.91 %
Expected payoff -2.56
Gross Profit 3214.76
Gross Loss -3332.55
Total Net Profit -117.79
-100%
-50%
0%
50%
100%

Comments