|
1 | | -"""Module for generating MT5 expert advisor template files. |
| 1 | +"""Module for creating template files for MetaTrader 5 integration. |
2 | 2 |
|
3 | | -Provides functionality to create template files for trading strategies. |
| 3 | +Provides functions for generating template files. |
4 | 4 | """ |
5 | 5 |
|
6 | | -import argparse |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +from pathlib import Path |
7 | 9 | from typing import Any |
8 | 10 |
|
| 11 | +import MetaTrader5 as Mt5 |
| 12 | + |
9 | 13 |
|
10 | 14 | def get_arguments() -> dict[str, Any]: |
11 | | - """Parse command line arguments. |
| 15 | + """Get the arguments for the template. |
12 | 16 |
|
13 | 17 | Returns: |
14 | | - dict[str, Any]: Dictionary containing the parsed arguments. |
| 18 | + dict[str, Any]: The arguments for the template. |
15 | 19 | """ |
16 | | - parser = argparse.ArgumentParser() |
17 | | - parser.add_argument("--file_name", type=str, action="store", default="demo") |
18 | | - parser.add_argument("--symbol", type=str, action="store", default="EURUSD") |
19 | | - return vars(parser.parse_args()) |
| 20 | + return { |
| 21 | + "symbol": "EURUSD", |
| 22 | + "time_frame": Mt5.TIMEFRAME_M1, |
| 23 | + "start_position": 0, |
| 24 | + "count": 100, |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | +def create_template(file_name: str) -> None: |
| 29 | + """Create a template file for the expert advisor. |
20 | 30 |
|
| 31 | + Args: |
| 32 | + file_name (str): The name of the file to create. |
21 | 33 |
|
22 | | -def main() -> None: |
23 | | - """Generate a template file for a trading strategy.""" |
24 | | - file_name = get_arguments()["file_name"] |
| 34 | + Returns: |
| 35 | + None |
| 36 | + """ |
25 | 37 | symbol = get_arguments()["symbol"] |
26 | 38 |
|
27 | | - with open(f"{file_name}.py", "w") as file: |
| 39 | + with Path(f"{file_name}.py").open("w") as file: |
28 | 40 | file.write( |
29 | 41 | f"""from mqpy.rates import Rates |
30 | 42 | from mqpy.tick import Tick |
| 43 | +from mqpy.book import Book |
31 | 44 | from mqpy.trade import Trade |
32 | | -
|
33 | | -# Initialize the trading strategy |
34 | | -trade = Trade( |
35 | | - expert_name="Moving Average Crossover", |
36 | | - version=1.0, |
37 | | - symbol="{symbol}", |
38 | | - magic_number=567, |
39 | | - lot=1.0, |
40 | | - stop_loss=25, |
41 | | - emergency_stop_loss=300, |
42 | | - take_profit=25, |
43 | | - emergency_take_profit=300, |
44 | | - start_time="9:15", |
45 | | - finishing_time="17:30", |
46 | | - ending_time="17:50", |
47 | | - fee=0.5, |
48 | | -) |
49 | | -
|
50 | | -# Main trading loop |
51 | | -prev_tick_time = 0 |
52 | | -short_window_size = 5 |
53 | | -long_window_size = 20 # Adjust the window size as needed |
54 | | -
|
55 | | -while True: |
56 | | - # Fetch tick and rates data |
57 | | - current_tick = Tick(trade.symbol) |
58 | | - historical_rates = Rates(trade.symbol, long_window_size, 0, 1) |
59 | | -
|
60 | | - # Check for new tick |
61 | | - if current_tick.time_msc != prev_tick_time: |
62 | | - # Calculate moving averages |
63 | | - short_ma = sum(historical_rates.close[-short_window_size:]) / short_window_size |
64 | | - long_ma = sum(historical_rates.close[-long_window_size:]) / long_window_size |
65 | | -
|
66 | | - # Generate signals based on moving average crossover |
67 | | - is_cross_above = short_ma > long_ma and current_tick.last > short_ma |
68 | | - is_cross_below = short_ma < long_ma and current_tick.last < short_ma |
69 | | -
|
70 | | - # Execute trading positions based on signals |
71 | | - trade.open_position(is_cross_above, is_cross_below, "Moving Average Crossover Strategy") |
72 | | -
|
73 | | - prev_tick_time = current_tick.time_msc |
74 | | -
|
75 | | - # Check if it's the end of the trading day |
76 | | - if trade.days_end(): |
77 | | - trade.close_position("End of the trading day reached.") |
78 | | - break |
79 | | -
|
80 | | -print("Finishing the program.") |
81 | | -print("Program finished.") |
| 45 | +from mqpy.utilities import Utilities |
| 46 | +
|
| 47 | +def main(): |
| 48 | + # Initialize the expert advisor |
| 49 | + expert = Trade( |
| 50 | + expert_name="{file_name}", |
| 51 | + version="1.0", |
| 52 | + symbol="{symbol}", |
| 53 | + magic_number=123456, |
| 54 | + lot=0.1, |
| 55 | + stop_loss=100, |
| 56 | + emergency_stop_loss=200, |
| 57 | + take_profit=100, |
| 58 | + emergency_take_profit=200, |
| 59 | + ) |
| 60 | +
|
| 61 | + # Initialize utilities |
| 62 | + utilities = Utilities() |
| 63 | +
|
| 64 | + while True: |
| 65 | + # Get the current tick |
| 66 | + tick = Tick("{symbol}") |
| 67 | +
|
| 68 | + # Get the current market book |
| 69 | + book = Book("{symbol}") |
| 70 | +
|
| 71 | + # Get the current rates |
| 72 | + rates = Rates("{symbol}", Mt5.TIMEFRAME_M1, 0, 100) |
| 73 | +
|
| 74 | + # Check if trading is allowed |
| 75 | + if utilities.check_trade_availability("{symbol}", 5): |
| 76 | + # Open a position |
| 77 | + expert.open_position( |
| 78 | + should_buy=True, |
| 79 | + should_sell=False, |
| 80 | + comment="Buy position opened by {file_name}", |
| 81 | + ) |
| 82 | +
|
| 83 | + # Close the market book |
| 84 | + book.release() |
| 85 | +
|
| 86 | +if __name__ == "__main__": |
| 87 | + main() |
82 | 88 | """ |
83 | 89 | ) |
0 commit comments