Since you're retrieving the price of a different option contract every Monday at 9:20am and 3:00pm, you will need to use a loop. However, you could make your code much more efficient by using a Foreign() call right before the BuyPrice or SellPrice assignment (each of which only occurs once per week) instead of using SetForeign on every intraday bar (roughly 400 times per week with 5 minute data or 200 times per week with 1 minute data).
Actually I just used the above code as example, my actual code runs daily intraday and I need to monitor each one minute candle price for stop loss and profit target. Currently it takes roughly 20-30 minutes to run a backtest for one year. From what I can understand from your reply is that there is no alternative to make it faster. Am I correct?
I told you how to improve the efficiency of the code that you posted. How could anyone determine how to improve code that you haven't shared?
Have you run "Code Check & Profile" from the Tools menu in the AFL Editor window? That might point you to some of your most time-consuming code, which you should make an effort to either improve or at least execute only when necessary. In your posted example, you're doing a lot of work when you're not going to use the result of that work.
@mradtke gave you important hint: only call Foreign if you really USE the result. Foreign is resource-heavy function. You should minimize its use to absolute minimum.
I use the result of Foreign, all I am asking is can I use it the way I want without using the FOR loop so that I can reduce the processing time? On a 32 core processor also optimization of 32 cases takes 1 hour as of now.
In the example you posted, you call SetForeign once for every intraday bar. You only use the result of SetForeign twice each week: once in the morning and once in the afternoon. That means that for each week of the backtest you're likely calling SetForeign hundreds of times and NOT using the result. Your processing time will improve dramatically if you don't do this.
Your question about using a for loop has already been answered.
@mradtke I have now updated the code to what I am trying to ask, now please do tell if there is a way to improve processing time of this code. I am using the Foreign result at every candle and also ticker symbol changes daily for Foreign. Assuming I am only running it for 29th Oct 2019 to 1st Nov 2019 for now, in actual code I am using dates like this for 3-4 years. I am only majorly concerned about the Set Foreign function that is there a better way to use it in this scenario?
It's very simple: If the option name only changes once a day, then you should only call Foreign or SetForeign once at the start of a day and save the prices you need in a local variable that can be used on all the remaining bars of that day.
Also, I'm not sure what you're trying to do with the first for loop and all the DateTimeConvert calls, but that seems extremely wasteful as well.
DateTimeConvert for loop is used to assign expiry date to all the bars in order to call the foreign function as amibroker itself does not understand what will be the expiry of the option symbol so have to manually feed it and I am making the list of my expiry dates in excel so have to convert it to the symbol format of my data vendor to match the symbol name in order to call the foreign function.
Also, could you demonstrate with a small AFL what you are telling me to do for calling foreign only at start of the day, if what I understood you are telling instead of