This script, designed for the MetaTrader platform, is an automated trading system, also known as an "Expert Advisor," that operates based on a strategy called "Martingale." Here's how it works in plain language:
-
Initial Setup: The script begins by setting up some initial parameters provided by the user. These include:
- Stop Loss: The maximum amount of loss (in points) the script will allow for a single trade.
- Take Profit: The amount of profit (in points) the script aims to achieve for a single trade.
- Lot Size: The initial size (volume) of the trades it will place.
- Martingale Coefficient: A multiplier used to increase the stop loss and take profit for subsequent trades after a losing trade.
- Magic Number: A unique identifier to distinguish trades made by this script from other trades.
- Stop After Profit: A switch to determine if the expert advisor will stop operating after it gets a profit. It also adjusts the "Stop Loss" and "Take Profit" values depending on the currency pair being traded.
-
Trading Logic: The script continually monitors the market for trading opportunities, but only places a trade after a previous trade has closed. It operates in a loop by first looking at the currently open orders (trades). If the EA sees one of its trades already open, it stops and does nothing. If it doesn't see any trades open, it will analyze the history of past trades, particularly those placed by this specific script, using the "Magic Number" to identify them.
-
Martingale Element (After a Loss): If the most recent trade closed with a loss, the script then performs the Martingale element of the strategy. The Martingale principle basically doubles down after losses, but in this case, the stop loss and take profit levels are increased. The new stop loss and take profit levels are based on the old stop loss/take profit amount, multiplied by the Martingale coefficient.
-
Martingale Element (After a Win): If the most recent trade was profitable, and the "Stop After Profit" option is enabled, the script will shut itself down. Otherwise, the script resets to use the initial (smaller) "Stop Loss" and "Take Profit" values for the next trade.
-
Trade Execution: After determining what trade to make, the script then performs the order to either buy or sell on the market depending on the last order opened.
- If the last trade was a "buy", the script will make a "sell" trade.
- If the last trade was a "sell", the script will make a "buy" trade. The script then places a market order, setting the stop loss and take profit according to the parameters calculated, and the "Magic Number" so it can track its own trades.
In essence, this script tries to recover from losses by increasing the stop loss and take profit on the next trade. This is a high-risk strategy, as a series of losing trades can rapidly deplete an account. It will trade only if there aren't any open orders done by itself on the market. It operates continuously, attempting to open a new position after each closed trade.
//+------------------------------------------------------------------+
//| Nevalyashka_Martingail.mq4 |
//| Copyright © 2010, Õëûñòîâ Âëàäèìèð |
//| cmillion@narod.ru |
//| |
//--------------------------------------------------------------------
#property copyright "Copyright © 2016, Õëûñòîâ Âëàäèìèð"
#property link "cmillion@narod.ru"
#property version "1.00"
#property strict
#property description "Ñîâåòíèê îòêðûâàåò ïîñëå óáûòêà îðäåðà ñ óâåëè÷åííûìè íà êîýôôèöèåíò ñòîïàìè"
//--------------------------------------------------------------------
extern int stoploss = 150,
takeprofit = 50;
extern double Lot = 0.1;
extern double KoeffMartin = 1.5;//êîýôôèöèåíò óâåëè÷åíèÿ ñòîïîâ
extern int Magic = 0;
extern bool StopAfteProfit = false;//îñòàíîâêà ïîñëå ïðîôèòà
double sl=0,tp=0;
//--------------------------------------------------------------------
int OnInit()
{
if(Digits==5 || Digits==3)
{
stoploss*=10;
takeprofit*=10;
}
sl=stoploss*Point;
tp=takeprofit*Point;
return(INIT_SUCCEEDED);
}
//--------------------------------------------------------------------
void OnTick()
{
int tip=0,i;
for(i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
return;
}
}
}
for(i=OrdersHistoryTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderProfit()<0)
{
sl = MathAbs(OrderStopLoss()-OrderOpenPrice())*KoeffMartin;
tp = MathAbs(OrderTakeProfit()-OrderOpenPrice())*KoeffMartin;
}
else
{
if(StopAfteProfit) ExpertRemove();
sl=stoploss*Point;
tp=takeprofit*Point;
}
tip=OrderType();
break;
}
}
}
if(tip==OP_BUY)
if(OrderSend(Symbol(),OP_SELL,Lot,Bid,3,NormalizeDouble(Ask+sl,Digits),NormalizeDouble(Bid-tp,Digits)," ",Magic,Blue)==-1)
Print("Error ",GetLastError()," Bid=",DoubleToStr(Bid,Digits)," sl=",DoubleToStr(Ask+sl,Digits)," tp=",DoubleToStr(Bid-tp,Digits));
if(tip==OP_SELL)
if(OrderSend(Symbol(),OP_BUY,Lot,Ask,3,NormalizeDouble(Bid-sl,Digits),NormalizeDouble(Ask+tp,Digits)," ",Magic,Blue)==-1)
Print("Error ",GetLastError()," Ask=",DoubleToStr(Ask,Digits)," sl=",DoubleToStr(Bid-sl,Digits)," tp=",DoubleToStr(Ask+tp,Digits));
return;
}
//-----------------------------------------------------------------
Comments