[ARCHIVE]Any rookie question, so as not to clutter up the forum. Professionals, don't pass it by. Can't go anywhere without you - 5. - page 203

 
Hello, could you please give me a command (if there is one) to randomly select a number either 0 or 1, thank you in advance?
 
hoz:


For starters, get used to putting brackets where you need them. Like this:

And in general, you should have a time range, not a fix. value. Your logic is not correct at all. Not 1 hour OR 3 hours, but 1 hour to 3 hours!!!
Thank you. Got it wrong.
 
stater:
Hello, could you please give me a command (if there is one) to randomly select a number either 0 or 1, thank you in advance?
int x =MathRand() % 2;
 

During testing of the Expert Advisor I have found errors of code execution:
- execution of the loop for(i=0;i<OrdersTotal();i++) is interrupted with the next tick (below in Bold),
- Buy orders are selected and closed in any order.

Can you please tell me how to find the cause and fix it?
Here is the code of the program and report from the tester's log.

Programme code.

//+------------------------------------------------------------------+
//|Poligon1.mq4 |
//| Copyright 2012, MetaQuotes Software Corp. |
//| Test of the loop operator.|
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link ""
//+------------------------------------------------------------------+
//Declared variables|
//+------------------------------------------------------------------+
int Mn=357;//Magic number
int i;//Iteration counter index
double PriTP;//TP series
int Tick;//Tick counter
//+------------------------------------------------------------------+
//| expert initialization function|
//+------------------------------------------------------------------+
int init()
{
//----Setting orders
OrderSend(Symbol(),OP_BUY,0.5,Ask,0,Bid-350*Point,Ask+350*Point,NULL,Mn,0,CLR_NONE);//
OrderSend(Symbol(),OP_BUY,1,Ask,0,Bid-250*Point,Ask+250*Point,NULL,Mn,0,CLR_NONE);//
OrderSend(Symbol(),OP_BUY,1.5,Ask,0,Bid-150*Point,Ask+150*Point,NULL,Mn,0,CLR_NONE);//
OrderSend(Symbol(),OP_BUY,2.5,Ask,0,Bid-150*Point,Ask+150*Point,NULL,Mn,0,CLR_NONE);//
OrderSend(Symbol(),OP_BUY,3.5,Ask,0,Bid-150*Point,Ask+150*Point,NULL,Mn,0,CLR_NONE);//
OrderSend(Symbol(),OP_BUYLIMIT,1,Ask-150*Point,0,Bid-400*Point,
Ask+250*Point,NULL,Mn,0,CLR_NONE);//
OrderSend(Symbol(),OP_SELLSTOP,3,Ask-300*Point,0,Bid+150*Point,
Ask-750*Point,NULL,Mn,0,CLR_NONE);//
//----
PriTP=Bid;//PriTP
Print("Kc0:",OrdersTotal=",OrdersTotal());//
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function|
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function|
//+------------------------------------------------------------------+
int start()
{
//----Tick counter
Tick++;//Tick
//----Close BUY orders
for(i=0;i<OrdersTotal();i++)//initialise the loop
{
Print("Kc1:"," Tick=",Tick," i=",i);//Message
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)//Select order i
{
if(OrderSymbol()==Symbol())//Symbol
{
if(OrderMagicNumber()==Mn)//Identification number
{
if(OrderType()==OP_BUY && OrderTakeProfit()>PriTP)//The transaction type of the currently selected order
{
OrderClose(OrderTicket(),OrderLots(),Bid,0,CLR_NONE);//Closing the position
}/Closed if(OrderType()==OP_BUY && OrderTakeProfit()>PriTP)
}// Closed if(OrderMagicNumber()==Mn)
}// Closed if(OrderSymbol()==Symbol()
}// Closed if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
}// Closed for(i=0;i<OrdersTotal();i++)
//----
return(0);
}

Entries from the tester's logbook.

2013.03.01 12:12:57 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=5 i=1
2013.03.01 12:12:57 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=5 i=0
2013.03.01 12:12:33 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=4 i=1
2013.03.01 12:12:33 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=4 i=0
2013.03.01 12:12:03 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=3 i=1
2013.03.01 12:12:03 2012.09.03 00:00 Poligon1 EURUSD,M15: close #4 buy 2.50 EURUSD at 1.25773 sl: 1.25616 tp: 1.25923 at 1.25770
2013.03.01 12:12:09 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=3 i=0
2013.03.01 12:11:45 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=2 i=2
2013.03.01 12:11:45 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=2 i=1
2013.03.01 12:11:45 2012.09.03 00:00 Poligon1 EURUSD,M15: close #2 buy 1.00 EURUSD at 1.25773 sl: 1.25516 tp: 1.26023 at price 1.25768
2013.03.01 12:11:45 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=2 i=0
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=1 i=3
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: close #5 buy 3.50 EURUSD at 1.25773 sl: 1.25616 tp: 1.25923 at 1.25766
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=1 i=2
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: close #3 buy 1.50 EURUSD at 1.25773 sl: 1.25616 tp: 1.25923 at 1.25766
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=1 i=1
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: close #1 buy 0.50 EURUSD at 1.25773 sl: 1.25416 tp: 1.26123 at 1.25766
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=1 i=0
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc0: OrdersTotal=7
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: open #7 sell stop 3.00 EURUSD at 1.25473 sl: 1.25916 tp: 1.25023 ok
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: open #6 buy limit 1.00 EURUSD at 1.25623 sl: 1.25366 tp: 1.26023 ok
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: open #5 buy 3.50 EURUSD at 1.25773 sl: 1.25616 tp: 1.25923 ok
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: open #4 buy 2.50 EURUSD at 1.25773 sl: 1.25616 tp: 1.25923 ok
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: open #3 buy 1.50 EURUSD at 1.25773 sl: 1.25616 tp: 1.25923 ok
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: open #2 buy 1.00 EURUSD at 1.25773 sl: 1.25516 tp: 1.26023 ok
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: open #1 buy 0.50 EURUSD at 1.25773 sl: 1.25416 tp: 1.26123 ok
2013.03.01 12:11:21 PM Poligon1 test started
2013.03.01 12:11:21 18 18:30 Poligon1 EURUSD, M15: loaded successfully

Regards. Shurkin

 
Shurkin:

When testing the Expert Advisor, we have detected some errors in the code:
- execution of the loop operator for(i=0;i<OrdersTotal();i++) is interrupted by the next tick (shown in Bold below),
- buy orders are selected and closed in random order.

Regards. Shurkin


1. Orders cannot be opened in the init() function (in the Strategy Tester, but it won't work on the demo)

2. When you close orders, count from the end to the beginning, and not vice versa, as you have.

 
Shurkin:

When testing Expert Advisor, code execution errors were detected:
- execution of loop statement for(i=0;i<OrdersTotal();i++) is interrupted by next tick (highlighted in Bold below),
- buy orders are selected and closed in random order.

Can you please tell me how to find the cause and fix it?
I am providing you the code of the program and report from the tester's journal.

The code of the program.

//+------------------------------------------------------------------+
//|Poligon1.mq4 |
//| Copyright 2012, MetaQuotes Software Corp. |
//| Loop operator test.|
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link ""
//+------------------------------------------------------------------+
//|Declared variables|
//+------------------------------------------------------------------+
int Mn=357;//Magic number
int i;//Iteration counter
double PriTP;//TP series
int Tick;//Tick counter
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----Set orders
OrderSend(Symbol(),OP_BUY,0.5,Ask,0,Bid-350*Point,Ask+350*Point,NULL,Mn,0,CLR_NONE);//
OrderSend(Symbol(),OP_BUY,1,Ask,0,Bid-250*Point,Ask+250*Point,NULL,Mn,0,CLR_NONE);//
OrderSend(Symbol(),OP_BUY,1.5,Ask,0,Bid-150*Point,Ask+150*Point,NULL,Mn,0,CLR_NONE);//
OrderSend(Symbol(),OP_BUY,2.5,Ask,0,Bid-150*Point,Ask+150*Point,NULL,Mn,0,CLR_NONE);//
OrderSend(Symbol(),OP_BUY,3.5,Ask,0,Bid-150*Point,Ask+150*Point,NULL,Mn,0,CLR_NONE);//
OrderSend(Symbol(),OP_BUYLIMIT,1,Ask-150*Point,0,Bid-400*Point,
Ask+250*Point,NULL,Mn,0,CLR_NONE);//
OrderSend(Symbol(),OP_SELLSTOP,3,Ask-300*Point,0,Bid+150*Point,
Ask-750*Point,NULL,Mn,0,CLR_NONE);//
//----
PriTP=Bid;//PriTP
Print("Kc0:"," OrdersTotal=",OrdersTotal());//PriTP
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function|
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----Counter Tick
Tick++;//Tick
//----Close BUY Orders
for(i=0;i<OrdersTotal();i++)//initialise loop
{
Print("Kc1:"," Tick=",Tick," i=",i);//Send message
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)//Select order i
{
if(OrderSymbol()==Symbol())//Symbol
{
if(OrderMagicNumber()==Mn)//Identification number
{
if(OrderType()==OP_BUY && OrderTakeProfit()>PriTP)//The operation type of the currently selected order
{
OrderClose(OrderTicket(),OrderLots(),Bid,0,CLR_NONE);//Closing the position
}/Closed if(OrderType()==OP_BUY && OrderTakeProfit()>PriTP)
}/Closed if(OrderMagicNumber()==Mn)
}// Closed if(OrderSymbol()==Symbol()
}// Closed if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
}// Closed for(i=0;i<OrdersTotal();i++)
//----
return(0);
}

Tester log entries.

2013.03.01 12:12:57 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=5 i=1
2013.03.01 12:12:57 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=5 i=0
2013.03.01 12:12:33 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=4 i=1
2013.03.01 12:12:33 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=4 i=0
2013.03.01 12:12:09 2012.03 03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=3 i=1
2013.03.01 12:12:09 2012.09.03 00:00 Poligon1 EURUSD,M15: close #4 buy 2.50 EURUSD at 1.25773 sl: 1.25616 tp: 1.25923 at price 1.25770
2013.03.01 12:12:09 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=3 i=0
2013.03.01 12:11:45 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=2 i=2
2013.03.01 12:11:45 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=2 i=1
2013.03.01 12:11:45 2012.09.03 00:00 Poligon1 EURUSD,M15: close #2 buy 1.00 EURUSD at 1.25773 sl: 1.25516 tp: 1.26023 at price 1.25768
2013.03.01 12:11:45 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=2 i=0
2013.03.01 12:11:21 2012.09.03 03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=1 i=3
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: close #5 buy 3.50 EURUSD at 1.25773 sl: 1.25616 tp: 1.25923 at price 1.25766
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=1 i=2
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: close #3 buy 1.50 EURUSD at 1.25773 sl: 1.25616 tp: 1.25923 at price 1.25766
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=1 i=1
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: close #1 buy 0.50 EURUSD at 1.25773 sl: 1.25416 tp: 1.26123 at 1.25766
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc1: Tick=1 i=0
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: Kc0: OrdersTotal=7
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: open #7 sell stop 3.00 EURUSD at 1.25473 sl: 1.25916 tp: 1.25023 ok
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: open #6 buy limit 1.00 EURUSD at 1.25623 sl: 1.25366 tp: 1.26023 ok
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: open #5 buy 3.50 EURUSD at 1.25773 sl: 1.25616 tp: 1.25923 ok
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: open #4 buy 2.50 EURUSD at 1.25773 sl: 1.25616 tp: 1.25923 ok
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: open #3 buy 1.50 EURUSD at 1.25773 sl: 1.25616 tp: 1.25923 ok
2013.03.01 12:11:21 2012.09.03 00:00 Poligon1 EURUSD,M15: open #2 buy 1.00 EURUSD at 1.25773 sl: 1.25516 tp: 1.26023 ok
2013.03.01 12:11:21 2012.09.03 00:00:00 Poligon1 EURUSD,M15: open #1 buy 0.50 EURUSD at 1.25773 sl: 1.25416 tp: 1.26123 ok
2013.03.01 12:11:21 Poligon1 test started
2013.03.01 12:11:21 Poligon1 EURUSD,M15: loaded successfully

Regards. Shurkin

I haven't watched your whole mess (you have to use SRC button above the input field to attach it) - it's not pleasant to scratch in code smeared all over the page, but the first thing that caught my eye is a line for(i=0; i<OrdersTotal() ; i++) . To close it, use the reverse loop: for(i=OrdersTotal()-1; i>=0; i -- )
 
Roger:
int x =MathRand() % 2;


Why would it return 0 or 1 instead of another number? According to the help,MathRand() returns a pseudorandom integer between 0 and 32767
 
hoz:

And why would it return 0 or 1 instead of another number? After all, according toMathRand() returns a pseudorandom integer in the range 0 to 32767.

This is the remainder of the division! Look in the arithmetic steps and in your printer!

Put it in and you will only see two results alternating:

  int x =MathRand() % 2;
  Print("x =",x);
 
Greetings all! I want to know how to stop the Expert Advisor, when the conditions for entering the market are fulfilled, it starts to write in the log file "Order triggered!" and does it several dozens of times per minute, as a result the log file becomes cosmically huge. How can I make it write only once, and not before the position is closed, because it may hang for a few hours...
 
Allis:
Greetings all! I want to know how to stop the Expert Advisor, when the conditions for entering the market are fulfilled, it starts to write in the log file "Order triggered!" and does it several dozens of times per minute, as a result the log file becomes cosmically huge. How do I make it write only once, and not before closing the position, because it may hang for a few hours...
Hello, Allis! Print should be prescribed, where there is no reading by ticks! I put in the print, if there is an error, and if it opened, and so will be written in the magazine that opened, and only once!
Reason: