Protobuf/C# out of date compared to Web Docs and Servers

Created at 21 Oct 2016, 13:33
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!
jaredthirsk's avatar

jaredthirsk

Joined 18.12.2015

Protobuf/C# out of date compared to Web Docs and Servers
21 Oct 2016, 13:33


It looks like the documentation website is out of sync with the protobuf that’s available on the website, and the protocol the server is using. 

Features I am interested in:

  • It looks like the website shows a place to subscribe to trendbars, but the provided protobuf definitions and C# code doesn’t seem to support this concept.  
  • [solved] Timestamp on ticks.  (Kind of important!)
  • Protobuf v3 - I believe this is better supported on .NET Core.

 

Website docs: https://connect.spotware.com/documentation/section/openapimessages

Outdated/incomplete Protobuf download link: https://connect.spotware.com/uploads/open-api-proto-lib.zip

Protobuf download link found here: https://connect.spotware.com/documentation/section/developer-library

 

[Interim Solution] Timestamp on ticks in C#:

       void ProcessIncomingDataStream(OpenApiMessagesFactory msgFactory, byte[] rawData)
        {
            var _msg = msgFactory.GetMessage(rawData);

            switch (_msg.PayloadType)
            {
                case (int)OpenApiLib.ProtoOAPayloadType.OA_SPOT_EVENT:
                    {
                        var payload = msgFactory.GetSpotEvent(rawData);
                        var timestampField = payload.UnknownFields[7];
                        var timestamp = timestampField.VarintList[0];
                        var utcTime = new DateTime(1970, 1, 1) + TimeSpan.FromMilliseconds(timestamp);
                        // ...
                        break;
                    }
            }
        }

I hope this helps somebody, but better yet, I hope for an updated .proto definitions and C# samples.


@jaredthirsk
Replies

jaredthirsk
21 Oct 2016, 20:22

RE: UPDATE:

jaredthirsk said:

Outdated/incomplete Protobuf download link: https://connect.spotware.com/uploads/open-api-proto-lib.zip

Protobuf download link found here: https://connect.spotware.com/documentation/section/developer-library

 

Update: within a couple of hours of me emailing Spotware, they updated their Protobuf.  Both tick timestamps and the subscribing/receiving trendbars appears to be in there.  Thanks, Spotware!

Now I have to figure out how to regenerate C# bindings -- the instructions in the readme here seem to be out of date because the file gen_csharp_src.bat doesn't appear to exist anywhere:

https://github.com/spotware/connect-csharp-samples

 

 


@jaredthirsk

jaredthirsk
21 Oct 2016, 20:46

RE: RE: UPDATE:

jaredthirsk said:

Now I have to figure out how to regenerate C# bindings -- the instructions in the readme here seem to be out of date because the file gen_csharp_src.bat doesn't appear to exist anywhere:

https://github.com/spotware/connect-csharp-samples

Ok I figured out how to regenerate protobuf C#:

  1. Clone https://github.com/jskeet/protobuf-csharp-port
  2. Run build\BuildAll.bat   (I had some errors about missing .NET Compact Framework, or something, but I ignored it.)
  3. Go to directory containing the 4 *.proto files from spotware
  4. Run this:  c:\path\to\protobuf-csharp-port\build_output\tools\protogen --protoc_dir=c:\path\to\protobuf-csharp-port\build_output\tools OpenApiMessages.proto OpenApiModelMessages.proto CommonModelMessages.proto CommonMessages.proto

It will generate 4 .cs files.  Include all 4 in a C# DLL project.

Note: current version of .proto files has two errors (missing ProtoOATrendbarPeriod and ProtoOATrendbar).  Hopefully spotware corrects this but I will document my workaround in the next message.


@jaredthirsk

jaredthirsk
21 Oct 2016, 22:11

RE: RE: RE: UPDATE:

jaredthirsk said:

Note: current version of .proto files has two errors (missing ProtoOATrendbarPeriod and ProtoOATrendbar). 

 

My quick interim fix - add this to OpenApiModelMessages.proto:  (this is a guess based on the web docs.  The docs don't contain field numbers so I have to guess.)

enum ProtoOATrendbarPeriod {
    M1 = 1;
    M2 = 2;
    M3 = 3;
    M4 = 4;
    M5 = 5;
    M10	= 6;
    M15	= 7;
    M30	= 8;
    H1 = 9;
    H4 = 10;
    H12	= 11;	
    D1 = 12;
    W1 = 13;
    MN1 = 14;
}

message ProtoOATrendbar {
	optional double open = 1;
    optional double close = 2;
	optional double high = 3;
	optional double low = 4;
	required int64 volume = 5;
	optional int64 utcTimestamp = 6;
	optional ProtoOATrendbarPeriod period = 7;
}

Either add this to CommonModelMessages.proto inside ProtoPayloadType (untested), or comment out PROTO_MESSAGE related code from the C# samples:

     PROTO_MESSAGE = 5;

 

I don't know how to generate OpenApiMessagesFactory.cs, so I made this change:

        public ProtoMessage CreateSubscribeForSpotsRequest(long accountId, string accessToken, string symbolName, string clientMsgId = null, List<ProtoOATrendbarPeriod> periods = null)
        {
            var _msg = ProtoOASubscribeForSpotsReq.CreateBuilder();
            _msg.SetAccountId(accountId);
            _msg.SetAccessToken(accessToken);
            _msg.SetSymblolName(symbolName);
            if (periods != null)
            {
                foreach (var period in periods)
                {
                    _msg.AddTrendbarPeriod(period);
                }
            }
            return CreateMessage((uint)_msg.PayloadType, _msg.Build().ToByteString(), clientMsgId);
        }

I can subscribe to one or more trendbar periods, and I see the data being sent,  but I am not receiving any trendbars from the server.  Any help would be appreciated.


@jaredthirsk

jaredthirsk
22 Oct 2016, 00:32

RE: RE: RE: UPDATE:

jaredthirsk said:

Ok I figured out how to regenerate protobuf C#:

  1. Clone https://github.com/jskeet/protobuf-csharp-port
  2. Run build\BuildAll.bat   (I had some errors about missing .NET Compact Framework, or something, but I ignored it.)
  3. Go to directory containing the 4 *.proto files from spotware
  4. Run this:  c:\path\to\protobuf-csharp-port\build_output\tools\protogen --protoc_dir=c:\path\to\protobuf-csharp-port\build_output\tools OpenApiMessages.proto OpenApiModelMessages.proto CommonModelMessages.proto CommonMessages.proto

I wish I could edit my post.   I also realized I needed to add -namespace=OpenApiLib to the command in #4.

 

 


@jaredthirsk

jaredthirsk
22 Oct 2016, 00:39

By the way, if anyone is interested in following my development, and possibly helping one another, you can see my open source code (MIT license) here: 

https://github.com/LionFire/Trading


@jaredthirsk

jaredthirsk
26 Oct 2016, 07:09

Dear programmer of the future,

As of Oct 25 2016, Spotware has kindly given another update to the .proto files, downloadable through the link mentioned above.  They haven't updated the C# sample (or I didn't check), but I have regenerated the associated C# files and checked them in to my open source github project which you can find here:

Note: I don't know how to regenerate the OpenApiDeveloperLibrary files (OpenApiMessagesFactory.cs, OpenApiMessagesPresentation.cs, OpenApiModelObjectsFactory.cs).  If someone can show me how to do these, please let me know. (But maybe they are manually created?)

 


@jaredthirsk