Okay, here's a breakdown of what the provided script does, explained in a way that's easy to understand even if you don't know programming.
This script is designed to automatically place buy and sell orders in the Forex market. It's an automated trading system (often called an "Expert Advisor" or EA) for the MetaTrader platform.
Here's the gist of its operation:
-
Overall Goal: The script aims to generate profits by automatically executing trades based on certain conditions. It focuses on both buying (going long) and selling (going short).
-
Initial Setup:
- The script starts by reading in some settings that you can adjust. These settings include:
- Whether to enable buying ("In_BUY")
- Whether to enable selling ("In_SELL")
- Distance for Buy and Sell orders to be placed on the price chart.
- Probability of the EA entering into a BUY or SELL order.
- It also sets up some internal variables for tracking time and identifying trades placed by the script.
- The script starts by reading in some settings that you can adjust. These settings include:
-
Trading Logic (The
start()
Function):- The core of the script runs within the
start()
function, which is executed repeatedly by MetaTrader. - Time Check: It first checks if the current time is the same as the last time it ran. If so, it does nothing (to avoid repeating actions on the same price tick).
- Trade Allowed Check: It then verifies whether trading is allowed at the moment (e.g., the market isn't closed).
- Buy/Sell Execution (
My_Play()
function): The most important part is calling theMy_Play()
function twice ? once for buying and once for selling. TheMy_Play()
function determines if conditions are right to initiate a trade and then places the order. - It uses random numbers to create a probability that is compared with the level parameters to open positions.
- It defines Stop Loss and Take Profit levels for the operations.
- The core of the script runs within the
-
The
My_Play()
Function (Detailed): This is where the actual trade placement logic resides.- Order Check: It scans through all existing orders to see if there are already orders placed by this script for the current currency pair.
- Conditions to Open a Trade: The script then checks several conditions:
- Is buying (or selling) enabled? (
flag
) - Are there any open orders? (
OrdersTotal()==0
) - Does a random number meet a certain threshold? This adds an element of randomness to the entry.
level*327.68 > MathRand()
- Is trading allowed?
- Is buying (or selling) enabled? (
- Order Placement: If all the conditions are met, the script sends an order to the broker to either buy or sell. It sets a Stop Loss and Take Profit level to manage risk.
- Error Handling: It checks if the order was successfully placed. If not, it pauses for a short time and tries again later.
-
Lot Sizing (
lot()
function): This determines the size of the trade.- Martingale Feature: If the "MartIn" (Martingale) setting is enabled, this function implements a basic Martingale strategy. The Martingale strategy involves doubling the lot size after each losing trade in an attempt to recover losses quickly. If the last order has a negative profit, the lot size will be bigger.
- Initial Lot Size: If the Martingale strategy is not enabled, it uses a default lot size, otherwise it checks if there are previous trades and increases lot size if they where negative.
In summary: This script is a basic automated trading system designed to enter buy and sell orders based on a combination of user-defined settings, random chance, and an optional Martingale lot sizing strategy. The intention is to automate a trading strategy to try to profit from price movements in the market.
Profitability Reports
//+------------------------------------------------------------------+
//| Precipice_MartIn.mq4 |
//| |
//| |
//+------------------------------------------------------------------+
#property copyright "Maximus_genuine "
#property link "gladmxm@bigmir.net"
//---- input parameters
//..........................................................................
extern bool MartIn =true;
//..........................................................................
extern bool In_BUY =true;
extern int step_BUY =89; //---âõîäíûå ïàðàìåòðû ïî ëîíãàì
extern int level_BUY=50;
//..........................................................................
extern bool In_SELL =true;
extern int step_SELL =89; //---âõîäíûå ïàðàìåòðû ïî øîðòàì
extern int level_SELL=50;
//..........................................................................
//---- other parameters
static int prevtime=0;
int ticket=0;
int x=1;
//-------------------------
int Magic_BUY =123;
int Magic_SELL =321;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//--------------------------------
if(Digits == 5) x=10;
MathSrand(TimeCurrent());
//--------------------------------
if (step_BUY< 20) step_BUY=20;
if (step_SELL<20) step_SELL=20;
if( level_BUY< 1) level_BUY= 1;
if( level_BUY>99) level_BUY=99;
if( level_SELL<1 ) level_SELL= 1;
if( level_SELL>99) level_SELL=99;
//--------------------------------
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//..........................................................................
if (Time[0] == prevtime) return(0);
prevtime = Time[0];
if (!IsTradeAllowed()) {
prevtime=Time[1]; Sleep(30000 + MathRand()); //--- ôîðìèðîâêà áàðà---
}
//..........................................................................
My_Play( Magic_BUY, In_BUY, level_BUY, step_BUY); //---òîðãîâëÿ ïî ëîíãàì
My_Play(Magic_SELL,In_SELL,level_SELL,step_SELL); //---òîðãîâëÿ ïî øîðòàì
//..........................................................................
return(0);//-----------âûõîä èç ñòàðòîâîé ôóíêöèè------------
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void My_Play(int mn,bool flag,int level,int step) {
int total=OrdersTotal();
for (int i = 0; i < total; i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES);//---ïðîõîä ïî îðäåðàì--
if (OrderSymbol() == Symbol() && OrderMagicNumber() == mn) {
return(0);
}
}
//..........................................................................
ticket = -1;
if ( flag &&
OrdersTotal()==0 &&
level*327.68 > MathRand() && //----ðàíäîìíûé âõîä â ðûíîê ---
IsTradeAllowed()) {
if (mn<200) {
ticket= OrderSend(Symbol(), OP_BUY,lot(),Ask,5*x,Bid-x*step*Point,Ask+x*step*Point,DoubleToStr(mn,0),mn,0,Blue);
}
else {
ticket= OrderSend(Symbol(),OP_SELL,lot(),Bid,5*x,Ask+x*step*Point,Bid-x*step*Point,DoubleToStr(mn,0),mn,0, Red);
}
RefreshRates();
if ( ticket < 0) { Sleep(30000); prevtime = Time[1]; }
} //-- Exit ---
return(0); }
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
double lot() { int k=1; if ( !MartIn ) return (0.1);
if (OrdersHistoryTotal()==0) return(0.1);
int i,accTotal=OrdersHistoryTotal()-1;
for (i=accTotal;i>-1;i--)// ïåðåáîð îðäåðîâ...
{ if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
{ Print("Îøèáêà ïðè äîñòóïå ê èñòîðè÷åñêîé áàçå (",GetLastError(),")");
break; } // ðàáîòà ñ îðäåðîì ...
if(OrderCloseTime()!=0 && OrderProfit()>0) bool mart=true; //-------
if (OrderCloseTime()!=0 && OrderProfit()<0 && !mart) k=2*k;
}//-end-for-
return(k*0.1);}//-----
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---