Recent

Author Topic: A bot for auto-trading  (Read 1504 times)

ezlage

  • Sr. Member
  • ****
  • Posts: 304
  • (Silêncio)
    • Lageteck
A bot for auto-trading
« on: December 18, 2019, 03:09:04 pm »
Hello, friends!

I'm doing some drafts to create a "robot" for cryptocurrencies and stablecoins automatic trading. I don't started to code yet, but I'm studying how I can achieve this goal. The bot will need to interact with some APIs, and I want to start from the WebSocket/HTTP based ones.

Preliminarily, I'm intended to use a TProcess inside of a TThread. The first one will send a command and listen to WebSocket data through pipeline (curl and/or some websocket client). The second will continuously read the output, separating each piece, converting and updating some database tables.

So, here is my question: Anybody sees a better solution or have recommendations about it?

Thank you!
Sorry by my poor english.

Atenciosamente,

Ezequiel Lage
ezequiel@lageteck.com

https://github.com/ezlage

LemonParty

  • New Member
  • *
  • Posts: 29
Re: A bot for auto-trading
« Reply #1 on: December 18, 2019, 03:33:34 pm »
Why don't you want to use libs like Synapse, Indy or fphttpclient?
They can directly manage an HTTP (or other protocols) communication.
For the simple Rest API things fphttpclient should be enough.

ezlage

  • Sr. Member
  • ****
  • Posts: 304
  • (Silêncio)
    • Lageteck
Re: A bot for auto-trading
« Reply #2 on: December 18, 2019, 03:38:50 pm »
Why don't you want to use libs like Synapse, Indy or fphttpclient?
They can directly manage an HTTP (or other protocols) communication.
For the simple Rest API things fphttpclient should be enough.

I can use it for some things, like send a command or get a non-continuously output, even through HTTPS. But for listen to a websocket channel, I need to send a command and keep listening and reading the output. I think that this feature isn't available on Synapse, Indy or fphttpclient.
« Last Edit: December 18, 2019, 03:44:46 pm by ezlage »
Sorry by my poor english.

Atenciosamente,

Ezequiel Lage
ezequiel@lageteck.com

https://github.com/ezlage

mr-highball

  • Full Member
  • ***
  • Posts: 114
    • Highball Github
Re: A bot for auto-trading
« Reply #3 on: December 18, 2019, 03:49:09 pm »
If you want to trade on coinbase pro you can use my library,
https://github.com/mr-highball/coinbase-pro

I do have a trading framework/bot... but I haven't released it yet :)
The idea to opensource is still there, but I'm not sure if I want to release strategies or not

mr-highball

  • Full Member
  • ***
  • Posts: 114
    • Highball Github
Re: A bot for auto-trading
« Reply #4 on: December 18, 2019, 03:56:38 pm »
also for threading, you can use my library which may simplify prototyping the multi threaded aspect,
https://github.com/mr-highball/ezthreads
« Last Edit: December 18, 2019, 04:05:18 pm by mr-highball »

ezlage

  • Sr. Member
  • ****
  • Posts: 304
  • (Silêncio)
    • Lageteck
Re: A bot for auto-trading
« Reply #5 on: December 18, 2019, 04:10:07 pm »
If you want to trade on coinbase pro you can use my library,
https://github.com/mr-highball/coinbase-pro

I do have a trading framework/bot... but I haven't released it yet :)
The idea to opensource is still there, but I'm not sure if I want to release strategies or not

Great! Thank you very much!
Your code will help me a lot when I start to work with Node.js-based APIs. If my work evolve, you can count on me about the code for websocket.

When I'm done, I'd like to release all my code, but, just like you, I have concerns about strategies. Generally, just releasing the code doesn't hurt us, but if too many people start using our strategies, they will become obsolete and unprofitable.



Sorry by my poor english.

Atenciosamente,

Ezequiel Lage
ezequiel@lageteck.com

https://github.com/ezlage

mr-highball

  • Full Member
  • ***
  • Posts: 114
    • Highball Github
Re: A bot for auto-trading
« Reply #6 on: December 18, 2019, 04:15:02 pm »
No problem, glad to help. If I do release my trading engine then I'll be sure to post back here since there is some interest.

ezlage

  • Sr. Member
  • ****
  • Posts: 304
  • (Silêncio)
    • Lageteck
Re: A bot for auto-trading
« Reply #7 on: December 18, 2019, 04:22:27 pm »
also for threading, you can use my library which may simplify prototyping the multi threaded aspect,
https://github.com/mr-highball/ezthreads
No problem, glad to help. If I do release my trading engine then I'll be sure to post back here since there is some interest.

Thank you again!
I took a look at your repositories in GitHub and I found very useful things. You did a great work!
Sorry by my poor english.

Atenciosamente,

Ezequiel Lage
ezequiel@lageteck.com

https://github.com/ezlage

mr-highball

  • Full Member
  • ***
  • Posts: 114
    • Highball Github
Re: A bot for auto-trading
« Reply #8 on: December 18, 2019, 04:28:54 pm »
Thank you :)

ezlage

  • Sr. Member
  • ****
  • Posts: 304
  • (Silêncio)
    • Lageteck
Re: A bot for auto-trading
« Reply #9 on: December 22, 2019, 02:59:58 pm »
-This code is now obsolete!-

Here is a proof of concept. The code is working well, I think.

Can someone share criticizes or suggestions? I ask because I'm not a expert, but I need to build a fast and fail-safe code.

Code: Pascal  [Select]
  1. unit wscatcher;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, UTF8Process, Process;
  9.  
  10. type
  11.  
  12.   { TWSCatcher }
  13.  
  14.   TWSCatcher = class(TThread)
  15.   private
  16.     FProcess: TProcessUTF8;
  17.     FInput, FOutput, FError: TStringList;
  18.   public
  19.     constructor Create;
  20.     destructor Destroy; override;
  21.     procedure Execute; override;
  22.     procedure Terminate;
  23.     procedure SendToSTDIN(const sData: string);
  24.     property ReceivedFromSTDOUT: TStringList read FOutput;
  25.     property ReceivedFromSTDERR: TStringList read FError;
  26.     property Process: TProcessUTF8 read FProcess;
  27.   end;
  28.  
  29. implementation
  30.  
  31. uses
  32.   SysUtils;
  33.  
  34. { TWSCatcher }
  35.  
  36. constructor TWSCatcher.Create;
  37. begin
  38.   inherited Create(True);
  39.   FInput:=TStringList.Create;
  40.   FError:=TStringList.Create;
  41.   FOutput:=TStringList.Create;
  42.   FProcess:=TProcessUTF8.Create(nil);
  43.   FProcess.Options:=[poNoConsole,poUsePipes];
  44. end;
  45.  
  46. destructor TWSCatcher.Destroy;
  47. begin
  48.   FProcess.Terminate(0);
  49.   FProcess.Free;
  50.   FOutput.Free;
  51.   FError.Free;
  52.   FInput.Free;
  53.   inherited Destroy;
  54. end;
  55.  
  56. procedure TWSCatcher.Execute;
  57. var
  58.   Send: string='';
  59.   Error: string='';
  60.   Received: string='';
  61.   ByteRead: byte=0;
  62. begin
  63.   FProcess.Execute;
  64.   with FProcess do while Running do begin
  65.     if Output.NumBytesAvailable>0
  66.       then begin
  67.         ByteRead:=Output.ReadByte;
  68.         if (ByteRead<>13) and (ByteRead<>10) and (ByteRead<>0)
  69.           then Received:=Received+Char(ByteRead)
  70.           else begin
  71.             if Received.Trim<>''
  72.               then FOutput.Add(Received.Trim);
  73.             Received:='';
  74.           end;
  75.       end;
  76.     if Stderr.NumBytesAvailable>0
  77.       then begin
  78.         ByteRead:=Stderr.ReadByte;
  79.         if (ByteRead<>13) and (ByteRead<>10) and (ByteRead<>0)
  80.           then Error:=Error+Char(ByteRead)
  81.           else begin
  82.             if Error.Trim<>''
  83.               then FError.Add(Error.Trim);
  84.             Error:='';
  85.           end;
  86.       end;
  87.     while FInput.Count>0 do begin
  88.       Send:=FInput.Strings[0].Trim+LineEnding;
  89.       if Send<>LineEnding
  90.         then Input.Write(Send[1],Length(Send));
  91.       FInput.Delete(0);
  92.     end;
  93.   end;
  94.   ByteRead:=0;
  95.   Received:='';
  96.   Error:='';
  97.   Send:='';
  98. end;
  99.  
  100. procedure TWSCatcher.Terminate;
  101. begin
  102.   FProcess.Terminate(0);
  103.   inherited Terminate;
  104. end;
  105.  
  106. procedure TWSCatcher.SendToSTDIN(const sData: string);
  107. begin
  108.   if (sData.Trim<>'') and (Pos(#13,sData)=0) and (Pos(#10,sData)=0) and (Pos(#0,sData)=0)
  109.     then FInput.Add(sData.Trim);
  110. end;
  111.  
  112. end.

This is the way to connect through WebSocket and listen to data:

Code: Pascal  [Select]
  1.   //[...]
  2.  
  3. var
  4.   WSC: TWSCatcher=nil;
  5.      
  6.   //[...]
  7.  
  8.   WSC:=TWSCatcher.Create;
  9.   WSC.Process.Executable:=FindDefaultExecutablePath('websocat');
  10.   WSC.Process.Parameters.Add('wss://api2.poloniex.com');
  11.   WSC.SendToSTDIN('{"command": "subscribe", "channel": "USDC_BTC"}');
  12.   WSC.Start;
  13.  
  14.   //[...]
« Last Edit: January 23, 2020, 10:27:15 pm by ezlage »
Sorry by my poor english.

Atenciosamente,

Ezequiel Lage
ezequiel@lageteck.com

https://github.com/ezlage

mr-highball

  • Full Member
  • ***
  • Posts: 114
    • Highball Github
Re: A bot for auto-trading
« Reply #10 on: December 22, 2019, 03:32:58 pm »
I'm not at my computer right now to look over the code, but it may be worth looking to see if you can get Websockets working without pipes/curl etc...
I'm not sure if this will work but synapse says they have websocket support. When I looked over some of the units, this one looked promising

https://github.com/synopse/mORMot/blob/master/SynCrtSock.pas#L9462

I'll try to test out what you have though when I can get some free time,
Cheers 🍻

ezlage

  • Sr. Member
  • ****
  • Posts: 304
  • (Silêncio)
    • Lageteck
Re: A bot for auto-trading
« Reply #11 on: December 22, 2019, 03:52:04 pm »
I'm not at my computer right now to look over the code, but it may be worth looking to see if you can get Websockets working without pipes/curl etc...
I'm not sure if this will work but synapse says they have websocket support. When I looked over some of the units, this one looked promising

https://github.com/synopse/mORMot/blob/master/SynCrtSock.pas#L9462

I'll try to test out what you have though when I can get some free time,
Cheers 🍻

I agree! I was trying to directly connect through WebSocket, send data and listen to responses, but, unfortunately, my knowledge isn't enough yet.
I took a look at Synapse. It doesn't support WebSocket yet, but this feature is in the project's wishlist.

Synopse seems to have WebSocket support, thank you for this information. I'm trying to figure out how to use it right now.

Thank you again, mr-highball!
Sorry by my poor english.

Atenciosamente,

Ezequiel Lage
ezequiel@lageteck.com

https://github.com/ezlage

ezlage

  • Sr. Member
  • ****
  • Posts: 304
  • (Silêncio)
    • Lageteck
Sorry by my poor english.

Atenciosamente,

Ezequiel Lage
ezequiel@lageteck.com

https://github.com/ezlage

ezlage

  • Sr. Member
  • ****
  • Posts: 304
  • (Silêncio)
    • Lageteck
Re: A bot for auto-trading
« Reply #13 on: January 23, 2020, 10:28:51 pm »
I finished the tests and the tuning. The code is now available here: https://github.com/ezlage/wscatcher.

Thanks to all of you!
Sorry by my poor english.

Atenciosamente,

Ezequiel Lage
ezequiel@lageteck.com

https://github.com/ezlage

mr-highball

  • Full Member
  • ***
  • Posts: 114
    • Highball Github
Re: A bot for auto-trading
« Reply #14 on: February 01, 2020, 07:22:18 am »
Thanks for sharing!