F2011B / ami2py

Python Package for reading an amibroker database
MIT License
20 stars 6 forks source link

Cannot save data by append_symbole_entry function? #6

Open technqvi opened 1 year ago

technqvi commented 1 year ago

Hi

I try to write data from CSV file to a database as an example. https://github.com/F2011B/ami2py#examples

It seems that there is no append_data_to_symbol method in this package but I use append_symbole_entry method instead. I assume both are the same.

However, Data from CSV.file didn't import into Amibroker Database by using this function. Beyond that it turns out I save a blank database instead as the figure below. After writing data to the database, it turns into an empty database. What is wrong with my code? image

This is my simple code. I have one thing different from your example I remove db.add_symbol("AAPL") because all of symbols are existing in Amibroker and I add db.ensure_symbol_folder(row['Ticker']) on for loop instead. I am not sure what is the right way to check where each symbol exists on the Database or not.

Please fix the problem so that I can import csv file to Amibroker using this ami2py.

import pandas as pd
from ami2py import AmiDataBase, SymbolEntry
#https://github.com/F2011B/ami2py

csv_path=r'D:\AB_DB\Script_ImportData\Fund_File\AB_Fund_World.csv'
ab_path='D:/AB_DB/AB_Fund_World'

db = AmiDataBase(ab_path)

df=pd.read_csv(csv_path)
df['Date']=pd.to_datetime(df['Date'],format='%Y-%m-%d')
df['Volume']=df['Volume'].astype(float)

#sample_symbols=['AAXJ','ASEA','TDEX.BK']
sample_symbols=['AAXJ']
df=df.query("Ticker in @sample_symbols")
df.reset_index(inplace=True,drop=True)
df['Day']=df['Date'].dt.day
df['Month']=df['Date'].dt.month
df['Year']=df['Date'].dt.year

df=df[['Ticker','Day','Month','Year','Open','High','Low','Close','Volume']]

df.info()
df

# Write By using append_symbole_entry
for index, row  in df.iterrows():
    db.ensure_symbol_folder(row['Ticker'])

    db.append_symbole_entry(
        row['Ticker'],    
        SymbolEntry(
            Close=row['Close'],
            High=row['High'],
            Low=row['Low'],
            Open=row['Open'],
            Volume=row['Volume'],
            Month=row.Month,
            Year=row.Year,
            Day=row.Day,
        ),
    )
    db.write_database()

This is my csv format.

image

Finally , I try to use db.append_symbol_data as below , it is nothing better. my data turn into empty database as well as the prior function.

symbolDict={}
for symbol in symbolList:

    dfTemp=df.query('Ticker==@symbol')
    dfTemp=dfTemp.drop(columns=['Ticker'])

    dict_data={}
    for col in dfTemp.columns:
      dict_data[col]= dfTemp[col].tolist()

    symbolDict[symbol]=dict_data

for key,item in symbolDict.items():
    print(key)
    print(item)

db.append_symbol_data(symbolDict)
db.write_database()
pymen commented 7 months ago

@technqvi i think i get same problem with writing to Amibroker db as you https://github.com/F2011B/ami2py/issues/8

Were you able to find some better ways to write to DB? Or any results with ami2py?

I get some success with MS SQL database, another way to write on windows side using OLE https://github.com/jawakow/Oanda2Ami/blob/master/Oanda2AmiGui.py