MQL5 TUTORIAL BASICS – 16 HOW TO SET A SELL LIMIT

video
play-sharp-fill

 

  1. Introduction to Setting a Sell Limit in MQL5 (00:00 – 00:12)
    • Introduction to the tutorial on setting a sell limit far above the current Bid price using MQL5.
  2. Opening MetaEditor and Creating a New File (00:12 – 00:32)
    • Instructions on opening MetaEditor and creating a new Expert Advisor file named “SimpleSellLimit”.
  3. Setting Up the Code Structure (00:32 – 00:43)
    • Removing unnecessary code above the OnTick function and preparing the code structure.
  4. Importing Trade.mqh and Creating CTrade Instance (00:43 – 01:06)
    • Including the ‘Trade.mqh’ file and creating an instance of CTrade for trading operations.
  5. Calculating the Bid Price and Normalizing It (01:06 – 01:35)
    • Using ‘SymbolInfoDouble’ to get the Bid price and normalizing it for currency pair precision.
  6. Checking for Open Orders and Positions (01:35 – 02:01)
    • Ensuring there are no open orders or positions before placing a sell limit.
  7. Placing a Sell Limit with trade.SellLimit (02:01 – 03:10)
    • Using ‘trade.SellLimit’ to place a sell limit order with parameters for lot size, price, and currency pair.
  8. Configuring Stop Loss and Take Profit Levels (03:10 – 03:25)
    • Setting stop loss and take profit levels for the sell limit order.
  9. Setting Order Expiration and Adding a Comment (03:25 – 03:43)
    • Configuring the order expiration to ‘ORDER_TIME_GTC’ and adding a comment with ‘NULL’.
  10. Compiling the Code and Testing in MetaTrader (03:43 – 04:10)
  • Compiling the code and testing the Expert Advisor in MetaTrader using the Strategy Tester.
  1. Observing the Sell Limit Order on the Chart (04:10 – 04:44)
  • Watching the sell limit order placed on the chart and its details.

 

In this video we are going set a sell limit.
A sell limit is a pending order far above the current Bid price, so let’s find out how to do that with MQL5.

In Metatrader first we need to click on a little symbol here or press F4. Now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue“, I will call this file: “SimpleSellLimit”, click on: “Continue“, “Continue“ and “Finish”. 

Now we can delete everything that is above the “OnTick” function and let’s also remove the two comment line here.
We are stating with an import, we include the file: “Trade.mqh”, this one comes with MQL5 and Metatrader 5 and we use it to create an instance of “CTrade” that will be called: “trade” and we are going to use it later on to open our sell limit.

First we need to find out about the current Bid price, we do it by using “SymbolInfoDouble”; “_Symbol” is the first parameter, “SYMBOL_BID” – all in capital letters – is the second parameter, “NormalizeDouble” and “_Digits” that will automatically calculate the number of digits behind the dot, so for this currency pair it would return three digits behind the dot while other currency pairs would return five digits behind the dot.

Now that we have the Bid price we need to check another condition; I would like to know if we have any open orders or open positions, so if “OrdersTotal” equals 0 (zero) that would mean that we have no other pending order and if “PositionsTotal” equals 0 (zero) that means that we have no open positions and in that case we want to set our sell limit, so let’s add two curly braces here, we can use “trade.SellLimit” because we have created an instance called: “trade” and we need to pass a few parameters here;
The first parameter here is for the lot size, we are going to open a sell limit for 10 micro lot,
The second parameter is the price, our sell limit should be opened whenever the price rises 200 points above the current Bid price,
Parameter 3 will be used to automatically get the current symbol on the chart, in my case it’s the Euro-US Dollar,
The next parameter would be the stop loss, you could set it to over 200 points above the current Bid price but in this case I don’t use it so I use 0 (zero), what we do use is the take profit, we want to close the position when the price falls from 200 points above the Bid price to 100 points above the Bid price, “ORDER_TIME_GTC” stands for: “it is good until we cancel the order”, “ORDER_TIME_DAY” would be used if you would like to place an order that is just for the current trading day, this also is all in capital letters. We have no expiration date and so we have no expiration time, so this parameter is also 0 (zero).
And the last one could be used to set a comment, if you don’t pass a string variable here you can use “NULL” – this is also in capital letters – and we use it instead of a string value, that’s basically it, so when you are done you can 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 a little button here or press F4 to go back to Metatrader. 

In Metatrader please click on: “View/ Strategy Tester” or press CTRL and R, here we want to pick the new file: “SimpleSellLimit.ex5”, please mark the visualization option here and start a test.

Now the Expert Advisor is running and we can see our sell limit here, it’s for 10 micro lots and when we zoom into the chart we see that it is far above the current Bid price and in this little video you have learned how to automatically set a sell limit and you have coded it yourself with a few lines of MQL5 code.

 

Download “CODE - SIMPLE SELL LIMIT”

SimpleSellLimit.mq5 – Downloaded 70 times – 798.00 B