The robot was sleeping and we were working
As it turned out, the robot was unable to make trades for the whole month. It happened as a result of a fairly trivial error: the terminal settings did not allow trading to the expert. And for the sake of purity of the experiment, I did not even look at the server and see what was going on there. And that played a cruel trick.
However, this does not prevent us from making some additions to the Expert Advisor code. This time let's pay attention to the risks.
Addendum #1
I am a supporter of very conservative trading systems, where trades are kept open for many months and risks are strictly controlled. In the case of this robot, we have a completely different picture. An Expert Advisor makes trades that are quickly closed. Risks are practically not controlled in any way. An important and useful addition will be limitation on aggregate riskwhich the robot may incur for one month.
In other words, we tell the robot, "If you lose X percent of your deposit this month, you're a bad boy, and we'll put you in a corner. Until next month. And next month we'll start all over again: we'll take the deposit at the beginning of the month at 100% and again we'll make sure that the robot doesn't lose too much.
The end module (function) has the following code:
//+——————————————————————————————————-+
//| Determining the fact that the robot has reached the allowable drawdown level |
bool is_mn_max_los(double percent_of_max_los) {
double start_depo_mn=0.0, tmp_value=0.0;
int i = 0; // counter for cycles
for (i=OrdersHistoryTotal()-1; i>=0; i-) {
if (!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
//if no order is selected, go to the next step of the loop
if(OrderCloseTime()>iTime(NULL,PERIOD_MN1,0))
tmp_value+=OrderProfit()+OrderCommission()+OrderSwap();
}
for (i=OrdersTotal()-1; i>=0; i-) {
if (!OrderSelect(i,SELECT_BY_POS,MODE_OPEN)) continue;
//if no order is selected, go to the next step of the loop
tmp_value+=OrderProfit()+OrderCommission()+OrderSwap();
}
start_depo_mn=AccountEquity()-tmp_value;
GlobalVariableSet("ForTrader_5min",iTime(NULL,PERIOD_M5,0));
if((-1)*tmp_value>(start_depo_mn/100)*percent_of_max_los)
return(true); //if we lost more than allowed - return true
return(false);
}
//| Determining the fact that the robot has reached the allowable drawdown level |
//+——————————————————————————————————-+
Addendum #2
Since when the permissible level of drawdown is reached, it has been decided close all trades opened by the robot, then we will need a function that provides this. I will take a ready function from my set which allows to close all orders of this type with the given magic number:
//+——————————————————————————————————-+
//| Transaction closing operation |
bool cbm(int magic, int slipage, int type) {
/*
close by magic (close all orders of this type with the given MagicNumber)
The maximum allowable slipage is taken into account
Used by function close_by_ticket.
*/
int n = 0;
for (int i2=OrdersTotal()-1; i2>=0; i2-) {
if (!OrderSelect(i2,SELECT_BY_POS,MODE_TRADES)) break;
if ((OrderType()==type) && (OrderMagicNumber()==magic)) { //if a trade of this type and its magic number match
close_by_ticket(OrderTicket(), slipage); // closing the deal
n++; //increase the counter of closed trades (actually - closing attempts)
} //end if (((OrderType()==OP_BUY) || (OrderType()==OP_SELL)) && (OrderMagicNumber()==magic))
} //end for (int i2=OrdersTotal()-1; i2>=0; i2-)
if(n>0) //if there were more than 0 closure attempts, the function returns true
return(true);
return(false);
} //end bool cbm(int magic, int slipage, int type)
//| Transaction closing operation |
//+——————————————————————————————————-+
Thus, the interaction of these functions in the robot will look like this:
if(is_mn_max_los(Percent_Of_Max_Los)) {
cbm(Magic_Number, Slipage, OP_BUY);
cbm(Magic_Number, Slipage, OP_SELL);
Comment("Within the current month the loss limit of ",Percent_Of_Max_Los, "% of the deposit at the beginning of the month!)
return(0);
}
The code of the whole trading robot took the following form: Download Expert
Conclusion
Unfortunately, the article turned out to be smaller than I had planned. The reason is my mistake in starting the robot last time. But now that the Expert Advisor is finally running, there will be enough material to evaluate its effectiveness, analyze errors (if any), and modernize the heart trade expert - signal search algorithm. In addition, it is planned to bring some additions to the calculation of working lots and risk-tracking (in addition to the additions that have been made).