Closing long positions

 

Hi!

Im new in the MQL world, with little knowledge of C# only...

Looking at the Order Types (in: https://www.mql5.com/en/docs/constants/tradingconstants/orderproperties#enum_order_type), I had a doubt. Is there a order type for closing long positions?

As I understood the ORDER_TYPE_SELL is for selling short, isnt it?

Bruno

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Order Properties - Documentation on MQL5
 

Order type for closing long position is ORDER_TYPE_SELL

Order type for closing short position is ORDER_TYPE_BUY

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Order Properties - Documentation on MQL5
 
biantoro:

Order type for closing long position is ORDER_TYPE_SELL

Order type for closing short position is ORDER_TYPE_BUY

Incomplete answer.

ORDER_TYPE_SELL is for opening short position or closing long position.

ORDER_TYPE_BUY is for opening long position or closing short position.

 
Enigma71fx:

Incomplete answer.

ORDER_TYPE_SELL is for opening short position or closing long position.

ORDER_TYPE_BUY is for opening long position or closing short position.

Thank you, guys!

I started to develop EAs using what I have learned in the article: https://www.mql5.com/en/articles/100 , (I think it is very well written and didatic) and modifying that code. In that article, the author didnt use closing long and short positions, instead he used stoploss and take profit. How do you deal with that? Im thinking to try something like:

-define conditions for enter long, close long, enter short and close short positions;

-check if is there is a long or short position opened;

 -if there isnt, then check enter long and enter short conditions (sending the corresponding orders to buy or sell short - ORDER_TYPE_SELL or ORDER_TYPE_BUY, case either is true);

 -else if there is a long position opened, then check for close long conditions (sending the order to close long position - ORDER_TYPE_SELL -, case true);

 -else if there is a short position opened, then check for close short conditions  (sending the order to close short position - ORDER_TYPE_BUY - , case true);

Step-By-Step Guide to writing an Expert Advisor in MQL5 for Beginners
Step-By-Step Guide to writing an Expert Advisor in MQL5 for Beginners
  • 2010.06.09
  • Samuel
  • www.mql5.com
The Expert Advisors programming in MQL5 is simple, and you can learn it easy. In this step by step guide, you will see the basic steps required in writing a simple Expert Advisor based on a developed trading strategy. The structure of an Expert Advisor, the use of built-in technical indicators and trading functions, the details of the Debug mode and use of the Strategy Tester are presented.
 
Icarus:

Thank you, guys!

I started to develop EAs using what I have learned in the article: https://www.mql5.com/en/articles/100 , (I think it is very well written and didatic) and modifying that code. In that article, the author didnt use closing long and short positions, instead he used stoploss and take profit. How do you deal with that? Im thinking to try something like:

-define conditions for enter long, close long, enter short and close short positions;

-check if is there is a long or short position opened;

 -if there isnt, then check enter long and enter short conditions (sending the corresponding orders to buy or sell short - ORDER_TYPE_SELL or ORDER_TYPE_BUY, case either is true);

 -else if there is a long position opened, then check for close long conditions (sending the order to close long position - ORDER_TYPE_SELL -, case true);

 -else if there is a short position opened, then check for close short conditions  (sending the order to close short position - ORDER_TYPE_BUY - , case true);

 

 

You could add - if you want to implement a trailing stop - if there is a position opened, check if the conditions for moving the stop loss on the position are met.
Reason: