-
You pasted quoted text inside a SRC bar. That is why you lost the formatting.
Play videoPlease edit your post.
For large amounts of code, attach it. - Don't add text inside
quoted text or put text inside a SRC block, put it outside. MQL4
Forum editor problem - MQL4 forum
- Where in your function do you set your count to zero before counting?
But when I tested it, it gives very strange results
- You are counting down (assuming history is ordered,) so the moment you find a loss, the count of consecutive wins after that loss, is what ever the count currently is. What you are returning is the first consecutive win count, not the latest.
- You assume history is ordered by date, it's not. Could EA
Really Live By Order_History Alone? (ubzen) - MQL4 forum Your going to have to sort history to do this properly.
Sorry, I couldn't figure out how to do it? I hope if anyone could tell me how to correct my code
Sorry, I couldn't figure out how to do it? I hope if anyone could tell me how to correct my code
There's not really a way to "correct" your code. Like whroeder said, you have to sort your history-orders (LIFO - Descending by Close time) programmatically before you can even begin to start counting. That's not easy in MQL if you don't know OOP concepts and the standard library. If you do, however, then it's relatively easy. First you make a class for history orders which extends CObject and then override the compare method for sorting. Next you'd want to derive a class from CArrayObj or CList to hold and sort the history deals. This is an example of how I'd do it ...
//+------------------------------------------------------------------+ //| Consecutive_wins.mq4 | //| nicholishen | //| www.reddit.com/u/nicholishenFX | //+------------------------------------------------------------------+ #property copyright "nicholishen" #property link "www.reddit.com/u/nicholishenFX" #property version "1.00" #property strict #property script_show_inputs input int Magic = 0; #include <Arrays\ArrayObj.mqh> //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class Deal : public CObject { public: int ticket; datetime time; double profit; // override CObject::Compare for sorting int Compare(const CObject *node,const int mode=0)const override { Deal *other = (Deal*)node; if(this.time > other.time) return -1; if(this.time < other.time) return 1; return 0; } }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class CWins : public CArrayObj { protected: int m_magic; public: CWins():m_magic(0){} CWins(int magic):m_magic(magic){} Deal* operator[](const int index)const{return (Deal*)At(index);} int ConsecutiveWins() { GetHistory(); int cnt =0; for(int i=0;i<Total();i++) { if(this[i].profit >=0) cnt++; else return cnt; } return cnt; } void SortedHistoryToLog() { GetHistory(); for(int i=0;i<Total() && i < 20;i++) //max 20 deals to log Print(this[i].time," - ",this[i].ticket," - $",this[i].profit); } protected: void GetHistory() { Clear(); for(int i=0;i<OrdersHistoryTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) && (m_magic == 0 || OrderMagicNumber() == m_magic) && (OrderType()==OP_BUY || OrderType() == OP_SELL)) { Deal *deal = new Deal; deal.ticket = OrderTicket(); deal.time = OrderCloseTime(); deal.profit = OrderProfit(); CArrayObj::Add(deal); } } Sort(); } }; //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- CWins cwins(Magic); Print("Consecutive wins = ",cwins.ConsecutiveWins()); cwins.SortedHistoryToLog(); } //+------------------------------------------------------------------+
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi
Using Mql4 I am trying to use the consecutive wins count to increase the lots multiplier after the winning trades starting from 1 until it reaches to the maximum lots multiplier and to reset the lots multiplier to 1 after any losing trade to start the cycle again with the consecutive win trades.
I created this code for the lots multiplier
extern bool RTrade = true;
extern int MaxLotMultiplier = 6;
extern double distanceMultiplier = 1;
double LotsMultiplier()
{
for(int Count = OrdersHistoryTotal()-1; ; Count--)
{
if(OrderSelect(Count,SELECT_BY_POS,MODE_HISTORY)==true)
{if(OrderSymbol() == Symbol() && OrderMagicNumber() == magicNumber)
{
if(OrderProfit() > 0 ) WinCount++;
else if(OrderProfit() < 0 )
{WinCount = 0; break;}
else break;
}
}
return WinCount;
}
Count--;
if(RTrade == true && noHistory==false && OrderProfit() > 0) LotsMultiplier_ = WinCount;
else if(RTrade == true && noHistory==false &&OrderProfit() < 0) LotsMultiplier_ = 1;
if(RTrade == false || noHistory==true ) LotsMultiplier_ = 1;
if(LotsMultiplier_ >= MaxLotMultiplier) LotsMultiplier_ = MaxLotMultiplier;
return (LotsMultiplier_);
}
But when I tested it, it gives very strange results as per the following Table
Can any one help Me?