Are threads ok in cBots?

15 Apr 2019, 20:28Are threads ok in cBots?#1
dalinarposts: 14since: 03 Apr 2019

Is it safe to create a Thread from within a cBot? 

My problem is that putting my logic within OnTick() is not fast enough always since it depends on the speed of the tick

So I'm thining about putting it in a Thread or some kind of Timer, does cTrader supply a timer mechansim?

 

 

15 Apr 2019, 20:29#2
dalinarposts: 14since: 03 Apr 2019

note: I'm not going to open or close trades from within the thread

15 Apr 2019, 21:52#3
Xavier Rposts: 90since: 12 May 2015

You can do:
 

         protected override void OnStart()
        {
            Timer.Start(TimeSpan.FromMilliseconds(100));
        }
        
        protected override void OnTimer()
        {
            Print("Test");
        }

 

If you need more timers you could use the Timer inside System.Windows.Forms, I read that is also single threaded, read the documentation here:
https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.timer?view=netframework-4.7.2

The timers inside System.Timers are not single threaded and I think they're not safe.


¨You are not what you have done, you are what you have overcome.¨ - Anonymous
17 Apr 2019, 18:09#4
Xavier Rposts: 90since: 12 May 2015

My bad tho, I tried it and it doesn't work outside forms, the one built in cTrader is enough for most tasks I think.


¨You are not what you have done, you are what you have overcome.¨ - Anonymous