Questions from Beginners MQL5 MT5 MetaTrader 5 - page 657
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
It seems to be all on point and even with references... Just when someone declares that he has read the documentation and asks to remove netting (which, by the way, is used on FORTS)... you have to be sarcastic here.
Make sure you don't get sent away. )
late, you're already there.
Example for a hedge account:
The first operation is Buy 0.02 and the second operation is Sell 0.01. Total two positions: Buy 0.02 and Sell 0.001. Then we close the Sell 0.01 position and close the Buy 0.02 position:
//| TestEA.mq5 |
//| Copyright © 2016, Vladimir Karputov |
//| http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2016, Vladimir Karputov"
#property link "http://wmua.ru/slesar/"
#property version "1.00"
#include <Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>
CTrade m_trade; // trading object
CPositionInfo m_position; // trade position object
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
static long counter=0;
counter++;
if(counter%300==0 && counter<700)
{
static int number=0;
number++;
if(number==1)
m_trade.Buy(0.02);
if(number==2)
m_trade.Sell(0.01);
}
if(counter>7000)
{
int positions= PositionsTotal();
if(positions == 0)
return;
int positions_total=PositionsTotal();
for(int i=positions_total-1;i>=0;i--)
if(m_position.SelectByIndex(i))
m_trade.PositionClose(m_position.Ticket(),-1);
}
}
//+------------------------------------------------------------------+
and Journal:
Trades 2016.02.25 00:13:35 deal #2 buy 0.02 EURUSD at 1.10167 done (based on order #2)
Trade 2016.02.25 00:13:35 deal performed [#2 buy 0.02 EURUSD at 1.10167]
Trade 2016.02.25 00:13:35 order performed buy 0.02 at 1.10167 [#2 buy 0.02 EURUSD at 1.10167]
TestEA (EURUSD,H1) 2016.02.25 00:13:35 CTrade::OrderSend: instant buy 0.02 EURUSD at 1.10167 [done at 1.10167]
Trade 2016.02.25 00:30:17 instant sell 0.01 EURUSD at 1.10157 (1.10157 / 1.10164 / 1.10157)
Trades 2016.02.25 00:30:17 deal #3 sell 0.01 EURUSD at 1.10157 done (based on order #3)
Trade 2016.02.25 00:30:17 deal performed [#3 sell 0.01 EURUSD at 1.10157]
Trade 2016.02.25 00:30:17 order performed sell 0.01 at 1.10157 [#3 sell 0.01 EURUSD at 1.10157]
TestEA (EURUSD,H1) 2016.02.25 00:30:17 CTrade::OrderSend: instant sell 0.01 EURUSD at 1.10157 [done at 1.10157]
TestEA (EURUSD,H1) 2016.02.25 02:12:06 PositionClose #3 POSITION_TYPE_SELL 0.01
Trade 2016.02.25 02:12:06 instant buy 0.01 EURUSD at 1.10131, close #3 (1.10123 / 1.10131 / 1.10123)
Trades 2016.02.25 02:12:06 deal #4 buy 0.01 EURUSD at 1.10131 done (based on order #4)
Trade 2016.02.25 02:12:06 deal performed [#4 buy 0.01 EURUSD at 1.10131]
Trade 2016.02.25 02:12:06 order performed buy 0.01 at 1.10131 [#4 buy 0.01 EURUSD at 1.10131]
TestEA (EURUSD,H1) 2016.02.25 02:12:06 CTrade::OrderSend: instant buy 0.01 position #3 EURUSD at 1.10131 [done at 1.10131]
TestEA (EURUSD,H1) 2016.02.25 02:12:06 PositionClose #2 POSITION_TYPE_BUY 0.02
Trade 2016.02.25 02:12:06 instant sell 0.02 EURUSD at 1.10123, close #2 (1.10123 / 1.10131 / 1.10123)
Trades 2016.02.25 02:12:06 deal #5 sell 0.02 EURUSD at 1.10123 done (based on order #5)
Trade 2016.02.25 02:12:06 deal performed [#5 sell 0.02 EURUSD at 1.10123]
Trade 2016.02.25 02:12:06 order performed sell 0.02 at 1.10123 [#5 sell 0.02 EURUSD at 1.10123]
TestEA (EURUSD,H1) 2016.02.25 02:12:06 CTrade::OrderSend: instant sell 0.02 position #2 EURUSD at 1.10123 [done at 1.10123]
What exactly is not clear? That you need to open an opposite order with the same volume to close the position?
Who knows - does"POSITION_PROFIT" include commission and swap?
If not, what parameter should be used to find out the amount of commission for an order in MQL5?
Who knows - does"POSITION_PROFIT" include commission and swap?
If no - what parameter is needed to find out the amount of accrued commission in MQL5?
POSITION_PROFIT is simply the profit of the position (without deduction of commissions, swaps ...). To find out the accumulated swap ofa position, do the following:
POSITION_SWAP
Cumulative swap
double
And the final commission and total swap, can be obtained after the position is closed, in the history of transactions:
DEAL_COMMISSION
Deal_commission
double
DEAL_SWAP
Accumulated swap at closing
double
Who knows - does"POSITION_PROFIT" include commission and swap?
If not, what parameter should be used to find out the amount of commission charged for an order in MQL5?
In Russian, an order cannot make a profit or a loss and is only an order to execute. When the order is executed, you receive your position in the market at your chosen price.
POSITION_PROFIT is just the profit of the position (without any commissions, swaps ...). You can find out the accumulated swap ofthe position like this:
And the final commission and the final swap, you can find out after the position is closed, in the history of transactions:
Well, this is all wrong, or rather incomplete.
Why do I need to know the commission after closing, or rather I need to know it, but at any time, let's assume that the account has a commission and a drawback or breakeven, how can I calculate so that the first triggering was on the level at which commission and swaps are overlapped in case of price rollback and closing. I add 1pp to cover commission. What about swaps, as the position may stay in the market for a week and during this time it may take 3pp to gain swaps and be closed with a drawback when Breakeven is triggered?
Or maybe I do not understand the calculations?
In Russian, an order can be neither a profit nor a loss, it is only an order to execute. Once the order is executed, you get your position in the market at your chosen price.
This is all wrong, or rather incomplete.
Why do I need to know the commission after the close, or rather I need to know it, but at any time, let's assume the account has a commission and trailing or break-even, how do I calculate so that the first hit was at a level that overlaps the commission and swaps in case of price rollback and closing. I add 1pp to cover commission. What about swaps, as the position may stay in the market for a week and during this time it may take 3pp to gain swaps and be closed with a drawback when Breakeven is triggered?
Or am I missing something in the calculations?
Who knows - does"POSITION_PROFIT" include commission and swap?
If not, what parameter is needed to know the amount of commission charged for an order in MQL5?
I'm not imposing the MT4Orders library. But maybe it would be easier to just call the good old OrderCommission?
It works not only on hedge accounts, but also on net ones.
I understand that"POSITION_PROFIT" already includes everything (swap, profit, commission).