OANDA Trading Bot CLI
import logging
import os
import time
import pandas as pd
import numpy as np
from oandapyV20 import API
import oandapyV20.endpoints.orders as orders
import oandapyV20.endpoints.trades as trades
import oandapyV20.endpoints.pricing as pricing
from oandapyV20.endpoints.accounts import AccountSummary, AccountList
from oandapyV20.exceptions import V20Error
from decimal import Decimal, ROUND_HALF_UP
import json
import threading
from datetime import datetime, timedelta
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
class ImprovedOandaTradingBot:
def __init__(self, instruments=None, initial_balance=100000):
if instruments is None:
instruments = ["EUR_JPY"]
# Fixed take profit and stop loss in dollars
Created: | Last Updated:
Here's a step-by-step guide for using the OANDA Trading Bot CLI template:
Introduction
This template provides a CLI-based OANDA Trading Bot for monitoring and managing trading orders. It includes separated API calls for open P&L, take profit, and stop loss. The bot uses the OANDA API to execute trades, monitor positions, and manage orders for specified instruments.
Getting Started
- Click "Start with this Template" to begin using the OANDA Trading Bot CLI template in the Lazy Builder interface.
Initial Setup
Before running the bot, you need to set up your OANDA API token as an environment secret:
- Go to the Environment Secrets tab in the Lazy Builder.
- Add a new secret with the key
OANDA_API_TOKEN
. - To get your OANDA API token:
- Log in to your OANDA account
- Navigate to the "Manage API Access" section
- Generate a new API token or copy your existing one
- Paste your OANDA API token as the value for the
OANDA_API_TOKEN
secret.
Test the App
- Click the "Test" button in the Lazy Builder interface to deploy and run the OANDA Trading Bot.
Using the App
Once the app is running, it will automatically:
- Initialize the OANDA account
- Start separate threads for price fetching, P&L monitoring, and order management
- Execute trades based on the implemented strategy
- Monitor and update open positions
- Log important information and errors
The bot will continue running and managing trades until you stop it. You can monitor its activity through the logs provided in the Lazy CLI.
Customizing the Bot
To customize the bot's behavior, you can modify the following parameters in the ImprovedOandaTradingBot
class:
instruments
: List of trading instruments (default is["EUR_JPY"]
)initial_balance
: Initial account balance (default is 100000)take_profit
: Fixed take profit amount in dollars (default is $1)stop_loss
: Fixed stop loss amount in dollars (default is $20)position_size
: Size of each trading position (default is 2.5)atr_period
: Period for ATR calculation (default is 14)atr_multiplier_tp
: ATR multiplier for take profit (default is 2.0)atr_multiplier_sl
: ATR multiplier for stop loss (default is 1.0)
To change these parameters, modify the values in the __init__
method of the ImprovedOandaTradingBot
class.
Important Notes
- The bot uses the OANDA practice environment by default. To switch to a live account, change the
environment
parameter in theAPI
initialization to "live". - The bot currently supports trading EUR_JPY and XAU_USD instruments. To add more instruments, update the
validate_instruments
method. - The trading strategy is based on Stochastic Oscillator, RSI, and Volume indicators. You can modify the
execute_trade
method to implement your own trading logic. - Always monitor the bot's performance and adjust parameters as needed to manage risk effectively.
Remember that trading involves financial risk. Use this bot responsibly and consider testing thoroughly with a practice account before using it with real funds.