In MQL5, you must work with correct definitions: you need to search for DEALS with the DEAL_ENTRY_OUT (Entry out) type. Search from the beginning of the day to the current moment (we add three days to the current moment).
So here's your # 1 homework:
get start date of day (hint: you need to use TimeToStruct , TimeCurrent )

- www.mql5.com
In MQL5, you must work with correct definitions: you need to search for DEALS with the DEAL_ENTRY_OUT (Entry out) type. Search from the beginning of the day to the current moment (we add three days to the current moment).
So here's your # 1 homework:
get start date of day (hint: you need to use TimeToStruct , TimeCurrent )
Thanks for your fast reply. i will come back with my code. I hope i will find my solution soon.
In MQL5, you must work with correct definitions: you need to search for DEALS with the DEAL_ENTRY_OUT (Entry out) type. Search from the beginning of the day to the current moment (we add three days to the current moment).
So here's your # 1 homework:
get start date of day (hint: you need to use TimeToStruct , TimeCurrent )
so this is what i have done since now. I have attached the file of my code into this comment. I dont know if its a good try or its wrong.
all it has to be done now it to change the lot size of the next trade if for example losestreak of today =1 lost deal then next trade's lot is 0.15
if losestreak of today=2 then next trades lot is 0.2
if losestreak of today = 3 then next trade's lot is 0.25
and if first trade of the day is Won then stop trading until next day at 9:00. Thats the whole idea of the programm i am trying to make
The code:
//+------------------------------------------------------------------+ //| ProjectName.mq5 | //| Copyright 2020, CompanyName | //| http://www.companyname.net | //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { datetime time_local=TimeLocal(); Print("Time Local: ",TimeToString(time_local,TIME_DATE|TIME_SECONDS)); MqlDateTime STime; TimeToStruct(time_local,STime); STime.hour=0; STime.min=0; STime.sec=0; datetime time_start_day=StructToTime(STime); Print("Time Start Day: ",TimeToString(time_start_day,TIME_DATE|TIME_SECONDS)); //--- request trade history HistorySelect(time_start_day,time_local+3600*24*3); } //+------------------------------------------------------------------+
Result:
2021.12.12 22:40:14.407 2021.01.04 00:02:00 Time Local: 2021.01.04 00:02:00 2021.12.12 22:40:14.407 2021.01.04 00:02:00 Time Start Day: 2021.01.04 00:00:00
Thanks for your reoky once again. So know i have to select the last 3 deals of today. But its not sure that they will be three. so i have to make it manually and scan every trade of the last 3 ones for today.
which function shouls i use?
if (OrderSelect(OrdersHistoryTotal() - 1, SELECT_BY_POS, MODE_HISTORY))
so that i check the last closed order and then search for
if (OrderSelect(OrdersHistoryTotal() - 2 , SELECT_BY_POS, MODE_HISTORY))
and then
if (OrderSelect(OrdersHistoryTotal() - 3 , SELECT_BY_POS, MODE_HISTORY))
and adapt a counter so that i can see if they are all lost?
Something like: lose_streak = 0
and when each of the last 3 orders is loss count it?
i mean for example if its loss loss loss lose_streak = 3 (loss loss loss is sorted form left to right like the first daily trade was the first one)
IF its loss loss
lose_streak = 2
i just cant think of how to use the DEAL_ENTRY_OUT of HistoryDealGetInteger() and the DEAL_PROFIT of HistoryDealGetDouble() so that i make it count lose streaks.
all i can think is that i need an IF that will ensures that we have for example 3 closed trades today or 2 closed orders etc. so we need and integer to count the today trades and then loop through them and find the consecutive
lose streak. Thanks in advance. I dont post anycode since i know that its totaly wrong
Please get out of your mind 'OrderSelect' and never use again! You must view the DEALS in the trading history -> that is, you must use 'HistoryDeals XXX'.
Example in
HistorySelect

- www.mql5.com
Please get out of your mind 'OrderSelect' and never use again! You must view the DEALS in the trading history -> that is, you must use 'HistoryDeals XXX'.
Example in
HistorySelect
So lets take it from the start.
I will explain my strategy that i am trying to build in an EA
I am using the EMA indicator of the 21 last closed candles in a 5 min chart
When the closeprice of the previous candle is above the openprice of the previous candle and the EMA's price is between them we enter a buy trade for 0.1 lot and 5 pips SL TP (OVERLAP)
When the closeprice of the previous candle is lower than the openprice of the previous candle and the EMA's price is between them we enter a selltrade for 0.1 lot and 5 pips SL TP (OVERLAP)
the EA will automate take trades only between 9:00-13:00
if the first trade of the day is WIN the EA will automate stop and wait next day's 9:00 to start again
If the first trade of the day is LOST and then another trade opportunity appears between 9:00 and 13:00 following the OVERLAP rules then i want the EA to Automate take a trade with 0.2 lot and same SL TP (5pips)
if the trade is WON the EA will automate stop until next day's 9:00 o'clock
if the trade is LOST again so we have 2 consecutive loses and another trade opportunity appears between 9-13:00 following the Overlap rules then i want the EA to take a trade with 0.4 lot and 5 pips TP 2.5 pips SL
Either this trade is won or not the EA will stop until next day's 9:00
I have managed to make an EA that Autotrade with the OVERLAP rules and Time rules but without the losestreak or WIN conditions.
Beside it the trial of me to make this Strategy on an EA.
I hope someone can help me get out of my problem and learn how to solve. Sorry for the big post and THANKS for spending your time helping me.
#include <Trade/Trade.mqh> int EMA_21_M5 = 0; double ema_21[]; //needed so that we can trade CTrade trade; ulong trade_ticket = 0; int OnInit(){ //setting up ema21close EMA_21_M5 = iMA(_Symbol,_Period, 21, 0, MODE_EMA,PRICE_CLOSE); ArraySetAsSeries(ema_21, true); return (INIT_SUCCEEDED); } void OnTick() { datetime time_local=TimeLocal(); ///Print("Time Local: ",TimeToString(time_local,TIME_DATE|TIME_SECONDS)); MqlDateTime STime; TimeToStruct(time_local,STime); STime.hour=0; STime.min=0; STime.sec=0; datetime time_start_day=StructToTime(STime); //Print("Time Start Day: ",TimeToString(time_start_day,TIME_DATE|TIME_SECONDS)); ////SETTING UP LOSESTREAK WINSTREAK COUNTERS int today_lose_streak = 0; int today_win_streak = 0; //--- request trade history //format the time and create a string string hoursAndMinutes = TimeToString(time_local,TIME_MINUTES); MqlRates PriceInformation[]; CopyBuffer(EMA_21_M5, 0, 1, 5, ema_21); ArraySetAsSeries(PriceInformation, true); //taking real time data for the symbol we want int Data=CopyRates(Symbol(), Period(), 0, Bars(Symbol(), Period()), PriceInformation); if (PositionSelectByTicket(trade_ticket) == false) trade_ticket = 0; static int previous_open_positions = 0; int current_open_positions = PositionsTotal(); HistorySelect(time_start_day,time_local+3600*24*3); int All_Deals = HistoryDealsTotal(); if(current_open_positions < previous_open_positions) // a position just got closed: { previous_open_positions = current_open_positions; //HistorySelect(time_start_day,time_local+3600*24*3); if(All_Deals < 1) {Print("NO TRADE DONE TODAY");} // 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...) double LAST_TRADE_PROFIT = HistoryDealGetDouble(temp_Ticket , DEAL_PROFIT); ///Print("Last Trade Profit : ", DoubleToString(LAST_TRADE_PROFIT)); if (LAST_TRADE_PROFIT<0) {today_lose_streak = 1;} if (LAST_TRADE_PROFIT>0) { today_win_streak = 1; Sleep(18000000); } if(All_Deals < 2) {Print("NOT 2 TRADES DONE TODAY");} // last deal (should be an DEAL_ENTRY_OUT type): ulong temp_Ticket2 = HistoryDealGetTicket(All_Deals-2); // here check some validity factors of the position-closing deal // (symbol, position ID, even MagicNumber if you care...) double LAST_TRADE_PROFIT2 = HistoryDealGetDouble(temp_Ticket , DEAL_PROFIT); ///Print("Last Trade Profit : ", DoubleToString(LAST_TRADE_PROFIT2)); if (LAST_TRADE_PROFIT2<0) { today_lose_streak = today_lose_streak +1; } } else if(current_open_positions > previous_open_positions) // a position just got opened: previous_open_positions = current_open_positions; //////////First trade of the day///////// //setting up the timeline we want our bot to take trades if ((StringSubstr(hoursAndMinutes,0,5) > "09:00") && (StringSubstr(hoursAndMinutes,0,5)< "13:00") && (HistoryDealGetTicket(All_Deals) < 1)) { //setting up buy if (ema_21[1] > PriceInformation[1].open && ema_21[1] < PriceInformation[1].close && trade_ticket <= 0){ //ask is the real time price we are going to have action (buy) double ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits); //STOP LOSS Variable double sl = ask-50*_Point; //TAKE PROFIT Variable double tp = ask+50*_Point; trade.Buy(0.10,//setting up the lottage _Symbol,ask,sl,tp,NULL); trade_ticket = trade.ResultOrder(); Sleep(300000); } //setting up sell else if (ema_21[1]<PriceInformation[1].open && ema_21[1]>PriceInformation[1].close && trade_ticket <= 0){ //ask is the real time price we are going to have action (sell) double bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits); //STOP LOSS Variable double sl = bid+50*_Point; //TAKE PROFIT Variable double tp = bid-50*_Point; trade.Sell(0.10,//setting up the lottage _Symbol,bid,sl,tp,NULL); trade_ticket = trade.ResultOrder(); Sleep(300000); } } //////////// When we have 1 losestreak(when we already have done a trade today and its lost)//////// if ((StringSubstr(hoursAndMinutes,0,5) > "09:00") && (StringSubstr(hoursAndMinutes,0,5)< "13:00") && (HistoryDealGetTicket(All_Deals) == 1) && (today_lose_streak==1)) { //setting up buy if (ema_21[1] > PriceInformation[1].open && ema_21[1] < PriceInformation[1].close && trade_ticket <= 0){ //ask is the real time price we are going to have action (buy) double ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits); //STOP LOSS Variable double sl = ask-50*_Point; //TAKE PROFIT Variable double tp = ask+50*_Point; trade.Buy(0.20,//setting up the lottage _Symbol,ask,sl,tp,NULL); trade_ticket = trade.ResultOrder(); Sleep(300000); } //setting up sell else if (ema_21[1]<PriceInformation[1].open && ema_21[1]>PriceInformation[1].close && trade_ticket <= 0){ //ask is the real time price we are going to have action (sell) double bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits); //STOP LOSS Variable double sl = bid+50*_Point; //TAKE PROFIT Variable double tp = bid-50*_Point; trade.Sell(0.20,//setting up the lottage _Symbol,bid,sl,tp,NULL); trade_ticket = trade.ResultOrder(); Sleep(300000); }//if } }//ontick

- 2021.12.10
- www.mql5.com
void OnTick() { datetime time_local=TimeLocal(); Print("Time Local: ",TimeToString(time_local,TIME_DATE|TIME_SECONDS)); MqlDateTime STime; TimeToStruct(time_local,STime); STime.hour=0; STime.min=0; STime.sec=0; datetime time_start_day=StructToTime(STime); Print("Time Start Day: ",TimeToString(time_start_day,TIME_DATE|TIME_SECONDS)); //--- request trade history HistorySelect(time_start_day,time_local+3600*24*3); uint total=HistoryDealsTotal(); ulong ticket=0; double price; double profit; datetime time; int lose_streak=0; bool win_streak = false; long type; long entry; for (uint i=0;i<total;i++) { if((ticket=HistoryDealGetTicket(i))>0) { price=HistoryDealGetDouble(ticket,DEAL_PRICE); time =(datetime)HistoryDealGetInteger(ticket,DEAL_TIME); type =HistoryDealGetInteger(ticket,DEAL_TYPE); entry =HistoryDealGetInteger(ticket,DEAL_ENTRY); profit=HistoryDealGetDouble(ticket,DEAL_PROFIT); /// Print("the ticket number is: ",ticket,"and the deal's profit is: ",profit); if (profit<0) {lose_streak +=1; }//if else {win_streak=true; }//else }//if }//for }//ontick
i tried to combine the code you learned me with the one in the example. That is my try. i Hope i am in a good way.
Thank's for the teaching!
I really appreciate it.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello!
I am trying to make and EA that will search in my historyordes and check if my last 4 orders of today are all lost so that i can change my lot for the next trades. For example if the consecutive loses are 2 increase next trades lot to 0.2 OR if the consucutive loses are 1 increase the
lot to 0.15. Also i want it to make my EA stop if i win a trade and start again next day.
Thank's in advance. I am a total new programmer in university and i am trying to make some EAs because i love trading and programming too.