Run on your own server
There may be cases where you'd need to run strategies on your own server rather than on Tradelets (Tradologics' servers). This guide will show you how.
Some plans on Tradologics allow you to run strategies on your own server. This can prove to be useful in cases when you need your strategy to communicate with external resources in your stack, use custom data sources, or if you are required to do so for compliance reasons.
Tradologics makes it as simple as possible to port your strategies and run them on your own server.
The basic premise of using your own server involves coding your strategy as you normally would. When writing the code, you will need to make sure that you are able to accept Tradehook data via
POST
and pass it into your strategy. Utilizing your own server is truly as simple as that.Here are the steps required to run strategies on your own server:
- 1.Write your strategy as you normally would.
- 2.Create a file with a function to parse the Tradehook payload's wrapper JSON (e.g.
server.py
). - 3.This file should import your strategy's main file (e.g.
strategy.py
). - 4.Make sure your Tradehook handler function accepts
JSON
bodyPOST
viahttp(s)
.
Please note that, at the moment, you can only run strategies running on your own server in either Paper or Broker modes. Backtesting on your own server is currently not possible, as it takes too long due to network latency.
{
"tradehook": "Tradehook name",
"data": "Actual Tradehook payload"
}
Example: Bar Tradehook
{
"tradehook": "bar",
"data": {
"assets": [
"AAPL:US",
"..."
],
"bars": {
"YYYY-MM-01": {
"AAPL": {
"o": "113.79",
"h": "117.26",
"l": "113.62",
"c": "115.56",
"v": 136210200,
"t": 782692,
"w": "116.11665173913042"
}
},
"YYYY-MM-02": {
"...": "..."
}
}
}
}
In order for your strategy to run on your server, you will need to write a handler function. This handler function will need to parse the Tradehook data and pass it on to your strategy.
We recommend that you use a hard-to-guess URL endpoint for your strategy. In the below example, we're using
/9d3c6c4cbffc4026a2da24fc068b5107
In this example, we assume that
strategy.py
(your strategy file) is located in the same directory as your server.py
file (your Tradehook's handler file), and that strategy.py
has a main function called strategy
.For this reason, you'll notice that we're importing it using
from . import strategy
and that we're calling it via strategy.strategy(...)
.server.py
from tradologics import server
#-------------------------------
# ↓ this should be a file with your strategy code
from . import strategy
#-------------------------------
server.start(strategy, endpoint="/9d3c6c4cbffc4026a2da24fc068b5107",
host="0.0.0.0", port=5000, debug=False)
Next, run this in your file:
$ python ./server.py
* Serving Flask app
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
In this example, we assume that
strategy.js
(your strategy file) and server.js
is located in the same directory.server.js
const Server = require('@tradologics/tradologics/server');
const { strategy } = require('./strategy');
Server.start(
strategy,
'/9d3c6c4cbffc4026a2da24fc068b5107',
'0.0.0.0',
'5000'
);
Next, run this in your file:
$ node server.js
In this example, we assume that
Strategy.java
(your strategy file) is located in the same directory as your Server.java
file (your Tradehook's handler file), and that Strategy.java
has the main function called strategy
.Server.java
import com.tradologics.server.*;
public class Server {
public static void main(String[] args) throws Exception {
Strategy handler = new Strategy();
ServerBuilder builder = new ServerBuilder(handler);
builder.host("0.0.0.0").port(5000).endpoint("/9d3c6c4cbffc4026a2da24fc068b5107").build().start();
}
}
strategy.java
import com.tradologics.server.*;
public class Strategy implements IStrategyHandler {
@Override
public void strategy(String tradehook, String payload) {
System.out.println("Strategy handler logic...");
System.out.println(tradehook);
System.out.println(payload);
}
}
build.gradle
// Add this section to your build.gradle file
jar {
manifest {
attributes 'Main-Class': 'Server'
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
// If you want to include resources into your compiled .jar file
into 'resources', {
from 'resources'
}
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}
Finally, run this in your file:
$ ./gradlew jar
$ java -jar build/libs/*.jar
* [Thread-0] INFO org.eclipse.jetty.util.log - Logging initialized @123ms
* [Thread-0] INFO spark.webserver.JettySparkServer - == Spark has ignited ...
* [Thread-0] INFO spark.webserver.JettySparkServer - >> Listening on 0.0.0.0:5000
* [Thread-0] INFO org.eclipse.jetty.server.Server - jetty-9.3.z-SNAPSHOT
* [Thread-0] INFO org.eclipse.jetty.server.ServerConnector - Started [email protected]{HTTP/1.1,[http/1.1]}{0.0.0.0:5000}
* [Thread-0] INFO org.eclipse.jetty.server.Server - Started @234ms
In this example, we assume that
strategy.go
(your strategy file) and server.go
is located in the proj
directory, main.go
in the project root.proj/server.go
package server
import (
"github.com/tradologics/go-sdk/server"
)
func Run() {
server.Start(Strategy, "/9d3c6c4cbffc4026a2da24fc068b5107", "0.0.0.0", 5000)
}
proj/strategy.go
package server
import (
"fmt"
)
func Strategy(event string, data []byte) {
fmt.Println("EVENT -->", event, "DATA -->", string(data))
}
proj/main.go
package main
import (
"fmt"
"proj/server" // ← your server.go file
)
func main() {
fmt.Println("Server running...")
server.Run()
}
proj/go.mod
module proj
go 1.17
require (
github/tradologics/go-sdk v0.2.0
)
Lastly, run this in your file:
$ go run main.go
* Server running...
Your handler URL is:
http://<your-ip-or-domain>:5000/9d3c6c4cbffc4026a2da24fc068b5107
The last step will be to point Tradologics to your server's URL, which can be done from the web console when creating your strategy.

That's it!
Once your strategy is started, all Tradehook data will be sent to your server's URL via POST request. Your strategy can communicate with the Tradologics API as it normally would.
Last modified 1yr ago