Loopy loop, thing runs away, little help please! - page 2

 

PumPuiMonkey:

All that's out there is the loop, so it should just close one next buy order at a time if it doesn't get a busy signal.

That's not correct. This code selects the order that is in position 0 and closes it if not busy and it's a BUY order. If the order in position 0 is anything other, it will quit. It won't go on to find the next buy. You will need to loop to do that.

All the loop is doing is running through the array indexes.

for( i=OrdersTotal()-1; i>=0; i--)
   {
    // Actions
   }

Basically means find how many orders there are in total and starting from the highest index do the actions in the bracket block. When finished the actions, move down the list of orders.

Becasue arrays start with zero, we need total-1. so if you have 5 orders, the array positions will be 0,1,2,3,4. The for says start at 4 and count backwards until you reach 0. If you have 1 order, i=1-1 =0 so start at position zero do action and then stop.

The first action you will need is to select the order from the array position

if (OrderSelect(i,SELECT_BY_POS)==true)
   {
   // Order Actions
   }

With the order selected, you can now work the Order Actions... First, test whether it's the correct order by symbol, magic, type etc... if it's not the order you are looking for, then continue to the next position in the array.

if(OrderSymbol()!=Symbol()||OrderType()>0 || OrderMagicNumber()!=magic)
            {
            continue;                                        // The order is not "ours"
            }

So it you get past this you know you have the correct order. Now do whatever you want to this order.... you can test your a>b or just close the order.

Close the bracket to Order Actions and then to Actions, the loop block has come to the end, it will go back and reduce i by 1 and run this test again until i is reduced to -1 where it will finish and carry on with the rest of the code.

I think it worthwhile trying to understand the concepts before deciding the loop is not required... most times it is needed. I hope I have explained it easily enough

V

And just wanted to add, Are you sure about not closing an order if the trade context is busy at that particular millisecond. Wouldn't it be better to wait for it to be free and then close?

 

Viffer,


I'm going to kiss you mate!! So the problem was it didn't exit the loop at the right time. All I had to do was tell it when to exit the loop, which is after it closed the first correct trade.


And that bit is here here: was the trade closed no, go back and try again, if it was closed set trade not busy and leave loop....thanks mate, and everyone else that gave input!! If you blokes were around here beer be on on me!!

if (Result < 1)
             {
              Print("Error ", GetLastError(), " While Closing Ticket ", MyTicket,
                    " Lots = ", OrderLots(), " Price = ", OrderClosePrice(), " Ask = ", Ask);
              TradeNotBusy();
              return(0); 
             }
            else
             {
             TradeNotBusy();
             return(0);
             }


 

viffer

Just clarify that trade context statement please. I thought we did exactly that. We have an include file that blocks us from trading if the trade context is busy, that's what the "if trade is busy thing is for". The trade sits in the loop till it's open to go...


If you can save me a millisecond I'd be extremely happy, I'm not getting much more than a trade a second, which is really frustrating. So frustrating were switching out to C++ FIX soon.

 
PumPuiMonkey:

viffer

Just clarify that trade context statement please. I thought we did exactly that. We have an include file that blocks us from trading if the trade context is busy, that's what the "if trade is busy thing is for". The trade sits in the loop till it's open to go...


If you can save me a millisecond I'd be extremely happy, I'm not getting much more than a trade a second, which is really frustrating. So frustrating were switching out to C++ FIX soon.

I'm maybe reading too much into your TradeBusy() function and interpreting it as IsTradeContextBusy()

The latter tests whether the terminal is busy sending something from another EA and is used to prevent clashes in instructions. The terminal will not que instructions but just fail with code 146. Of course a millisecond later, the terminal could be free again, so the normal method of dealing with this error is test for it and sleep until the terminal is free to process the order. As soon as it's free, send the order. At the end of the day, you want the order closed so you shouldn't skip it if the terminal is busy... As I say, TradeBusy() is a bespoke function for your EA so I could be way off the mark!

V

 
We que them. Not sure how, but we wrote a special include file that handles that. As soon as trade context is open next trade goes in.
 
PumPuiMonkey:
We que them. Not sure how, but we wrote a special include file that handles that. As soon as trade context is open next trade goes in.

Yeah, easy enough to que them yourself.... what I was getting at in your code is this bit...

if (TradeBusy() < 0 && OrderType() == 0) 
             {
              Print(Symbol(), " Trade is still busy when attempting to close Buy trade");
              return(0);
             }

which, depending on what is returned by the function,basically means if the trade is busy (assuming it is the same as IsTradeContextBusy()), skip this order... it will not close this order until the EA runs the next tick. It is normally preferable to wait until the terminal is free and then send the order rather than skip it completely.

Personally, I use continue instead of return... perhaps someone could clarify, but I think return would end the start function so not only do you fail to send this order, It won't send anymore and will just wait to start a new tick.

 
K I see, thank you. I didn't realize that. That would certainly speed things up. So I just need to change that to continue so it keeps sending it. I'll change it see what it does. You been a great help ! Always appreciated man....
 
PumPuiMonkey:
K I see, thank you. I didn't realize that. That would certainly speed things up. So I just need to change that to continue so it keeps sending it. I'll change it see what it does. You been a great help ! Always appreciated man....

Continue will still skip the order, it just means continue with the for loop, so it will go back to the begining of for, decrease i and carry on.

I wouldn't have TradeBusy() as part of the order selection at all...but when you are ready to close test then, If it's busy, wait untill it's free, then carry on.

while (IsTradeContextBusy())
      {
      Print("Waiting for trade context.");
      Sleep(MathRand()/10);
      }
 
K, I see. I'll get the clever half of the operation to have a look soon as he gets back from the black hole that doesn't get internet in the UK he's in. How that's even possible I don't know, thought UK would have internet everywhere by now....
 
Ah... Norfolk :)
Reason: