Discussion of article "Step-by-Step Guide to Writing an Expert Advisor in MQL5 for Beginners" - page 27

 
Rustam Ayupov #:

Dear Vladimir ! I got to your titanic work for dummies, my priorities are changing, demo trading will be its own way, and the main thing now is your school for beginners, I chew valuable squeezes of your experience, it is invaluable, there are no words. If I get to how to insert a trailing stop into this EA, I will probably do it as a priority, because as a result of forward optimisation, the EA creates long takeprofits, their safety net has become screaming. And in the course of training in your school I will probably learn to see mistakes, without which there are no working programmes, and correct them. THANK YOU.

Sincerely, Rustam.

You are welcome, Rustam!

With respect, Vladimir.

 
Valeriy Yastremskiy #:

Reply or New Comment, at the bottom Attach a file.

THANK YOU for the ingenious simplicity) And a question, how would you, with your trailing experience, insert one of these into this EA ?

 
MrBrooklin #:

Please, Rustam!

Regards, Vladimir.

I found a unique, simple to genius, "trailing method" of the Expert Advisor, the author himself, which, as is traditional for this development, is not executed as the author commented. He, like me, is probably also a dummy, but a big one) He promised that when the price reverses, the previously opened position is closed, well, yes, it would be great if it were... but only two open positions are hanging, in both directions, and are not closed. Earlier, before your first advice, there were a hail of open positions in both directions, which were not closed. Try to squeeze this cherry on the cake?

 
Rustam Ayupov #:

THANK YOU for the ingenious simplicity) And a question, how would you, with your trailing experience, insert one into this EA ?

If the result with trailing is better than without it, then leave it.

 
Valeriy Yastremskiy #:

If the result is better with the trawl than without it, then leave it.

It's not difficult to insert, but what to insert? To test and evaluate.

 
Rustam Ayupov #:

It's not hard to put it in, but what is it to put in? To test it out.

trawl code into your EA without trawl. It is better to put the trawl code into a function. And call the trawl by condition, if there is an order.

 
Rustam Ayupov #:

I found a unique, simple to genius, "trailing method" of the Expert Advisor, the author himself, which, as is traditional for this development, is not executed as the author commented. He, like me, is probably also a dummy, but a big one) He promised that when the price reverses, the previously opened position is closed, well, yes, it would be great if it were... but only two open positions are hanging, in both directions, and are not closed. Earlier, before your first advice, there were a hail of open positions in both directions, which were not closed. Try to squeeze this cherry on the cake?

Hello, Rustam! In order for us to continue to cooperate productively, you first need to learn how to attach the EA code with problems to your message. If you have problems while testing the EA, you need to save this code under a new name, compile it and then attach it to the message. You must save the EA under a new name and compile it !!! Otherwise, we will get confused in different variants when communicating with you.

If for some reason you cannot attach the code of the EA, you can use another way:

  1. In MetaEditor, highlight the EA code with problems by pressing Ctrl+A.
  2. Copy the EA code by pressing the Ctrl+C button.
  3. Create a post that you want to post here on the forum and by pressing the AL+S button open a field where you need to paste the copied code by pressing the Ctrl+V button.
  4. Add your post to the forum

If you do not learn to attach or place in your posts the code of the EA with problems, then further dialogue with you becomes useless.

Regards, Vladimir.

 
MrBrooklin #:

Hello, Rustam! In order for us to continue to cooperate productively, you first need to learn how to attach the EA code with problems to a message. If you have problems while testing the EA, you need to save this code under a new name, compile it and then attach it to the message. You must save the EA under a new name and compile it !!! Otherwise, you and I will get confused in different variants when communicating with you.

If for some reason you cannot attach the code of the EA, you can use another way:

  1. In MetaEditor, highlight the EA code with problems by pressing Ctrl+A.
  2. Copy the EA code by pressing the Ctrl+C button.
  3. Create a post that you want to post here on the forum and by pressing the AL+S button open a field where you need to paste the copied code by pressing the Ctrl+V button.
  4. Add the message to the forum

If you do not learn how to attach or post the EA code with problems in your posts, then further dialogue with you becomes useless.

Regards, Vladimir.

Sorry for the delay, I burned out over the weekend, but found the cause of the last problem, as it turned out the broker FXopen, just did not grow up to MT5. Well and taught me the simplicity of file selection, to send. The first two are your optimisation of the author's work, where you stopped the author's inattention in the code, with massive opening of positions, instead of one, a single, the second two are his declaration of closing a position when the price reverses. The second is worth working on, after all, the author, if not finished, but wanted to profitably).

 
Rustam Ayupov #:

Sorry for the delay, over the weekend burned out, 24 hours, but found the cause of the last problem, as it turned out the broker FXopen, just did not grow up to MT5. Well and taught me the simplicity of file selection, to send. The first two are your optimisation of the author's work, where you stopped the author's inattention in the code, with massive opening of positions, instead of one, a single, the second two are his declaration of closing a position when the price reverses. The second is worth working on, after all, the author, if not finished, but wanted to profitably).

