116 lines
3.9 KiB
Python
116 lines
3.9 KiB
Python
from NN.environments.trading_env import TradingEnvironment
|
|
import logging
|
|
import numpy as np
|
|
import pandas as pd
|
|
import os
|
|
import sys
|
|
from datetime import datetime, timedelta
|
|
|
|
# Add the project root directory to the path
|
|
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
# Configure logging
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Create a mock data interface class
|
|
class MockDataInterface:
|
|
def __init__(self, symbol, timeframes):
|
|
self.symbol = symbol
|
|
self.timeframes = timeframes
|
|
self.dataframes = {}
|
|
|
|
# Create mock data for each timeframe
|
|
for tf in timeframes:
|
|
self.dataframes[tf] = self._create_mock_data(tf)
|
|
|
|
def _create_mock_data(self, timeframe):
|
|
# Generate timestamps
|
|
end_time = datetime.now()
|
|
if timeframe == '1m':
|
|
start_time = end_time - timedelta(minutes=1000)
|
|
freq = 'T' # minute frequency
|
|
elif timeframe == '5m':
|
|
start_time = end_time - timedelta(minutes=5000)
|
|
freq = '5T'
|
|
else: # '15m'
|
|
start_time = end_time - timedelta(minutes=15000)
|
|
freq = '15T'
|
|
|
|
dates = pd.date_range(start=start_time, end=end_time, freq=freq)
|
|
|
|
# Create price data with some random walk behavior
|
|
np.random.seed(42) # For reproducibility
|
|
price = 1000.0
|
|
prices = [price]
|
|
for _ in range(len(dates) - 1):
|
|
price = price * (1 + np.random.normal(0, 0.005)) # 0.5% daily volatility
|
|
prices.append(price)
|
|
|
|
# Calculate OHLCV data
|
|
df = pd.DataFrame(index=dates)
|
|
df['close'] = prices
|
|
df['open'] = df['close'].shift(1).fillna(df['close'].iloc[0] * 0.999)
|
|
df['high'] = df['close'] * (1 + abs(np.random.normal(0, 0.001, len(df))))
|
|
df['low'] = df['open'] * (1 - abs(np.random.normal(0, 0.001, len(df))))
|
|
df['volume'] = np.random.normal(1000, 100, len(df))
|
|
|
|
return df
|
|
|
|
# Create mock data interface
|
|
di = MockDataInterface('ETH/USDT', ['1m', '5m', '15m'])
|
|
|
|
# Create environment
|
|
env = TradingEnvironment(di, initial_balance=1000.0, max_position=0.1)
|
|
|
|
# Run multiple episodes to accumulate some trade history
|
|
for episode in range(3):
|
|
logger.info(f"Episode {episode+1}/3")
|
|
|
|
# Reset environment
|
|
observation = env.reset()
|
|
|
|
# Run episode
|
|
for step in range(100):
|
|
# Choose action: 0=Buy, 1=Sell, 2=Hold
|
|
# Use a more deliberate pattern to generate trades
|
|
if step % 10 == 0:
|
|
action = 0 # Buy
|
|
elif step % 10 == 5:
|
|
action = 1 # Sell
|
|
else:
|
|
action = 2 # Hold
|
|
|
|
# Take action
|
|
observation, reward, done, info = env.step(action)
|
|
|
|
# Print trade information if a trade was made
|
|
if 'trade_result' in info:
|
|
trade = info['trade_result']
|
|
print(f"\nTrade executed:")
|
|
print(f"Action: {['BUY', 'SELL', 'HOLD'][trade['action']]}")
|
|
print(f"Price: {trade['price']:.4f}")
|
|
print(f"Position change: {trade['prev_position']:.4f} -> {trade['new_position']:.4f}")
|
|
print(f"Entry price: {trade.get('entry_price', 0):.4f}")
|
|
if trade.get('realized_pnl', 0) != 0:
|
|
print(f"Realized PnL: {trade['realized_pnl']:.4f}")
|
|
print(f"Balance: {trade['balance_before']:.2f} -> {trade['balance_after']:.2f}")
|
|
|
|
# End episode if done
|
|
if done:
|
|
break
|
|
|
|
# Render environment with final state
|
|
print("\n\nFinal environment state:")
|
|
env.render()
|
|
|
|
# Print detailed information about the last 5 positions
|
|
positions = env.get_last_positions(5)
|
|
print("\nDetailed position history:")
|
|
for i, pos in enumerate(positions):
|
|
print(f"\nPosition {i+1}:")
|
|
for key, value in pos.items():
|
|
if isinstance(value, float):
|
|
print(f" {key}: {value:.4f}")
|
|
else:
|
|
print(f" {key}: {value}") |