PositionType()

 

Hi,


I'm very new to mql5, so I apologize for struggling on this simple topic already.


I found an EA that is supposed to open a buy-order when:

int signal = 0 &&  PositionType() != POSITION_TYPE_BUY

or a sell order for:

int signal = 1 &&  PositionType() != POSITION_TYPE_SELL


For some reason the Program always just opened one sell- and one buy-order, although the signal variable kept changing.

To have a closer look of what is the problem, I add:

int posType = PositionType();

to find out what is the value of PositionType and why does it happen like this.


After the first run the signal = 1 and posType = -1, so a sell-order with 1 Lot has been placed. After that run posType changed to 1.

A few ticks later the signal changed to 0, whilst the posType = 1, so a  buy-order has been placed with 2 Lots, but posType remained 1.


So now I'm uncertain what PositionType() actually does. Shouldn't it change to 0 after the buy-position has been placed? 

 
tak88:

Hi,


I'm very new to mql5, so I apologize for struggling on this simple topic already.


I found an EA that is supposed to open a buy-order when:

int signal = 0 &&  PositionType() != POSITION_TYPE_BUY

or a sell order for:

int signal = 1 &&  PositionType() != POSITION_TYPE_SELL


For some reason the Program always just opened one sell- and one buy-order, although the signal variable kept changing.

To have a closer look of what is the problem, I add:

int posType = PositionType();

to find out what is the value of PositionType and why does it happen like this.


After the first run the signal = 1 and posType = -1, so a sell-order with 1 Lot has been placed. After that run posType changed to 1.

A few ticks later the signal changed to 0, whilst the posType = 1, so a  buy-order has been placed with 2 Lots, but posType remained 1.


So now I'm uncertain what PositionType() actually does. Shouldn't it change to 0 after the buy-position has been placed? 


There is no PositionType() function, but if you're referring to to the CPositionInfo::PositionType method it returns an enum of type ENUM_POSITION_TYPE so you would need to check it by proper comparison. 

CPositionInfo pos; 
...
if(pos.PositionType()==POSITION_TYPE_BUY)
   ...
 
nicholishen:

There is no PositionType() function, but if you're referring to to the CPositionInfo::PositionType method it returns an enum of type ENUM_POSITION_TYPE so you would need to check it by proper comparison. 


Ok, anyways the situation is the same!

After the buy-order is getting replaced through a sell-order with the double volume, the Position Type remains what it has been after the first order was placed.

 
tak88:

Ok, anyways the situation is the same!

After the buy-order is getting replaced through a sell-order with the double volume, the Position Type remains what it has been after the first order was placed.

Use CPositionInfo::Select-functions.

 
fxsaber:

Use CPositionInfo::Select-functions.


Ok, I'll try again: I used this Class already in order to find out if my position in placed long or short! Unfortunately the result of PositionType() remains the same after the first Order was placed.

In other words:

If my first position was a buy-position with 1 Lot, the result I get from PositionType() = ENUM_POSITION_TYPE::POSITION_TYPE_BUY and remains with that value, also if there is a sell-position opened later with more Lots!

 
tak88 :

Ok, I'll try again: I used this Class already in order to find out if my position in placed long or short! Unfortunately the result of PositionType() remains the same after the first Order was placed.

In other words:

If my first position was a buy-position with 1 Lot, the result I get from PositionType() =  ENUM_POSITION_TYPE::POSITION_TYPE_BUY and remains with that value, also if there is a sell-position opened later with more Lots!


Please describe:

what is your trading account: netting or hedge?

describe the discovery chain (step by step) (for example BUY 1.0, SELL 2.0, BUY 1.0) ...

 

For example.

Trading account "Hedge", seven positions are opened. On the schedule, I attached the expert ListPositions:

ListPositions

As you can see, the advisor ListPositions correctly determines the type of all OPEN positions.

 
Vladimir Karputov:

For example.

Trading account "Hedge", seven positions are opened. On the schedule, I attached the expert ListPositions:

As you can see, the advisor ListPositions correctly determines the type of all OPEN positions.


my first position was a sell-position with 0.1 Lot, the result I get from PositionType() = ENUM_POSITION_TYPE::POSITION_TYPE_SELL and remains with that value, also if there is a buy-position opened later with 0.2 lots

The EA is always running in the same Symbol, so I guess hedging is not possible with MT5. Before I get the value of PositionType(), I always call the Select-function first to make sure I'm calling the right symbol.

 
tak88 :


The EA is always running in the same Symbol, so I guess hedging is not possible with MT5. Before I get the value of PositionType(), I always call the Select-function first to make sure I'm calling the right symbol.


I did not get anything.

... "The EA ..." - what kind of advisor are you talking about?

... "so I guess hedging is not possible with MT5" ... - There are two types of positions in MetaTrader: netting and hedging

 
Vladimir Karputov:

I did not get anything.

... "The EA ..." - what kind of advisor are you talking about?

... "so I guess hedging is not possible with MT5" ... - There are two types of positions in MetaTrader: netting and hedging

Ok, this post kind of answered my question. The program I was running, was written for a netting account at a time where hedging wasn't possible on mt5. But my account is a hedging account
So I guess if I request Information about the positition, I'll always get the Information of the first openend position. 
 
tak88:
Ok, this post kind of answered my question. The program I was running, was written for a netting account at a time where hedging wasn't possible on mt5. But my account is a hedging account. 
So I guess if I request Information about the positition, I'll always get the Information of the first openend position. 
To get information about a position, you first need to select this position. Choose from a common list of all items.

Reason: