Code sometimes not triggering?

 

Hi,

First of all, here is the graph

https://www.mql5.com/en/charts

And here the code:

bool hassold = false;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   
   return(0);
  }
//+------------------------------------------------------------------+
//| Stochastic oscillator                                            |
//+------------------------------------------------------------------+
int start()
{

double kingnow;
double kingprevious;
double dognow;
double dogprevious;

kingnow = iStochastic(NULL,0,14,5,3,0,0,MODE_MAIN,1);
kingprevious = iStochastic(NULL,0,14,5,3,0,0,MODE_MAIN,2);
dognow = iStochastic(NULL,0,14,5,3,0,0,MODE_SIGNAL,1);
dogprevious = iStochastic(NULL,0,14,5,3,0,0,MODE_SIGNAL,2);

if (kingnow<20 && kingnow>kingprevious)
{// {OrderSend(Symbol(),OP_BUY,0.1,Ask,3,0.99*Ask,1.02*Bid,"AutoBuy",0,0,Green);
 OrderSelect(0,SELECT_BY_POS);
 if (OrderOpenTime() != 0)
 OrderClose(OrderTicket(),0.1,Ask,3,Yellow);
 hassold = false;
}

if (kingnow>dognow && kingprevious<dogprevious && hassold == true)
{
 OrderSelect(0,SELECT_BY_POS);
 if (OrderOpenTime() != 0)  
 OrderClose(OrderTicket(),0.1,Ask,3,Yellow);
 hassold = false;
}

//OrderSelect(0,SELECT_BY_POS);
//if (OrderOpenTime() == 0)
//hassold = false;

if (kingnow>80 && kingnow<kingprevious && hassold == false)
{
OrderSend(Symbol(),OP_SELL,0.1,Bid,3,NULL,NULL,"AutoSell",0,0,Red);
hassold = true;
 }

   return(0);

}

Following my coding, on the 5th of October, a sell should have been triggered. (as kingnow is > 80 and is < to kingprevious)

However, there is no sale. Why? Thanks a lot for your help!

 
Arminius :

Hi,

First of all, here is the graph

https://www.mql5.com/en/charts

And here the code:

Following my coding, on the 5th of October, a sell should have been triggered. (as kingnow is > 80 and is < to kingprevious)

However, there is no sale. Why? Thanks a lot for your help!

Was the OrderSend() called but failed ? or was the OrderSend() not called ? what were the actual values of kingnow and kingprevious ? did you Print() them ?

Why aren't you doing this ? What are Function return values ? How do I use them ?

 
Arminius :

Hi,

First of all, here is the graph

https://www.mql5.com/en/charts

And here the code:

Following my coding, on the 5th of October, a sell should have been triggered. (as kingnow is > 80 and is < to kingprevious)

However, there is no sale. Why? Thanks a lot for your help!


Your OrderClose function is garbage

for closing and selecting by position you have to know

if the position you close is from your EA and if the orderType() is the Type you wanna close

now you close trades without knowing what the trade is you do the OrderClose command

Select all your trades one by one with using an orderloop counting down

check every position if it is one you wanna select

then if so you do your orderclose

See this Loops and Closing or Deleting Orders

 

I don't get your point with orderType? All the trades from the EA are shorts, I just use the EA and closing orders seems to be working fine. (anyways it's a test)

The problem here is not that it's not closing but rather that it's not selling.

@RaptorUK

I looks like there was just nothing called. Those are the orders before and after.

2013.07.16 11:10:28 2012.11.20 00:00 Third test GOLD,Daily: close #29 sell 0.10 GOLD at 1726.03 at price 1730.13
2013.07.16 11:10:26 2012.11.13 00:00 Third test GOLD,Daily: open #29 sell 0.10 GOLD at 1726.03 ok
2013.07.16 11:10:12 2012.09.30 22:01 Third test GOLD,Daily: close #28 sell 0.10 GOLD at 1760.08 at price 1771.05
2013.07.16 11:10:05 2012.09.18 00:00 Third test GOLD,Daily: open #28 sell 0.10 GOLD at 1760.08 ok


... I got it. It was 79.9492 -> no trigger.

For the print function I had tried that in previous code but it never displayed the text on the graph so I just abandoned it.

I just read what you wrote on the other topic... this time from beginning to end. Last time I had stopped reading prematurely. I had thought it was just talking about what a function is returning. My bad. Thanks for the help!

 
Arminius :

I don't get your point with orderType? All the trades from the EA are shorts, I just use the EA and closing orders seems to be working fine. (anyways it's a test)

The problem here is not that it's not closing but rather that it's not selling.

you haven't done following Loops and Closing or Deleting Orders so you can't know if this is placed on an account

if it selects the right trade So your test might be looking good to you but it keeps garbage the code you are using here

Your Slippage is 3 that is 3 Points on a 5 digit account it will result often in requote price changed

then your trade will not be placed ......

but you have hassold made true

Also NULL is a string !!! .... 0 is what you have to use here for your TP or SL level

Reason: