What am I doing wrong? - page 2

 
FXtrader2008:

Actually, there is perhaps another way to launch trades that just occurred to me.


You could run your EA in a continous "while" loop and use the internal clock on your computer to launch the trades, instead of waiting for an incoming tick to launch the Start() function.


could you explain that method in more detail? I'm still new to mq4 programming.


also, i wrote a new script in light of the "ticking" problem...


#include <stdlib.mqh>
#include <WinUser32.mqh>




extern double Lots = 1.0;
extern double Hours = 15;
extern double Minutes = 30;



int start()
{


//begin--------------------------------------------------------------+
if(OrdersTotal() == 0)
{
if(Hour() == Hours)
{
if(Minute() == Minutes)

{
OrderSend(Symbol(),OP_BUY,Lots,Ask,10,Ask-15*Point,Ask+30*Point,"BUYER",0,0,Green);
}
}
}
//end----------------------------------------------------------------+

//begin--------------------------------------------------------------+
if(OrdersTotal() == 1)
{
if(Hour() == Hours)
{
if(Minute() == Minutes)

{
OrderSend(Symbol(),OP_SELL,Lots,Bid,10,Bid+15*Point,Bid-30*Point,"Seller",0,0,Red);
}
}
}
//end----------------------------------------------------------------+


return(0);
}
//+------------------------------------------------------------------+


it's set to run at 15:30, but when i test it in the simulator on gbp/jpy, it places more sell trades than buy trades... i only want it to place 2 trades, one sell, and one buy... what am i doing wrong?

 

deymer,


When I run your code in the backtester on GBPJPY I get a mass of "130" and "4107" errors.


Take a look at the same code here https://book.mql4.com/trading/ordersend.


You need to add error correction code to your EA. Brokers typically increase the spread during news events. I think that is why I am getting all those errors.



With respect to the "while" loop suggestion, let me work on that a bit and see what I come up with.

 
FXtrader2008:

deymer,


When I run your code in the backtester on GBPJPY I get a mass of "130" and "4107" errors.


Take a look at the same code here https://book.mql4.com/trading/ordersend.


You need to add error correction code to your EA. Brokers typically increase the spread during news events. I think that is why I am getting all those errors.



With respect to the "while" loop suggestion, let me work on that a bit and see what I come up with.



thank you, i really apprecaite all your help

 
deymer:

thank you, i really apprecaite all your help

also... the following ea you sent me still sends more than one trade within the designated minute, it's as if it ignores the OrdersTotal parameter completely...


int start()

{


if(OrdersTotal() == 0)

{

if(Hour() == 15)

{

if(Minute() == 30)

{

OrderSend(Symbol(),OP_BUY,1.0,Ask,1,Ask-15*Point,Ask+30*Point,"BUYER",0,0,Green);

}

}

}

return(0);

}


 
deymer:

also... the following ea you sent me still sends more than one trade within the designated minute, it's as if it ignores the OrdersTotal parameter completely...


int start()

{


if(OrdersTotal() == 0)

{

if(Hour() == 15)

{

if(Minute() == 30)

{

OrderSend(Symbol(),OP_BUY,1.0,Ask,1,Ask-15*Point,Ask+30*Point,"BUYER",0,0,Green);

}

}

}

return(0);

}


Change the conditional-if statement to read: if(OrdersTotal()<40)


Then it should continue opening orders until it reaches 40 open orders then will stop, however the orders will not be evenly spaced ---- they will be spaced by the rate of the incoming ticks.


I am continuing to workout a method to open orders 2 seconds apart. I have discovered that you cannot make it do the timing thing in the backtester. Therefore I am waiting for living trading to start so I can test the code in live trading ----- therefore later today I should have something for you.

 
FXtrader2008:

Change the conditional-if statement to read: if(OrdersTotal()<40)


Then it should continue opening orders until it reaches 40 open orders then will stop, however the orders will not be evenly spaced ---- they will be spaced by the rate of the incoming ticks.


I am continuing to workout a method to open orders 2 seconds apart. I have discovered that you cannot make it do the timing thing in the backtester. Therefore I am waiting for living trading to start so I can test the code in live trading ----- therefore later today I should have something for you.











thank you, i really do appreciate your help. i'll keep fiddling around with it until i get something to work.

 

deymer,


Here's some test code that I wrote for opening trades at 2 second intervals. The good news is: I believe it does open trades independently of incoming ticks. I say that because the incoming ticks are very slow right now and the trades appear to be opening faster than the incoming ticks. The bad news is: I placed a 2 second sleep function in the code to ensure the 2 second spacing, however, the interaction time with the trade server is taking 2 to 6 seconds even in a slow market. Thus the total time between the trades was 4 to 8 seconds.




The following code is test code. You will find as time goes by you will write hundreds of little scripts to learn how the MT4 and trade server interact.


If this test code opens trades in a manner that appeals to you simply integrate the appropriate code lines in your EA.




int start()

{


//============================================================
// Printout Open Time for Open Orders
// This is tempoary code to learn how much time
// elapses between trade launches
//============================================================

NewMin=Minute();

if(NewMin!=OldMin)
{ //---------begin NewMin!=OldMin
OldMin=NewMin;
Tot=OrdersTotal()-1;

for(i=Tot;i>=0;i--)
{ //---------begin for loop
OrderSelect(i,SELECT_BY_POS);
OpenTime=TimeToStr(OrderOpenTime(),TIME_SECONDS);
Print("Ticket# ",OrderTicket()," opened @ ",OpenTime);
} //---------end for loop
} //---------end NewMin!=OldMin


//=========================================================
// Stop opening orders if 5 orders are already open
//=========================================================

if(OrdersTotal()>=5)
{
return(0);
}


//=======================================================
// Open a single OrderClose
//=======================================================

OrderSend(Symbol(),OP_BUY,Lots,Ask,10,Ask-15*Point,Ask+30*Point,"BUYER",0,0,Green);
LastOrderTime=GetTickCount();


//=======================================================
// Open additional orders at 2 second intervals
//=======================================================

while(OrdersTotal()<5)
{
Sleep(2000);
CurrentTime=GetTickCount();

if(CurrentTime-LastOrderTime>2)
{
OrderSend(Symbol(),OP_BUY,Lots,Ask,10,Ask-15*Point,Ask+30*Point,"BUYER",0,0,Green);
LastOrderTime=CurrentTime;
}
}

return(0)
}
 

i'll try to implement that in my ea, but in the mean time, i just have one last simple question...


i have this simple code to place 2 trades at the beginning of the minute of 15:30... but the problem is, it places the 2 trades, then when the trades close, it brings the OrderTotal back to 0, and it reopens 2 more trades, and it does this until the minute has passed... is there a way to have the ea be disabled after it places the 2 trades?



#include <stdlib.mqh>

#include <WinUser32.mqh>


int start()

{if(OrdersTotal() == 0)

{if(Hour() == 15)

{if(Minute() == 30)

{OrderSend(Symbol(),OP_BUY,1.0,Ask,1,Ask-15*Point,Ask+30*Point,"BUYER",0,0,Green);

OrderSend(Symbol(),OP_SELL,1.0,Bid,1,Bid+15*Point,Bid-30*Point,"BUYER",0,0,Red);

}

}

}

return(0);

}


 

if you want to do something ONE time and not repeat that process until something else occurs, then you need to add a switch variable. The variable acts as a switch. When the switch is activated it will allow certain things to occur, when the switch is turned off the regular process is allowed to go forward. The switch is used in conjunction with a conditional-if statement. In the following code I added a switch entitled "TradeSwitch"; it toggles between "0" and "1". The switch is activated inside the conditional-if statement, and it is deactivated elsewhere; in this case, when the time reaches the 20th hour.


int TradeSwitch=0;


int start()


if(TradeSwitch==0)

{

{if(OrdersTotal() == 0)

{if(Hour() == 15)

{if(Minute() == 30)

{OrderSend(Symbol(),OP_BUY,1.0,Ask,1,Ask-15*Point,Ask+30*Point,"BUYER",0,0,Green);

OrderSend(Symbol(),OP_SELL,1.0,Bid,1,Bid+15*Point,Bid-30*Point,"BUYER",0,0,Red);

TradeSwitch=1;

}

}

}

}

if(Hour()>20)

{

TradeSwitch=0;

}



return(0);

}

Reason: