Updating data inside a loop

Created at 14 Mar 2014, 03:04
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!
AI

AimHigher

Joined 04.01.2014

Updating data inside a loop
14 Mar 2014, 03:04


while (1 == 1)
 {

   iPosCnt = Positions.Count;

   Thread.Sleep(1000);

 }
 //end while

How can I get the Positions.Count to reflect the current number of positions when run inside a loop like in the code above? When I run it like in the code above, it only reflects the count on the first tick and does not reflect positions opened or closed while the code is running inside OnTick(). 

If for whatever reason that isn't possible (it should be and hopefully is), is there a way to artificially trigger OnTick() to get accurate position and pending order updates when the frequency of ticks is low?

 


@AimHigher
Replies

Spotware
14 Mar 2014, 09:04

There is no such functionality at the moment. We have plans to add method RefreshData() in the future.


@Spotware

AimHigher
14 Mar 2014, 18:06

First of all, I really want to like your platform, especially since I can't stand how the company behind the "other" platform is forcing things on the users, and being able to code in .NET is a huge plus. However, the more I try to write code for cAlgo, the more roadblocks I find. I can possible understand that market prices are not updated outside of a tick but why on earth is position/order data not updated? That should have been implemented from day 1.

Just to make sure that you answered all parts of my question, is there no way to artificially trigger OnTick() either?

In the other platform, one can use applications like https://fx1.net/mt4ticker.php that will simulate sending ticks to the platform. Is something like that possible in cAlgo?

In the other platform, at least in builds as of last year, ticks could be simulated by using the following code inside a loop e.g. in a script:

int InternalMsg = RegisterWindowMessageA("OtherPlatform_Internal_Message");
int hWnd = WindowHandle(Symbol(),Period());
PostMessageA(hWnd,InternalMsg,2,1);

This would trigger a tick on the chart the script was attached to (obviously requiring reference to user32.dll).

Since an application like mt4ticker does not, to my knowledge, exist for cAlgo, could you share with us what triggers a tick in cAlgo? That way, a simulated tick could be created either by
a separate robot or by a separate application, while we wait for a RefreshData() feature to be available.

Regards,

Aim


@AimHigher

AimHigher
14 Mar 2014, 22:14

Actually, looking at it a bit further and after playing around with the "other" platforms OnTimer() event, it appears that if all I am looking for is data like positions and pending orders, I can use System.Timers and run my logic inside of a method like the one below

private void OnTimedEvent(object sender, ElapsedEventArgs e)

It appears to be working when i tested it, e.g using Positions.Count and PendingOrders.Count. 

By working I mean that Positions.Count would update to reflect a position opening or closing without any tick coming in.

I might be wrong here so I'd appreciate some confirmation. 

Aim


@AimHigher

Spotware
17 Mar 2014, 09:57

OnTick method is invoked when platform receives new quotes for the corresponding symbol.

Using of System.Threading.Timer class is not recommended, because its callback is invoked in different thread, while cAlgo.API is not thread safe. It could cause crash of application.

If you want to invoke OnTick handler more frequently than ticks come, you can subscribe OnTick handler to MarketDepth.Updated event.

For example:

protected override void OnStart()
{
  MarketData.GetMarketDepth(Symbol).Updated += OnTick;
}

In this case OnTick handler will be invoked on any update of DOM.


@Spotware

AimHigher
17 Mar 2014, 15:55

RE:

Spotware said:

OnTick method is invoked when platform receives new quotes for the corresponding symbol.

Using of System.Threading.Timer class is not recommended, because its callback is invoked in different thread, while cAlgo.API is not thread safe. It could cause crash of application.

If you want to invoke OnTick handler more frequently than ticks come, you can subscribe OnTick handler to MarketDepth.Updated event.

For example:

protected override void OnStart()
{
  MarketData.GetMarketDepth(Symbol).Updated += OnTick;
}

In this case OnTick handler will be invoked on any update of DOM.

Thank you for showing how me how to trigger a tick e.g. on MarketDepth.Updated.

I am a bit puzzled by you comment regarding using a timer. To be specific, I am using System.Timers.Timer and not System.Threading.Timer and I am following the example from Spotware in this post /forum/cbot-support/357#4

If this is not "safe" to use, would it not be safe either to run a separate cBot that would simply call base.OnTick() at a specified interval? Part of the code would then look something like this:

using System.Timers;
//...
 
namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC)]
    
    public class RobotWithTimer : RobotWithOnTick
    {
        private readonly Timer _timer = new Timer();
 
        protected override void OnStart()
        {
            _timer.Elapsed += base.OnTick();   
            _timer.Interval = (Interval); // Timer will tick every Interval (milliseconds)
            _timer.Enabled = true; 
            _timer.Start(); 
 
        }
 
        protected override void OnTick()
        {
            // Will be empty

            }

        protected override void OnStop()
        {
            // Put your deinitialization logic here

            _timer.Stop();
        }

   //...     

If this is still not "safe", is there a way to add conditions to make it safer, i.e. reduce the risk of a crash?

Thanks for your help.

Aim


@AimHigher

Spotware
17 Mar 2014, 17:31

System.Timers.Timer uses ThreadPool threads. Please avoid using System.Timers.Timer because while you are accessing cAlgo.API objects from timer's thread, cAlgo could decide to update such objects from another thread. In such case there is a possibility that data will be corrupted and entire application could fail.

We are going to implement cAlgo.API.Timer which will guarantee that only one thread works with API objects per time.


@Spotware

Spotware
03 Jul 2014, 11:50

RefreshData() method and Timer object have been added to the API

http://www.spotware.com/about/news-updates/ctrader-and-calgo-updates---july-2014/637


@Spotware