How to use python to import csv file to Amibroker?

I came across to see ami2py (python package) as link below
https://github.com/F2011B/ami2py
In this link, it show how to simply import data to amibroker data.

I apply this code a bit , I read csv file to dataframe and code as example.
I can not do it. At the moment ,I open the issue to his github , you can read detail and my code as this link https://github.com/F2011B/ami2py/issues/6

Anyone can help me to fix this problem or any idea to write python code in order to import csv file to Amibroker database?
Please kindly show the sample code .

Thank you for any ideas.
Pongthorn

Import csv files from AFL - AFL Programming - AmiBroker Community Forum

see this.

1 Like

I try as per your advice. it didn't work. there is nothing to update to the database.

this is my code.

from win32com.client import Dispatch
import os

oab = Dispatch('Broker.Application')
file_path= "D:\PythonDev\MyQuantFinProject\Test-Yahoo_To_AB.csv"
format_path="D:\PythonDev\MyQuantFinProject\wizard.format"
oab.Import( 0, file_path, format_path )        

oab.SaveDatabase()

this is my csv file and format ( I copy it from C:\Program Files\AmiBroker\Formats and paste on the same place as csv file)

image

This csv can be imported to Amibroker perfectly by Wizard.
image

OLE (Windows) works with the first instance of AB, so make sure you are checking or using only one instance,and keep it open.

Add this line at the end

oab.RefreshAll()

This will update to current DB that is selected and in use.

For changing DB etc, there are more commands you can use in same format. oab.<command()>
AmiBroker Object Model

from win32com.client import Dispatch
import os
oab = Dispatch('Broker.Application')

file_path= "D:\PythonDev\MyQuantFinProject\AB-Python\Test-Yahoo_To_AB.csv"
format_path="D:\PythonDev\MyQuantFinProject\AB-Python\wizard.format"

if os.path.exists(file_path) and  os.path.exists(format_path):
 oab.Import( 0, file_path, format_path )        
 oab.SaveDatabase()
 oab.RefreshAll()   
else:
 print("not foud one of them")   

I have several databases, but below is the first database instance updated from CSV.
image

You may take a look here: Calling Amibroker from Python

2 Likes

Here are the steps to import a CSV file into Amibroker using Python:

import win32com.client

# Connect to Amibroker
ab = win32com.client.Dispatch("Broker.Application")
# Import the CSV file into Amibroker
ab.Import("C:\\path\\to\\file.csv", "database_name")
# Close the connection to Amibroker
ab = None

By following these steps, you can import CSV file into Amibroker using Python. Note that you will need to have Amibroker installed on your computer to use this method.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.