Once again thank u Kelvinhand sir for pointing out to the right direction.
Here the article: https://www.mail-archive.com/[email protected]/msg51063.html
1. data_import.py:this allows you to execute all AFL files in a directory (like the sample in the docs); it runs an Exploration on the last 30 days of data as well as a full Backtest on all of the data, saves both sets of results, and converts the Explore results to one HTML file.
PHP:
# Code adapted from TJ's sample in documentation by [email protected]
# File revision: $Revision: 203 $
import os
from win32com.client import Dispatch
# START MODIFY TO FIT YOUR SETUP
# These pairs (tuples) contain a data folder and a format file which defines
# how the data is stored
data_folders = (
("C:\\ua\\Files\\ForExport", "mycsinew.format"),
("C:\\ua\\Files\\Cash", "mycsinew.format")
)
# END MODIFY TO FIT YOUR SETUP
# Create AmiBroker object
ab = Dispatch("Broker.Application")
for (data_folder,format_file) in data_folders:
for file in os.listdir(data_folder):
if file[-3:].lower() == "csv":
print "Importing:", file, "using:", format_file
ab.Import(0, data_folder + "\\" + file, format_file)
ab.SaveDatabase()
PHP:
# Script modified and ported to Python from TJ's sample in the documentation by [email protected]
# File revision: $Revision: 205 $
import os
from datetime import date
from win32com.client import Dispatch
# START MODIFY TO FIT YOUR SETUP
AFLFolder = "C:\\Program Files (x86)\\AmiBroker\\Formulas\\Custom\\Strategies\\_Production"
ResultFolder = "C:\\Home\\Peter\\Documents\\Work\\Trading\\AmiBroker\\_Production"
# END MODIFY TO FIT YOUR SETUP
# Create AmiBroker object and get Analysis object
ab = Dispatch("Broker.Application")
aa = ab.Analysis
# set apply to and range
aa.ClearFilters()
# MODIFY FOR YOUR SETUP: this runs against watchlist #8. Note the VBScript/JScript
# syntax shown in the docs doesn't work in Python.
aa.Filter(0, "watchlist", 8)
aa.ApplyTo = 2 # use filters
today = date.today()
# Open the HTML file we're going to write to and print the header
htmlfile = open(ResultFolder + "\\DailySignals.html", "w")
htmlfile.write("<html><head><title>Daily Signals</title></head><body><h1>Daily Signals: "+
str(today)+"</h1><hr/>\n")
# Enumerate files. Note: it's my understanding Python prefers to work with
# directories separated with the forward slash ('/') character. However,
# since these paths are being passed over to AB, I'd prefer to just use the
# backslash ('\\'). This seems to work OK.
all_files = os.listdir(AFLFolder)
for file in all_files:
fileparts = file.split(".")
if len(fileparts) >= 2 and fileparts[1].lower() == "afl":
basename = fileparts[0]
if (aa.LoadFormula(AFLFolder + "\\" + file)):
print "Successfully loaded AFL script:", file
aa.RangeMode = 2 # last n days mode
aa.RangeN = 30 # last 30 days
# Run exploration first for signals
aa.Explore()
resultFile = ResultFolder + "\\" + basename + "_explore.csv"
print "Explore file : " + resultFile
aa.Export(resultFile)
# Read the CSV file we just wrote and turn into HTML table.
# No lectures about mixing data with logic please, yes, I know
# how to use templating engines, not warranted here. :-)
with open(resultFile) as csv:
first = True
htmlfile.write("<h2>"+file+"</h2><table border=1>\n")
for line in csv:
htmlfile.write("<tr>\n")
fields = line.split(',')
for field in fields:
if first:
htmlfile.write("<th bgcolor=eeeeee>"+field+"</th>")
else:
htmlfile.write("<td>"+field+" </td>")
htmlfile.write("\n</tr>\n")
first = False
htmlfile.write("</table><p/>\n")
# Full backtest, for verification and record-keeping
aa.RangeMode = 0 # all data
aa.Backtest(0) # IMPORTANT: you need to specify parameter 0 to get PORTFOLIO backtest
resultFile = ResultFolder + "\\" + basename + "_backtest.csv"
print "Backtest file: " + resultFile
aa.Export(resultFile)
# Write footer and close file
htmlfile.write("</body></html>\n")
htmlfile.close()
<!-- neodl5.grp.bf1.yahoo.com Mon Dec 29 02:15:34 PST 2014 -->
Last edited: