Now we want to find out how we can create an Expert Advisor that is able to output the position properties directly on the chart.
To do that please click the little button here or press F4 on your keyboard and now you should see the Metaeditor window and here you want to click on “File, New, Expert Advisor” from template (template), Continue (Next).
I will call this one: SimplePositionInfo.
Continue (Next) and Continue (Next) and Finish, and now everything above the “OnTick“ function here can be deleted and please also remove the two comment lines.
The first thing we need to know is: “if positions for this currency pair exists”. To find it out we use “PositionSelect” and it will check the current symbol on the chart, and if that is true we are going to continue. If we would have no open position this value would be false. Let’s open the “if” statement and now we are going to “count down the number of positions until zero”. That is done by using a “for” loop, it uses the function “PositionsTotal” and will subtract one (-1) as long as the counter is above or equal zero.
Now we want to calculate the ticket number. That is done by using the function “PositionGetTicket” for the currently selected position. We can also calculate the currency pair for the position by using “PositionGetString(POSITION_SYMBOL). The open price for the position is calculated by using: “PositionGetDouble(POSITION_PRICE_OPEN)” – that is the open price here in this column.
To calculate the current position price we use: “PositionGetDouble(POSITION_PRICE_CURRENT)”
If you want to know the position profit you can use: “PositionGetDouble(POSITION_PROFIT)” but don’t forget to calculate the position swap because position swap fees can have a big impact on your profit.
You can get the information by using: “PositionGetDouble(POSITION_SWAP)”. If you don’t see any swap fees here they are simply hidden, just enlarge this part, right click and select: “Columns , Swap” and here it is! Currently we don’t pay any swap fees but if you intend to hold your position overnight that can be very costly! So, what else do we need? We want to calculate the current position net profit. That is done by simply adding position profit and position swap. Now if the position symbol equals the symbol on the chart by using the “comment” function it will output a text followed by the calculated value for each text and continue in a new line. This is the end of the “if” statement we also need to close the “for” loop from above and that should be it.
Oh, this one belongs to the first “if” statement and the final one will close the “OnTick” function.
If you don’t know if you have the right amount of braces, you can mark one and Metaeditor is going to mark the other one that belongs to this one. So, let’s mark this one, and yes it belongs to the for end brace.
Now let’s compile the code. Everything is fine. So, now you can click on the little button here press F4 to go back to Metatrader.
In Metatrader we are just going to open a chart, let’s close this position here and open a new one for testing purposes. Now you can simply drag the file: “SimplePositionInfo” on your chart, please allow automated trading and click “OK”. When the next price change comes in we should see this.
Let’s try another currency pair! Drag it on the chart, allow automated trading, wait for the next tick… Oh I forgot I need to open a position! So, let’s do a sell position here! This is the new sell position and when the price changes we see the information that we have calculated for our open position.
If you know how to do that, you can use the information to calculate a lot of automated things for your Expert Advisor. For example: you could close your position if the profit has reached the 10 Dollar mark or whatever you prefer.
Okay! Now you know how to calculate position properties on different Forex charts and you have coded it yourself with a few lines of MQL5 code.