Python Forum
My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor
#1
Hi, I'm trying to make a bot for trading, I'm still very new to this. I use Meta Trader 5.

I tested the code to get a price on Jupyter Lab, and it worked fine. Then I copy pasted the same exact code to Visual Code Studio and getting an error, how come the same exact code works on one editor and not on the other editor ?

This is the code :
import MetaTrader5 as mt5
from datetime import datetime
symbol = 'XAUUSD.GK'
price = mt5.symbol_info_tick(symbol).ask
print (price)
On Jupyter It gave me the exact thing I want, 1973.035 , which is the price I want to check.

But on Visual Code Studio, it doesn't work and gave me this error message AttributeError: 'NoneType' object has no attribute 'ask'

I also have tried running this code on both Jupyter and Visual Code Studio :
from sysconfig import get_paths as gp; print(gp()['include'])

and it refers to the same folder path. C:\Users\Name\AppData\Local\Programs\Python\Python310\Include

Can anyone please tell how to fix it ?

-edit-

Problem solved.
I have to logout and use :
authorized=mt5.initialize(path="C:/Program Files/[THIS IS THE BROKER'S FOLDER APP]/terminal64.exe",login=accountnumber, server="demoserver",password="yourownpass")

It should use the terminal64.exe from the broker's folder app, and it has to be initialized first to use it.

Please close the thread. It's solved.
Reply
#2
Where do you connect to metatrader? Maybe this happens automatically when using metatrader in jupyter notebook. In python you have to call MetaTrader5.initialize().

Documentation is here:

https://www.mql5.com/en/docs/python_metatrader5
Reply
#3
(Nov-15-2023, 02:46 PM)deanhystad Wrote: Where do you connect to metatrader? Maybe this happens automatically when using metatrader in jupyter notebook. In python you have to call MetaTrader5.initialize().

Documentation is here:

https://www.mql5.com/en/docs/python_metatrader5

Yea it seems exactly like that. I have to disconnect first and reconnect using Visual Code Studio calling that MetaTrader5.initialize().

I still don't get it why my Python didn't work, but Jupyter worked, now that it works on Python, it also works on Jupyter. So Jupyter seems to connect better it's so weird running exact same code but works differently just because of the code editor.

===

I have another question though, why I cannot pip install metatrader5 using latest phyton (3.12.0) , I had to install older version (3.10.0), is the problem from Phyton 3.12.0 or from Meta Trader 5 ?
Reply
#4
Jupyter is weird in a lot of ways.

What happened when you tried to install MetaTrader5 with Python 3.12? There should be message.
Reply
#5
This is the error message, but once I downgraded to 3.10. I could install it.
ERROR: Could not find a version that satisfies the requirement MetaTrader5 (from versions: none)
ERROR: No matching distribution found for MetaTrader5
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm trying to import a dataset into a Jupyter Notebook Hisilat03 0 750 Mar-24-2025, 09:26 AM
Last Post: Hisilat03
  I am getting an IndentError on my python code in VS Code and i dont know why jcardenas1980 11 4,316 Mar-22-2025, 09:49 AM
Last Post: Pedroski55
  Jupyter Notebook cell ? flaviu2 6 5,023 Feb-10-2025, 09:39 AM
Last Post: mysqlguider
  Visual Studio Code help aaronrousch 4 3,855 Jan-25-2025, 05:55 AM
Last Post: ndc85430
  I cannot create a virtual environment on visual studio code using python Willem_Aucamp316 2 3,521 Nov-27-2024, 02:20 PM
Last Post: menator01
  Code Completion in Visual Studio for External Libraries mjakov 0 606 Aug-25-2024, 02:48 PM
Last Post: mjakov
  import data (.csv) into Jupyter notebook oranstprotonme 2 1,249 Aug-14-2024, 07:08 PM
Last Post: oranstprotonme
  Merge Python code with Micro Python code? adzy 2 972 Jul-03-2024, 11:41 AM
Last Post: kkinder
  Troubleshooting Jupyter Notebook installation with Python using pip and a venv Drone4four 1 2,290 Jun-04-2024, 10:55 PM
Last Post: Drone4four
  Navigating file directories and paths inside Jupyter Notebook Mark17 5 8,665 Oct-29-2023, 12:40 PM
Last Post: Mark17

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020