Error 130 - page 2

 

I've made some changes and have commented them so you can see what I have done . . . it's a bit of a hack but it seems to work.

Results aren't too good . . .

Files:
 

Hello Raptor and thank you very much for the changes. I have been reading over and examining both codes over and over again, and not been able to find out where in the original code it says allow 1 order to be open at a time. And when LastDirection == 0 or Crossed() == 0, I see no block of code to be executed. I see only when Crossed() == 1, it buys and closes sell, and when Crossed() == 2 it sells and closes buy. Could you explain the logic a little bit? I could just copy your code, but I am really curious, and this EA code (which seemed to me to be an easy educational exercise for a beginner) has turn to be an advance programming puzzle!

Anyway, I am sure I'll get it eventually. Could you explain this logic more?

Regards,

tapo

 
tapo:

Hello Raptor and thank you very much for the changes. I have been reading over and examining both codes over and over again, and not been able to find out where in the original code it says allow 1 order to be open at a time.

Here:

   total = OrdersTotal();
   if (total < 1 && isNewBar())
   {

. . so if OrdersTotal is less than 1 . . in other words no open orders, then allow an order to be placed . .

 
RaptorUK:

Here:

. . so if OrdersTotal is less than 1 . . in other words no open orders, then allow an order to be placed . .

very quick reply :) thank you

I commented out "total < 1", removed the EA and put it back on the EU 5-min chart. At this very same moment the short MA is below the long MA, but the EA did not trigger sell. If it was the other way round, it would have triggered buy. I don't know why! Now I'm gonna leave it for some time. let it generate some trades and see.

Edit: I have changed the TF to 1-min because the market is about to close.

Files:
 
tapo:

If it was the other way round, it would have triggered buy. I don't know why! Now I'm gonna leave it for some time. let it generate some trades and see.

You need to read the rest of my comments . . . Crossed is only 1 or 2 until it is called the next time . . . you call it 4 times potentially . . .

if (CurrentDirection != LastDirection)    // <-----  if the direction is the same as at the last call . . . . 
   {
      LastDirection = CurrentDirection;
      return (LastDirection);
   }
   else return(0);                        // <---  . . . . do this,  return 0
 
RaptorUK:

You need to read the rest of my comments . . . Crossed is only 1 or 2 until it is called the next time . . . you call it 4 times potentially . . .

I have read and perhaps I have not yet got everything. LastDirection is returned only when it is different to CurrentDirection at the time Crossed() is executed, so it is made the same and then returned.

Whereas when it is the same at the time Crossed() is executed, 0 is returned. I know that.

What I don't understand is when Crossed() returns 0 why it makes different. Because there is no block which will get executed when Crossed() == 0. So it should not have any effect, should it?

Beside that, Here are the results for live test. Still the EA does not want to open short positions somehow.

 
tapo:

What I don't understand is when Crossed() returns 0 why it makes different. Because there is no block which will get executed when Crossed() == 0. So it should not have any effect, should it?

Correct . . but for a short order to be placed . . .

if(Crossed(shortEma, longEma) == 2)

. . Crossed has to be 2 not 0

Imagine shortEma < longEma so Crossed would be 2, this line of code will happen before the line of code above . . .

if (Crossed(shortEma, longEma) == 1)

. . . and the result is false . . . but the call makes Crossed return 2, so when the check is made to place the short order Crossed is called again, shortEma is still < longEma so Crossed will return 0

In summary, when shortEma < longEma the first time Crossed is called it will return 2, the next and subsequent times it is called it will return 0

 

In summary, when shortEma < longEma the first time Crossed is called it will return 2, the next and subsequent times it is called it will return 0

That's right.

In my code there are 4 calls for Crossed() like this

if (Crossed(shortEma, longEma) == 1) ...;
if (Crossed(shortEma, longEma) == 2) ...;    //without an 'else', Crossed will be called twice.

for (...)
{
  if (...)
  {
    if (Crossed(shortEma, longEma) == 1) ...;
  }
  else          //This 'else' protects Crossed() from being called unnecessarily within the scope of the 'for' loop
  {
    if (Crossed(shortEma, longEma) == 2) ...;
  }
}

Do you think using using 'else if' in the second call and putting the position closing commands under the position opening commands will solve the problem?

 
tapo:

That's right.

In my code there are 4 calls for Crossed() like this

Do you think using using 'else if' in the second call and putting the position closing commands under the position opening commands will solve the problem?

I think you should look at the modifications I made and understand what I did and why . . then you will understand.
 
RaptorUK:
I think you should look at the modifications I made and understand what I did and why . . then you will understand.

Sure. That was my next step, and I've looked at it by now.

What I think is that you did great in preventing calling Crossed more than once in each iteration of the the start() function. What I did not understand though is assigning 0 to direction right after an order is opened. I mean it's fine by itself, but we still need to use it in the for loop, do we not?

Reason: