Created
November 23, 2022 14:13
-
-
Save lawrence910426/d57a35520dbb3bbf0402b92dbd2eb253 to your computer and use it in GitHub Desktop.
Hello world to quantitative trading with MT5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//+------------------------------------------------------------------+ | |
//| HelloWorld.mq5 | | |
//| Copyright 2022, MetaQuotes Ltd. | | |
//| https://www.mql5.com | | |
//+------------------------------------------------------------------+ | |
#property copyright "Copyright 2022, MetaQuotes Ltd." | |
#property link "https://www.mql5.com" | |
#property version "1.00" | |
#include <Trade\Trade.mqh> | |
int MaSlow, MaFast; | |
CTrade Trade; | |
input int slowPeriod = 10, | |
fastPeriod = 5; | |
//+------------------------------------------------------------------+ | |
//| Expert initialization function | | |
//+------------------------------------------------------------------+ | |
int OnInit() { | |
MaSlow = iMA(_Symbol, _Period, slowPeriod, 0, MODE_SMA, PRICE_CLOSE); | |
MaFast = iMA(_Symbol, _Period, fastPeriod, 0, MODE_SMA, PRICE_CLOSE); | |
if (MaSlow == INVALID_HANDLE || MaFast == INVALID_HANDLE) | |
return INIT_FAILED; | |
// Invokes `OnTimer` on every bar close | |
Sleep((TimeCurrent() % GetCountDown()) * 1000); | |
EventSetTimer(GetCountDown()); | |
return INIT_SUCCEEDED; | |
} | |
//+------------------------------------------------------------------+ | |
//| Maps the PERIOD enumeration to time intervals in second. | | |
//+------------------------------------------------------------------+ | |
int GetCountDown() { | |
if(_Period == PERIOD_D1) return 86400; | |
if(_Period == PERIOD_H1) return 3600; | |
if(_Period == PERIOD_M30) return 1800; | |
if(_Period == PERIOD_M20) return 1200; | |
if(_Period == PERIOD_M15) return 900; | |
if(_Period == PERIOD_M12) return 720; | |
if(_Period == PERIOD_M10) return 600; | |
if(_Period == PERIOD_M5) return 300; | |
if(_Period == PERIOD_M3) return 180; | |
if(_Period == PERIOD_M1) return 60; | |
return 0; | |
} | |
//+------------------------------------------------------------------+ | |
//| Expert deinitialization function | | |
//+------------------------------------------------------------------+ | |
void OnDeinit(const int reason) { } | |
//+------------------------------------------------------------------+ | |
//| Expert timer function | | |
//+------------------------------------------------------------------+ | |
void OnTimer() { | |
double slowValue[1], fastValue[1]; | |
if (CopyBuffer(MaSlow, 0, 0, 1, slowValue) != 1) return; | |
if (CopyBuffer(MaFast, 0, 0, 1, fastValue) != 1) return; | |
double latestPrice = iClose(_Symbol, _Period, 0); | |
PositionSelect(_Symbol); | |
double currentPosition = PositionGetDouble(POSITION_VOLUME), | |
targetPosition = 0; | |
int direction = PositionGetInteger(POSITION_TYPE); | |
currentPosition *= direction == POSITION_TYPE_SELL ? -1 : 1; | |
if (slowValue[0] < fastValue[0]) { | |
targetPosition = -1; | |
} | |
if (slowValue[0] > fastValue[0]) { | |
targetPosition = 1; | |
} | |
printf("%lf %lf", targetPosition, currentPosition); | |
// MQL omits precision errors for equality of floating point numbers | |
if (currentPosition != targetPosition) { | |
Trade.PositionClose(_Symbol); | |
if (targetPosition > 0) { | |
Trade.Buy(1, _Symbol, 0.0 | |
// TODO: Uncomment this line to enable stoploss at distance of 0.1 | |
/*, latestPrice - 0.1 */ | |
); | |
} | |
if (targetPosition < 0) { | |
Trade.Sell(1, _Symbol, 0.0 | |
// TODO: Uncomment this line to enable stoploss at distance of 0.1 | |
/* , latestPrice + 0.1 */ | |
); | |
} | |
} | |
} | |
//+------------------------------------------------------------------+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment