Putting in order and adding a tralling stop

This article is a continuation of an earlier series on the creation of trade expert (cf. 61 и 63 issues of ForTrader.org). Unfortunately, both for me and for many readers, the work in this direction was suspended for several months. There were quite a few reasons for that, but the main thing is that I am going back to this work.

The results of the work done

The short testing period that took place was able to demonstrate some viability of the idea. The profit was insignificant, but more importantly, there was no loss. In this article I will try to take some actions to improve the performance of the program. To begin with I will try to clarify some obscure points in the code and correct some minor errors.

Errors and ambiguities in the expt code

Line 18 of the code:
Pause_Of_Trade_Try = 2; //pause between attempts to open an order (in seconds)

Here the pause is not in seconds, but in tenths of seconds, i.e., 2 = 0.2 seconds.

Lines 118...119 (and similar code later in the robot):
return(OrderTicket());
break; //exit from the loop

Here it may not be quite clear what it is worth breakbecause we have already exited the loop by returning the function value, which means that this operator is not executed. It's just more my habit to place a break operator wherever I need to exit a loop. But if memory serves me right, I've encountered programming languages where the loop is executed without break even after return. Well, all programmers know that, for example, in switch there is a "failure" to the next operator if breal is not used in C. Put it specifically here at all not necessarily.

Line 169 (and the sl function body):
double sl(int sl_value, int type, string symb="NONE", int rmode=1) {

Here it may not be quite clear why the parameter rmode is needed and why, depending on its value, the function counts SL prices for purchases either From Ask Price (at value 1), or from the price of Bid (with a value of 2), and for sales from either Bid or Ask, respectively.

The fact is that there are often disputes, What price should be used to calculate the StopLoss price. There is an opinion that when we get a stop, we should get exactly the loss value that we are waiting for (i.e. we put SL=100 in the parameter and get 100 points of loss on the stop). And there is an opinion that we should count the stop so that it captures spreadThe second case is when the spread will not allow us to set a stop at a specified distance. In the second case we get a stop equal to 100+ points.

I use the first variant more often, and it is for this variant that the function works with rmode=1 (i.e., by default). By introducing this parameter I show that I am aware of both points of view, that I generally do not impose any of them (if you're a supporter of the second option, just change 1 to 2 in the code), although I personally use in my work exactly the first option.

The rest of the code seems quite logical and "transparent" to me, but I will probably explain them in the future as well.

Innovations in the trading expert

The first result, albeit in a short time, has been obtained. It is too early to talk about changing/optimizing the trading signal search algorithm. Therefore, we will add a couple of new functions. The first one is trailing stop. For this, I will take a function that has already become standard in many of my robots, and modify it a bit (so that it can be used simultaneously on different characters). The function has several input parameters, which are represented in the code as:

//+———————————————————————-+
//+                           trailing stop +
extern bool UseTrailing = true; //enable/disable T-SL
extern int TrailingStop = 50; // Fixed trailing size
extern int TrailingStep = 1; // Trailing step
//+                           trailing stop +
//+———————————————————————-+

The function to work on the current symbol looks like this:

//+——————————————————————————————————————-+
//|                                              trailing stop loss |
void T_SL() {
if(!UseTrailing) return;
int i = 0;
for(i=0; i<OrdersTotal(); i++) {
if(!(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))) continue;
if(OrderSymbol() != Symbol() || OrderMagicNumber()!=Magic_Number) continue;

if(OrderType()==OP_BUY) {
if(NormalizeDouble(Bid-OrderOpenPrice(),Digits)>NormalizeDouble(TrailingStop*Point,Digits)) {
if(NormalizeDouble(OrderStopLoss(),Digits)<NormalizeDouble(Bid-(TrailingStop+TrailingStep-1)*Point,Digits))
OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Bid-TrailingStop*Point,Digits), OrderTakeProfit(), 0, CLR_NONE);
} //end if(NormalizeDouble(Bid-OrderOpenPrice(),Digits)>NormalizeDouble(TrailingStop*Point,Digits))
} //end if(OrderType()==OP_BUY)

if(OrderType()==OP_SELL) {
if(NormalizeDouble(OrderOpenPrice()-Ask,Digits)>NormalizeDouble(TrailingStop*Point,Digits)) {
if(NormalizeDouble(OrderStopLoss(),Digits)>NormalizeDouble(Ask+(TrailingStop+TrailingStep-1)*Point,Digits))
OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Ask+TrailingStop*Point,Digits), OrderTakeProfit(), 0, CLR_NONE);
} //end if(NormalizeDouble(OrderOpenPrice()-Ask,Digits)>NormalizeDouble(TrailingStop*Point,Digits))
} //end if(OrderType()==OP_SELL)
} //end for(i=0; i<OrdersTotal(); i++)
} //end void T_SL()
//|                                                trailing stop loss |
//+——————————————————————————————————————-+

For our robot, it needs to be brought to a form in which it can process all trades (with a given magic number) of all instruments:

//+——————————————————————————————————————-+
//|                                                 trailing stop loss |
void T_SL() {
if(!UseTrailing) return;
string symb=";
int i = 0;


double ask=0.0, bid=0.0, dig=0.0, pp=0.0;
for(i=0; i<OrdersTotal(); i++) {
if(!(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))) continue;
if(OrderMagicNumber()!=Magic_Number) continue;
symb=OrderSymbol();
pp=MarketInfo(symb,MODE_POINT);
dig=MarketInfo(symb,MODE_DIGITS);
ask=MarketInfo(symb,MODE_ASK);
bid=MarketInfo(symb,MODE_BID);
if(OrderType()==OP_BUY) {
if(NormalizeDouble(bid-OrderOpenPrice(),dig)>NormalizeDouble(TrailingStop*pp,dig)) {
if(NormalizeDouble(OrderStopLoss(),dig)<NormalizeDouble(bid-(TrailingStop+TrailingStep-1)*pp,dig))
OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(bid-TrailingStop*pp,dig), OrderTakeProfit(), 0, CLR_NONE);
} //end if(NormalizeDouble(Bid-OrderOpenPrice(),Digits)>NormalizeDouble(TrailingStop*Point,Digits))
} //end if(OrderType()==OP_BUY)

if(OrderType()==OP_SELL) {
if(NormalizeDouble(OrderOpenPrice()-ask,dig)>NormalizeDouble(TrailingStop*pp,dig)) {
if(NormalizeDouble(OrderStopLoss(),dig)>NormalizeDouble(ask+(TrailingStop+TrailingStep-1)*pp,dig))
OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(ask+TrailingStop*pp,dig), OrderTakeProfit(), 0, CLR_NONE);
} //end if(NormalizeDouble(OrderOpenPrice()-Ask,Digits)>NormalizeDouble(TrailingStop*Point,Digits))
} //end if(OrderType.()==OP_SELL)
} //end for(i=0; i<OrdersTotal(); i++)
} //end void T_SL()
//|                                                trailing stop loss |
//+——————————————————————————————————————-+

After that, all that remains is to put the function call inside the start() function (see in the final version).

The second feature that might be useful, and which I decided to include in the robot, is sound signal when a trade is opened by an Expert Advisor. This will be a very small addition, so I will not describe it in detail, to see the full code of the robot you can here.

Conclusion

In the next version expert There will be more interesting functions than today. They will be aimed at risk control and trading volume management, but in the meantime, let's follow. But this version will show us.

Комментарии ( 3 )

  1. The second feature that could be useful and which I decided to include in my robot is an audible signal when a trade is opened by a trading Expert Advisor. This will be a very small addition, so I will not describe it in detail, you can check out the full code of the robot here.

  2. The next version of the Expert Advisor will have more interesting functions than today. They will be aimed at risk control and management of trade volumes, but in the meantime, let us follow. But this version will show us

Leave a Reply

Back to top button