MQL5 TUTORIAL BASICS – 33 SIMPLE SELL POSITION MODIFIER

video
play-sharp-fill

 

  1. Introduction to Reducing Position Size for Existing Sell Positions (00:00 – 00:13)
    • Introduction to the tutorial on reducing the position size for an existing sell position in MQL5.
  2. Opening MetaEditor and Creating a New Expert Advisor File (00:13 – 00:34)
    • Instructions on opening MetaEditor and creating a new Expert Advisor file named “Simple Sell Position Modifier”.
  3. Setting Up the Code Structure and Including Trade.mqh (00:34 – 00:44)
    • Removing unnecessary code and including the ‘Trade.mqh’ file for trading functions.
  4. Creating an Instance of CTrade and Calculating the Bid Price (00:44 – 01:15)
    • Creating an instance of the CTrade class and calculating the bid price using ‘SymbolInfoDouble’.
  5. Opening a Test Sell Position (01:15 – 01:43)
    • Opening a test sell position if there are no open positions using ‘trade.Sell’.
  6. Implementing the Change Position Size Function (01:43 – 02:06)
    • Creating a function ‘change position size’ to adjust the size of the position based on the bid price.
  7. Calculating Balance and Equity (02:06 – 02:44)
    • Calculating the account balance and equity using ‘AccountInfoDouble’.
  8. Looping Through Open Positions and Adjusting Size (02:44 – 03:31)
    • Using a for loop to go through open positions and adjusting the size of sell positions when equity is above the balance.
  9. Compiling and Testing the Expert Advisor in MetaTrader (03:31 – 04:42)
    • Compiling the code and instructions on testing the Expert Advisor in MetaTrader using the Strategy Tester.
  10. Observing the Expert Advisor in Action (04:42 – 05:59)
  • Running the Expert Advisor in MetaTrader and observing the automatic reduction of the position size.

 

In this video we are going to find out how to reduce the position size for an existing sell position, here we have nine micro lot and now the position size is reduced so let’s find out how to code that in mql5.

To do that please click on the little icon here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, continue, I will call this file simple sell position modifier, click on continue, continue and finish.

Now you can delete everything above the on tick function and the two comment lines here, we start by including the file trade dot mqh, this one comes with mql5 and we use it to open trades, to do that we create an instance of ctrade that will be called trade.
As we are only going to close sell positions we need to calculate the bid price, this is done by using symbol info double for the current symbol on the chart, we use symbol underscore bid as the second parameter and I also like to use normalize double and underscore digits because this will automatically calculate the right number of digits behind the dot.
Afterwards, we want to check if we have no open positions, that would be the case when positions total delivers a return value of zero and in that case we use trade dot sell to open a test position for ten micro lot. Actually, this is a sell position not a buy position and now we want to reduce the position size, so we call the function change position size with the bid price but before we can do that we need to code that function here. We use the return type void, the name of the new function is change position size and here we will use the double value for the bid price that we have passed here.
First we want to calculate the balance that is done by using account info double, the parameter we use is account underscore balance – all in capital letters –, let’s repeat that for the equity, this is almost similar, once again we use account info double but this time the parameter is account underscore equity.
Afterwards, we use a for loop to go through all the open positions, in our case it’s only one test position, you wouldn’t do that on a real account, on a real account you would use the for loop to go through all the open positions.
Afterwards, we need to get the order symbol for the current position that is done by using position get symbol for the current counter value in the for loop, so now we can check if the current symbol on the chart equals the order symbol and if that is the case we need to find out the position ticket because we will need the ticket number to close a part of the existing position. We do it by using position get integer, the parameter is position underscore ticket and as we are only going to close sell positions we also want to get the position direction that’s done by using position get integer for the position type, so let’s find out if this is a sell position, that would be the case if the position direction has the value position type sell, and finally we want to check if the equity is ten points or more above the balance, that’s the case when we make a profit and in that case we are going to use trade dot position close partial for the current position ticket, we want to reduce the current position size by one micro lot, this would be a tolerance value, we use minus one in this case, let’s close all the open loops here and that’s about it. 

Now if this was too fast for you or if you don’t know what all the code here does maybe you want to watch the other videos in this basic video series or maybe even the premium course is interesting for you, for now please click on the compile button here or press F7 you shouldn’t get any errors and if that is the case you can click on the little button here or press F4 to go back to Met trader.

And in Metatrader you want to click on view, strategy tester or you could press control and r on your keyboard, this is the new strategy tester, here we have to select “Run single tests of an Expert Advisor”, please pick the new file simple sell position modifier dot ex5, mark the visualization option here and start a test.

And here our little expert advisor is running, we have ten, now nine micro lot and as soon as the price goes down we should see that the position size is changing here – that’s the case now – so our little expert advisor works and in this little video you have learned how to code an expert advisor that is able to automatically reduce the position size of an existing sell position and you have coded it yourself with a few lines of mql5 code.