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?
for(int i= PositionsTotal() - 1; i >= 0; i--)
Forum on trading, automated trading systems and testing trading strategies
is it okay to loop through all open position every tick?
Dominik Egert, 2023.10.16 12:18
for(int i= PositionsTotal() - 1; i >= 0; i--)
Alternative.
for (uint i = PositionsTotal(); (bool)i--;)
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.
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.
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.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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?