Author: www.fx-chaos.by.ru

This script, named "FX-CHAOS," is designed to automatically trade on the Forex market using the MetaTrader platform. Here's a breakdown of its logic:

1. Initial Setup & Checks:

  • Personal Information: The script starts by displaying the copyright and a link to the developer's website.
  • Settings: The script uses settings (parameters) that you can adjust, such as:
    • Minimum stop-loss and take-profit levels (to manage risk and potential profit).
    • A "magic number" (to identify orders placed by this specific script if you are running multiple).
    • Trading hours (to specify when the script is allowed to trade).
    • Alert sounds.
    • Colors for buy and sell trades on the chart.
  • Account Verification: The script can be configured to only work on a specific account number. It checks if the current account matches the allowed account, and stops if they don't match.
  • Time filter: The script verifies if the last order was closed less than 4 hours ago.

2. Determining Trade Size (Lots):

  • Dynamic Lot Sizing: The script adjusts the trade size (number of lots) based on your account balance. As your balance increases, the script is programmed to use larger trade sizes.
  • Balance Brackets: The script divides account balances into tiers, each with a corresponding "MAX_Lots" value. If the account balance is within the defined range, the script calculates the value and assigns the lot sizes to a set of predefined variables.
  • Lot Size Distribution: Depending on the value calculated in the previous step, the script determines 5 different lot sizes for each trade.

3. Identifying Trade Opportunities (Buy/Sell Signals):

  • Technical Indicators: The script uses custom indicators (like "zzf" and "FX-AO") to analyze the price charts. These indicators generate buy and sell signals based on their internal calculations.
  • Price Analysis: The script compares the current and previous day's high and low prices to the current price.
  • Combining Signals: The script combines the signals from the indicators and price analysis to decide whether to open a buy or sell trade.
  • Opening Conditions (lFlagBuyOpen , lFlagSellOpen, lFlagBuyOpen2, lFlagSellOpen2): The script uses if statements combined with the previously calculated values in order to create the buy and sell signals.

4. Opening Trades:

  • Order Placement: If the script identifies a buy or sell signal and other conditions are met (like the number of open orders), it sends an order to the broker to open a trade.
  • Order Details: The order includes:
    • The currency pair being traded.
    • The type of order (buy or sell).
    • The trade size (lots, determined earlier).
    • The current market price (ask price for buy, bid price for sell).
    • Slippage (the acceptable difference between the requested price and the actual price).
    • Stop-loss and take-profit levels (to automatically limit losses and secure profits).
    • The script's name and magic number (for identification).
  • Limited Number of Orders: The script places a maximum of 5 orders.
  • Equity Condition: Orders 2 through 5 are only opened if the account equity (balance plus open profits/losses) is greater than the account balance.

5. Managing Existing Trades:

  • Order Selection: The script checks if any orders are already open.
  • Stop Loss Modification: For orders that are already open and if equity is greater than balance plus a threshold, the script automatically moves the stop-loss order closer to the current market price, locking in profits as the price moves in a favorable direction (this is known as a "trailing stop").

6. Closing Trades:

  • Closing Signals: The script defines separate signals for closing buy and sell trades (lFlagBuyClose, lFlagSellClose).
  • Order Closing: If a closing signal is triggered for an existing trade, the script sends an order to close the trade at the current market price.
  • Sound Alert: If closing signal is triggered for an existing trade, the script triggers a sound alert.

7. Time-Based Restrictions:

  • Trading Hours: If you have enabled the "Use Hour Trade" option, the script will only execute trades during the hours you've specified.

In simple terms, this script is like a robot trader that uses technical indicators to find trading opportunities, adjusts its trade size based on your account balance, and automatically opens and manages trades according to its programmed logic.

Disclaimer: Trading on the Forex market involves risk. This is only a description of the script's logic, and does not constitute financial advice. Past performance is not indicative of future results.

Price Data Components
Series array that contains close prices for each barSeries array that contains open prices of each barSeries array that contains the highest prices of each barSeries array that contains the lowest prices of each bar
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt Closes Orders by itself It can change open orders parameters, due to possible stepping strategy
Miscellaneous
It plays sound alerts
4 Views
0 Downloads
0 Favorites

Profitability Reports

AUD/USD Oct 2024 - Jan 2025
0.00 %
Total Trades 133
Won Trades 0
Lost trades 0
Win Rate 0.00 %
Expected payoff -5.57
Gross Profit 0.00
Gross Loss -741.20
Total Net Profit -741.20
-100%
-50%
0%
50%
100%
GBP/USD Oct 2024 - Jan 2025
3544.00 %
Total Trades 83
Won Trades 73
Lost trades 10
Win Rate 0.88 %
Expected payoff 29.21
Gross Profit 2494.80
Gross Loss -70.40
Total Net Profit 2424.40
-100%
-50%
0%
50%
100%
NZD/USD Oct 2024 - Jan 2025
0.00 %
Total Trades 117
Won Trades 0
Lost trades 117
Win Rate 0.00 %
Expected payoff -15.64
Gross Profit 0.00
Gross Loss -1830.00
Total Net Profit -1830.00
-100%
-50%
0%
50%
100%
FX-CHAOS
//+------------------------------------------------------------------+
//|                                                     FX-CHAOS.mq4 |
//|                                      http://www.fx-chaos.by.ru// |
//|                                                      version 1.0 |
//+------------------------------------------------------------------+

#property copyright "www.fx-chaos.by.ru"
#property link      "http://www.fx-chaos.by.ru"

#define MIN_STOPLOSS_POINT 10 
#define MIN_TAKEPROFIT_POINT 10 
#define MAGIC 26626

double TrailingStopPoint;


 string sNameExpert = "FX-CHAOS";
 int nAccount =0;
 int nSlippage = 0;
 bool lFlagUseHourTrade = False;
 int nFromHourTrade = False;
 int nToHourTrade = False;
 bool lFlagUseSound = False;
 string sSoundFileName = "alert.wav";
 color colorOpenBuy = Blue;
 color colorCloseBuy = Aqua;
 color colorOpenSell = Red;
 color colorCloseSell = Aqua;
 int numorder,k;
 
  double dbStopLoss;
 double dsStopLoss;
 double dbTakeProfit;
 double dsTakeProfit;
int DELTA=3000;
int MAX_Lots;
double dLots1=0.1;
double dLots2=0.5;
double dLots3=0.4;
double dLots4=0.3;
double dLots5=0.2;

static int prevtime = 0;

/////////////////////
void deinit() {
   Comment("");
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start(){

 int n = Bars;
   

for ( int z=0;z<HistoryTotal();z++)
{
if (OrderSelect (z,SELECT_BY_POS, MODE_HISTORY))
datetime oct=OrderCloseTime();
}
if(CurTime()-oct<14400) return(0);




 
  // Ñêîëüêî ïîçèöèé ó íàñ îòêðûòî?
   int total = OrdersTotal();



 

//ëîòñ//////////
 
 if (AccountBalance()<3000+1*DELTA)
   MAX_Lots = 3.0;
  if (AccountBalance()>=3000+1*DELTA && AccountBalance()<3000+2*DELTA)
   MAX_Lots = 4.5;
  if (AccountBalance()>=3000+2*DELTA && AccountBalance()<3000+3*DELTA)
   MAX_Lots = 6.0;
     if (AccountBalance()>=3000+3*DELTA && AccountBalance()<3000+4*DELTA)
   MAX_Lots = 7.5;
     if (AccountBalance()>=3000+4*DELTA && AccountBalance()<3000+5*DELTA)
   MAX_Lots = 9.0;
     if (AccountBalance()>=3000+5*DELTA && AccountBalance()<3000+6*DELTA)
   MAX_Lots = 10.5;
     if (AccountBalance()>=3000+6*DELTA && AccountBalance()<3000+7*DELTA)
   MAX_Lots = 12.0;
     if (AccountBalance()>=3000+7*DELTA && AccountBalance()<3000+8*DELTA)
   MAX_Lots = 13.5;
     if (AccountBalance()>=3000+8*DELTA && AccountBalance()<3000+9*DELTA)
   MAX_Lots = 15.0;
   if (AccountBalance()>=3000+9*DELTA)
   MAX_Lots = 15.0;
   
   
 if (MAX_Lots >1 && MAX_Lots <2 ){
   
   dLots1 = 0.1;
   dLots2 = 0.5;
   dLots3 = 0.4;
   dLots4 = 0.3;
   dLots5 = 0.2;
}

 if (MAX_Lots >2 && MAX_Lots <4){
   
   dLots1 = 0.2;
   dLots2 = 1.0;
   dLots3 = 0.8;
   dLots4 = 0.6;
   dLots5 = 0.4;
}

 if (MAX_Lots >4 && MAX_Lots <5){
   
   dLots1 = 0.3;
   dLots2 = 1.5;
   dLots3 = 1.2;
   dLots4 = 0.9;
   dLots5 = 0.6;
}

 if (MAX_Lots >5 && MAX_Lots <7){
   
   dLots1 = 0.4;
   dLots2 = 2.0;
   dLots3 = 1.6;
   dLots4 = 1.2;
   dLots5 = 0.8;
}

 if (MAX_Lots >7 && MAX_Lots <8){
   
   dLots1 = 0.5;
   dLots2 = 2.5;
   dLots3 = 2.0;
   dLots4 = 1.5;
   dLots5 = 1.0;
}

 if (MAX_Lots >8 && MAX_Lots <10){
   
   dLots1 = 0.6;
   dLots2 = 3.0;
   dLots3 = 2.4;
   dLots4 = 1.8;
   dLots5 = 1.2;
}

 if (MAX_Lots >10 && MAX_Lots <11){
   
   dLots1 = 0.7;
   dLots2 = 3.5;
   dLots3 = 2.8;
   dLots4 = 2.1;
   dLots5 = 1.4;
}

 if (MAX_Lots >11 && MAX_Lots <13){
   
   dLots1 = 0.8;
   dLots2 = 4.0;
   dLots3 = 3.2;
   dLots4 = 2.4;
   dLots5 = 1.6;
}

 if (MAX_Lots >13 && MAX_Lots <14){
   
   dLots1 = 0.9;
   dLots2 = 4.5;
   dLots3 = 3.6;
   dLots4 = 2.7;
   dLots5 = 1.8;
}


 if (MAX_Lots >14 && MAX_Lots <16){
   
   dLots1 = 1.0;
   dLots2 = 5.0;
   dLots3 = 4.0;
   dLots4 = 3.0;
   dLots5 = 2.0;
}


///////////////


 




   if (lFlagUseHourTrade){
      if (!(Hour()>=nFromHourTrade && Hour()<=nToHourTrade)) {
         Comment("Time for trade has not come else!");
         return(0);
      }
   }
   
   if(Bars < 100){
      Print("bars less than 100");
      return(0);
   }
   
   if (nAccount > 0 && nAccount != AccountNumber()){
      Comment("Trade on account :"+AccountNumber()+" FORBIDDEN!");
      return(0);
   }
 
   double ZZF1440=iCustom(NULL, 1440, "zzf",0,1);
   
      double FXAO=iCustom(NULL, 240, "FX-AO",0,0);
   
      double   CLOSE =  iClose(NULL,0,0);
      double   OPEN =  iOpen(NULL,240,0); 
    double   OPEN0 =  iOpen(NULL,1440,0); 
   
      
   double ZZF240=iCustom(NULL, 240, "zzf",0,1);
  

     double   HIGH0 =  iHigh(NULL,1440,1)+2*Point+MarketInfo(Symbol(),MODE_SPREAD);
     double   LOW0 =  iLow(NULL,1440,1)-2*Point;
     
     double   HIGH1 =  iHigh(NULL,240,1)+2*Point+MarketInfo(Symbol(),MODE_SPREAD);
     double   LOW1 =  iLow(NULL,240,1)-2*Point;
     

      
        
   bool lFlagBuyOpen = false, lFlagSellOpen = false, lFlagBuyClose = false, lFlagSellClose = false, lFlagBuyOpen2 = false, lFlagSellOpen2 = false, lFlagBuyOpen3 = false, lFlagSellOpen3 = false, lFlagBuyOpen4 = false, lFlagSellOpen4 = false;

lFlagBuyOpen =(OPEN0<HIGH0 && CLOSE>HIGH0 && CLOSE<ZZF1440 && FXAO<0);

  lFlagSellOpen = (OPEN0>LOW0 && CLOSE<LOW0 && CLOSE>ZZF1440 && FXAO>0);

lFlagBuyOpen2 =(OPEN<HIGH1 && CLOSE>HIGH1 && CLOSE<ZZF240);

  lFlagSellOpen2 = (OPEN>LOW1 && CLOSE<LOW1 && CLOSE>ZZF240);






///////////////////////////////////////////ïåðâûé îðäåð

if((lFlagBuyOpen || lFlagSellOpen) && (!GlobalVariableCheck("") || GlobalVariableGet("Symbol()"+"")!=Time[0] && total<1))
     {
      if(lFlagBuyOpen)
        {  
 numorder = OrderSend(Symbol(), OP_BUY, dLots1, Ask, nSlippage, dbStopLoss, dbTakeProfit, sNameExpert, MAGIC, 0, colorOpenBuy); 
           
         
           if(numorder>0)
           {
            if(OrderSelect(numorder,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
            GlobalVariableSet("Symbol()"+"MAGICMA"+"TimeBuySell",Time[0]);
           }
         else Print("Error opening BUY order : ",GetLastError());return(0); 
        }
               
      if(lFlagSellOpen)
        { 
 numorder = OrderSend(Symbol(),OP_SELL, dLots1, Bid, nSlippage, dsStopLoss, dsTakeProfit, sNameExpert, MAGIC, 0, colorOpenSell); 
        
     
          if(numorder>0)
           { 
            if(OrderSelect(numorder,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
            GlobalVariableSet("Symbol()"+"MAGICMA"+"TimeBuySell",Time[0]);
           }
         else Print("Error opening SELL order : ",GetLastError());return(0);
        }
   }

   
/////////////////////////////////////////âòîðîé îðäåð      
   
 
if((lFlagBuyOpen2 || lFlagSellOpen2) && (!GlobalVariableCheck("") || GlobalVariableGet("Symbol()"+"")!=Time[0] && total>0 && total<2&& AccountEquity()>AccountBalance()))//
     {
      if(lFlagBuyOpen2 && OrderType()==OP_BUY)
        {  
 numorder = OrderSend(Symbol(), OP_BUY, dLots2, Ask, nSlippage, dbStopLoss, dbTakeProfit, sNameExpert, MAGIC, 0, colorOpenBuy); 
           if(numorder>0)
           {
            if(OrderSelect(numorder,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
            GlobalVariableSet("Symbol()"+"MAGICMA"+"TimeBuySell",Time[0]);
           }
         else Print("Error opening BUY order : ",GetLastError());return(0); 
        }
               
      if(lFlagSellOpen2 && OrderType()==OP_SELL)
        { 
 numorder = OrderSend(Symbol(),OP_SELL, dLots2, Bid, nSlippage, dsStopLoss, dsTakeProfit, sNameExpert, MAGIC, 0, colorOpenSell); 
          if(numorder>0)
           {
            if(OrderSelect(numorder,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
            GlobalVariableSet("Symbol()"+"MAGICMA"+"TimeBuySell",Time[0]);
           }
         else Print("Error opening SELL order : ",GetLastError());return(0);
        }
   }

   
////////////////////////////////////////////òðåòèé îðäåð

 
if((lFlagBuyOpen2 || lFlagSellOpen2) && (!GlobalVariableCheck("") || GlobalVariableGet("Symbol()"+"")!=Time[0] && total>1 && total<3&& AccountEquity()>AccountBalance()))
     {
      if(lFlagBuyOpen2 && OrderType()==OP_BUY)
        {  
 numorder = OrderSend(Symbol(), OP_BUY, dLots3, Ask, nSlippage, dbStopLoss, dbTakeProfit, sNameExpert, MAGIC, 0, colorOpenBuy); 
           if(numorder>0)
           {
            if(OrderSelect(numorder,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
            GlobalVariableSet("Symbol()"+"MAGICMA"+"TimeBuySell",Time[0]);
           }
         else Print("Error opening BUY order : ",GetLastError());return(0); 
        }
               
      if(lFlagSellOpen2 && OrderType()==OP_SELL)
        { 
 numorder = OrderSend(Symbol(),OP_SELL, dLots3, Bid, nSlippage, dsStopLoss, dsTakeProfit, sNameExpert, MAGIC, 0, colorOpenSell); 
          if(numorder>0)
           {
            if(OrderSelect(numorder,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
            GlobalVariableSet("Symbol()"+"MAGICMA"+"TimeBuySell",Time[0]);
           }
         else Print("Error opening SELL order : ",GetLastError());return(0);
        }
   }


////////////////////////////////////////////4 îðäåð

 
if((lFlagBuyOpen2 || lFlagSellOpen2) && (!GlobalVariableCheck("") || GlobalVariableGet("Symbol()"+"")!=Time[0] && total>2 && total<4&& AccountEquity()>AccountBalance()))
     {
      if(lFlagBuyOpen2 && OrderType()==OP_BUY)
        {  
 numorder = OrderSend(Symbol(), OP_BUY, dLots4, Ask, nSlippage, dbStopLoss, dbTakeProfit, sNameExpert, MAGIC, 0, colorOpenBuy); 
           if(numorder>0)
           {
            if(OrderSelect(numorder,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
            GlobalVariableSet("Symbol()"+"MAGICMA"+"TimeBuySell",Time[0]);
           }
         else Print("Error opening BUY order : ",GetLastError());return(0); 
        }
               
      if(lFlagSellOpen2 && OrderType()==OP_SELL)
        { 
 numorder = OrderSend(Symbol(),OP_SELL, dLots4, Bid, nSlippage, dsStopLoss, dsTakeProfit, sNameExpert, MAGIC, 0, colorOpenSell); 
          if(numorder>0)
           {
            if(OrderSelect(numorder,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
            GlobalVariableSet("Symbol()"+"MAGICMA"+"TimeBuySell",Time[0]);
           }
         else Print("Error opening SELL order : ",GetLastError());return(0);
        }
   }


////////////////////////////////////////////5 îðäåð

 
if((lFlagBuyOpen2 || lFlagSellOpen2) && (!GlobalVariableCheck("") || GlobalVariableGet("Symbol()"+"")!=Time[0] && total>3 && total<5&& AccountEquity()>AccountBalance()))//&& AccountEquity()>AccountBalance()
     {
      if(lFlagBuyOpen2 && OrderType()==OP_BUY)
        {  
 numorder = OrderSend(Symbol(), OP_BUY, dLots5, Ask, nSlippage, dbStopLoss, dbTakeProfit, sNameExpert, MAGIC, 0, colorOpenBuy); 
           if(numorder>0)
           {
            if(OrderSelect(numorder,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
            GlobalVariableSet("Symbol()"+"MAGICMA"+"TimeBuySell",Time[0]);
           }
         else Print("Error opening BUY order : ",GetLastError());return(0); 
        }
               
      if(lFlagSellOpen2 && OrderType()==OP_SELL)
        { 
 numorder = OrderSend(Symbol(),OP_SELL, dLots5, Bid, nSlippage, dsStopLoss, dsTakeProfit, sNameExpert, MAGIC, 0, colorOpenSell); 
          if(numorder>0)
           {
            if(OrderSelect(numorder,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
            GlobalVariableSet("Symbol()"+"MAGICMA"+"TimeBuySell",Time[0]);
           }
         else Print("Error opening SELL order : ",GetLastError());return(0);
        }
   }



   
   
   


///////////////////////////////////   
   
//çàêðûòèå îðäåðà/////////////

    lFlagBuyClose =false;
   
  lFlagSellClose =false;

   
/////////////////////////////   
     
   
  
   if (ExistPositions()){
      if(OrderType()==OP_BUY){
         if (lFlagBuyClose){
            bool flagCloseBuy = OrderClose(OrderTicket(), OrderLots(), Bid, nSlippage, colorCloseBuy); 
            if (flagCloseBuy && lFlagUseSound) 
               PlaySound(sSoundFileName); 
            return(0);
         }
      }
      if(OrderType()==OP_SELL){
         if (lFlagSellClose){
            bool flagCloseSell = OrderClose(OrderTicket(), OrderLots(), Ask, nSlippage, colorCloseSell); 
            if (flagCloseSell && lFlagUseSound) 
               PlaySound(sSoundFileName); 
            return(0);
         }
      }
   }

   
   if (total>1 && AccountEquity()>AccountBalance()+(3000*dLots1)){
      
      for (int i=0; i<OrdersTotal(); i++) { 
         if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { 
            bool lMagic = true;
            if (MAGIC > 0 && OrderMagicNumber() != MAGIC)
               lMagic = false;
            
            if (OrderSymbol()==Symbol() && lMagic) { 
               if (OrderType()==OP_BUY && 27 > 0) { 
                 
                     if (OrderStopLoss()<Bid-27*Point || OrderStopLoss()==0) 
                        ModifyStopLoss(Bid-27*Point); 
                  
               } 
               if (OrderType()==OP_SELL) { 
              
                     if (OrderStopLoss()>Ask+27*Point || OrderStopLoss()==0)  
                        ModifyStopLoss(Ask+27*Point); 
                   
               } 
            } 
         } 
      } 
   }
   return (0);
}

                 
    
////////////////////////

bool ExistPositions() {
	for (int i=0; i<OrdersTotal(); i++) {
		if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
         bool lMagic = true;
         
         if (MAGIC > 0 && OrderMagicNumber() != MAGIC)
            lMagic = false;

			if (OrderSymbol()==Symbol() && lMagic) {
				return(True);
			}
		} 
	} 
	return(false);
}




void ModifyStopLoss(double ldStopLoss) { 
   bool lFlagModify = OrderModify(OrderTicket(), OrderOpenPrice(), ldStopLoss, OrderTakeProfit(), 0, CLR_NONE); 
   if (lFlagModify && lFlagUseSound) 
      PlaySound(sSoundFileName); 
} 


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 ---