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