top of page

Status:

In Progress

Version:

v1.01  | Apr. 8 / 2024

Algorithmic

Servers

De-Fi

Finance

dydx.gif

De-Fi Automated Trading

 

​

In Canada, regulations that cracked down on exchanges that weren't able to prove user deposits were being placed in a 1:1 guaranteed account (no risk of loss) to ensure users funds remain provably solvent caused massive effects in the availability of exchanges willing to comply with Canada's new stringent laws and regulations, and as a result, the majority of major exchanges non-reliant on Canadian customers pulled out completely, going so far to ban any access to their sites and services through IP-blocking. Binance, the exchange I utilized for connecting my API parameters to my algorithm's automated triggers (https://www.erickessir.com/trading-model-project) suddenly became useless as I was blocked from their services with no way around it. I looked for alternatives that gave me API trading capabilities along with leverage (margin) trading exceeding at least 10x, but found no centralized exchange options. As a result I needed to go the decentralized route that ensured Canadian regulations had no bearing on my activites and enabled my alogirthm to function again.

DYDX, a decentralized exchange that cannot be enforced by any government regulations, has decent liquidity, and has an expansive API to interact with the orderbook via scripts and webhook requests that can be formulated into trading parameters the above model requires. The account that interacts with the orderbook is pseudo-anonymous and only requires a private wallet such as Metamask to interact and sign De-Fi transactions. DYDX utilizes a layer 2 platform for all trades ensuring they're executed immediately, and with relatively low fees in comparison to centralized exchanges. DYDX offers perpetual futures with leverage, but openly states they will restrict anyone from a restricted Country (which includes Canada), BUT they can only enforce their policy based on the IP location of the computer/Server. A simple VPN, or ensuring the hosting server is located in a non-restricted country is all that is needed to bypass their restrictions since there is no KYC due to their decentralized nature.

​

Through a few months of research, implementation, and testing, I have successfully transferred the bulk of the previous trading model project to DYDX's platform through a privately hosted server in Singapore that has the required repositories to handle the API request and send it to the DYDX trading account to execute the trades. Below you will see the steps taken to setup and initiate the automated trading algorithm that still utilizes the previous forked projects logic, theories, and models.

​

The first step was to establish a remote server in a geographical location accepted by DYDX. The service Render.com offers fast, affordable servers with the option to host in Singapore, a Country accepted by DYDX. On DYDX's end, all they see is a API request from a server from Singapore with the mnemonic credentials connecting to the decentralized crypto wallet. At the bottom of the photo below, you will see the repository section and the github used that automatically handles the webhook request from TradingView (TV) and passes it along to DYDX properly. All credit belongs to the Author Junta for enabling the API logic from TV to DYDX.

1-01.JPG

The repository utilized to execute trades from TradingView.

https://tv-connector.gitbook.io/docs

 

​

Next to actually connect the decentralized wallet with the remote server, I first needed to acquire the 24 word mnemonic secret phrase that's generated from the source wallet address from Metamask as an example. Once I got the phrase I just needed to paste it into the secret file section in the server as an .env file with the correct format. NOTE: the secret phrase in the below example is made up and does not correlate to any wallet.

13-01.JPG
2-01.JPG
3.JPG
8-01.JPG

After everything is connected on the server and wallet side, all that was left was to grab the deploy hook URL to paste into the webhook window of the TV alert.

10.JPG

The last step was pasting the webhook URL, and setting up the JSON format alert message in the TV alerts. From the above example you can see the message setup indicating the variables and parameters for a trade to execute. When the price crosses the sloped line with the alert enabled, TV sends the contents of the message to the webhook URL thats pasted in the Notifications tab.

 

​

This concludes the section of executing automated trades through DYDX, but I will also explain the steps I took to accessing the event history of the server by generating a SSH ed25519 key, adding it to the server and establishing direct connection to the server through my personal computer on command prompt. With a simple request in CM I'm able to download a .csv file of all the trades made through the server. Below I will show and explain the process to establishing the connection.

6.JPG
7-01.JPG

First the key and passphrase needs to be generated by the computer in command prompt by entering the following command: C:\Users\User>ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519

After a SSH key is generated and stored in a folder that can be opened in a text document and copied for the next step.

9.JPG
5-01.JPG

Now I was able to paste the key into the server which generated a SSH URL that allows me to call the server through the command prompt to download files.

4-01.JPG
11.JPG
12_edited.jpg

Lastly with the command: 

scpsrvcoa68ef79t8c73ed8qj0@ssh.singapore.render.com:/app/data/exports/mainnet/tradeHistoryDydxV4.csvC:/Users/Eric/Documents/dydx

The computer access the servers memory in the respective folder and downloads the file requested which is a .csv file. This allows me to track and store all trades, dispositions, ACB, etc. This is especially useful for tax purposes and equity tracking.

 

​

That concludes this project for now. Currently it's left as "In Progress" as there may be updates to the repository that provides more paramter options to tailor and add more logic to the algorithm. Currently I'm limited to market orders only, and executing inividual orders. There is no OCO (one-cancels-the-other) options (adding TP and SL positions automatically) which causes issues with setting up TP and SL levels. Currently these must be placed manually before a trade is executed to ensure risks and rewards are setup correctly. Further options and products may become available through DYDX giving more capabilities in the future. Any updates to the project will be added below and once there is little to no updates the project will be marked as "Completed".

bottom of page