3 Code
3 Code
//| QuantStrategyEA.mq5 |
//| Example EA for Advanced Quant Behavior Strategy |
//+------------------------------------------------------------------+
#property copyright "Achilleus Capital"
#property link "Quant Trading Firm"
#property version "1.00"
#include <Trade\Trade.mqh>
#include <Indicators\Indicators.mqh>
CTrade Trade;
// Input parameters
input double RiskPercent = 1.0; // Risk per trade in percentage
input int MA_Period = 50; // Period for the Simple Moving Average
(SMA)
input int ATR_Period = 14; // ATR period for volatility-based stop
input double ATR_Multiplier = 2.0; // Multiplier to set Stop Loss distance
input double LotSize_Min = 0.01; // Minimum lot size
input double LotSize_Factor = 0.1; // Factor to adjust lot size based on
account equity
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Create handles for indicators
smaHandle = iMA(_Symbol, _Period, MA_Period, 0, MODE_SMA, PRICE_CLOSE);
if(smaHandle == INVALID_HANDLE)
{
Print("Failed to create SMA handle");
return(INIT_FAILED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Release indicator handles
if(smaHandle != INVALID_HANDLE)
IndicatorRelease(smaHandle);
if(atrHandle != INVALID_HANDLE)
IndicatorRelease(atrHandle);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Make sure we have enough bars
if(Bars(_Symbol, _Period) < (MathMax(MA_Period, ATR_Period)+1))
return;
//+------------------------------------------------------------------+
//| Function: CalculateLotSize |
//| Calculates lot size based on account equity and risk per trade |
//+------------------------------------------------------------------+
double CalculateLotSize(double price, double atrValue)
{
// Calculate risk in money
double accountEquity = AccountInfoDouble(ACCOUNT_EQUITY);
double riskMoney = accountEquity * (RiskPercent / 100.0);
// Stop loss in price units, using ATR value multiplied by our multiplier
double stopLossPrice = atrValue * ATR_Multiplier;
// Calculate lot size: riskMoney divided by stop loss (in monetary terms per
lot)
// Note: Adjust the multiplier below based on instrument specifications (e.g.,
contract size)
double lotSize = riskMoney / (stopLossPrice * LotSize_Factor);