Well with this, the last one, I'm still sorting it out, trying to understand something and start to navigate in it, while to the insurance of long takeprofits. I am already ready for the author's surprises in the future, the road will be travelled by the one who walks).

Files:
 
Rustam Ayupov #:

Well with this last one, I am still trying to understand something and start to orientate myself in it, for the time being to hedge long takeprofits. I am already ready for the author's surprises in the future, the road will be travelled by the one who walks).

Hello, Rustam! I attach the working version of this Expert Advisor at the bottom of the message. The changes have been made to this section of the code:

//--- Putting everything together 
   if(Buy_Condition_1 && Buy_Condition_2)
     {
      if(Buy_Condition_3 && Buy_Condition_4 && PositionsTotal()==0)
        {
         ZeroMemory(mrequest);
         mrequest.action = TRADE_ACTION_DEAL;                                  // immediate order execution
         mrequest.price = NormalizeDouble(latest_price.ask,_Digits);           // latest ask price
         mrequest.sl = NormalizeDouble(latest_price.ask - STP*_Point,_Digits); // Stop Loss
         mrequest.tp = NormalizeDouble(latest_price.ask + TKP*_Point,_Digits); // Take Profit
         mrequest.symbol = _Symbol;                                            // currency pair
         mrequest.volume = Lot;                                                 // number of lots to trade
         mrequest.magic = EA_Magic;                                             // Order Magic Number
         mrequest.type = ORDER_TYPE_BUY;                                        // Buy Order
         mrequest.type_filling = ORDER_FILLING_FOK;                             // Order execution type
         mrequest.deviation=100;                                                // Deviation from current price
         //--- send order
         bool check=OrderSend(mrequest,mresult);
         // get the result code
         if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed
           {
            Alert("A Buy order has been successfully placed with Ticket#:",mresult.order,"!!");
           }
         else
           {
            Alert("The Buy order request could not be completed -error:",GetLastError());
            ResetLastError();           
            return;
           }
        }
     }
/*
 2. Check for a Short/Sell Setup : MA-8 decreasing downwards, 
 previous price close below it, ADX > 22, -DI > +DI
*/.
//--- Declare bool type variables to hold our Sell Conditions
   bool Sell_Condition_1 = (maVal[0]<maVal[1]) && (maVal[1]<maVal[2]);  // MA-8 decreasing downwards
   bool Sell_Condition_2 = (p_close <maVal[1]);                         // Previous price closed below MA-8
   bool Sell_Condition_3 = (adxVal[0]>Adx_Min);                         // Current ADX value greater than minimum (22)
   bool Sell_Condition_4 = (plsDI[0]<minDI[0]);                         // -DI greater than +DI

//--- Putting everything together
   if(Sell_Condition_1 && Sell_Condition_2)
     {
      if(Sell_Condition_3 && Sell_Condition_4 && PositionsTotal()==0)
        {
         ZeroMemory(mrequest);
         mrequest.action=TRADE_ACTION_DEAL;                                // immediate order execution
         mrequest.price = NormalizeDouble(latest_price.bid,_Digits);           // latest Bid price
         mrequest.sl = NormalizeDouble(latest_price.bid + STP*_Point,_Digits); // Stop Loss
         mrequest.tp = NormalizeDouble(latest_price.bid - TKP*_Point,_Digits); // Take Profit
         mrequest.symbol = _Symbol;                                          // currency pair
         mrequest.volume = Lot;                                              // number of lots to trade
         mrequest.magic = EA_Magic;                                          // Order Magic Number
         mrequest.type= ORDER_TYPE_SELL;                                     // Sell Order
         mrequest.type_filling = ORDER_FILLING_FOK;                          // Order execution type
         mrequest.deviation=100;                                             // Deviation from current price
         //--- send order
         bool check=OrderSend(mrequest,mresult);
         // get the result code
         if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed
           {
            Alert("A Sell order has been successfully placed with Ticket#:",mresult.order,"!!");
           }
         else
           {
            Alert("The Sell order request could not be completed -error:",GetLastError());
            ResetLastError();
            return;
           }
        }
     }
   return;
  }
//+------------------------------------------------------------------+

What is highlighted in yellow was added:

&& PositionsTotal()==0

and also removed the condition related to Buy_opened and Sell_opened:

         // any opened Buy position?
         if(Buy_opened)
           {
            Alert("We already have a Buy Position!!!");
            return;    // Don't open a new Buy Position
           }
.
.
.
         // any opened Sell position?
         if(Sell_opened)
           {
            Alert("We already have a Sell position!!!");
            return;    // Don't open a new Sell Position
           }

Regards, Vladimir.