I've got this function which i don't know why it doesn't work. I want the EA to place a buy order is last closed sell trade was a loss.
then if LastS()<0 ..then buy .. i.e if last order profit is negative and order was a sell, then buy. But this does not seem to place any orders. Is there anything wrong??
please help
-
Using OrdersTotal directly and/or no Magic number filtering on your
OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual
trading.)
Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum - Do not assume history is ordered by date, it's not.
Could EA Really Live By Order_History Alone? (ubzen) - MQL4 and MetaTrader 4 - MQL4 programming forum
Count how many lost orders from the last profit order - MQL4 and MetaTrader 4 - MQL4 programming forum
please help
- YOU didn't write this.
- It isn't sending BUY orders because there is no code to SEND buy orders.
- READ the documentation. --> https://docs.mql4.com/
- READ the book. --> https://book.mql4.com/
- Put forth some effort and do not expect others to work for free.
- docs.mql4.com
ill this will add magic number, will this work?
double LastS() { double ProS = 0; for(int i=OrdersHistoryTotal()-1; i>=0; i--) { if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) == true) // match the symbol if(OrderSymbol() == Symbol &&MagicNumber()==Magic number.. { if(OrderType() == OP_SELL) ProS +=OrderClosePrice()-OrderOpenPrice(); } } return( ProS);
Will this work?
ill this will add magic number, will this work?
Will this work?
That isn't referencing the magic number of the ticket.
It's OrderMagicNumber()
Please see this documentation page.
https://docs.mql4.com/trading/ordermagicnumber
I'd use something more like this:
int magic = 123; //my magic number for(int x=OrdersTotal()-1;x>=0;x--){ //scan the order basket if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES)==true){ //select a ticket via index position X from the current trade basket (Change if you want history...) if(OrderSymbol()==Symbol() && OrderMagicNumber() == magic){ //does the ticket symbol match the chart? And does the ticket magic number match my defined magic number if(OrderType()==OP_BUY){ /* DO SOMETHING HERE FOR BUY TICKETS*/} if(OrderType()==OP_SELL){/* DO SOMETHING HERE FOR SELL TICKETS*/} } } }
I didn't test that, but it should give you a good example to work from.
-Jack
- docs.mql4.com
double last;
for(int i=OrdersHistoryTotal()-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS,MODE_HISTORY); //error was here
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
//for buy order
if(OrderType()==OP_BUY && OrderClosePrice()>=OrderOpenPrice()) last=1;
if(OrderType()==OP_BUY && OrderClosePrice()<=OrderOpenPrice()) last=0;
}
}
What about this code?
Thanks.
//+------------------------------------------------------------------+ //| Get Profits | //+------------------------------------------------------------------+ double GetProfits(ENUM_ORDER_TYPE Type, int Count) { //---- int Cnt = 0; double Profits = 0.0; for(int i = OrdersHistoryTotal() - 1; i >= 0; i--) { if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) == false) { Print("Error in history!"); break; } if(OrderSymbol() == _Symbol && OrderMagicNumber() == MagicNum) { if(OrderType() == Type) { Cnt++; Profits += OrderProfit(); } } if(Cnt == Count) break; } //---- return(Profits); }
Get the total profits of last xx trades
Get the total profits of last xx trades
I just mean the profit of last closed order.Not the sum of all closed orders.,
I just mean the profit of last closed order.Not the sum of all closed orders.,
double LastOrderClosedProfit() { int ticket =-1; datetime last_time = 0; for(int i=OrdersHistoryTotal()-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)&&OrderSymbol()==_Symbol&&OrderCloseTime()>last_time) { last_time = OrderCloseTime(); ticket = OrderTicket(); } } if(!OrderSelect(ticket,SELECT_BY_TICKET)) { Print("OrderSelectError: ",GetLastError()) return 0.0; } return OrderProfit(); }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I've got this function which i don't know why it doesn't work. I want the EA to place a buy order is last closed sell trade was a loss.
then if LastS()<0 ..then buy .. i.e if last order profit is negative and order was a sell, then buy. But this does not seem to place any orders. Is there anything wrong??