dynamic lotsize not working. help! - page 4

 
Michael Charles Schefe #:

[E]ven tho[ugh] i would add an int variable=PositionsTotal() and reused this int for all those lines in OnTick; i do not see anymore optimising opportunities there. i woul also do same for ask and bid prices, however, this would only be out of habit, i do not think it would make the code run faster since these lines are not used at every tick -- correct?

The idea is that PositionsTotal() gets updated in the trading conditions in my code snippet's current form. A global int's value would remain unchanged until its PositionsTotal() definition is updated─causing erroneous orders to be sent. Again, you'll need to return the trade results or hackishly use Sleep(100) or so following each sent order. Just be aware that Sleep() is not compatible with the Tester─it can be enabled/disabled with a bool input.

Yes, the trading conditions blocks only run when they're true.

 
Michael Charles Schefe #:
even tho i would add an int variable=PositionsTotal() and reused this int for all those lines in OnTick; i do not see anymore optimising opportunities there. i woul also do same for ask and bid prices, however, this would only be out of habit, i do not think it would make the code run faster since these lines are not used at every tick -- correct?
There are 10 if statements in a row. Apparently, the conditions of only one of them can be met (it should not be possible for the conditions of several if statements to be met simultaneously). But there's no 'else' or 'return'. This means that the conditions of all 10 if statements will be checked each time.

Even if you add else statements, that's still far from all you can do. Because the conditions for the if statements are, roughly speaking, the same. Why check the same thing multiple times?

 
Ryan L Johnson #:

The idea is that PositionsTotal() gets updated in the trading conditions in my code snippet's current form. A global int's value would remain unchanged until its PositionsTotal() definition is updated─causing erroneous orders to be sent. Again, you'll need to return the trade results or hackishly use Sleep(100) or so following each sent order. Just be aware that Sleep() is not compatible with the Tester─it can be controlled with a bool input.

Yes, the trading conditions blocks only run when they're true.

if it is used in Ontick, then, wont PositionsTotal be allowed to update the 1 time per each tick?

EDIT: But then again if this was so, then a loop that closes trades, if the loop is in ontick, then, couldnt work.

 
Vladislav Boyko #:
There are 10 if statements in a row. Apparently, the conditions of only one of them can be met (it should not be possible for the conditions of several if statements to be met simultaneously). But there's no 'else' or 'return'. This means that the conditions of all 10 if statements will be checked each time.

Even if you add else statements, that's still far from all you can do. Because the conditions for the if statements are, roughly speaking, the same. Why check the same thing multiple times?

yes, agreed, i just assumed that checking a single variable several times would be faster than requesting update of PositionsTotal several times.

But as you said, we have digressed from original topic, not that this is too far from it.

back to bed for me! 4am here! Thanks for every1s time and efforts.
 
Michael Charles Schefe #:
if it is used in Ontick, then, wont PositionsTotal be allowed to update the 1 time per each tick?
Yes, that's the idea─to update PositionsTotal() before doing anything else. As Vladislav Boyko alluded, you can reorganize the code if you like, e.g., make all conditions that are contingent upon PositionsTotal() == 1 in one parent condition with other conditions as nested conditions... or encapsulate them into custom functions. Whatever you prefer.
 
Vladislav Boyko #:
Why check the same thing multiple times?

What does PositionsTotal() affect? ​​I haven't delved into the code, but I'm sure it can be checked once, not 10 times.

positionType can be checked once, not 10 times. Furthermore, it's best to avoid string comparisons when you can avoid it.

Why check the following a million times? Calculate 'initialOpenPrice - X * _Point' in advance and check it once.

&& SymbolInfoDouble(_Symbol, SYMBOL_ASK) <= initialOpenPrice - 160 * _Point)
 
Michael Charles Schefe #:
EDIT: But then again if this was so, then a loop that closes trades, if the loop is in ontick, then, couldnt work.
You can write a loop to do whatever you like in OnTick() as long as you have the position ticket (see positionTicket and the iteration of the loop in my GetPositions() custom function).
 
Ryan L Johnson #:
You can write a loop to do whatever you like in OnTick() as long as you have the position ticket.

yeah i see that. been many years since coding for mt4, but still revert to mental think of using positions rather than ticket numbers for even simply closing all trades at once!

and that is why i created an int at start of Ontick for PositionsTotal, which i meant to use that int variable instead of updating PositionsTotal million times, but have not fully replaced all those PositionsTotal(s) with the int variable. will get around to replacing them on 1 day!

 
Michael Charles Schefe #:
yeah i see that. been many years since coding for mt4, but still revert to mental think of using positions rather than ticket numbers for even simply closing all trades at once!
void CloseAllPositions()
  {
   if(PositionsTotal() >= 1)
    {
     for(int i = PositionsTotal() - 1; i >= 0; i--) // count all currency pair positions
      {
       // get the ticket number
       PositionTicket = PositionGetTicket(i);
       trade.PositionClose(PositionTicket, 0);
      }
    }
  }
 
Ryan L Johnson #:

isnt the if line made redundant by the first loop? ie i>= 0

EDIT code below extracted from my file.

// Global Enviro - preprocessor
#define total (ushort)PositionsTotal()
// OnTick
//   ushort total0 = total;
// custom function call(ed) -only when already confirmed that a position is open.
   for(int k = total - 1; k >= 0; k--)