print out the trades its taking on chart import ccxt
import pandas as pd
import os
from datetime import datetime, timedelta
import backtrader as bt
import matplotlib.pyplot as plt
TAKE_PROFIT_DAYS_LIST = [1, 3, 5] # Days to take profit
REENTRY_OPTIONS = [False, True] # Re-entry enabled or not
INITIAL_CASH = 10000.0
COMMISSION = 0.001 # 0.1%
====================== Data Fetching ======================
def fetch_ohlcv(exchange, symbol, timeframe, since, limit=1000):
"""
Fetch OHLCV data from the exchange.
"""
all_ohlcv = []
while True:
try:
data = exchange.fetch_ohlcv(symbol, timeframe=timeframe, since=since, limit=limit)
if not data:
break
all_ohlcv += data
since = data[-1][0] + 1 # Increment timestamp
if len(data) < limit:
break
except ccxt.NetworkError as e:
print(f"Network error: {e}. Retrying...")
continue
except ccxt.ExchangeError as e:
print(f"Exchange error: {e}. Skipping...")
break
return all_ohlcv
def process_and_save_data():
"""
Fetch and save historical data for each token and timeframe.
"""
exchange = ccxt.binance()
for symbol, announcement_date in TOKENS.items():
for timeframe in TIMEFRAMES:
print(f"Fetching {timeframe} data for {symbol}...")
# Convert announcement_date to datetime object
announcement_datetime = datetime.strptime(announcement_date, "%Y-%m-%d")
# Fetch data starting 7 days before announcement to 7 days after
start_datetime = announcement_datetime - timedelta(days=7)
end_datetime = announcement_datetime + timedelta(days=7)
since = int(exchange.parse8601(start_datetime.isoformat() + 'T00:00:00Z'))
ohlcv = fetch_ohlcv(exchange, symbol, timeframe, since)
if not ohlcv:
print(f"No data fetched for {symbol} on timeframe {timeframe}. Skipping...")
continue
# Convert to DataFrame
df = pd.DataFrame(ohlcv, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
df['datetime'] = pd.to_datetime(df['timestamp'], unit='ms')
df.set_index('datetime', inplace=True)
df.drop('timestamp', axis=1, inplace=True)
# Filter data within the desired range
df = df.loc[start_datetime:end_datetime]
# Check if DataFrame is empty after filtering
if df.empty:
print(f"DataFrame for {symbol} on {timeframe} is empty after filtering. Skipping...")
continue
# Ensure expected columns are present
expected_columns = ['open', 'high', 'low', 'close', 'volume']
if not all(column in df.columns for column in expected_columns):
print(f"CSV file {symbol} on {timeframe} is missing required columns. Skipping...")
continue
# Create directory if not exists
token_dir = os.path.join(DATA_DIR, symbol.replace('/', '-'))
os.makedirs(token_dir, exist_ok=True)
# Save to CSV without the index name
file_path = os.path.join(token_dir, f"{timeframe}.csv")
df.to_csv(file_path, index=True, header=True)
print(f"Saved data to {file_path}")
class MemeTokenStrategy(bt.Strategy):
params = (
('take_profit_days', 1),
('reentry', False),
('ma_period', 5), # Period for Moving Average (if needed for dynamic entry)
('printlog', False),
)
code
def log(self, txt, dt=None):
''' Logging function '''
if self.params.printlog:
dt = dt or self.datas[0].datetime.datetime(0)
print(f'{dt} - {txt}')
def __init__(self):
# Indicators
self.dataclose = self.datas[0].close
self.order = None
self.entry_price = None
self.entry_time = None
self.take_profit_time = None
self.stop_loss_triggered = False
# For stop-loss on 3-minute candle, track the last 3 closes
self.close_history = []
def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
# Order is submitted/accepted - no action needed
return
if order.status in [order.Completed]:
if order.isbuy():
self.log(f'BUY EXECUTED, Price: {order.executed.price}')
self.entry_price = order.executed.price
self.entry_time = self.datas[0].datetime.datetime(0)
self.take_profit_time = self.entry_time + timedelta(days=self.params.take_profit_days)
elif order.issell():
self.log(f'SELL EXECUTED, Price: {order.executed.price}')
self.order = None
elif order.status in [order.Canceled, order.Margin, order.Rejected]:
self.log('Order Canceled/Margin/Rejected')
self.order = None
def notify_trade(self, trade):
if not trade.isclosed:
return
self.log(f'TRADE PROFIT, GROSS {trade.pnl}, NET {trade.pnlcomm}')
def next(self):
dt = self.datas[0].datetime.datetime(0)
# Update close history
self.close_history.append(self.dataclose[0])
if len(self.close_history) > 3:
self.close_history.pop(0)
# Debug: Print last three closes
if len(self.close_history) == 3:
self.log(f'Last three closes: {self.close_history}')
# Check if take profit time has been reached
if self.entry_time and dt >= self.take_profit_time and self.position:
self.log('TAKE PROFIT TIME REACHED, SELL CREATE')
self.order = self.sell(size=5000)
return
if not self.position:
if self.stop_loss_triggered and self.params.reentry:
# Re-entry condition: price recovers above entry price within 24 hours
if self.dataclose[0] > self.entry_price:
time_since_entry = dt - self.entry_time
if time_since_entry <= timedelta(hours=24):
self.log('RE-ENTERING, BUY CREATE')
self.order = self.buy(size=5000)
self.stop_loss_triggered = False
return
else:
if not self.entry_time:
# Initial entry: Buy at the first candle after announcement
self.log('BUY CREATE at {}'.format(self.dataclose[0]))
self.order = self.buy(size=5000)
else:
# Manage the open position
if len(self.close_history) == 3:
three_min_close = min(self.close_history)
if three_min_close < self.entry_price:
self.log('STOP LOSS HIT, SELL CREATE')
self.order = self.sell(size=5000)
self.stop_loss_triggered = True
def stop(self):
self.log(f'(Take Profit Days {self.params.take_profit_days}) Ending Value %.2f' % (self.broker.getvalue()), dt=None)
====================== Backtesting Function ======================
def run_backtest(symbol, timeframe, take_profit_days, reentry):
"""
Run a single backtest scenario using PandasData.
"""
cerebro = bt.Cerebro()
code
# Add strategy
cerebro.addstrategy(
MemeTokenStrategy,
take_profit_days=take_profit_days,
reentry=reentry,
printlog=False
)
# Load data
token_dir = os.path.join(DATA_DIR, symbol.replace('/', '-'))
data_path = os.path.join(token_dir, f"{timeframe}.csv")
if not os.path.exists(data_path):
print(f"Data file {data_path} not found. Skipping...")
return None
# Read CSV into DataFrame
try:
df = pd.read_csv(data_path, parse_dates=['datetime'], index_col='datetime')
except Exception as e:
print(f"Error reading CSV for {symbol} on {timeframe}: {e}")
return None
# Ensure columns are ordered correctly
expected_columns = ['open', 'high', 'low', 'close', 'volume']
if not all(column in df.columns for column in expected_columns):
print(f"CSV file {data_path} is missing required columns.")
return None
# Check if DataFrame is empty
if df.empty:
print(f"DataFrame for {symbol} on {timeframe} is empty after filtering. Skipping...")
return None
# Debug: Print first few rows
print(f"First 5 rows of the DataFrame for {symbol} on {timeframe}:")
print(df.head())
# Create Backtrader data feed from DataFrame
data = bt.feeds.PandasData(
dataname=df,
open='open',
high='high',
low='low',
close='close',
volume='volume',
timeframe=bt.TimeFrame.Minutes,
compression=1,
)
cerebro.adddata(data)
# Set initial cash
cerebro.broker.setcash(INITIAL_CASH)
# Set commission
cerebro.broker.setcommission(commission=COMMISSION)
# Run backtest
try:
strategies = cerebro.run()
final_value = cerebro.broker.getvalue()
return final_value
except Exception as e:
print(f"Error running backtest for {symbol} on {timeframe}: {e}")
return None
====================== Main Execution ======================
def main():
# Step 1: Fetch and save data
print("=== Fetching Historical Data ===")
process_and_save_data()
print("Data fetching complete.\n")
code
# Step 2: Run backtests
print("=== Running Backtests ===")
results = []
for symbol in TOKENS.keys():
for take_profit_days in TAKE_PROFIT_DAYS_LIST:
for reentry in REENTRY_OPTIONS:
scenario = f"Token: {symbol}, Take Profit: {take_profit_days} day(s), Re-entry: {'ON' if reentry else 'OFF'}"
print(f"Running Backtest - {scenario}")
final_value = run_backtest(symbol, '1m', take_profit_days, reentry)
if final_value is not None:
results.append({
'Token': symbol,
'Take_Profit_Days': take_profit_days,
'Reentry': reentry,
'Final_Portfolio_Value': final_value
})
print(f"Final Portfolio Value: {final_value:.2f}\n")
else:
print(f"Backtest for {scenario} failed.\n")
# Step 3: Analyze results
if results:
df_results = pd.DataFrame(results)
print("=== Backtest Results ===")
print(df_results)
# Optionally, save results to CSV
df_results.to_csv('backtest_results.csv', index=False)
print("\nResults saved to backtest_results.csv")
# Visualize results using a pivot table
try:
pivot = df_results.pivot_table(index=['Token', 'Take_Profit_Days'], columns='Reentry', values='Final_Portfolio_Value')
pivot.plot(kind='bar', figsize=(10, 6))
plt.title('Backtest Final Portfolio Values')
plt.ylabel('Final Portfolio Value (USD)')
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig('backtest_results.png')
plt.show()
print("Results plot saved to backtest_results.png")
except Exception as e:
print(f"Error plotting results: {e}")
else:
print("No results to display.")
if name == "main":
main()
To print out the trades your strategy is executing and visualize them on a chart, you'll need to make a few modifications to your existing script. Here's a comprehensive guide to help you achieve this:
Enable Logging of Trades to the Console
Modify the Strategy to Collect Trades
Plot Trades on the Chart
Let's go through each step in detail.
1. Enable Logging of Trades to the Console
Currently, your strategy's logging is controlled by the printlog parameter, which is set to False by default. To print trade executions to the console, you'll need to set this parameter to True when adding the strategy to Cerebro.
Modification Steps:
Locate the cerebro.addstrategy() call in the run_backtest function.
Set printlog=True to enable logging.
Updated run_backtest Function:
python
def run_backtest(symbol, timeframe, take_profit_days, reentry):
"""
Run a single backtest scenario using PandasData.
"""
cerebro = bt.Cerebro()
# Add strategy with printlog=True to enable console logging
cerebro.addstrategy(
MemeTokenStrategy,
take_profit_days=take_profit_days,
reentry=reentry,
printlog=True # Enable logging
)
# ... [rest of the function remains unchanged]
2. Modify the Strategy to Collect Trades
To have a structured list of trades that you can print or further analyze, it's beneficial to collect trade details within the strategy.
Modification Steps:
Add a list to store trade details in the __init__ method.
Append trade information in the notify_trade method.
Updated MemeTokenStrategy Class:
python
class MemeTokenStrategy(bt.Strategy):
params = (
('take_profit_days', 1),
('reentry', False),
('ma_period', 5), # Period for Moving Average (if needed for dynamic entry)
('printlog', False),
)
def log(self, txt, dt=None):
''' Logging function '''
if self.params.printlog:
dt = dt or self.datas[0].datetime.datetime(0)
print(f'{dt} - {txt}')
def __init__(self):
# Indicators
self.dataclose = self.datas[0].close
self.order = None
self.entry_price = None
self.entry_time = None
self.take_profit_time = None
self.stop_loss_triggered = False
# For stop-loss on 3-minute candle, track the last 3 closes
self.close_history = []
# Collect trades
self.trades = []
def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
# Order is submitted/accepted - no action needed
return
if order.status in [order.Completed]:
if order.isbuy():
self.log(f'BUY EXECUTED, Price: {order.executed.price}')
self.entry_price = order.executed.price
self.entry_time = self.datas[0].datetime.datetime(0)
self.take_profit_time = self.entry_time + timedelta(days=self.params.take_profit_days)
# Record the buy trade
self.trades.append({
'type': 'BUY',
'price': order.executed.price,
'datetime': self.entry_time
})
elif order.issell():
self.log(f'SELL EXECUTED, Price: {order.executed.price}')
# Record the sell trade
self.trades.append({
'type': 'SELL',
'price': order.executed.price,
'datetime': self.datas[0].datetime.datetime(0)
})
self.order = None
elif order.status in [order.Canceled, order.Margin, order.Rejected]:
self.log('Order Canceled/Margin/Rejected')
self.order = None
def notify_trade(self, trade):
if not trade.isclosed:
return
self.log(f'TRADE PROFIT, GROSS {trade.pnl}, NET {trade.pnlcomm}')
# ... [rest of the class remains unchanged]
3. Plot Trades on the Chart
Backtrader's plotting functionality can automatically mark buy and sell orders on the chart. To visualize the trades, you'll need to invoke the cerebro.plot() method after running the backtest.
Modification Steps:
After running the backtest in the run_backtest function, call cerebro.plot() to generate the plot.
Optionally, collect and display the list of trades after the backtest.
Updated run_backtest Function:
python
def run_backtest(symbol, timeframe, take_profit_days, reentry):
"""
Run a single backtest scenario using PandasData.
"""
cerebro = bt.Cerebro()
# Add strategy with printlog=True to enable console logging
cerebro.addstrategy(
MemeTokenStrategy,
take_profit_days=take_profit_days,
reentry=reentry,
printlog=True # Enable logging
)
# Load data
token_dir = os.path.join(DATA_DIR, symbol.replace('/', '-'))
data_path = os.path.join(token_dir, f"{timeframe}.csv")
if not os.path.exists(data_path):
print(f"Data file {data_path} not found. Skipping...")
return None
# Read CSV into DataFrame
try:
df = pd.read_csv(data_path, parse_dates=['datetime'], index_col='datetime')
except Exception as e:
print(f"Error reading CSV for {symbol} on {timeframe}: {e}")
return None
# ... [rest of the data loading remains unchanged]
# Create Backtrader data feed from DataFrame
data = bt.feeds.PandasData(
dataname=df,
open='open',
high='high',
low='low',
close='close',
volume='volume',
timeframe=bt.TimeFrame.Minutes,
compression=1,
)
cerebro.adddata(data)
# Set initial cash
cerebro.broker.setcash(INITIAL_CASH)
# Set commission
cerebro.broker.setcommission(commission=COMMISSION)
# Run backtest
try:
strategies = cerebro.run()
final_value = cerebro.broker.getvalue()
# Plot the trades on the chart
cerebro.plot(style='candlestick')
# Retrieve and print trades
strategy = strategies[0]
if hasattr(strategy, 'trades'):
print("\n--- Executed Trades ---")
for trade in strategy.trades:
print(f"{trade['datetime']} - {trade['type']} at {trade['price']}")
else:
print("No trades were executed.")
return final_value
except Exception as e:
print(f"Error running backtest for {symbol} on {timeframe}: {e}")
return None
4. (Optional) Customize the Plot for Better Visualization
You can further customize the plot to enhance the visualization of trades, such as adding markers or adjusting the plot style.
Example: Adding Trade Markers
Backtrader automatically adds markers for buy and sell orders, but you can enhance them by specifying the markers and their sizes.
# Run backtest
try:
strategies = cerebro.run()
final_value = cerebro.broker.getvalue()
# Plot the trades on the chart with customized markers
cerebro.plot(style='candlestick', plottrades=True, trademarkers=True, marker='o', markersize=10)
# Retrieve and print trades
strategy = strategies[0]
if hasattr(strategy, 'trades'):
print("\n--- Executed Trades ---")
for trade in strategy.trades:
print(f"{trade['datetime']} - {trade['type']} at {trade['price']}")
else:
print("No trades were executed.")
return final_value
except Exception as e:
print(f"Error running backtest for {symbol} on {timeframe}: {e}")
return None
5. Final Adjustments in the main Function
Ensure that all backtests run sequentially and plots are displayed appropriately. Depending on your environment, plotting multiple backtests might open multiple windows. If you're running multiple backtests, consider saving plots instead of displaying them interactively.
Example: Saving Plots Instead of Displaying
Modify the run_backtest function to save plots instead of displaying them:
python
import os
def run_backtest(symbol, timeframe, take_profit_days, reentry):
# ... [previous code]
try:
strategies = cerebro.run()
final_value = cerebro.broker.getvalue()
# Create a unique filename for the plot
plot_filename = f"plot_{symbol.replace('/', '_')}_{timeframe}_TP{take_profit_days}_Reentry{int(reentry)}.png"
# Save the plot to a file
cerebro.plot(style='candlestick', plottrades=True, trademarkers=True, marker='o', markersize=10, saveplot=True, plotfile=plot_filename)
# Alternatively, using matplotlib directly:
# fig = cerebro.plot(style='candlestick')
# fig[0][0].savefig(plot_filename)
print(f"Plot saved to {plot_filename}")
# Retrieve and print trades
strategy = strategies[0]
if hasattr(strategy, 'trades'):
print("\n--- Executed Trades ---")
for trade in strategy.trades:
print(f"{trade['datetime']} - {trade['type']} at {trade['price']}")
else:
print("No trades were executed.")
return final_value
except Exception as e:
print(f"Error running backtest for {symbol} on {timeframe}: {e}")
return None
Note: Backtrader's cerebro.plot() does not have a saveplot parameter by default. To save plots, you might need to handle it with Matplotlib directly as shown in the commented lines.
6. Running the Improved Script
After applying the above modifications, running your script will:
Print trade executions to the console with timestamps, trade types (BUY/SELL), and prices.
Plot the trading chart with buy and sell markers indicating where trades were executed.
Save plots (if you choose to implement the saving functionality) for later review.
Sample Console Output:
code
=== Fetching Historical Data ===
Fetching 1m data for ACT/USDT...
Saved data to data/ACT-USDT/1m.csv
Fetching 3m data for ACT/USDT...
Saved data to data/ACT-USDT/3m.csv
Fetching 1m data for PNUT/USDT...
Saved data to data/PNUT-USDT/1m.csv
Fetching 3m data for PNUT/USDT...
Saved data to data/PNUT-USDT/3m.csv
Data fetching complete.
=== Running Backtests ===
Running Backtest - Token: ACT/USDT, Take Profit: 1 day(s), Re-entry: OFF
2024-11-04 00:00:00 - BUY CREATE at 1.23
2024-11-04 00:05:00 - BUY EXECUTED, Price: 1.23
2024-11-05 00:00:00 - TAKE PROFIT TIME REACHED, SELL CREATE
2024-11-05 00:00:00 - SELL EXECUTED, Price: 1.30
--- Executed Trades ---
2024-11-04 00:05:00 - BUY at 1.23
2024-11-05 00:00:00 - SELL at 1.30
Final Portfolio Value: 10056.78
# ... Similar output for other backtests
Sample Plot:
Your plot will display candlestick charts with buy markers (e.g., green arrows) and sell markers (e.g., red arrows) indicating where trades occurred.
(Note: This is a sample image. Your actual plot will reflect your specific trades.)
7. Additional Recommendations
Ensure Data Quality: Verify that the fetched OHLCV data is accurate and covers the necessary timeframe around the announcement dates.
Backtest Multiple Scenarios: Experiment with different take_profit_days and reentry options to observe how they affect the trading performance.
Optimize Strategy Parameters: Consider implementing parameter optimization to find the most profitable settings for your strategy.
Handle Exceptions Gracefully: Enhance error handling to manage unexpected scenarios, such as missing data or network issues during data fetching.
By following these steps, you should be able to print out the trades your strategy executes and visualize them on charts, providing valuable insights into your trading strategy's performance.
If you encounter any issues or have further questions, feel free to ask!