Hello,
I would like to get opened position price, ticket number and it’s commission and profit. What could be the issue in the code?
Thanks in advance.
//+------------------------------------------------------------------+
//| test_profitcommission_01.mq5 |
//| Copyright 2017, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#include<Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\OrderInfo.mqh>
#include <Trade\DealInfo.mqh>
CTrade trade;
CPositionInfo pos;
CDealInfo deal;
ulong myticket;
int OnInit()
{
//---
OpenPosition();
//---
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
//---
}
void OnTick()
{
//---
deal.Ticket(myticket);
double xcom=deal.Commission();
double xpro=deal.Profit();
Comment("Profit = ",xpro," com = ",xcom);
}
void OpenPosition()
{
trade.SetExpertMagicNumber(12345);
trade.SetDeviationInPoints(10);
trade.SetTypeFilling(ORDER_FILLING_RETURN);
trade.LogLevel(2);
trade.SetAsyncMode(true);
if(!trade.Buy(1,_Symbol))
{
//--- failure message
Print("Buy() method failed. Return code=",trade.ResultRetcode(),". Code description: ",trade.ResultRetcodeDescription());
}
else
{
Print("Buy() method executed successfully. Return code=",trade.ResultRetcode()," (",trade.ResultRetcodeDescription(),")");
myticket=trade.RequestOrder();
double xopenprice=trade.ResultPrice();
Print("Open price is = ",xopenprice);
Print("Ticket number is = ",myticket);
}
}