DevSolve_DS_SDK_UnitTest/Unit_Test.mq5

461 lines
34 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 14:49:27 +02:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| Unit_Test.mq5 |
//| Copyright 2023 - Dev.Solve LTDA |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Macro definitions. |
//+------------------------------------------------------------------+
#include "MacroDefinitions.mqh"
//+------------------------------------------------------------------+
//| Includes. |
//+------------------------------------------------------------------+
#include "EngineInputs.mqh"
#include "Chart.mqh"
#include "Engine.mqh"
#include "Strategies/Strategies.mqh"
#include "Stoploss/Stoploss.mqh"
#include "Takeprofit/Takeprofit.mqh"
#include "Money/Money.mqh"
#include "ExpertFilter.mqh"
//+------------------------------------------------------------------+
//| Global expert object |
//+------------------------------------------------------------------+
//--- engine
CEngine DevSolve_SDK;
//--- filters
CExpertFilter filters;
//+------------------------------------------------------------------+
//| Global Variables |
//+------------------------------------------------------------------+
int used_money=TipoDeRisco; // flag of using money
int used_strategy=EstrategiaSelecionada; // flag of using of strategy
int used_stoploss=TipoDeStopLoss; // flag of using of stop loss
int used_takeprofit=TipoDeTakeProfit; // flag of using of takeprofit
//--- other ticker
string other_ticker;
//--- only brokers and registered accounts
const string allowed_brokers[] = {"Genial Investimentos Corretora de Valores Mobili<00>rios S.A.", "XP Investimentos CCTVM S/A", "Modal DTVM Ltda.",
"Raw Trading Ltd", "ActivTrades Corp"
};
const long allowed_accounts[] = {514384619, 424634, 1859113, 3376653, 193804, 7381966};
//--- verify this EA is valid until
datetime allowed_until = D'2024.03.26 00:00';
int password_status=-1;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//+------------------------------------------------------------------+
//| Checking Broker and Account |
//+------------------------------------------------------------------+
string broker = AccountInfoString(ACCOUNT_COMPANY);
long account = AccountInfoInteger(ACCOUNT_LOGIN);
//---
printf("Corretora: %s", broker);
printf("N<00>mero da conta: %d", account);
for(int i=0; i<ArraySize(allowed_brokers); i++)
{
if(broker==allowed_brokers[i])
{
for(int j=0; j<ArraySize(allowed_accounts); j++)
{
if(account==allowed_accounts[j])
{
password_status = 1;
Print("Conta cadastrada no sistema.");
break;
}
}
break;
}
}
//--- not verify account/broker
if(password_status==-1)
Print("Conta n<00>o habilidata DS_UNIT_TEST.");
//+------------------------------------------------------------------+
//| Initializing expert (init phase first) |
//+------------------------------------------------------------------+
if(!DevSolve_SDK.Init(EXPERT_NAME,TickerAtivo,TempoGrafico,false,MagicNumber,Lote))
{
//--- failed
printf(__FUNCTION__+": erro na inicializa<00><00>o do expert");
//DevSolve_SDK.Deinit();
return(INIT_FAILED);
}
//+------------------------------------------------------------------+
//| General configs (init phase tuning) |
//+------------------------------------------------------------------+
//--- general configs
DevSolve_SDK.ExpirationOrders(RemoverOrdensPendentes);
DevSolve_SDK.SetSimpleHour(UsarHorarioSimplificado);
DevSolve_SDK.SetTypeSimpleHour(IntervaloDeNegociacao);
DevSolve_SDK.GetHours(HoraInicioNegociacao,HoraTerminoEnvioOrdens,HoraTerminoNegociacao);
DevSolve_SDK.Magic(MagicNumber);
DevSolve_SDK.Period(TempoGrafico);
//--- initialize other ticker
if(!SetOtherTicker(TickerIndicator))
{
//--- failed
printf(__FUNCTION__+": erro na na escolha do ticker do indicador.");
DevSolve_SDK.Deinit();
return(INIT_FAILED);
}
DevSolve_SDK.SetOtherTicker(other_ticker);
//---
DevSolve_SDK.SetTypeOperation(TipoDeOperacao);
DevSolve_SDK.ShiftOrderPoints(DeslocarEntradaEmPontos);
DevSolve_SDK.SetTickFlagIn(FlagDeEntrada);
DevSolve_SDK.SetTickFlag(FlagDeTick);
//--- closing trade configs
DevSolve_SDK.GetTypeClosing(TipoDeEncerramentoTrade);
//+------------------------------------------------------------------+
//| Config of Template Chart |
//+------------------------------------------------------------------+
//--- chart options
bool result=false;
//--- Show grid
ChartShowGridGet(result,0);
if(result)
ChartShowGridSet(!result,0);
//--- Back color
if(ChartBackColorGet(0)!=BackColorInput)
ChartBackColorSet(BackColorInput,0);
//--- Fore color
if(ChartForeColorGet(0)!=ForeColorInput)
ChartForeColorSet(ForeColorInput,0);
//--- Bear color
if(ChartBearColorGet(0)!=BearColorInput)
ChartBearColorSet(BearColorInput,0);
//--- Bull color
if(ChartBullColorGet(0)!=BullColorInput)
ChartBullColorSet(BullColorInput,0);
//--- Up color
if(ChartUpColorGet(0)!=UpColorInput)
ChartUpColorSet(UpColorInput,0);
//--- Down color
if(ChartDownColorGet(0)!=DownColorInput)
ChartDownColorSet(DownColorInput,0);
//--- Chart line color
if(ChartLineColorGet(0)!=ForeColorInput)
ChartLineColorSet(ForeColorInput,0);
//+------------------------------------------------------------------+
//| Config Filters Usage |
//+------------------------------------------------------------------+
filters.Init(other_ticker,TempoGrafico,MagicNumber);
//--- linear regression filter
if(!DevSolve_SDK.InitLinearRegressionFilter((LinearRegressionFilter_Is_Usage && !DesligarTodosOsFiltros),
other_ticker,
LinearRegressionFilter_TimeFrame,
PeriodoLinearRegressionFilter,
LinearRegressionDirection,
LinearRegressionFilterUseLocation,
LinearRegressionLocation_Buy,
LinearRegressionLocation_Sell))
{
//--- if optimization
if(MQLInfoInteger(MQL_OPTIMIZATION))
{
//--- set error params
DevSolve_SDK.SetErrorParams(true);
}
else
{
//--- failed
printf(__FUNCTION__+": erro ao criar filtros de regress<00>o linear.");
DevSolve_SDK.Deinit();
return(INIT_FAILED);
}
}
//+------------------------------------------------------------------+
//| Initializing money used |
//+------------------------------------------------------------------+
if(IS_MONEY_FIXED_LOT_USAGE)
{
CMoneyFixedLot *money=new CMoneyFixedLot;
if(money==NULL)
{
//--- failed
printf(__FUNCTION__+": erro ao criar classe de manejo de risco.");
DevSolve_SDK.Deinit();
return(INIT_FAILED);
}
//--- Add money to expert (will be deleted automatically))
if(!DevSolve_SDK.InitMoney(money))
{
//--- failed
printf(__FUNCTION__+": erro ao inicializar manejo de risco.");
DevSolve_SDK.Deinit();
return(INIT_FAILED);
}
//--- global informations
money.Lots(Lote);
}
//+------------------------------------------------------------------+
//| 14. MACD Strategy |
//+------------------------------------------------------------------+
if(IS_STRATEGY_MACD)
{
//---create strategy
CMACDStrategy *strategy=new CMACDStrategy;
if(strategy==NULL)
{
//--- failed
printf(__FUNCTION__+": erro ao criar estrat<00>gia de MACD");
DevSolve_SDK.Deinit();
return(INIT_FAILED);
}
//--- Add strategy to expert (will be deleted automatically))
if(!DevSolve_SDK.InitStrategy(strategy))
{
//--- failed
printf(__FUNCTION__+": erro ao inicializar estrat<00>gia");
DevSolve_SDK.Deinit();
return(INIT_FAILED);
}
//--- initialize fields of the current strategy
strategy.SetDirection(SentidoOperacaoMACD);
strategy.Set_m_macd_timeframe(macd_timeframe);
strategy.Set_m_macd_fast_ema_period(macd_fast_ema_period);
strategy.Set_m_macd_slow_ema_period(macd_slow_ema_period);
strategy.Set_m_macd_signal_period(macd_signal_period);
strategy.Set_m_macd_applied_price(macd_applied_price);
strategy.Set_m_atr_timeframe(macd_TimeFrameATR);
strategy.Set_m_atr_period(macd_PeriodoATR);
strategy.Set_m_atr_multiplier(macd_MultiplierATR);
strategy.Set_m_atr_candle_counter(macd_atr_candle_counter);
strategy.Set_m_macd_stddev_period(macd_stddev_period);
strategy.Set_m_macd_stddev_multiplier(macd_stddev_multiplier);
strategy.Set_M_MACD_TYPE_ENTRY(TipoDeEntradaMACD);
}
//+------------------------------------------------------------------+
//| Initializing stop loss used |
//+------------------------------------------------------------------+
if(IS_STOPLOSS_NULL_USAGE)
{
//---create strategy rompimento
CExpertStopLoss *stoploss=new CExpertStopLoss;
if(stoploss==NULL)
{
//--- failed
printf(__FUNCTION__+": erro ao criar classe de stop loss");
DevSolve_SDK.Deinit();
return(INIT_FAILED);
}
//--- Add stop loss to expert (will be deleted automatically))
if(!DevSolve_SDK.InitStopLoss(stoploss))
{
//--- failed
printf(__FUNCTION__+": erro ao inicializar stop loss");
DevSolve_SDK.Deinit();
return(INIT_FAILED);
}
}
if(IS_STOPLOSS_ATR_USAGE)
{
//---create stop loss
CStopLossATR *stoploss=new CStopLossATR;
if(stoploss==NULL)
{
//--- failed
printf(__FUNCTION__+": erro ao criar classe de stop loss");
DevSolve_SDK.Deinit();
return(INIT_FAILED);
}
//--- Add stop loss to expert (will be deleted automatically))
if(!DevSolve_SDK.InitStopLoss(stoploss))
{
//--- failed
printf(__FUNCTION__+": erro ao inicializar stop loss");
DevSolve_SDK.Deinit();
return(INIT_FAILED);
}
stoploss.SetMultiplier(StopLossATR_Multiplicador);
stoploss.SetPeriod(StopLossATR_Periodo);
stoploss.SetTimeFrame(StopLossATR_TimeFrame);
}
//+------------------------------------------------------------------+
//| Initializing take profit used |
//+------------------------------------------------------------------+
if(IS_TAKEPROFIT_NULL_USAGE)
{
//---create take profit
CExpertTakeProfit *takeprofit=new CExpertTakeProfit;
if(takeprofit==NULL)
{
//--- failed
printf(__FUNCTION__+": erro ao criar classe de take profit");
DevSolve_SDK.Deinit();
return(INIT_FAILED);
}
//--- Add take profit to expert (will be deleted automatically))
if(!DevSolve_SDK.InitTakeProfit(takeprofit))
{
//--- failed
printf(__FUNCTION__+": erro ao inicializar take profit");
DevSolve_SDK.Deinit();
return(INIT_FAILED);
}
}
if(IS_TAKEPROFIT_ATR_USAGE)
{
CTakeProfitATR *takeprofit=new CTakeProfitATR;
if(takeprofit==NULL)
{
//--- failed
printf(__FUNCTION__+": erro ao criar classe de take profit");
DevSolve_SDK.Deinit();
return(INIT_FAILED);
}
//--- Add take profit to expert (will be deleted automatically))
if(!DevSolve_SDK.InitTakeProfit(takeprofit))
{
//--- failed
printf(__FUNCTION__+": erro ao inicializar take profit");
DevSolve_SDK.Deinit();
return(INIT_FAILED);
}
takeprofit.GetTimeFrame(TakeProfitATR_TimeFrame);
takeprofit.GetPeriod(TakeProfitATR_Periodo);
takeprofit.GetMultiplier(TakeProfitATR_Multiplicador);
}
//+------------------------------------------------------------------+
//| Validation Settings (init phase validation) |
//+------------------------------------------------------------------+
//--- Check all trading objects parameters
if(!DevSolve_SDK.ValidationSettings())
{
//--- falided
DevSolve_SDK.Deinit();
return(INIT_FAILED);
}
//+------------------------------------------------------------------+
//| Initializing Filters (init phase validation) |
//+------------------------------------------------------------------+
if(!DevSolve_SDK.InitFilters(GetPointer(filters)))
{
//--- falided
printf(__FUNCTION__+": erro na inicializa<00><00>o dos filtros");
DevSolve_SDK.Deinit();
return(INIT_FAILED);
}
//+------------------------------------------------------------------+
//| Initializing Indicators (init phase complete) |
//+------------------------------------------------------------------+
if(!DevSolve_SDK.InitIndicators())
{
//--- falided
printf(__FUNCTION__+": erro na inicializa<00><00>o dos indicadores");
DevSolve_SDK.Deinit();
return(INIT_FAILED);
}
//--- end OnInit Function
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
DevSolve_SDK.Deinit();
//--- delete filter
filters.Clear();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//--- check licence time
datetime now=TimeCurrent();
if(now>allowed_until)
{
password_status=-1;
Print("EA com licen<00>a vencida. A licen<00>a do EA expirou em: " + TimeToString(allowed_until, TIME_DATE|TIME_MINUTES));
}
//--- failiure checking broker/account
if(password_status==-1)
return;
//--- ok
DevSolve_SDK.OnTick();
}
//+------------------------------------------------------------------+
//| TradeTransaction function |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction& trans,
const MqlTradeRequest& request,
const MqlTradeResult& result)
{
//---
DevSolve_SDK.OnTradeTransaction(trans,request,result);
}
//+------------------------------------------------------------------+
//| Tester function |
//+------------------------------------------------------------------+
double OnTester()
{
//---
double ret=DevSolve_SDK.OnTester();
//---
return(ret);
}
//+------------------------------------------------------------------+
//| Symbol Function |
//+------------------------------------------------------------------+
bool SetOtherTicker(string value)
{
if(value=="")
{
bool res=false;
string symbol=Symbol();
//--- SYMBOL SINTETIC
if(SymbolExist(symbol,res))
{
if(res)
{
other_ticker=symbol;
return(true);
}
}
//--- WIN
if(StringFind(symbol,"WIN",0)!=-1)
{
other_ticker="WIN$N";
return(true);
}
//--- WDO
if(StringFind(symbol,"WDO",0)!=-1)
{
other_ticker="WDO$N";
return(true);
}
//--- IND
if(StringFind(symbol,"IND",0)!=-1)
{
other_ticker="IND$N";
return(true);
}
//--- DOL
if(StringFind(symbol,"DOL",0)!=-1)
{
other_ticker="DOL$N";
return(true);
}
//--- OTHER
other_ticker=symbol;
return(true);
}
else
{
bool res=false;
if(!SymbolExist(value,res))
return(false);
//---
other_ticker=value;
return(true);
}
//--- failed
return(false);
}
//+------------------------------------------------------------------+