|
| 1 | +import argparse |
| 2 | + |
| 3 | + |
| 4 | +def get_arguments(): |
| 5 | + parser = argparse.ArgumentParser() |
| 6 | + parser.add_argument("--file_name", type=str, action="store", default="demo") |
| 7 | + parser.add_argument("--symbol", type=str, action="store", default="EURUSD") |
| 8 | + return vars(parser.parse_args()) |
| 9 | + |
| 10 | + |
| 11 | +def main(): |
| 12 | + file_name = get_arguments()["file_name"] |
| 13 | + |
| 14 | + with open(f"{file_name}.py", "w") as file: |
| 15 | + file.write( |
| 16 | + """from mqpy.src.rates import Rates |
| 17 | +from mqpy.src.tick import Tick |
| 18 | +from mqpy.src.trade import Trade |
| 19 | +
|
| 20 | +# Initialize the trading strategy |
| 21 | +trade = Trade( |
| 22 | + expert_name="Moving Average Crossover", |
| 23 | + version=1.0, |
| 24 | + symbol="EURUSD", |
| 25 | + magic_number=567, |
| 26 | + lot=1.0, |
| 27 | + stop_loss=25, |
| 28 | + emergency_stop_loss=300, |
| 29 | + take_profit=25, |
| 30 | + emergency_take_profit=300, |
| 31 | + start_time="9:15", |
| 32 | + finishing_time="17:30", |
| 33 | + ending_time="17:50", |
| 34 | + fee=0.5, |
| 35 | +) |
| 36 | +
|
| 37 | +# Main trading loop |
| 38 | +prev_tick_time = 0 |
| 39 | +short_window_size = 5 |
| 40 | +long_window_size = 20 # Adjust the window size as needed |
| 41 | +
|
| 42 | +while True: |
| 43 | + # Fetch tick and rates data |
| 44 | + current_tick = Tick(trade.symbol) |
| 45 | + historical_rates = Rates(trade.symbol, long_window_size, 0, 1) |
| 46 | +
|
| 47 | + # Check for new tick |
| 48 | + if current_tick.time_msc != prev_tick_time: |
| 49 | + # Calculate moving averages |
| 50 | + short_ma = sum(historical_rates.close[-short_window_size:]) / short_window_size |
| 51 | + long_ma = sum(historical_rates.close[-long_window_size:]) / long_window_size |
| 52 | +
|
| 53 | + # Generate signals based on moving average crossover |
| 54 | + is_cross_above = short_ma > long_ma and current_tick.last > short_ma |
| 55 | + is_cross_below = short_ma < long_ma and current_tick.last < short_ma |
| 56 | +
|
| 57 | + # Execute trading positions based on signals |
| 58 | + trade.open_position(is_cross_above, is_cross_below, "Moving Average Crossover Strategy") |
| 59 | +
|
| 60 | + prev_tick_time = current_tick.time_msc |
| 61 | +
|
| 62 | + # Check if it's the end of the trading day |
| 63 | + if trade.days_end(): |
| 64 | + trade.close_position("End of the trading day reached.") |
| 65 | + break |
| 66 | +
|
| 67 | +print("Finishing the program.") |
| 68 | +print("Program finished.") |
| 69 | +""" |
| 70 | + ) |
0 commit comments