Your code | Print("Ticket: ",buyticketnumber); if(!OrderSelect (buyticketnumber,SELECT_BY_TICKET,MODE_TRADES)){ Print("M-Ticket: ",OrderTicket(),"OOP: ",OrderOpenPrice()); Print( "Martingale error ",GetLastError()," occurred"); } |
Your output | 2017.02.20 12:03:41.098 2017.02.01 01:06:00 TheDescisionTaker EURUSDmicro,M1: Martingale error 4051 occurred Where is M-Ticket? Code and output do not match! 2017.02.20 12:03:41.098 2017.02.01 01:06:00 TheDescisionTaker EURUSDmicro,M1: Ticket: 1 |
Your code | Print("Ticket: ",buyticketnumber); if(!OrderSelect (buyticketnumber,SELECT_BY_TICKET,MODE_TRADES)){ Print("M-Ticket: ",OrderTicket(),"OOP: ",OrderOpenPrice()); Print( "Martingale error ",GetLastError()," occurred"); } |
Your output | 2017.02.20 12:03:41.098 2017.02.01 01:06:00 TheDescisionTaker EURUSDmicro,M1: Martingale error 4051 occurred Where is M-Ticket? Code and output do not match! 2017.02.20 12:03:41.098 2017.02.01 01:06:00 TheDescisionTaker EURUSDmicro,M1: Ticket: 1 |
My main question though is why I'm getting that 4051? There are no data type mismatches that I can see. I don't see what function parameter is causing the problem.
Try following to narrow down cause of the error:
- save or print GetLastError() value as the first thing after OrderSelect()
- Orderxxx() functions results are undefined if OrderSelect() fails, so you probably shouldn't call OrderTicket() and OrderOpenPrice() at that place
- ticket number 1 may happen only as the first order in the strategy tester. Otherwise, you have a bug in the code that determines the ticket number.
If OrderSelect fails because the ticket number (or index) doesn't exist, you get Error 4051.
Additionally, some print statements get skipped in the Experts log. Try right-clicking the Experts log and selecting "view"
datas_brother: There is no "M-Ticket".
Print("M-Ticket: ", OrderTicket(), "OOP: ", OrderOpenPrice()); | Where is the output from the print statement containing "M-Ticket?" Your code and the output does not match. |
Try following to narrow down cause of the error:
- save or print GetLastError() value as the first thing after OrderSelect()
- Orderxxx() functions results are undefined if OrderSelect() fails, so you probably shouldn't call OrderTicket() and OrderOpenPrice() at that place
- ticket number 1 may happen only as the first order in the strategy tester. Otherwise, you have a bug in the code that determines the ticket number.
The OrderTicket() and OrderOpenPrice() were put in after the error started to be able to follow the flow in the journal.
You are right, it is the first ticket in the tester.
OK, it's not OrderSelect().
Anyway, you should not call OrderTicket() and OrderOpenPrice() when OrderSelect() fails. Behavior is undefined.
Are you absolutely sure that the order with the ticket number 1 exists?
Is it possible that it was a pending order that was deleted or expired at some point?
My main question though is why I'm getting that 4051? There are no data type mismatches that I can see. I don't see what function parameter is causing the problem.
If OrderSelect fails because the ticket number (or index) doesn't exist, you get Error 4051.
Additionally, some print statements get skipped in the Experts log. Try right-clicking the Experts log and selecting "view"
So try adding this:
if (UseMartingale)
{ Print("Ticket: ",buyticketnumber);
if(!OrderSelect (buyticketnumber,SELECT_BY_TICKET,MODE_TRADES))
{
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS)) Print(OrderTicket());
}
Print("M-Ticket: ",OrderTicket(),"OOP: ",OrderOpenPrice());
Print( "Martingale error ",GetLastError()," occurred");}
else
if (Ask<OrderOpenPrice()-MartLimit*Point)
Martingale(0);
}
I'm fairly confident that you won't get a match between buyticketnumber and the printed ticket numbers.
There is no point printing the value of OrderTicket() or OrderOpenPrice() if OrderSelect() has failed. All the values of Orderxxxxx() are irrelevant if the OrderSelect failed.
So try adding this:
if (UseMartingale)
{ Print("Ticket: ",buyticketnumber);
if(!OrderSelect (buyticketnumber,SELECT_BY_TICKET,MODE_TRADES))
{
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS)) Print(OrderTicket());
}
Print("M-Ticket: ",OrderTicket(),"OOP: ",OrderOpenPrice());
Print( "Martingale error ",GetLastError()," occurred");}
else
if (Ask<OrderOpenPrice()-MartLimit*Point)
Martingale(0);
}
I'm fairly confident that you won't get a match between buyticketnumber and the printed ticket numbers.
There is no point printing the value of OrderTicket() or OrderOpenPrice() if OrderSelect() has failed. All the values of Orderxxxxx() are irrelevant if the OrderSelect failed.
if (LongCount <= MaxTradeBuy)
if (UseMartingale)
if (buyticketnumber>0)
if(!OrderSelect (buyticketnumber,SELECT_BY_TICKET,MODE_TRADES))
Print( "Martingale error ",GetLastError()," occurred");
else
if (Ask<OrderOpenPrice()-MartLimit*Point)
Martingale(0);
I was up til 1:00 am trying to find the problem. First, adding that for-loop gave me identical ticket numbers. I also threw out all of the print statements and the problem remained. I then followed the logic painstakingly all the way to the end. The problem was with the structure. The code in my OP is repeated for sell within the same function. If there is only a buy order open the "Check for martingale" part was also being run for short positions. Since there were none open it threw taht error. The solution was checking if buyticketnumber and sellticketnumber != 0.
if (LongCount <= MaxTradeBuy)
if (UseMartingale)
if (buyticketnumber>0)
if(!OrderSelect (buyticketnumber,SELECT_BY_TICKET,MODE_TRADES))
Print( "Martingale error ",GetLastError()," occurred");
else
if (Ask<OrderOpenPrice()-MartLimit*Point)
Martingale(0);
Error 130 may be due to a lack of braces after OrderSelect()
{
Print ("Real Ticket: ",OrderTicket());
if (OrderMagicNumber()==MagicNumberLong)
if (OrderSymbol()==Symbol())
if (OrderType()==OP_BUY)
{
if(Ask-OrderTakeProfit()>Trail*Point)
TrailingStop(OrderTicket());
oldticketnumber = OrderTicket();
if (oldticketnumber > buyticketnumber)
buyticketnumber = oldticketnumber; //Sets buyticketnumber to newest order
}
}
}
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
for ( i=OrdersTotal()-1;i>=0;i--)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
Print ("Real Ticket: ",OrderTicket());
if (OrderMagicNumber()==MagicNumberLong)
if (OrderSymbol()==Symbol())
if (OrderType()==OP_BUY)
{
if(Ask-OrderTakeProfit()>Trail*Point)
TrailingStop(OrderTicket());
oldticketnumber = OrderTicket();
if (oldticketnumber > buyticketnumber)
buyticketnumber = oldticketnumber; //Sets buyticketnumber to newest order
}
}
//+ Check for Martingale condition +
if (LongCount <= MaxTradeBuy)
if (UseMartingale)
{ Print("Ticket: ",buyticketnumber);
if(!OrderSelect (buyticketnumber,SELECT_BY_TICKET,MODE_TRADES))
{
Print("M-Ticket: ",OrderTicket(),"OOP: ",OrderOpenPrice());
Print( "Martingale error ",GetLastError()," occurred");}
else
if (Ask<OrderOpenPrice()-MartLimit*Point)
Martingale(0);
}
2017.02.20 12:03:41.845 2017.02.01 01:07:00 TheDescisionTaker EURUSDmicro,M1: Ticket: 1
2017.02.20 12:03:41.845 2017.02.01 01:07:00 TheDescisionTaker EURUSDmicro,M1: Real Ticket: 1
2017.02.20 12:03:41.098 2017.02.01 01:06:00 TheDescisionTaker EURUSDmicro,M1: Martingale error 4051 occurred
2017.02.20 12:03:41.098 2017.02.01 01:06:00 TheDescisionTaker EURUSDmicro,M1: Ticket: 1
2017.02.20 12:03:41.098 2017.02.01 01:06:00 TheDescisionTaker EURUSDmicro,M1: Real Ticket: 1
2017.02.20 12:03:40.380 2017.02.01 01:05:00 TheDescisionTaker EURUSDmicro,M1: Martingale error 4051 occurred
2017.02.20 12:03:40.380 2017.02.01 01:05:00 TheDescisionTaker EURUSDmicro,M1: Ticket: 1
What it should be doing is getting the ticket number of the last order, checking if the martingale option is true (user input) and checking if a new order price level was reached. if all are true send "0" (similar for sell but "1") to the Martigale(). It doesn't seem to reach the final "if" to check the price.
I hope someone sees my mistake because sure don't.
Thanks