Documentation example doesn't work

Created at 25 May 2023, 09:18
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!
CO

coffeedarkmatter

Joined 25.05.2023

Documentation example doesn't work
25 May 2023, 09:18


I've been trying to make the documentation example from this page work. Unfortunately, it doesn't: the ScrollViewer won't appear on the chart. (Even after waiting for 5 minutes.)

Does the example from that page work for you on cTrader 4.6.7?

When I change the for loop of that example to the following, the ScrollViewer does appear:

for (int iSymbol = 1; iSymbol < Symbols.Count; iSymbol++)
{
 string symbolName = Symbols[iSymbol];
 grid.AddChild(new TextBlock
 {
	 Text = symbolName,
	 Margin = 5,
	 ForegroundColor = Color.Black,
	 FontWeight = FontWeight.ExtraBold
 }, iSymbol, 0);
 grid.AddChild(new Button
 {
	 Text = "hello",
	 Margin = 5,
	 ForegroundColor = Color.Black,
	 FontWeight = FontWeight.ExtraBold
 }, iSymbol, 1);
}

So without the code that uses the symbol (such as its description) the ScrollViewer does render. But that's somewhat of the core of the example.

Thanks,

 


@coffeedarkmatter
Replies

pick
25 May 2023, 13:24

It works, but it takes a long time - mainly due to the Symbols.GetSymbol call (which must be done on the main thread) combined with how many symbols there are to load.

You can reduce the number of loops and it will populate faster, but if you want all symbols, below is a solution that I just quickly wrote up. It will populate the symbol names, then move on to filling in the descriptions.

Keep in mind that this is only a quick implementation and that it could probably be made more efficient:

protected override void Initialize()
{
	ScrollViewer scrollViewer = new ScrollViewer
	{
		HorizontalAlignment = HorizontalAlignment.Center,
		VerticalAlignment = VerticalAlignment.Center,
		BackgroundColor = Color.Gold,
		Opacity = 0.7,
		HorizontalScrollBarVisibility = ScrollBarVisibility.Auto,
		VerticalScrollBarVisibility = ScrollBarVisibility.Visible,
		Height = 300
	};

	Grid grid = new Grid(Symbols.Count + 1, 2)
	{
		BackgroundColor = Color.Gold,
		HorizontalAlignment = HorizontalAlignment.Center,
		VerticalAlignment = VerticalAlignment.Center,
	};

	grid.AddChild(new TextBlock
	{
		Text = "Name",
		Margin = 5,
		ForegroundColor = Color.Black,
		FontWeight = FontWeight.ExtraBold
	}, 0, 0);

	grid.AddChild(new TextBlock
	{
		Text = "Description",
		Margin = 5,
		ForegroundColor = Color.Black,
		FontWeight = FontWeight.ExtraBold
	}, 0, 1);

	Chart.AddControl(scrollViewer);
	scrollViewer.Content = grid;

	List<string> strings = Symbols.ToList();
	for (int i = 0; i < strings.Count; i++)
	{
		grid.AddChild(new TextBlock
		{
			Text = strings[i],
			Margin = 5,
			ForegroundColor = Color.Black,
			FontWeight = FontWeight.ExtraBold
		}, i + 1, 0);
	}

	foreach (string s in strings)
	{
		this.BeginInvokeOnMainThread(() =>
		{
			Symbol sym = Symbols.GetSymbol(s);
			grid.AddChild(new Button
			{
				Text = sym.Description,
				Margin = 5,
				ForegroundColor = Color.Black,
				FontWeight = FontWeight.ExtraBold
			}, strings.IndexOf(s) + 1, 1);
		});
	}
}

 


@pick

coffeedarkmatter
26 May 2023, 18:39

Thanks Pick! That code works. And I now also understand how cTrader operates a bit better. I much appreciate your reply and the time taken. :)

How did you know the code did work, but needed threading to give a visual result earlier? I looked at the chart and indicator and didn't notice a spinning wheel or loading sign. And in the Task Manager the cTrader's CPU usage was very low. So I mistakenly assumed the code didn't do anything.


@coffeedarkmatter

pick
30 May 2023, 11:22

RE:

coffeedarkmatter said:

Thanks Pick! That code works. And I now also understand how cTrader operates a bit better. I much appreciate your reply and the time taken. :)

How did you know the code did work, but needed threading to give a visual result earlier? I looked at the chart and indicator and didn't notice a spinning wheel or loading sign. And in the Task Manager the cTrader's CPU usage was very low. So I mistakenly assumed the code didn't do anything.

Sorry for the delayed response. I'm glad that you could reproduce a working sample! I initially just reduced the number of iterations in the loop to 5 and it populated. Then, I just removed the graphical side of it and began logging with the timings to find the culprit. 


@pick

coffeedarkmatter
30 May 2023, 17:07

Thanks for the reply Pick. It's not delayed from my perspective, I'm just happy to get insights from people with a lot more experience in cTrader than me. :)

I appreciate you listing the steps you've took. This is going to help me in the future with cTrader. Thanks!


@coffeedarkmatter