https://www.mql5.com/en/forum/107126 4 the next time use search
im trying to do something if the order lost, i tryied to use orderprofit but it always give me the same number (lose/win) really wierd someone have any idea
If it's the same ticket in the history pool, then it's the same profit. Selecting with MODE_HISTORY grabs all closed & cancelled trades. You may want to use the MODE_TRADES for open/active trades.
![]() ![]() |
`Thanks for your help, but it didnt work, im try to increace my lot by 2 if the last order lost, and to go back to default if the order didnt lose! im using bolinger and it arount 50%, im using the open\ close price function in order to know if i lost \win. but it doesnt give me the result as they occerred this is the code int type_order; double money_b, money_s, x, y, ope, clo; if (Order== SIGNAL_TRBUY) { Order=IDLE; trade=false; if(OrderSelect(ticketb, SELECT_BY_TICKET)==true) { type_order=OrderType(); Print("type for the order ",OrderType()); ope=OrderOpenPrice(); clo=OrderClosePrice(); if (OrderType()==0) { if(clo> ope) //win { money_b= 2; } else { money_b=-2; } if( money_b>0) //win count=0; if(money_b<0) //lose { lot=lot*2; count++; } } if (OrderType()==1) { if (clo< ope) { money_s=2; } else { money_s=-2; } if( money_s>0) //win count=0; if(money_s<0) //lose { lot=lot*2; count++; } } } } |
the last order lost
u mean a closed order or not
Where do you define the variable ticketB
i assume that there is the problem
the last order lost
u mean a closed order or not
Where do you define the variable ticketB
i assume that there is the problem
thanks again this is the full code, i mean to the last closed order, as my ticketb =
ticketb=OrderSend(Symbol(),OP_SELL,lot,Bid,3,0,0,"bolinger",MagicNumber,0,Green);
//+------------------------------------------------------------------+ //| MA_with_TP_SL.mq4 | //| sell when MA crossing without SL and TP (working) | //| http://ftsa.co.il | //+------------------------------------------------------------------+ #property link "" #include <stderror.mqh> #include <stdlib.mqh> #define IDLE 0 #define SIGNAL_BUY 1 #define SIGNAL_SELL 2 #define SIGNAL_TRBUY 3 #define SIGNAL_TRSELL 4 #define SIGNAL_P 5 #define SIGNAL_L 6 bool trade=false; int ticketb=0, count=0; //int ticketb=0; int Order = IDLE; bool flagbuy = true; bool flagsell = true; double BOLL; double BOLH, MA; extern double lotSize=0.1; extern int TP=400; extern int SL=200; extern int Per=20; extern double DEV=2; extern int shift=0; extern int MagicNumber=7272; double lot , lotSize1; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- if (count==0) lot=lotSize; if (OrdersTotal()==0) { trade=false; Order = IDLE; } if (OrdersTotal()!=0) { for (int i=1; i<=OrdersTotal(); i++) //Cycle for all orders.. { //displayed in the terminal if(OrderSelect(i-1,SELECT_BY_POS)==true)//If there is the next one { if (OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) { trade=true; break; } else {trade=false;} } } } //////////////////// INDICATORS\ BOLH=iCustom(NULL,0,"Bands",Per,0,DEV,MODE_UPPER,shift);////the last zero its the shift BOLL= iCustom(NULL,0,"Bands",Per,0,DEV,MODE_LOWER,shift); MA=iMA(NULL,0,Per,0,0,0,shift); Comment("BOLL = " ,BOLL ,"BOLH = ", BOLH, "\nclose+1", Close[1]); // show the price of the boll high //////////////////CHECK FOR BUY if (trade==false && Order==IDLE && flagbuy==false) { if ( Close[1]<BOLL ) { Order=SIGNAL_BUY; flagbuy=true; } } //////////////////CHECK FOR SELL if (trade==false && Order==IDLE && flagsell==false) { if ( Close[1]>BOLH ) { Order=SIGNAL_SELL; flagsell=true; } } /////////////////////////BUY_ order if (trade==false) { if (Order == SIGNAL_BUY ) { ticketb=OrderSend(Symbol(),OP_BUY,lot,Ask,3, 0, 0,"bolinger",MagicNumber ,0 ,Green); //open buy if(OrderSelect( ticketb, SELECT_BY_TICKET )==true) OrderModify(ticketb,OrderOpenPrice(),Ask- SL*Point,Ask+ (TP * Point) ,0,Green); if(OrderSelect( ticketb, SELECT_BY_TICKET )==true) { Alert("LONG ORDER OPENED IN bolinger"," Symbol= ",Symbol()," Period=",Period(), " Ticket= ",ticketb); Order = SIGNAL_TRBUY; } else { Print("OrderSelect returned the error of ",GetLastError()); Alert("EROR LONG ORDER IN bolinger"," Symbol= ",Symbol()," Period=",Period()," EROR= ",GetLastError()); Order= IDLE; } } } /////////////////////////////////// END /////////////////////////sell if (trade==false) { if (Order == SIGNAL_SELL ) { ticketb=OrderSend(Symbol(),OP_SELL,lot,Bid,3,0,0,"bolinger",MagicNumber,0,Green); // open sell if(OrderSelect( ticketb, SELECT_BY_TICKET )==true) OrderModify(ticketb,OrderOpenPrice(),Bid+ (SL*Point),Bid-(TP*Point),0,Blue); if(OrderSelect( ticketb, SELECT_BY_TICKET )==true) { Alert("SHORT ORDER OPENED IN bolinger"," Symbol= ",Symbol()," Period=",Period(), " Ticket= ",ticketb); Order = SIGNAL_TRBUY; } else { Print("OrderSelect returned the error of ",GetLastError()); Alert("ERROR SHORT ORDER IN bolinger "," Symbol= ",Symbol()," Period=",Period()," EROR= ",GetLastError()); Order= IDLE; } } } /////////////////////////////////// END OrderSelect(i,SELECT_BY_POS,MODE_HISTORY); /////////////////check order status buy int type_order; double money_b, money_s, x, y, ope, clo; if (Order== SIGNAL_TRBUY) { Order=IDLE; trade=false; if(OrderSelect(ticketb, SELECT_BY_TICKET)==true) { type_order=OrderType(); Print("type for the order ",OrderType()); ope=OrderOpenPrice(); clo=OrderClosePrice(); if (OrderType()==0) { if(clo> ope) //win { x= clo- ope; Print("buy if x= + then order profit =", x); money_b= 2; } else { money_b=-2; x= clo - ope; Print("buy if x= - then order lost =", x); } if( money_b>0) //win count=0; if(money_b<0) //lose { lot=lot*2; count++; } } if (OrderType()==1) { if (clo< ope) { y= ope- clo; Print("sell if y= + then order profit =", y); money_s=2; } else { Print("sell if x= - then order lost =", y); money_s=-2; } if( money_s>0) //win count=0; if(money_s<0) //lose { lot=lot*2; count++; } } } } if (MA<Ask) { flagbuy=false; } if (MA>Ask) { flagsell=false; } /////////////////////////////////// //---- return(0); } //+------------------------------------------------------------------+
u shold put the whole thing /////////////////check order status buy (your code)
on the begining of the EA OR made it for one function:
void CheckOrderStatusBuy()
{
the whole code
}
& check it here
if (count==0 && CheckOrderStatusBuy() == 0)
lot=lotSize;
(somthing like this)
i did move it to be in function, and still im getting the wrong info, i added print statement in order to see the numbers, and its the wrong numbers, i think im doing something wrong with my orderselect()
or with the location of the function.
thank again
//+------------------------------------------------------------------+ // Shahar BIrebdorf #property link "" #include <stderror.mqh> #include <stdlib.mqh> #define IDLE 0 #define SIGNAL_BUY 1 #define SIGNAL_SELL 2 #define SIGNAL_TRBUY 3 #define SIGNAL_TRSELL 4 #define SIGNAL_P 5 #define SIGNAL_L 6 bool trade=false; int ticketb=0, count=0; int Order = IDLE; bool flagbuy = true; bool flagsell = true; double BOLL; double BOLH, MA; extern double lotSize=0.1; extern int TP=400; extern int SL=200; extern int Per=20; extern double DEV=2; extern int shift=0; extern int MagicNumber=7272; double lot , lotSize1; //+------------------------------------------------------------------+ //| expert initialization function | //+--- //| expert start function | //+------------------------------------------------------------------+ int start() { //---- if (count == 0) // return to default postion lot=lotSize; CheckOrderStatusBuy(); if (OrdersTotal()==0) { trade=false; Order = IDLE; } if (OrdersTotal()!=0) { for (int i=1; i<=OrdersTotal(); i++) //Cycle for all orders.. { //displayed in the terminal if(OrderSelect(i-1,SELECT_BY_POS)==true)//If there is the next one { if (OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) { trade=true; break; } else {trade=false;} } } } //////////////////// INDICATORS\ BOLH=iCustom(NULL,0,"Bands",Per,0,DEV,MODE_UPPER,shift);////the last zero its the shift BOLL= iCustom(NULL,0,"Bands",Per,0,DEV,MODE_LOWER,shift); MA=iMA(NULL,0,Per,0,0,0,shift); Comment("BOLL = " ,BOLL ,"BOLH = ", BOLH, "\nclose+1", Close[1]); // show the price of the boll high //////////////////CHECK FOR BUY if (trade==false && Order==IDLE && flagbuy==false) { if ( Close[1]<BOLL ) { Order=SIGNAL_BUY; flagbuy=true; } } //////////////////CHECK FOR SELL if (trade==false && Order==IDLE && flagsell==false) { if ( Close[1]>BOLH ) { Order=SIGNAL_SELL; flagsell=true; } } /////////////////////////BUY_ order if (trade==false) { if (Order == SIGNAL_BUY ) { ticketb=OrderSend(Symbol(),OP_BUY,lot,Ask,3, 0, 0,"bolinger",MagicNumber ,0 ,Green); //open buy if(OrderSelect( ticketb, SELECT_BY_TICKET )==true) OrderModify(ticketb,OrderOpenPrice(),Ask- SL*Point,Ask+ (TP * Point) ,0,Green); if(OrderSelect( ticketb, SELECT_BY_TICKET )==true) { Alert("LONG ORDER OPENED IN bolinger"," Symbol= ",Symbol()," Period=",Period(), " Ticket= ",ticketb); Order = SIGNAL_TRBUY; } else { Print("OrderSelect returned the error of ",GetLastError()); Alert("EROR LONG ORDER IN bolinger"," Symbol= ",Symbol()," Period=",Period()," EROR= ",GetLastError()); Order= IDLE; } } } /////////////////////////////////// END /////////////////////////sell if (trade==false) { if (Order == SIGNAL_SELL ) { ticketb=OrderSend(Symbol(),OP_SELL,lot,Bid,3,0,0,"bolinger",MagicNumber,0,Green); // open sell if(OrderSelect( ticketb, SELECT_BY_TICKET, MODE_HISTORY )==true) OrderModify(ticketb,OrderOpenPrice(),Bid+ (SL*Point),Bid-(TP*Point),0,Blue); if(OrderSelect( ticketb, SELECT_BY_TICKET )==true) { Alert("SHORT ORDER OPENED IN bolinger"," Symbol= ",Symbol()," Period=",Period(), " Ticket= ",ticketb); Order = SIGNAL_TRBUY; } else { Print("OrderSelect returned the error of ",GetLastError()); Alert("ERROR SHORT ORDER IN bolinger "," Symbol= ",Symbol()," Period=",Period()," EROR= ",GetLastError()); Order= IDLE; } } } ////////////// /////////////////check order status buy //CheckOrderStatusBuy(); if (MA<Ask) { flagbuy=false; } if (MA>Ask) { flagsell=false; } /////////////////////////////////// //---- return(0); } //+------------------------------------------------------------------+ void CheckOrderStatusBuy() { if (Order== SIGNAL_TRBUY) { Order=IDLE; trade=false; int type_order; double money_b, money_s, x, y, ope, clo, profit, stoploss, po; if(OrderSelect(ticketb, SELECT_BY_TICKET, MODE_HISTORY)==true) { type_order=OrderType(); Print("type for the order ",OrderType()); ope=OrderOpenPrice(); clo=OrderClosePrice(); profit= OrderTakeProfit(); stoploss= OrderStopLoss(); po= OrderProfit(); Print("open price= ", ope, " close price= ", clo," TP= " , profit, " SL= " , OrderStopLoss(), "orderprofit= ", po); if (OrderType()==0) //long { if(clo> ope) //win { x= clo- ope; Print("buy if x= + then order profit =", x); money_b= 2; } else //lost { money_b= -2; x= clo - ope; Print("buy if x= - then order lost =", x); } if( money_b>0) //win count=0; if(money_b<0) //lose { lot=lot*2; count++; //set the counter flag not = to 0 } } if (OrderType()==1) { if (clo < ope) { y= ope - clo; //need to be a positve number in order to know that order won Print("sell if y= + then order profit =", y); money_s= 2; } else { Print("sell if x= - then order lost =", y); money_s= -2; } if( money_s > 0) //win count=0; if(money_s < 0) //lose { lot=lot*2; count++; } } } } }
i did move it to be in function, and still im getting the wrong info, i added print statement in order to see the numbers, and its the wrong numbers, i think im doing something wrong with my orderselect()
or with the location of the function.
thank again
nothing is wrong with OrderSelect() the only thing is that, if u compiling or closing the terminal, ticketb turns back to 0
my suggestion is to use GlobalVariableSet(), GlobalVariableGet(), GlobalVariableCheck(), avoid it
or use OrderSelect(OrdersHistoryTotal()-1, SELECT_BU_POS,MODE_HISTORY); Instead of ticketb
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
im trying to do something if the order lost, i tryied to use orderprofit but it always give me the same number (lose/win) really wierd someone have any idea