Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

master #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions metatrader/backtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

@author: samuraitaiga
'''
import logging
from __future__ import absolute_import, division
import os
from mt4 import get_mt4
from mt4 import DEFAULT_MT4_NAME
from __builtin__ import str
from metatrader.mt4 import get_mt4
from metatrader.mt4 import DEFAULT_MT4_NAME
from metatrader.report import BacktestReport
from metatrader.report import OptimizationReport


class BackTest(object):
"""
Expand All @@ -18,7 +20,7 @@ class BackTest(object):
symbol(string): currency symbol. e.g.: USDJPY
from_date(datetime.datetime): backtest from date
to_date(datetime.datetime): backtest to date
model(int): backtest model
model(int): backtest model
0: Every tick
1: Control points
2: Open prices only
Expand All @@ -27,6 +29,7 @@ class BackTest(object):
replace_report(bool): replace report flag. replace report is enabled if True

"""

def __init__(self, ea_name, param, symbol, period, from_date, to_date, model=0, spread=5, replace_repot=True):
self.ea_name = ea_name
self.param = param
Expand All @@ -51,8 +54,8 @@ def _create_conf(self, alias=DEFAULT_MT4_NAME):
Notes:
create config file(.conf) which is used parameter of terminal.exe
in %APPDATA%\\MetaQuotes\\Terminal\\<UUID>\\tester
file contents goes to

file contents goes to
TestExpert=SampleEA
TestExpertParameters=SampleEA.set
TestSymbol=USDJPY
Expand All @@ -79,8 +82,8 @@ def _create_conf(self, alias=DEFAULT_MT4_NAME):
fp.write('TestExpert=%s\n' % self.ea_name)
fp.write('TestExpertParameters=%s.set\n' % self.ea_name)
fp.write('TestSymbol=%s\n' % self.symbol)
fp.write('TestModel=%s\n' % self.model)
fp.write('TestPeriod=%s\n' % self.period)
fp.write('TestModel=%s\n' % self.model)
fp.write('TestSpread=%s\n' % self.spread)
fp.write('TestOptimization=%s\n' % str(self.optimization).lower())
fp.write('TestDateEnable=true\n')
Expand Down Expand Up @@ -110,9 +113,9 @@ def _create_param(self, alias=DEFAULT_MT4_NAME):
fp.write('%s,F=1\n' % k)
fp.write('%s,1=%s\n' % (k, value))
interval = values.pop('interval')
fp.write('%s,2=%s\n' % (k,interval))
fp.write('%s,2=%s\n' % (k, interval))
maximum = values.pop('max')
fp.write('%s,3=%s\n' % (k,maximum))
fp.write('%s,3=%s\n' % (k, maximum))
else:
# if this value won't be optimized, write unused dummy data for same format.
fp.write('%s,F=0\n' % k)
Expand All @@ -130,7 +133,6 @@ def _create_param(self, alias=DEFAULT_MT4_NAME):
fp.write('%s,2=0\n' % k)
fp.write('%s,3=0\n' % k)


def _get_conf_abs_path(self, alias=DEFAULT_MT4_NAME):
mt4 = get_mt4(alias=alias)
conf_file = os.path.join(mt4.appdata_path, 'tester', '%s.conf' % self.ea_name)
Expand All @@ -141,31 +143,28 @@ def run(self, alias=DEFAULT_MT4_NAME):
Notes:
run backtest
"""
from report import BacktestReport

self.optimization = False

self._prepare(alias=alias)
bt_conf = self._get_conf_abs_path(alias=alias)

mt4 = get_mt4(alias=alias)
mt4.run(self.ea_name, conf=bt_conf)

ret = BacktestReport(self)
return ret

def optimize(self, alias=DEFAULT_MT4_NAME):
"""
"""
from report import OptimizationReport

self.optimization = True
self._prepare(alias=alias)
bt_conf = self._get_conf_abs_path(alias=alias)

mt4 = get_mt4(alias=alias)
mt4.run(self.ea_name, conf=bt_conf)

ret = OptimizationReport(self)
return ret

Expand Down
39 changes: 24 additions & 15 deletions metatrader/mt4.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@
"""
import os
import logging
import subprocess

try:
import winreg
except:
import _winreg as winreg
import codecs

_mt4s = {}

DEFAULT_MT4_NAME = 'default'
# mt4 program file path is written in origin.txt
# mt4 program file path is written in origin.txt
ORIGIN_TXT = 'origin.txt'
MT4_EXE = 'terminal.exe'


class MT4(object):
"""
Notes:
meta trader4 class which can lunch metatrader4.
this class will only lunch metatrader4,
because metatrader4 can lunch either normal mode or backtest mode.
because metatrader4 can lunch either normal mode or backtest mode.
"""
prog_path = None
appdata_path = None
Expand Down Expand Up @@ -45,11 +53,10 @@ def run(self, ea_name, conf=None):
Notes:
run terminal.exe.
Args:
conf(string): abs path of conf file.
details see mt4 help doc Client Terminal/Tools/Configuration at Startup
conf(string): abs path of conf file.
details see mt4 help doc Client Terminal/Tools/Configuration at Startup
"""
import subprocess


if conf:
prog = '"%s"' % os.path.join(self.prog_path, MT4_EXE)
conf = '"%s"' % conf
Expand Down Expand Up @@ -91,24 +98,26 @@ def has_mt4_subdirs(appdata_path):

return ret


def is_uac_enabled():
"""
Note:
check uac is enabled or not from reg value.
Returns:
True if uac is enabled, False if uac is disabled.
"""
import _winreg

reg_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System', 0, _winreg.KEY_READ)
value, regtype = _winreg.QueryValueEx(reg_key, 'EnableLUA')

reg_key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System', 0,
winreg.KEY_READ)
value, regtype = winreg.QueryValueEx(reg_key, 'EnableLUA')

if value == 1:
#reg value 1 means UAC is enabled
# reg value 1 means UAC is enabled
return True
else:
return False


def get_appdata_path(program_file_dir):
"""
Returns:
Expand All @@ -128,7 +137,6 @@ def get_appdata_path(program_file_dir):
if ORIGIN_TXT in files:
origin_file = os.path.join(root, ORIGIN_TXT)

import codecs
with codecs.open(origin_file, 'r', 'utf-16') as fp:
line = fp.read()
if line == program_file_dir:
Expand All @@ -145,18 +153,19 @@ def get_appdata_path(program_file_dir):

return app_dir


def initizalize(ntpath, alias=DEFAULT_MT4_NAME):
"""
Notes:
initialize mt4
Args:
ntpath(string): mt4 install folder path.
e.g.: C:\\Program Files (x86)\\MetaTrader 4 - Alpari Japan
e.g.: C:\\Program Files (x86)\\MetaTrader 4 - Alpari Japan
alias(string): mt4 object alias name. default value is DEFAULT_MT4_NAME
"""
global _mt4s
if alias not in _mt4s:
#store mt4 objecct with alias name
# store mt4 objecct with alias name
_mt4s[alias] = MT4(ntpath, )
else:
logging.info('%s is already initialized' % alias)
Expand Down
Loading