René's URL Explorer Experiment


Title: Compare_agent.py · Issue #6 · six519/PastebinPython · GitHub

Open Graph Title: Compare_agent.py · Issue #6 · six519/PastebinPython

X Title: Compare_agent.py · Issue #6 · six519/PastebinPython

Description: """ RL + MetaTrader5 trading bot template Train with historical data (PPO from stable-baselines3) Optionally execute trades via MetaTrader5 (set LIVE=True to enable) CAVEAT: This is an educational template. Backtest & paper-trade first. ...

Open Graph Description: """ RL + MetaTrader5 trading bot template Train with historical data (PPO from stable-baselines3) Optionally execute trades via MetaTrader5 (set LIVE=True to enable) CAVEAT: This is an educational ...

X Description: """ RL + MetaTrader5 trading bot template Train with historical data (PPO from stable-baselines3) Optionally execute trades via MetaTrader5 (set LIVE=True to enable) CAVEAT: This is ...

Opengraph URL: https://github.com/six519/PastebinPython/issues/6

X: @github

direct link

Domain: patch-diff.githubusercontent.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Compare_agent.py","articleBody":"\"\"\"\nRL + MetaTrader5 trading bot template\n- Train with historical data (PPO from stable-baselines3)\n- Optionally execute trades via MetaTrader5 (set LIVE=True to enable)\nCAVEAT: This is an educational template. Backtest \u0026 paper-trade first.\n\"\"\"\n\nimport time\nimport numpy as np\nimport pandas as pd\nimport gym\nfrom gym import spaces\nimport MetaTrader5 as mt5\nfrom stable_baselines3 import PPO\nfrom stable_baselines3.common.vec_env import DummyVecEnv\nfrom stable_baselines3.common.callbacks import CheckpointCallback\n\n# -------------------------\n# USER CONFIG\n# -------------------------\nSYMBOL = \"EURUSD\"\nTIMEFRAME = mt5.TIMEFRAME_M5     # 5 minute bars\nLOOKBACK = 50                   # observation window (bars)\nSTART_POS = 0                   # for historical fetch offset\nLOT_SIZE = 0.01                 # trade lot size\nLIVE = False                    # \u003c-- Set to True only after full testing (demo account first!)\nMODEL_PATH = \"ppo_mt5_model\"\nTRAIN_TIMESTEPS = 20000         # adjust as you like\n# -------------------------\n\n# -------------------------\n# Helper: connect to MT5\n# -------------------------\ndef mt5_connect():\n    if not mt5.initialize():\n        raise RuntimeError(f\"MT5 initialize() failed, error={mt5.last_error()}\")\n    info = mt5.terminal_info()\n    if info is None:\n        raise RuntimeError(\"Failed to get terminal info after initialize()\")\n    print(\"MT5 terminal initialized:\", info.product)\n    # Ensure symbol is available\n    if not mt5.symbol_select(SYMBOL, True):\n        raise RuntimeError(f\"Failed to select symbol {SYMBOL}\")\n    return True\n\ndef mt5_shutdown():\n    mt5.shutdown()\n\n# -------------------------\n# Get historical OHLCV\n# -------------------------\ndef fetch_bars(symbol, timeframe, n_bars):\n    # copy_rates_from_pos returns numpy array with fields: time, open, high, low, close, tick_volume, ...\n    rates = mt5.copy_rates_from_pos(symbol, timeframe, START_POS, n_bars)\n    if rates is None:\n        raise RuntimeError(f\"Failed to fetch rates for {symbol}: {mt5.last_error()}\")\n    df = pd.DataFrame(rates)\n    df['time'] = pd.to_datetime(df['time'], unit='s')\n    return df\n\n# -------------------------\n# Simple trading Gym env\n# -------------------------\nclass MT5TradingEnv(gym.Env):\n    \"\"\"\n    Observation: last LOOKBACK closes normalized + current position (0/1/-1)\n    Actions: 0=hold, 1=buy (long), 2=sell (short/close long)\n    Reward: change in account equity approximated by price moves * position\n    NOTE: Simplified; this is a research template, not production-ready.\n    \"\"\"\n    def __init__(self, df: pd.DataFrame, lookback=LOOKBACK):\n        super(MT5TradingEnv, self).__init__()\n        self.df = df.reset_index(drop=True)\n        self.lookback = lookback\n        self.ptr = lookback  # current index in df\n        self.position = 0    # -1 short, 0 flat, 1 long\n        self.entry_price = 0.0\n        # Observations: lookback closes (normalized) + position\n        self.observation_space = spaces.Box(low=-np.inf, high=np.inf, shape=(lookback + 1,), dtype=np.float32)\n        # Actions: hold(0), buy(1), sell(2)\n        self.action_space = spaces.Discrete(3)\n\n    def _get_obs(self):\n        closes = self.df.loc[self.ptr - self.lookback:self.ptr - 1, \"close\"].values.astype(np.float32)\n        # normalize closes by dividing by last close\n        norm = closes / (closes[-1] + 1e-9) - 1.0\n        obs = np.concatenate([norm, np.array([float(self.position)])], axis=0)\n        return obs\n\n    def reset(self):\n        self.ptr = self.lookback\n        self.position = 0\n        self.entry_price = 0.0\n        return self._get_obs()\n\n    def step(self, action):\n        done = False\n        reward = 0.0\n        price = float(self.df.loc[self.ptr, \"close\"])\n        # Action logic\n        if action == 1:  # buy\n            if self.position == 0:\n                self.position = 1\n                self.entry_price = price\n            elif self.position == -1:\n                # close short and go long\n                reward += (self.entry_price - price)  # profit from short\n                self.position = 1\n                self.entry_price = price\n        elif action == 2:  # sell\n            if self.position == 0:\n                self.position = -1\n                self.entry_price = price\n            elif self.position == 1:\n                reward += (price - self.entry_price)  # profit from long\n                self.position = -1\n                self.entry_price = price\n        # Move pointer\n        self.ptr += 1\n        if self.ptr \u003e= len(self.df):\n            done = True\n        else:\n            # reward can also be shaped by unrealized pnl:\n            next_price = float(self.df.loc[self.ptr, \"close\"])\n            unrealized = 0.0\n            if self.position == 1:\n                unrealized = next_price - self.entry_price\n            elif self.position == -1:\n                unrealized = self.entry_price - next_price\n            # small per-step reward = unrealized PnL scaled\n            reward += unrealized * 0.1\n\n        obs = self._get_obs() if not done else np.zeros(self.observation_space.shape, dtype=np.float32)\n        info = {\"ptr\": self.ptr}\n        return obs, float(reward), done, info\n\n# -------------------------\n# Order helpers\n# -------------------------\ndef send_order(symbol, action, lot=LOT_SIZE, deviation=20):\n    \"\"\"\n    action: 1=buy, 2=sell\n    This function sends a ORDER_TYPE_BUY / ORDER_TYPE_SELL market order.\n    Basic error checking included. For production you need more robust code.\n    \"\"\"\n    price = mt5.symbol_info_tick(symbol).ask if action == 1 else mt5.symbol_info_tick(symbol).bid\n    request = {\n        \"action\": mt5.TRADE_ACTION_DEAL,\n        \"symbol\": symbol,\n        \"volume\": float(lot),\n        \"type\": mt5.ORDER_TYPE_BUY if action == 1 else mt5.ORDER_TYPE_SELL,\n        \"price\": float(price),\n        \"deviation\": deviation,\n        \"magic\": 234000,\n        \"comment\": \"RL-bot\",\n        \"type_filling\": mt5.ORDER_FILLING_IOC,\n    }\n    result = mt5.order_send(request)\n    return result\n\n# -------------------------\n# Main: training flow\n# -------------------------\ndef train_agent():\n    mt5_connect()\n    # fetch historical bars\n    n_bars = 5000\n    df = fetch_bars(SYMBOL, TIMEFRAME, n_bars)\n    print(f\"Fetched {len(df)} bars for {SYMBOL}\")\n    # Create env\n    env = DummyVecEnv([lambda: MT5TradingEnv(df, lookback=LOOKBACK)])\n    # model\n    model = PPO(\"MlpPolicy\", env, verbose=1)\n    # save checkpoints\n    cb = CheckpointCallback(save_freq=5000, save_path=\"./logs/\", name_prefix=\"ppo_mt5\")\n    model.learn(total_timesteps=TRAIN_TIMESTEPS, callback=cb)\n    model.save(MODEL_PATH)\n    mt5_shutdown()\n    print(\"Training complete, model saved to\", MODEL_PATH)\n\n# -------------------------\n# Real-time execution loop (paper/live)\n# -------------------------\ndef run_live_loop(model_path=MODEL_PATH, poll_seconds=5):\n    mt5_connect()\n    model = PPO.load(model_path)\n    print(\"Loaded model:\", model_path)\n    # We'll maintain a small in-memory buffer of recent bars\n    n_history = LOOKBACK + 10\n    df = fetch_bars(SYMBOL, TIMEFRAME, n_history)\n    # pointer is at last bar\n    while True:\n        try:\n            latest = fetch_bars(SYMBOL, TIMEFRAME, 1)\n            if latest['time'].iloc[-1] \u003e df['time'].iloc[-1]:\n                # append new bar\n                df = pd.concat([df, latest]).reset_index(drop=True)\n                if len(df) \u003e n_history:\n                    df = df.iloc[-n_history:].reset_index(drop=True)\n                # Build an env instance for this single-step decision\n                env = MT5TradingEnv(df, lookback=LOOKBACK)\n                obs = env.reset()\n                action, _states = model.predict(obs, deterministic=True)\n                print(f\"[{pd.to_datetime('now')}] Action: {action} | Price: {df['close'].iloc[-1]}\")\n                # Send order if LIVE\n                if LIVE:\n                    if int(action) == 1:\n                        res = send_order(SYMBOL, 1)\n                        print(\"Order send result:\", res)\n                    elif int(action) == 2:\n                        res = send_order(SYMBOL, 2)\n                        print(\"Order send result:\", res)\n                else:\n                    # paper-trade: just log what would happen\n                    print(\"LIVE=False -\u003e paper trade logged only\")\n            else:\n                # no new bar yet\n                pass\n            time.sleep(poll_seconds)\n        except KeyboardInterrupt:\n            print(\"Stopping live loop (KeyboardInterrupt).\")\n            break\n        except Exception as e:\n            print(\"Exception in live loop:\", str(e))\n            time.sleep(5)\n    mt5_shutdown()\n\n# -------------------------\n# If run as script\n# -------------------------\nif __name__ == \"__main__\":\n    import argparse\n    parser = argparse.ArgumentParser()\n    parser.add_argument(\"--mode\", choices=[\"train\", \"run\"], default=\"train\")\n    args = parser.parse_args()\n    if args.mode == \"train\":\n        print(\"Starting training...\")\n        train_agent()\n    elif args.mode == \"run\":\n        print(\"Starting live/paper run loop...\")\n        run_live_loop()","author":{"url":"https://github.com/vicks4u","@type":"Person","name":"vicks4u"},"datePublished":"2025-09-16T01:33:48.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/6/PastebinPython/issues/6"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:faf28f0e-7f99-1187-ffd5-ac0559c0cebd
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idD3FC:1B8E26:9F3FCF6:CEDD49C:69763FB6
html-safe-nonce8690d524eee3a54443c64f3c2f8f1addea5dbb02a672dd6bbc20aa80d97592b4
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEM0ZDOjFCOEUyNjo5RjNGQ0Y2OkNFREQ0OUM6Njk3NjNGQjYiLCJ2aXNpdG9yX2lkIjoiNDM1MDAxNjI5NzMyOTExNTA2MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac9a8c04189011bd17de1f756c3fc1b3134b5e02789ee5f8e2f63df42297564a35
hovercard-subject-tagissue:3420004965
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/six519/PastebinPython/6/issue_layout
twitter:imagehttps://opengraph.githubassets.com/1d20a5fb68b7fa64eb3d4d226ba354dc5cf64a5991c80ac7caccd76c9e67bf4c/six519/PastebinPython/issues/6
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/1d20a5fb68b7fa64eb3d4d226ba354dc5cf64a5991c80ac7caccd76c9e67bf4c/six519/PastebinPython/issues/6
og:image:alt""" RL + MetaTrader5 trading bot template Train with historical data (PPO from stable-baselines3) Optionally execute trades via MetaTrader5 (set LIVE=True to enable) CAVEAT: This is an educational ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamevicks4u
hostnamegithub.com
expected-hostnamegithub.com
Nonec6814b4cc7afd45cd6e64525d0cff0e76dd802f315a5b0e55a7abda1d1d070d0
turbo-cache-controlno-preview
go-importgithub.com/six519/PastebinPython git https://github.com/six519/PastebinPython.git
octolytics-dimension-user_id483547
octolytics-dimension-user_loginsix519
octolytics-dimension-repository_id8210586
octolytics-dimension-repository_nwosix519/PastebinPython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id8210586
octolytics-dimension-repository_network_root_nwosix519/PastebinPython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release4ea235bfed58ef16c8a5642b3ac64b74f10c9f52
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://patch-diff.githubusercontent.com/six519/PastebinPython/issues/6#start-of-content
https://patch-diff.githubusercontent.com/
Sign in https://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2Fsix519%2FPastebinPython%2Fissues%2F6
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2Fsix519%2FPastebinPython%2Fissues%2F6
Sign up https://patch-diff.githubusercontent.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=six519%2FPastebinPython
Reloadhttps://patch-diff.githubusercontent.com/six519/PastebinPython/issues/6
Reloadhttps://patch-diff.githubusercontent.com/six519/PastebinPython/issues/6
Reloadhttps://patch-diff.githubusercontent.com/six519/PastebinPython/issues/6
six519 https://patch-diff.githubusercontent.com/six519
PastebinPythonhttps://patch-diff.githubusercontent.com/six519/PastebinPython
Notifications https://patch-diff.githubusercontent.com/login?return_to=%2Fsix519%2FPastebinPython
Fork 48 https://patch-diff.githubusercontent.com/login?return_to=%2Fsix519%2FPastebinPython
Star 90 https://patch-diff.githubusercontent.com/login?return_to=%2Fsix519%2FPastebinPython
Code https://patch-diff.githubusercontent.com/six519/PastebinPython
Issues 4 https://patch-diff.githubusercontent.com/six519/PastebinPython/issues
Pull requests 0 https://patch-diff.githubusercontent.com/six519/PastebinPython/pulls
Actions https://patch-diff.githubusercontent.com/six519/PastebinPython/actions
Projects 0 https://patch-diff.githubusercontent.com/six519/PastebinPython/projects
Wiki https://patch-diff.githubusercontent.com/six519/PastebinPython/wiki
Security 0 https://patch-diff.githubusercontent.com/six519/PastebinPython/security
Insights https://patch-diff.githubusercontent.com/six519/PastebinPython/pulse
Code https://patch-diff.githubusercontent.com/six519/PastebinPython
Issues https://patch-diff.githubusercontent.com/six519/PastebinPython/issues
Pull requests https://patch-diff.githubusercontent.com/six519/PastebinPython/pulls
Actions https://patch-diff.githubusercontent.com/six519/PastebinPython/actions
Projects https://patch-diff.githubusercontent.com/six519/PastebinPython/projects
Wiki https://patch-diff.githubusercontent.com/six519/PastebinPython/wiki
Security https://patch-diff.githubusercontent.com/six519/PastebinPython/security
Insights https://patch-diff.githubusercontent.com/six519/PastebinPython/pulse
New issuehttps://patch-diff.githubusercontent.com/login?return_to=https://github.com/six519/PastebinPython/issues/6
New issuehttps://patch-diff.githubusercontent.com/login?return_to=https://github.com/six519/PastebinPython/issues/6
Compare_agent.pyhttps://patch-diff.githubusercontent.com/six519/PastebinPython/issues/6#top
https://github.com/vicks4u
https://github.com/vicks4u
vicks4uhttps://github.com/vicks4u
on Sep 16, 2025https://github.com/six519/PastebinPython/issues/6#issue-3420004965
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.