muntje:
dont trade buy if last deal was buy closed in loss
dont trade buy if last deal was buy closed in loss
Then, on the first loss, you stop trading forever.
Well not specially cause if the next trade is a sell trade, after that it will trade again right. Its only to check the last trade. Last trade buy is closed in loss? If true , only sell trades are allowed. After the sell trade it has to be possible to trade buys again
Try this. I did not compile or test.
double LatestBuyPnL() { if(HistorySelect(0, TimeCurrent()==false) return 0; for(int i=HistoryDealsTotal()-1;i>=0;i--) // scan deals from latest to earliest { ulong ticket = HistoryDealGetTicket(i); if(HistoryDealGetInteger(ticket, DEAL_TYPE) == DEAL_TYPE_SELL && HistoryDealGetInteger(ticket, DEAL_ENTRY) == DEAL_ENTRY_OUT) { //You have a deal out which is sell ==> So the original position was a buy return HistoryDealGetDouble(ticket, DEAL_PROFIT); } } return 0; }
use global boolean flags
bool last_sell_deal_win = false; bool last_buy_deal_win = false;
if (!PositionSelect(Symbol())){ HistorySelect(TimeCurrent()-3600, TimeCurrent()); for (int i = 0; i < HistoryDealsTotal(); i++){ ulong ticket = HistoryDealGetTicket(i); ulong last_ticket = HistoryDealGetTicket(HistoryDealsTotal() - i - 1); // if HistoryDealsTotal is greater than 1 you might want to use this instead of "ticket" variable if (ticket > 0){ double dealProfit = HistoryDealGetDouble(ticket, DEAL_PROFIT); ulong dealTime = HistoryDealGetInteger(ticket, DEAL_TIME); ulong dealType = HistoryDealGetInteger(ticket, DEAL_TYPE); if(dealType == DEAL_TYPE_SELL && dealProfit > 0){ last_sell_deal_win = true; } else if(dealType == DEAL_TYPE_SELL && dealProfit < -1){ last_sell_deal_win = false; } if(dealType == DEAL_TYPE_BUY && dealProfit > 0){ last_buy_deal_win = true; } else if(dealType == DEAL_TYPE_BUY && dealProfit < -1){ last_buy_deal_win = false; } } else{ Print("Failed to get ticket for index ", i); } } }
if(mySellTradeSignal && last_sell_deal_win){ Sell(); // it is allowed to make a new sell position } if(myBuyTradeSignal && last_buy_deal_win){ Buy(); // it is allowed to make a new buy position }
If both flags become false, you have to reset them so the EA can continue to place trades
if(!last_sell_deal_win && !last_buy_deal_win){ last_sell_deal_win = true; //reset the flag last_buy_deal_win = true; //reset the flag }
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
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
Hi all!
I try to create a code to check if the last buy position (what is closed) if it closed ik loss or in profit.
i Try to create a bool function so before the trade started , the EA is sure that the last buy position is not closed in loss (false).
if its closed in loss it return true and then i would like my EA dont open the trade.
I read a lot and saw a lot of example but still cant get it working.
i will show my code maybe somebody figure out whats wrong with my code.
i test also the sell type cause ctrade reverse the closed position (a sell trade end with a buy ticket)
even with youtube tutorials i can t make it work!
Hope someone can help me a little hand
Btw im a newbie , but you already figure that out 🧐