This README takes shows how to earn $ by running a trader bot on mainnet, and beyond.
Then, you can go beyond: optimize trading strategy, and more.
In a new console:
# clone the repo and enter into it
git clone https://github.com/oceanprotocol/pdr-backend
cd pdr-backend
# Create & activate virtualenv
python -m venv venv
source venv/bin/activate
# Install modules in the environment
pip install -r requirements.txt
If you're running MacOS, then in console:
codesign --force --deep --sign - venv/sapphirepy_bin/sapphirewrapper-arm64.dylib
Simulation allows us to quickly build intuition, and assess the performance of the data / model / trading strategy (backtest).
Let's simulate! In console:
python pdr_backend/predictoor/approach3/runtrade.py
What runtrade.py
does:
- Set simulation parameters.
- Grab historical price data from exchanges and stores in
csvs/
dir. It re-uses any previously saved data. - Run through many 5min epochs. At each epoch:
- Build a model
- Predict up/down
- Trade.
- (It logs this all to screen, and to
out*.txt
.)
- Plot total profit versus time.
The baseline settings use a linear model inputting prices of the previous 10 epochs as inputs, a simulated 0% trading fee, and a trading strategy of "buy if predict up; sell 5min later". You can play with different values in runtrade.py.
Profit isn't guaranteed: fees, slippage and more eats into them. Model accuracy makes a huge difference too.
(This README has more info about the simulator's code structure.)
Predictoor contracts run on Oasis Sapphire testnet and mainnet. Sapphire is a privacy-preserving EVM-compatible L1 chain.
Let's get our bot running on testnet first.
First, tokens! You need (fake) ROSE to pay for gas, and (fake) OCEAN to stake and earn. Get them here.
Then, copy & paste your private key as an envvar. In console:
export PRIVATE_KEY=<YOUR_PRIVATE_KEY>
Now, set other envvars. In console:
#other envvars for testnet and mainnet
export ADDRESS_FILE="${HOME}/.ocean/ocean-contracts/artifacts/address.json"
export PAIR_FILTER=BTC/USDT
export TIMEFRAME_FILTER=5m
export SOURCE_FILTER=binance
#testnet-specific envvars
export RPC_URL=https://testnet.sapphire.oasis.dev
export SUBGRAPH_URL=https://v4.subgraph.sapphire-testnet.oceanprotocol.com/subgraphs/name/oceanprotocol/ocean-subgraph
export STAKE_TOKEN=0x973e69303259B0c2543a38665122b773D28405fB # (fake) OCEAN token address
export OWNER_ADDRS=0xe02a421dfc549336d47efee85699bd0a3da7d6ff # OPF deployer address
(envvars.md has details.)
Then, run a simple trading bot. In console:
python pdr_backend/trader/main.py
Your bot is running, congrats! Sit back and watch it in action.
(You can track at finer resolution by writing more logs to the code, or querying Predictoor subgraph.)
Time to make it real: let's get our bot running on Sapphire mainnet.
First, real tokens! Get ROSE via this guide and OCEAN via this guide.
Then, copy & paste your private key as an envvar. (You can skip this if it's same as testnet.) In console:
export PRIVATE_KEY=<YOUR_PRIVATE_KEY>
Now, set other envvars. In console:
#envvars for testnet and mainnet
#(can skip this, since same as testnet)
#mainnet-specific envvars
export RPC_URL=https://sapphire.oasis.io
export SUBGRAPH_URL=https://v4.subgraph.sapphire-mainnet.oceanprotocol.com/subgraphs/name/oceanprotocol/ocean-subgraph
export STAKE_TOKEN=0x39d22B78A7651A76Ffbde2aaAB5FD92666Aca520 # OCEAN token address
export OWNER_ADDRS=0x4ac2e51f9b1b0ca9e000dfe6032b24639b172703 # OPF deployer address
Then, run the bot. In console:
python pdr_backend/trader/main.py
This is where there's real $ at stake. Good luck!
Track performance, as in testnet.
You've gone through all the essential steps to earn $ by running a trader bot on mainnet.
The next sections describe how to go beyond, by optimizing the trading strategy and more.
Once you're familiar with the above, you can set your own trading strategy and optimize it for $. Here's how:
- Fork
pdr-backend
repo. - Change trader bot code as you wish, while iterating with simulation.
- Bring your trader bot to testnet then mainnet.
To help, here's the code structure of the bot:
- It runs
trader_agent.py::TraderAgent
found inpdr_backend/trader/
- It's configured by envvars and
trader_config.py::TraderConfig
To scale up compute or run without tying up your local machine, you can run bots remotely. Get started here.
To get extra-fast block iterations, you can run a local test network (with local bots). It does take a bit more up-front setup. Get started here.
You will lose money trading if your $ out exceeds your $ in. Do account for trading fees, order book slippage, cost of prediction feeds, and more. Everything you do is your responsibility, at your discretion. None of this repo is financial advice.