is it okay to loop through all open position every tick?

 

Hi,

I need to calculate a couple of variable such as lastOpenPriceBuy, lastOpenPriceSell, lastLotBuy, lastLotSell and also totalProfitBuy and totalProfitSell, to do that I create a PositionInfo() function.

void PositionInfo (string CurrentSymbol, ulong MagicNumber,
                   double& LastLotOpenPositionBuy, double& LastLotOpenPositionSell,
                   double& LastBuyPrice, double& LastSellPrice,
                   double& LastSLPrice, double& LastTPPrice,
                   int& PositionCountBuy, int& PositionCountSell
                   double& totalProfitBuy, double& totalProfitSell) {
//string CurrentSymbol = SymbolArray[SymbolLoop];
   CPositionInfo pos;

   for(int  i= 0; i <= PositionsTotal(); i++) {
      ulong ticket = PositionGetTicket(i);
      if(pos.SelectByTicket(ticket)) {
         if(pos.Symbol() == CurrentSymbol && pos.Magic() == MagicNumber) {
            if(pos.PositionType() == POSITION_TYPE_BUY) {
               LastLotOpenPositionBuy  = pos.Volume();
               LastBuyPrice            = pos.PriceOpen();
               LastSLPrice             = pos.StopLoss();
               LastTPPrice             = pos.TakeProfit();
               profit     = position.Profit();
               swap       = position.Swap();
               commission = position.Commission();
               totalProfitBuy += profit + swap + commission;
               PositionCountBuy++;
            }
            if(pos.PositionType() == POSITION_TYPE_SELL) {
               LastLotOpenPositionSell = pos.Volume();
               LastSellPrice           = pos.PriceOpen();
               LastSLPrice             = pos.StopLoss();
               LastTPPrice             = pos.TakeProfit();
               profit     = position.Profit();
               swap       = position.Swap();
               commission = position.Commission();
               totalProfitBuy += profit + swap + commission;
               PositionCountSell++;
            }
         }
      }
   }
}

Since position count buy and sell doesn't need to be calculated every tick and since there's a profit variable that needs to be calculated every tick, in terms of resource usage, is it better to separate the function to PositionInfo() that calculate position count at every new bar and ProfitInfo() that calculate profit at every tick or it doesn't really matter since the function needs to calculate profit at every tick might as well calculate all of the variable at every tick?

 
Luandre Ezra:

Hi,

I need to calculate a couple of variable such as lastOpenPriceBuy, lastOpenPriceSell, lastLotBuy, lastLotSell and also totalProfitBuy and totalProfitSell, to do that I create a PositionInfo() function.

Since position count buy and sell doesn't need to be calculated every tick and since there's a profit variable that needs to be calculated every tick, in terms of resource usage, is it better to separate the function to PositionInfo() that calculate position count at every new bar and ProfitInfo() that calculate profit at every tick or it doesn't really matter since the function needs to calculate profit at every tick might as well calculate all of the variable at every tick?

In general, if you can determine the change you want to track, you should only track that.

Your for loop has a conceptual issue, change this:

for(int  i= PositionsTotal() - 1; i >= 0; i--) 

Tracking position changes on a hedging account can be done by checking the last/most recent available ticket number and the count of positions. - If they have not changed since last check, then there is no change to the positions itself. Only Profit/Loss and Margin in use might have changed.


 

Alternative.

for (uint i = PositionsTotal(); (bool)i--;)
 
Dominik Egert #:
In general, if you can determine the change you want to track, you should only track that.

That's what I think first and it resulting to another issue when initializing the variable

void OnTick() {
   
   // this code below also run every tick
   int CountPositionBuy = 0, CountPositionSell = 0;
   double totalProfitBuy = 0, totalProfitSell = 0, lastBuyLot = 0, lastSellLot = 0;
   
   // run every new bar
   If(ProcessThisIteration)
   PositionInfo(CountPositionBuy, CountPositionSell, lastLotBuy, lastLotSell);

   ...


   // run every new tick
   If(!ProcessThisIteration)
   ProfitInfo(totalProfitBuy, totalProfitSell);

   ...
   ClosePositionAtProfit(CountPositionBuy, CountPositionSell, totalProfitBuy, totalProfitSell);
}

Here's an example code from my EA, since initializing variable runs on every tick, when PositionInfo function runs and fill the variable with value, at next tick the variable will be reset to 0. Since ClosePositionAtProfit needs variable CountPositionBuy and CountPositionSell to work properly then the function never run properly.

My best bet is saving the value to global variable and reset it when there's no open position but looking for another opinion of there's a better way to fix it.

 
fxsaber #:

Alternative.

Smart, and I like the tweeky smiley at the end...
 
Luandre Ezra #:

That's what I think first and it resulting to another issue when initializing the variable

Here's an example code from my EA, since initializing variable runs on every tick, when PositionInfo function runs and fill the variable with value, at next tick the variable will be reset to 0. Since ClosePositionAtProfit needs variable CountPositionBuy and CountPositionSell to work properly then the function never run properly.

My best bet is saving the value to global variable and reset it when there's no open position but looking for another opinion of there's a better way to fix it.

This depends much on the context, but you could also use static variables or a struct on global scope to store your state.
 

The amount of work (or even larger) done by your function is so trivial for any processor to perform.

It will take nanoseconds, not even 1 msec.

IMO, do not bother yourself with these micro-optimizations.

Reason: