Parallel processing in Indicators and cBots

Created at 12 May 2024, 17:59
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
MA

matcwhite

Joined 03.09.2023

Parallel processing in Indicators and cBots
12 May 2024, 17:59


Hello,

I was just wondering what components of the Automate API were thread-safe? I am investigating the use of Parallel processing to improve the performance of some of my indicators & cBots. However, I often get errors such as “Unable to invoke target method in current thread. Use `BeginInvokeOnMainThread` method”, which I presume means that whatever I'm doing is not thread-safe?

Here's my test code.

using cAlgo.API;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace cAlgo {
	[Indicator(AccessRights = AccessRights.None)]
	public class ParallelTasks : Indicator {
		
	
		protected override void Initialize() {

			var symbolLabels = new List<string>();

			foreach (var p in Positions) {
				if (!symbolLabels.Contains(p.SymbolName)) symbolLabels.Add(p.SymbolName);
			}

			Parallel.ForEach(symbolLabels, sn => {
				var symbol = Symbols.GetSymbol(sn);
				Print("Symbol {0} loaded", sn);
			});

		}

		public override void Calculate(int index) {

		}
	}
}

I suspect that using the ‘BeginInvokeOnMainThread’ method might make it work, but would negate any advantage of the parallel processing?

Many thanks,

Mat

 


@matcwhite
Replies

PanagiotisCharalampous
13 May 2024, 06:18

Hi there,

There is no explicit list regarding this but most methods are not thread safe. Read below

https://help.ctrader.com/ctrader-automate/thread-safety/

Best regards,

Panagiotis


@PanagiotisCharalampous

matcwhite
13 May 2024, 15:18 ( Updated at: 14 May 2024, 05:22 )

RE: Parallel processing in Indicators and cBots

PanagiotisCharalampous said: 

Hi there,

There is no explicit list regarding this but most methods are not thread safe. Read below

https://help.ctrader.com/ctrader-automate/thread-safety/

Best regards,

Panagiotis

Appreciate the prompt reply, thank you!

Mat


@matcwhite