CHinGsMAroonCLK_2[1].2
Download scriptThis automated trading script is designed to manage trading positions on the MetaTrader platform by employing a dynamic entry and management strategy. It focuses on identifying specific price movements and adjusting trade sizes accordingly while maintaining strict oversight of account safety.
Core Trading Logic
1. Automated Entry Rules The script monitors both "Buy" and "Sell" opportunities simultaneously. It is programmed to enter a trade only when market conditions meet specific criteria:
- Spread Control: The script checks the current market spread (the difference between the buy and sell price) to ensure it is within a "regular" range before opening any position. This prevents entering trades during periods of extreme volatility or low liquidity.
- Initial Entry: If no positions are currently open, the script initiates a trade based on the base lot size provided in the settings.
2. Dynamic Position Scaling (Grid/Martingale Hybrid) Rather than opening all trades at once, the script manages a sequence of positions:
- Distance Tracking: After an initial position is opened, the script waits for the price to move against the trade by a minimum pre-defined distance (the "MinGS" setting).
- Incremental Scaling: If the price continues to move against the position, the script opens subsequent trades. The volume of these new trades is increased automatically using a multiplier. This means that as the price moves further away from the entry point, the size of the new trades grows to help average out the entry price.
- Trade Limits: To prevent excessive risk, the script has a hard cap on the maximum number of concurrent trades it will open in one direction.
3. Unified Take Profit Management One of the primary features of this script is how it handles closing profits:
- Synchronized Exit: When the script opens multiple positions to average out a price, it synchronizes their "Take Profit" levels.
- Trailing Targets: As new trades are added to an existing sequence, the script adjusts the exit targets of the older trades so that the entire group of positions closes at the same price point. This ensures that the total profit for the sequence is realized collectively.
Account Safety and Monitoring
1. Automated Identity Management The script contains a built-in identification system. It recognizes various currency pairs (such as EURUSD, GBPJPY, etc.) and assigns a unique "Magic Number" to each. This allows the script to manage trades on different charts independently without interfering with other open orders or different currency pairs.
2. Margin Protection The script includes an automated safety monitor that tracks the health of your trading account:
- Margin Level Alerts: It constantly calculates your account's Margin Level (the ratio of equity to used margin).
- Email Notifications: If your margin level drops below a specific warning threshold, the script is designed to send an automated email notification to the user. This serves as a "early warning system" to alert the trader that the account may be reaching a risky level of leverage.
Summary of Operation
In plain terms, this tool acts as an autonomous manager that enters the market, identifies when the price is moving against it, and systematically adds larger positions at set intervals to lower the average entry cost. It then manages these multiple positions as a single unit, aiming to close them all at a specific profit target while providing real-time alerts if the account's financial health becomes compromised.
//+------------------------------------------------------------------+
//| clk200_maroon73_chings73_v2.2 |
//| Copyright © Nov_2007, CHinGsMAroonCLK |
//| chingmaroonclk@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, CHinGsMAroonCLK."
#property link "http://chingsforex.blogspot.com/"
extern int MinGS=20;
extern int TP=27;
extern double lot=0.10;
extern int MaxTrades=7;
extern int RegularSpread=3;
extern double Multiplier=2;
extern double LotInc=0;
extern double MarginLevelAlert=1000;
int magic;
double lot2;
datetime tob,tos,toe; //Time Out Buy & Sell & email
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
int init()
{
if (Symbol() == "AUDCADm" || Symbol() == "AUDCAD") { magic = 211001; }
if (Symbol() == "AUDJPYm" || Symbol() == "AUDJPY") { magic = 211002; }
if (Symbol() == "AUDNZDm" || Symbol() == "AUDNZD") { magic = 211003; }
if (Symbol() == "AUDUSDm" || Symbol() == "AUDUSD") { magic = 211004; }
if (Symbol() == "CHFJPYm" || Symbol() == "CHFJPY") { magic = 211005; }
if (Symbol() == "EURAUDm" || Symbol() == "EURAUD") { magic = 211006; }
if (Symbol() == "EURCADm" || Symbol() == "EURCAD") { magic = 211007; }
if (Symbol() == "EURCHFm" || Symbol() == "EURCHF") { magic = 211008; }
if (Symbol() == "EURGBPm" || Symbol() == "EURGBP") { magic = 211009; }
if (Symbol() == "EURJPYm" || Symbol() == "EURJPY") { magic = 211010; }
if (Symbol() == "EURUSDm" || Symbol() == "EURUSD") { magic = 211011; }
if (Symbol() == "GBPCHFm" || Symbol() == "GBPCHF") { magic = 211012; }
if (Symbol() == "GBPJPYm" || Symbol() == "GBPJPY") { magic = 211013; }
if (Symbol() == "GBPUSDm" || Symbol() == "GBPUSD") { magic = 211014; }
if (Symbol() == "NZDJPYm" || Symbol() == "NZDJPY") { magic = 211015; }
if (Symbol() == "NZDUSDm" || Symbol() == "NZDUSD") { magic = 211016; }
if (Symbol() == "USDCHFm" || Symbol() == "USDCHF") { magic = 211017; }
if (Symbol() == "USDJPYm" || Symbol() == "USDJPY") { magic = 211018; }
if (Symbol() == "USDCADm" || Symbol() == "USDCAD") { magic = 211019; }
if (magic == 0) { magic = 211999; }
return (0);
return(0);
}
void OpenBuy()
{
int ticket,err;
if (!GlobalVariableCheck("InTrade")) {
GlobalVariableSet("InTrade", CurTime()); // set lock indicator
ticket = OrderSend(Symbol(),OP_BUY,lot2,Ask,1,0,Ask+TP*Point,"EA Order",magic,0,Red);
GlobalVariableDel("InTrade"); // clear lock indicator
}
}
void OpenSell()
{
int ticket,err;
if (!GlobalVariableCheck("InTrade")) {
GlobalVariableSet("InTrade", CurTime()); // set lock indicator
ticket = OrderSend(Symbol(),OP_SELL,lot2,Bid,1,0,Bid-TP*Point,"EA Order",magic,0,Red);
GlobalVariableDel("InTrade"); // clear lock indicator
}
}
void ManageBuy()
{
int lasttradetime = 0;
double lastopenprice=0;
double maxlots = 0;
double lasttp=0;
int lastordertype=-1;
int lastorderticket=0;
int y=0;
for (y = 0; y < OrdersTotal(); y++)
{
OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
if ((OrderMagicNumber() != magic) || (OrderType()!=OP_BUY)) { continue; }
if (OrderOpenTime() > lasttradetime) {
lasttradetime = OrderOpenTime();
lastopenprice = OrderOpenPrice();
lastordertype=OrderType();
lastorderticket=OrderTicket();
lasttp=OrderTakeProfit();
}
if (OrderLots() > maxlots) { maxlots = OrderLots(); }
}
int lvl=MathRound(MathLog((maxlots-LotInc)/lot)/MathLog(Multiplier)+1);
Print(lvl);
if (lvl<0) lvl=0;
lot2=lot*MathPow(Multiplier,lvl)+LotInc;
if ((lvl==0) && (((Ask-Bid)/Point)<=RegularSpread)) OpenBuy();
if ((lastopenprice-Ask>MinGS*Point) && (lvl<MaxTrades) && (((Ask-Bid)/Point)<=RegularSpread)) {
OpenBuy();
return(0);
}
/////////// TP
/*
double sumlots=0;
double sump=0;
double avgp=0;
for (y = 0; y < OrdersTotal(); y++)
{
OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
if ((OrderMagicNumber() != magic) || (OrderType()!=OP_BUY)) { continue; }
sumlots=sumlots+OrderLots();
sump=OrderOpenPrice()*OrderLots()+sump;
}
if (sumlots>0) {
avgp=NormalizeDouble(sump/sumlots,Digits);
double tpp=NormalizeDouble(TP/10/sumlots*Point+avgp,Digits);
for (y = 0; y < OrdersTotal(); y++)
{
OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
if ((OrderMagicNumber() != magic) || (OrderType()!=OP_BUY) || (OrderTakeProfit()==tpp)) { continue; }
OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),tpp,0,Red);
}
}
*/
for (y = 0; y < OrdersTotal(); y++)
{
OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
if ((OrderMagicNumber() != magic) || (OrderType()!=OP_BUY) || (OrderTakeProfit()==lasttp) || (lasttp==0)) { continue; }
OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),lasttp,0,Red);
}
}
void ManageSell()
{
int lasttradetime = 0;
double lastopenprice=0;
double maxlots = 0;
double lasttp=0;
int lastordertype=-1;
int lastorderticket=0;
int y=0;
for (y = 0; y < OrdersTotal(); y++)
{
OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
if ((OrderMagicNumber() != magic) || (OrderType()!=OP_SELL)) { continue; }
if (OrderOpenTime() > lasttradetime) {
lasttradetime = OrderOpenTime();
lastopenprice = OrderOpenPrice();
lastordertype=OrderType();
lastorderticket=OrderTicket();
lasttp=OrderTakeProfit();
}
if (OrderLots() > maxlots) { maxlots = OrderLots(); }
}
int lvl=MathRound(MathLog((maxlots-LotInc)/lot)/MathLog(Multiplier)+1);
if (lvl<0) lvl=0;
lot2=lot*MathPow(Multiplier,lvl)+LotInc;
if ((lvl==0) && (((Ask-Bid)/Point)<=RegularSpread)) OpenSell();
if ((Bid-lastopenprice>MinGS*Point) && (lastopenprice>0) && (lvl<MaxTrades) && (((Ask-Bid)/Point)<=RegularSpread)) {
OpenSell();
return(0);
}
/////////// TP
/* double sumlots=0;
double sump=0;
double avgp=0;
for (y = 0; y < OrdersTotal(); y++)
{
OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
if ((OrderMagicNumber() != magic) || (OrderType()!=OP_SELL)) { continue; }
sumlots=sumlots+OrderLots();
sump=OrderOpenPrice()*OrderLots()+sump;
}
if (sumlots>0) {
avgp=NormalizeDouble(sump/sumlots,Digits);
double tpp=NormalizeDouble(-TP/10/sumlots*Point+avgp,Digits);
for (y = 0; y < OrdersTotal(); y++)
{
OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
if ((OrderMagicNumber() != magic) || (OrderType()!=OP_SELL) || (OrderTakeProfit()==tpp)) { continue; }
OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),tpp,0,Red);
}
}
*/
for (y = 0; y < OrdersTotal(); y++)
{
OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
if ((OrderMagicNumber() != magic) || (OrderType()!=OP_SELL) || (OrderTakeProfit()==lasttp) || (lasttp==0)) { continue; }
OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),lasttp,0,Red);
}
}
void CheckMargin()
{
if (AccountMargin()!=0) {
if (((AccountEquity()/AccountMargin()*100)<MarginLevelAlert) && (TimeCurrent()>toe+3600)) {
SendMail("Account Margin Warning","Account Free Margin is "+AccountFreeMargin());
toe=TimeCurrent();
}
}
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
ManageBuy();
ManageSell();
CheckMargin();
//----
return(0);
}
//+------------------------------------------------------------------+
Profitability Reports
Expected payoff
Average expected payoff across the available backtests for each symbol.
Comments