Save/Update that double each time a position closes :
double LAST_TRADE_PROFIT=0; // global variable void OnTrade() { static int previous_open_positions = 0; int current_open_positions = PositionsTotal(); if(current_open_positions < previous_open_positions) // a position just got closed. { previous_open_positions = current_open_positions; HistorySelect(TimeCurrent()-300, TimeCurrent()) // 5 minutes ago up to now :) int All_Deals = HistoryDealsTotal(); if(All_Deals < 1) Print("Some nasty shit error has occurred :s"); ulong temp_Ticket = HistoryDealGetTicket(All_Deals-1); // last deal (should be an DEAL_ENTRY_OUT type) // here check some validity factors of the position-closing deal (symbol, position ID, even MagicNumber if you care...) LAST_TRADE_PROFIT = HistoryDealGetDouble(temp_Ticket , DEAL_PROFIT); } else if(current_open_positions > previous_open_positions) previous_open_positions = current_open_positions; // a position just got opened. }
if you want to get that LAST_TRADE_PROFIT variable refreshed anywhere outside OnTrade() and OnTradeTransaction() events, you need to load more history, and search through deals differently.
- 2018.07.03
- www.mql5.com
Code2219 or probably 2319 Thanks but it is now working. I made an EA based on your code:
//+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { for(int i=0; i<5; i++) { //-- first open a position, for the exercice: Trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,0.01,Ask(_Symbol),0,0); Sleep(4000); Trade.PositionClose(_Symbol,3); Sleep(4000); OnTrade(); // I had to put this here, otherwise no output } Sleep(12000); ExpertRemove(); } //+------------------------------------------------------------------+ //| Trade function | //+------------------------------------------------------------------+ void OnTrade() { static int previous_open_positions = 0; int current_open_positions = PositionsTotal(); // a position just got closed: if(current_open_positions < previous_open_positions) { previous_open_positions = current_open_positions; HistorySelect(TimeCurrent()-300, TimeCurrent()); // 5 minutes ago int All_Deals = HistoryDealsTotal(); if(All_Deals < 1) Print("Some nasty shit error has occurred :s"); // last deal (should be an DEAL_ENTRY_OUT type): ulong temp_Ticket = HistoryDealGetTicket(All_Deals-1); // here check some validity factors of the position-closing deal // (symbol, position ID, even MagicNumber if you care...) LAST_TRADE_PROFIT = HistoryDealGetDouble(temp_Ticket , DEAL_PROFIT); } // a position just got opened: else if(current_open_positions > previous_open_positions) previous_open_positions = current_open_positions; Print ("Last Trade Profit " + DoubleToString(LAST_TRADE_PROFIT)); }
you don't need to call OnTrade() from OnTick() !!!
OnTick() { // open/close your positions here as usual // OnTrade() will be called automatically with each trade-related operation } void OnTrade() { static int previous_open_positions = 0; int current_open_positions = PositionsTotal(); if(current_open_positions < previous_open_positions) // a position just got closed: { previous_open_positions = current_open_positions; HistorySelect(TimeCurrent()-300, TimeCurrent()); // 5 minutes ago int All_Deals = HistoryDealsTotal(); if(All_Deals < 1) Print("Some nasty shit error has occurred :s"); // last deal (should be an DEAL_ENTRY_OUT type): ulong temp_Ticket = HistoryDealGetTicket(All_Deals-1); // here check some validity factors of the position-closing deal // (symbol, position ID, even MagicNumber if you care...) LAST_TRADE_PROFIT = HistoryDealGetDouble(temp_Ticket , DEAL_PROFIT); Print("Last Trade Profit : ", DoubleToString(LAST_TRADE_PROFIT)); } else if(current_open_positions > previous_open_positions) // a position just got opened: previous_open_positions = current_open_positions; }
Also those Sleeps aren't needed
It works. Thank you very much.
Hi
Allthough its an old thread , I'm struggling with this code..
static int previous_open_positions = 0; int current_open_positions = PositionsTotal(); if(current_open_positions < previous_open_positions)
Since your set previous_open_positions to 0, this "if" is only true when PositionsTotal() is negative !?!
Can that be possible?
Hi
Allthough its an old thread , I'm struggling with this code..
Since your set previous_open_positions to 0, this "if" is only true when PositionsTotal() is negative !?!
Can that be possible?
Thanks for the info!
Didnt notice the "static" keyword, nor did I knew of if.
Whats the difference with using a global variable? Don't see any to be honest.
Thanks
This wont work on a live account!
You have no chance when you do not deal with delayed, async processing. The only chance to get the results is to use OnTradeTransaction(), cause you will face the challenge, that from time to time you wont receive the valid/final results when your EA doesn´t wait for the postponed transaction results. Like that it may work in 80-90% of the cases, maybe less, maybe more, but surely not in all cases!
See: https://www.mql5.com/en/forum/357245
- 2020.12.05
- www.mql5.com
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
With two trials to get the profit of the last closed position (Hedging account), I am not arriving anywhere.
The first trial is personal, and it goes like this:
I am not even receiving a proper ticket:
The second trial was given in MQL5 forum, and while I get all the profits, how can I be sure that the orders of the loop correspond to the order of opening positions?
It gives something like this:
Any help?