⛳
MQL5で最後の約定の履歴の損益を取得する
double get_last_position_profit()
{
long time_date = 0;
double profit = 0.0;
// 2週間前の時刻を計算(UNIXタイムスタンプ形式)
long twoWeeksAgo = TimeCurrent() - 2 * 7 * 24 * 60 * 60; // 2週間 = 14日 * 24時間 * 60分 * 60秒
// 現在時刻までの2週間の取引履歴を選択
HistorySelect(twoWeeksAgo, TimeCurrent());
for(int i = HistoryDealsTotal() - 1; i >= 0; i--)
{
ulong ticket = HistoryDealGetTicket(i);
if(ticket > 0)
{
if(MagicNumber == HistoryDealGetInteger(ticket, DEAL_MAGIC))
{
if(Symbol() == HistoryDealGetString(ticket, DEAL_SYMBOL))
{
if(HistoryDealGetInteger(ticket,DEAL_ENTRY) == DEAL_ENTRY_OUT)
{
if(time_date < HistoryDealGetInteger(ticket, DEAL_TIME))
{
time_date = HistoryDealGetInteger(ticket, DEAL_TIME);
if(HistoryDealGetDouble(ticket, DEAL_PROFIT) != 0)
{
profit = HistoryDealGetDouble(ticket, DEAL_PROFIT);
}
}
}
}
}
}
}
return profit;
}
Discussion