Error 130 - page 3

 
tapo:

I mean it's fine by itself, but we still need to use it in the for loop, do we not?

The stuff in the loop is to do with the trailing SL and Closing orders . . . this happens when the Cross changes . . . so why do you need to forget what the Cross is set to when doing any of that stuff ? So when do you need to forget what the Cross is set to ?
 
RaptorUK:
The stuff in the loop is to do with the trailing SL and Closing orders . . . this happens when the Cross changes . . . so why do you need to forget what the Cross is set to when doing any of that stuff ? So when do you need to forget what the Cross is set to ?
I'm not sure if you've got what I meant. What I am saying is that we need the values 1 & 2 of the variable 'direction' for the stuff in the loop. When the Cross changes while we have a short position (for example), we want to open a long position and close this short position. Right? So how can we close the short position after we make direction = 0?
 
tapo:
I'm not sure if you've got what I meant. What I am saying is that we need the values 1 & 2 of the variable 'direction' for the stuff in the loop. When the Cross changes while we have a short position (for example), we want to open a long position and close this short position. Right? So how can we close the short position after we make direction = 0?

That's easy to answer . . . you need to go back to my mods and really understand what is going on.

Only one order can be open at any one time . . . the long position cannot be opened until the short position is closed . . . so direction will not be changed to 0 until the short position is closed and the long position can then be opened, then direction can be set to 0

 
RaptorUK:

That's easy to answer . . . you need to go back to my mods and really understand what is going on.

Only one order can be open at any one time . . . the long position cannot be opened until the short position is closed . . . so direction will not be changed to 0 until the short position is closed and the long position can then be opened, then direction can be set to 0


hmmm, it sounds like a riddle. Good that you enjoy you weekend time by telling riddles :)

I am not sure how one should understand your code. direction is made 0 before the control reaches the for loop. I don't know. I've gone through your code many times and now again, and not figured it out. Is it fine with you to just say it straight away? Is it because isNewBar? If it is so, then we have another issue that needs to be fixed, because we don't want positions to close for non-confirmed signals.

 
tapo:

hmmm, it sounds like a riddle. Good that you enjoy you weekend time by telling riddles :)

LOL . . . if only you knew how funny that was . . . . I hate riddles with a passion you can only imagine. What you are seeing as a riddle is actually the explanation . . .

Let me try once more, this is in relation to the code I modified.

The first thing to keep in mind is that only one order can be open at once . . .

**The 2nd thing to bear in mind is that Crossed only returns 1 or 2 when it is called for the first time after the cross has changed. On subsequent calls it returns 0 until the cross changes the next time . . .

1. The first time Crossed is called it will return 1 or 2 depending on the values of shortEma & longEma, the value returned is stored in direction (global)

2. No order is opened so a long or short order will be placed (dependant on the value stored in direction), if the OrderSend works direction is set to 0

3. Trailing stops can happen, they do not depend on direction

4. On the next tick because direction is 0 Crossed is called, if the cross hasn't changed it returns 0 (this was the 2nd thing to keep in mind . . .**), an order is already open so no new order can be opened, the cross hasn't changed so no order will be closed.

5. On the next tick because direction is 0 Crossed is called, if the cross has changed 1 or 2 will be returned, an order is already open so no new order can be opened, but now an order can be closed based on the value of direction . . .

6. On the next tick direction is 1 or 2 so Crossed is NOT called, the last order was closed in step 5 so there is no order open, so now a new order is opened based on the value stored in direction, when the order is opened direction is set to 0

7. return to step 3 and continue . . . .

 
RaptorUK:

The first thing to keep in mind is that only one order can be open at once . . .

Wow, was that it? Is it because of this condition

if (total < 1 ....

or it's something in MQL4?

If it it because of the above condition, what's the exact purpose of this condition?

To be clearer, I want to achieve only 1 single thing with regards to opening a trade when another trade is already in progress. I want to prevent opening a trade in the same direction of an existing trade, and allow opening a trade in the opposite direction. Why am I looking for that? Because, I am planning to set a trend filter .. when the trend is up, all short signals should be ignored, whereas long signals will be taken only if there is no long position open when they come. The opposite is true when the trend is down.

So does MT4 take every signal which comes to it? If yes, then we have to use the above line to prevent that.

Can you tell me about that?

Thank you,

tapo

PS at the moment I don't care about SL, TS and TP, because they work differently. The have to be checked at each and every tick. This is different to opening a trade or closing it based on a signal which needs to be checked only when a bar is closed. Hence I may remove SL, TS and TP if they cause some confusion. I am still finding that the for loop is executed at every tick which is suitable for trailing stops, but not for trade closing based on signals.

 

Yes, of course it is because of this . . .

if (total < 1 ....

It shouldn't be the case that I understand your code better than you . . . should it ?

You need to go through your code line by line, understand what it does under all possible variable conditions . . . if you can't do that . . well, you need to forget coding.

If you want to allow the possibility of having 2 orders open, one Buy and one Sell then you can check as follows . .

total = OrdersTotal();
if ( ( total < 1 || (OrderSelect(0, SELECT_BY_POS, MODE_TRADES) && OrderType() = OP_BUY) ) && isNewBar() )
   {
       // do stuff to place a new Sell order

I'm not sure what you mean by "So does MT4 take every signal which comes to it?" . . . can you explain ?

 
RaptorUK:

Yes, of course it is because of this . . .

It shouldn't be the case that I understand your code better than you . . . should it ?

You need to go through your code line by line, understand what it does under all possible variable conditions . . . if you can't do that . . well, you need to forget coding.

If you want to allow the possibility of having 2 orders open, one Buy and one Sell then you can check as follows . .

I'm not sure what you mean by "So does MT4 take every signal which comes to it?" . . . can you explain ?

This is not my code. It's from the tutorials in forex-tsd, although I modified it a little bit.

Before I explain my question that you mentioned. Let me make clear what I am looking for altogether. I am/were looking for

1. The way to make the EA wait for a close of a candle to act on any trigger/signal. I got this partially, and am delaying it for a later time.

2. Taking short entries. I've got this one now.

3. Opening positions solely based on crossovers, not just on any situation where one MA is above or below the other. I've got this, but still there is something to it, and am delaying it for a later time.

4. I want the EA to open positions in the following manner:

If there is no position already opened, then just open a position in any direction (based on the trigger of course). And if there is a position already opened, then the EA cannot open a position in the same direction, but can do in the opposite direction. Does this mean that I want to have both long and short positions floating together? No, because I can tell the program to open a long and close the already-opened short and vice versa.

And this # 4 is what the recent posts are about, and what I am trying to understand at the moment. Base on that, to answer you question about my question, I am asking if an EA triggers long, long, short, long, short, short, short, and the was nothing in the code about SL, TP or any form of closing, how many position we'll have floating in total? If 7, then MT4 does not restrict something here. In this case, I want to code my own restrictions. I explained the restrictions in the previous paragraph.

I hope this is very clear now.

Cheers,

tapo

Reason: