Skip to content

Instantly share code, notes, and snippets.

@bukowa
Last active November 30, 2023 12:13
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bukowa/821e248897dedead26027b9164305dd2 to your computer and use it in GitHub Desktop.
ATASPythonSocket

Install WebSocketSharp via nutget and add file websocket-sharp.dll to Atas program files

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Reference Include="ATAS.Indicators">
<HintPath>..\..\..\..\..\Program Files (x86)\ATAS Platform\ATAS.Indicators.dll</HintPath>
</Reference>
<Reference Include="ATAS.Indicators.Technical">
<HintPath>..\..\..\..\..\Program Files (x86)\ATAS Platform\ATAS.Indicators.Technical.dll</HintPath>
</Reference>
<Reference Include="OFT.Attributes">
<HintPath>..\..\..\..\..\Program Files (x86)\ATAS Platform\OFT.Attributes.dll</HintPath>
</Reference>
<Reference Include="Utils.Common">
<HintPath>..\..\..\..\..\Program Files (x86)\ATAS Platform\Utils.Common.dll</HintPath>
</Reference>
<Reference Include="websocket-sharp">
<HintPath>..\..\..\..\..\Program Files (x86)\ATAS Platform\websocket-sharp.dll</HintPath>
</Reference>
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy /y $(ProjectDir)$(OutDir)$(TargetFileName) &quot;C:\Users\buk\Documents\ATAS\Indicators&quot;" />
</Target>
</Project>
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using ATAS.Indicators;
using ATAS.Indicators.Technical.Properties;
using OFT.Attributes;
using Utils.Common.Localization;
using Utils.Common.Logging;
using WebSocketSharp;
[DisplayName("ATASPythonSocket")]
[LocalizedDescription(typeof(Resources), "ATASPythonSocket")]
public class ATASPythonSocket : Indicator
{
private WebSocket _webSocket;
#region ctor
public ATASPythonSocket()
: base(true)
{
this.LogInfo("Initializing...");
Panel = IndicatorDataProvider.NewPanel;
this.LogInfo("Initialized. Now websocket...");
this.InitializeWebSocket();
}
#endregion
#region Protected methods
protected override void OnCalculate(int bar, decimal value)
{
// Assuming you have some data to send to the server, replace the following line with your data
var dataToSend = $"Bar: {bar}, Value: {value}";
// Send data to the server
SendDataToServer(dataToSend);
// Receive data from the server (replace int with your expected data type)
var receivedData = ReceiveDataFromServer();
// Process the received data as needed
ProcessReceivedData(receivedData);
}
#endregion
private void InitializeWebSocket()
{
this.LogInfo("Initalizing websocket.");
_webSocket = new WebSocket("ws://localhost:8765");
_webSocket.OnOpen += (sender, e) => this.LogInfo("WebSocket connection opened");
_webSocket.OnMessage += (sender, e) => this.LogInfo($"Received message: {e.Data}");
_webSocket.OnClose += (sender, e) => this.LogInfo("WebSocket connection closed");
_webSocket.Connect();
_webSocket.OnMessage += (sender, e) =>
{
this.LogInfo($"Received message: {e.Data}");
// convert message to int
var receivedData = int.Parse(e.Data);
this[receivedData] = receivedData;
};
}
private void SendDataToServer(string data)
{
if (_webSocket.IsAlive)
{
_webSocket.Send(data);
}
else
{
// Handle the case when the WebSocket connection is not open
Console.WriteLine("WebSocket connection is not open");
}
}
private int ReceiveDataFromServer()
{
// Implement your logic to receive and parse data from the server
// For simplicity, I'm returning a placeholder value (replace it with your actual logic
return 42;
}
private void ProcessReceivedData(int receivedData)
{
// Implement your logic to process the received data
Console.WriteLine($"Processed data: {receivedData}");
}
// // Make sure to dispose of the WebSocket when the indicator is no longer needed
// protected void Dispose(bool isDisposing)
// {
// if (isDisposing)
// {
// _webSocket?.Close();
// _webSocket?.Dispose();
// }
//
// base.Dispose(isDisposing);
// }
}
import asyncio
import websockets
async def handle_client(websocket, path):
try:
print(f"Client connected: {websocket.remote_address}")
# Handle messages from the client
async for message in websocket:
print(f"Received message from client: {message}")
# Bar: {bar}, Value: {value}
msgsplit = message.split(",")
bar = msgsplit[0].split(":")[1].strip()
value = msgsplit[1].split(":")[1].strip()
print(f"Bar: {bar}, Value: {value}")
# Send the response back to the client
await websocket.send(bar)
print(f"Sent response to client: {bar}")
except websockets.exceptions.ConnectionClosedError:
pass
finally:
print(f"Client disconnected: {websocket.remote_address}")
async def main():
# Start the WebSocket server on localhost, port 8765
server = await websockets.serve(handle_client, "localhost", 8765)
print("WebSocket server started. Listening on ws://localhost:8765")
# Keep the server running
await server.wait_closed()
# Run the WebSocket server
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment