edits
This commit is contained in:
parent
9b6d3f94ed
commit
4be322622e
@ -875,76 +875,83 @@ async def main():
|
|||||||
parser.add_argument('--demo', action='store_true', help='Run in demo mode (no real trading)')
|
parser.add_argument('--demo', action='store_true', help='Run in demo mode (no real trading)')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# Initialize exchange
|
# Initialize exchange with async support
|
||||||
exchange = ccxt.mexc({
|
exchange_id = 'mexc'
|
||||||
|
exchange_class = getattr(ccxt.async_support, exchange_id)
|
||||||
|
exchange = exchange_class({
|
||||||
'apiKey': MEXC_API_KEY,
|
'apiKey': MEXC_API_KEY,
|
||||||
'secret': MEXC_SECRET_KEY,
|
'secret': MEXC_SECRET_KEY,
|
||||||
'enableRateLimit': True,
|
'enableRateLimit': True,
|
||||||
})
|
})
|
||||||
|
|
||||||
# Create environment
|
try:
|
||||||
env = TradingEnvironment(
|
# Create environment
|
||||||
exchange=exchange,
|
env = TradingEnvironment(
|
||||||
symbol="ETH/USDT",
|
exchange=exchange,
|
||||||
timeframe="1m",
|
symbol="ETH/USDT",
|
||||||
leverage=MAX_LEVERAGE,
|
timeframe="1m",
|
||||||
initial_balance=INITIAL_BALANCE,
|
leverage=MAX_LEVERAGE,
|
||||||
is_demo=args.demo or args.mode != 'live' # Only trade for real in live mode
|
initial_balance=INITIAL_BALANCE,
|
||||||
)
|
is_demo=args.demo or args.mode != 'live'
|
||||||
|
)
|
||||||
# Fetch initial data
|
|
||||||
await env.fetch_initial_data()
|
|
||||||
|
|
||||||
# Create agent
|
|
||||||
agent = Agent(state_size=STATE_SIZE, action_size=env.action_space)
|
|
||||||
|
|
||||||
# Try to load existing model
|
|
||||||
model_loaded = agent.load()
|
|
||||||
if not model_loaded and args.mode in ['eval', 'live']:
|
|
||||||
logger.warning("No pre-trained model found. Consider training first!")
|
|
||||||
|
|
||||||
if args.mode == 'train':
|
|
||||||
# Training mode
|
|
||||||
logger.info("Starting training mode")
|
|
||||||
await train_agent(agent, env, num_episodes=args.episodes)
|
|
||||||
|
|
||||||
elif args.mode == 'eval':
|
# Fetch initial data
|
||||||
# Evaluation mode
|
await env.fetch_initial_data()
|
||||||
logger.info("Starting evaluation mode")
|
|
||||||
eval_reward, eval_profit, win_rate = evaluate_agent(agent, env, num_episodes=args.episodes)
|
|
||||||
|
|
||||||
elif args.mode == 'live':
|
# Create agent
|
||||||
# Live trading mode
|
agent = Agent(state_size=STATE_SIZE, action_size=env.action_space)
|
||||||
logger.info("Starting live trading mode with real-time data")
|
|
||||||
logger.info(f"Demo mode: {args.demo}")
|
|
||||||
|
|
||||||
# Live trading loop
|
# Try to load existing model
|
||||||
async for candle in get_live_prices("ETH/USDT", "1m"):
|
model_loaded = agent.load()
|
||||||
# Update environment with new data
|
if not model_loaded and args.mode in ['eval', 'live']:
|
||||||
await env._update_with_new_data(candle)
|
logger.warning("No pre-trained model found. Consider training first!")
|
||||||
|
|
||||||
|
if args.mode == 'train':
|
||||||
|
# Training mode
|
||||||
|
logger.info("Starting training mode")
|
||||||
|
await train_agent(agent, env, num_episodes=args.episodes)
|
||||||
|
|
||||||
# Only trade if we have enough data
|
elif args.mode == 'eval':
|
||||||
if len(env.data) >= env.window_size:
|
# Evaluation mode
|
||||||
# Get current state
|
logger.info("Starting evaluation mode")
|
||||||
state = env.get_state()
|
eval_reward, eval_profit, win_rate = evaluate_agent(agent, env, num_episodes=args.episodes)
|
||||||
|
|
||||||
|
elif args.mode == 'live':
|
||||||
|
# Live trading mode
|
||||||
|
logger.info("Starting live trading mode with real-time data")
|
||||||
|
logger.info(f"Demo mode: {args.demo}")
|
||||||
|
|
||||||
|
# Live trading loop
|
||||||
|
async for candle in get_live_prices("ETH/USDT", "1m"):
|
||||||
|
# Update environment with new data
|
||||||
|
await env._update_with_new_data(candle)
|
||||||
|
|
||||||
# Select action (no exploration in live trading)
|
# Only trade if we have enough data
|
||||||
action = agent.select_action(state, training=False)
|
if len(env.data) >= env.window_size:
|
||||||
|
# Get current state
|
||||||
# Convert action number to readable format
|
state = env.get_state()
|
||||||
action_names = ["HOLD", "BUY", "SELL", "CLOSE"]
|
|
||||||
logger.info(f"Price: ${candle['close']:.2f} | Action: {action_names[action]}")
|
# Select action (no exploration in live trading)
|
||||||
|
action = agent.select_action(state, training=False)
|
||||||
# Take action
|
|
||||||
_, reward, _ = env.step(action)
|
# Convert action number to readable format
|
||||||
|
action_names = ["HOLD", "BUY", "SELL", "CLOSE"]
|
||||||
# Print statistics
|
logger.info(f"Price: ${candle['close']:.2f} | Action: {action_names[action]}")
|
||||||
if len(env.trades) > 0:
|
|
||||||
wins = sum(1 for trade in env.trades if trade.get('pnl_percent', 0) > 0)
|
# Take action
|
||||||
win_rate = wins / len(env.trades) * 100
|
_, reward, _ = env.step(action)
|
||||||
total_pnl = sum(trade.get('pnl_dollar', 0) for trade in env.trades)
|
|
||||||
logger.info(f"Balance: ${env.balance:.2f} | Trades: {len(env.trades)} | "
|
# Print statistics
|
||||||
f"Win Rate: {win_rate:.1f}% | Total PnL: ${total_pnl:.2f}")
|
if len(env.trades) > 0:
|
||||||
|
wins = sum(1 for trade in env.trades if trade.get('pnl_percent', 0) > 0)
|
||||||
|
win_rate = wins / len(env.trades) * 100
|
||||||
|
total_pnl = sum(trade.get('pnl_dollar', 0) for trade in env.trades)
|
||||||
|
logger.info(f"Balance: ${env.balance:.2f} | Trades: {len(env.trades)} | "
|
||||||
|
f"Win Rate: {win_rate:.1f}% | Total PnL: ${total_pnl:.2f}")
|
||||||
|
|
||||||
|
finally:
|
||||||
|
# Clean up exchange connection
|
||||||
|
await exchange.close()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
try:
|
try:
|
||||||
|
@ -66,12 +66,21 @@ python main.py --mode eval --episodes 10
|
|||||||
### Live Trading Mode
|
### Live Trading Mode
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Demo mode (no real trades)
|
# Demo mode (simulated trading with real market data)
|
||||||
python main.py --mode live --demo
|
python main.py --mode live --demo
|
||||||
# Real trading
|
|
||||||
|
# Real trading (actual trades on MEXC)
|
||||||
python main.py --mode live
|
python main.py --mode live
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Demo mode simulates trading using real-time market data but does not execute actual trades. It still:
|
||||||
|
- Logs all trading decisions and performance metrics
|
||||||
|
- Updates the model based on market data (if in training mode)
|
||||||
|
- Displays real-time analytics and position information
|
||||||
|
- Calculates theoretical profits/losses
|
||||||
|
- Saves performance data to TensorBoard
|
||||||
|
|
||||||
|
This makes it perfect for testing strategies without financial risk.
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
|
4
crypto/gogo2/trading_bot.log
Normal file
4
crypto/gogo2/trading_bot.log
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
2025-03-10 10:31:19,097 - INFO - Fetching initial 60 candles for ETH/USDT...
|
||||||
|
2025-03-10 10:31:24,545 - ERROR - Error fetching initial data: object list can't be used in 'await' expression
|
||||||
|
2025-03-10 10:38:32,233 - INFO - Fetching initial 60 candles for ETH/USDT...
|
||||||
|
2025-03-10 10:38:38,055 - ERROR - Error fetching initial data: object list can't be used in 'await' expression
|
Loading…
x
Reference in New Issue
Block a user