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
  Navigating file directories and paths inside Jupyter Notebook Mark17 5 718 Oct-29-2023, 12:40 PM
Last Post: Mark17
  How to set PYTHONPATH in Visual Studio Code? aupres 5 4,238 Aug-15-2023, 03:51 PM
Last Post: snippsat
  how do I open two instances of visual studio code with the same folder? SuchUmami 3 894 Jun-26-2023, 09:40 AM
Last Post: snippsat
  Code works but doesn't give the right results colin_dent 2 726 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  Visual Studio Code NewPi 3 1,062 May-16-2023, 11:13 PM
Last Post: snippsat
  How to programmatically stop a program in Jupyter Notebook? Mark17 11 37,248 Feb-12-2023, 01:41 PM
Last Post: jp21in
Thumbs Up Python 3 Jupyter notebook ternary plot data nicholas 0 953 Jan-21-2023, 05:01 PM
Last Post: nicholas
  Code used to work 100%, now sometimes works! muzicman0 5 1,448 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  Graphs from Jupyter notebook to word Scott 0 890 Nov-28-2022, 06:16 AM
Last Post: Scott
  Visual Studio Code venv ibm_db error mesi1000 7 2,833 Nov-13-2022, 12:36 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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