1
0
Fork 0
forked from e187038/SP_GVA1
SP_GVA1/SP_Pedro1/Homework1/SP_Pedro_Resistencias02.mq5

391 lines
31 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 16:25:41 +02:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| SimpleEATemplatev1.2.mq5 coded on DELL-SP3|
//| Copyright 2024, Singularity Partners Sarl. |
//| https://www.SingularityPartners.ch |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, Singularity Partners Sarl."
#property link "https://www.SingularityPartners.ch"
#property version "1.0" // based on A6.16.4
// Idea of this EA is (Describe the intention of this EA)
//Versi<EFBFBD>n 1.3 Intento garantizar s<EFBFBD>lo un corto o largo abierto y estrategia con alg<EFBFBD>n beneficio 2019-2024
// Results from 1 Jan 2017 until 1 Jan 2024: UR:3 RDD:22% Sharpe Ratio:2.08 %Profit:33% or so. T=980 R7=36,529
//mejores resultados si s<EFBFBD>lo se utiliza se<EFBFBD>al de compra?
#include <Math\Stat\Math.mqh>
#define CUANTASVELAS 20
int mitick=0;
uint vsmr=0;
double sumadeproporciones=0.0;
double puntoderesistencia,soporte;
datetime cuando_fue;
bool velacreada=false;
datetime current_candle_time,last_candle_time;
//+------------------------------------------------------------------+
//| Input Variables |
//+------------------------------------------------------------------+
//Input Variables
input group "Copyright 2024, www.SingularityPartners.ch "
input double IL = 5; // IL Initial Lot
input double SL = 0.05; // SL Stop Loss as Percent of Balance
//input double TP = 0.03; // TP Take Profit as Percent of Balance
input double TPP = 2600; // TPP Take Profit in Points originalmente venia con 1000
input double SLP = 2600; // SLP Stop Loss in Points
input group "Signal Inputs"
input uint Period = 67; // Period iMA
input uint Shift = 6; // Shift iMA
input double Proporcion=0.00023 ;// Proporcion -> la media es 0.00005
input ENUM_MA_METHOD METHOD = MODE_LWMA;
input ENUM_APPLIED_PRICE APPLIED_PRICE = PRICE_WEIGHTED;
input uint CBA =9; // CBA CopyBufferAmount
input uint EMAFirst = 2 ;
input uint EMASecond = 4 ;
input uint rsiPeriod = 60; // Per<EFBFBD>odo del RSI originalmente era 14
input uint overboughtLevel = 100; // Nivel de sobrecompra
input uint oversoldLevel = 0; // Nivel de sobreventa
//Include Files
#include<Trade\Trade.mqh>
CTrade trade; //Trade Class
// Variables Definitions
//bool signal;
int signal=-1;
bool CS; // Close Shorts - en espa<EFBFBD>ol ci<EFBFBD>rrame cortos
bool CL; // Close Longs - en espa<EFBFBD>ol ci<EFBFBD>rrame largos.
int LT; // Counter of Long Trades
int ST; // Counter of Short Trades
int T; // Counter of Total Trades
int L; // Code Line
double EMAArray[];
double OnTester()
{
double RDD = TesterStatistics ( STAT_EQUITY_DDREL_PERCENT ) ;
double Numerator = (TesterStatistics (STAT_PROFIT) + TesterStatistics (STAT_INITIAL_DEPOSIT));
double Denominator = (TesterStatistics (STAT_INITIAL_DEPOSIT)*(1)*RDD*0.01); // 5+5/12 as Measured from Jan 2019 until May 2024
//string UR = TesterStatistics ((STAT_PROFIT+STAT_INITIAL_DEPOSIT)/STAT_INITIAL_DEPOSIT/7/STAT_EQUITY_DDREL_PERCENT*0.01);
double UR = Numerator/Denominator;
//double URR2 = UR/RDD/RDD;
string concatenatedString = DoubleToString(UR,0)+ "." + DoubleToString(RDD,0) ; // UR.RDD
return StringToDouble(concatenatedString);
}
void OnTick()
{
// double maximosdevela [CUANTASVELAS+1];
// double maximo1=0,maximo2=0;
// int velamax1=1,velamax2=1;
mitick++;
// Print ("Estas cosicas que vienen a continuaci<00>n se producen en el tick n<00>mero ",mitick);
// Trade structures
MqlTradeRequest request;
MqlTradeResult result;
ZeroMemory(request);
//+------------------------------------------------------------------+
//| Price |
//+------------------------------------------------------------------+
// Price
double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
//+------------------------------------------------------------------+
//| Balance, Equity, Profit |
//+------------------------------------------------------------------+
// Balance, Equity, Profit
double Balance=NormalizeDouble(AccountInfoDouble(ACCOUNT_BALANCE),0);
double Equity=NormalizeDouble(AccountInfoDouble(ACCOUNT_EQUITY),0);
double Profit=NormalizeDouble(AccountInfoDouble(ACCOUNT_PROFIT),0);
//+------------------------------------------------------------------+
//| Longs Shorts Counter |
//+------------------------------------------------------------------+
LT=0; ST=0; T=0;
for(int i = 0; i < PositionsTotal(); i++)
{
ulong ticket = PositionGetTicket(i);
PositionSelectByTicket(ticket);
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) // if it's a long'
{
LT ++;
}
else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) // if it's short
{
ST ++;
}
T=(LT+ST);
}
//+------------------------------------------------------------------+
//| Sensing |
//+------------------------------------------------------------------+
/*double totalcierres=0;
for (int numvela=1;numvela<=CUANTASVELAS;numvela++) {
totalcierres = iClose(_Symbol, _Period, numvela) + totalcierres;
}
double mediacierres=totalcierres/CUANTASVELAS; // esa media de cierres de x velas que me servir<EFBFBD> para se<EFBFBD>al.
*/
//int val_index=iHighest(NULL,0,MODE_CLOSE,20,0); no me sirve para la 2<EFBFBD> vela max
//ArraySort(maximosdevela);
// Print ("el elemento maximo es: ",maximosdevela[velamax1], " o ",maximo1," en posicion: ",velamax1, " y el 2<00> max: ",maximosdevela [velamax2], " o ",maximo2, " con la vela ",velamax2);
//ExpertRemove();
if (velacreada) {
puntoderesistencia=calculaResistencia(CUANTASVELAS);
soporte=calculaSoporte(CUANTASVELAS);
Print (" Te digo que el soporte es ",soporte, " y la resis ",puntoderesistencia);
velacreada=false;
}
else {
// Obtener la hora de apertura de la vela actual
current_candle_time = iTime(_Symbol, PERIOD_CURRENT, 1);
// Comprobar si la vela actual es diferente de la <EFBFBD>ltima vela registrada
if(current_candle_time != last_candle_time)
{
// Se ha creado una nueva vela
Print("Nueva vela detectada en ", _Symbol, " a las ", TimeToString(current_candle_time, TIME_DATE|TIME_MINUTES));
// Actualizar la hora de la <EFBFBD>ltima vela registrada
last_candle_time = current_candle_time;
velacreada=true;
}
}
signal=-1;
// double actualvela=iClose(_Symbol, _Period, 0);
if (soporte>puntoderesistencia)
vsmr++;
else if (Bid>puntoderesistencia) // de momento s<EFBFBD>lo hago ventas a base de take profit.
// Print ("Ruptura de resistencia <00>acho haz algo! ");
signal=0;
else if (Ask<soporte)
signal=1;
//Print ("EMA ha sido : ",EMA,"- signal : ",signal, " con Valor EMA de first ",EMAArray[EMAFirst], "con valor EMA de Second ",EMAArray[EMASecond], " Propor ",Proporcion);
//+------------------------------------------------------------------+
//| Control |
//+------------------------------------------------------------------+
// aqu<EFBFBD> comprueba sl y tp, si se produjera alguno, pone CL o CS a 1.
if(CL==0)if(LT>=1)if(Equity<Balance*(1-SL)){CL=1;L=__LINE__;Print("************ CL=1 L:",L);} // Stop Loss when Equity goes below (1-SL)
if(CS==0)if(ST>=1)if(Equity<Balance*(1-SL)){CS=1;L=__LINE__;Print("************ CS=1 L:",L);}
//+------------------------------------------------------------------+
//| Actuation |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| OPENING LONGS |
//+------------------------------------------------------------------+
if(signal==0 && PositionsTotal()<1) {
trade.Buy(IL,NULL,Ask,(Ask-SLP*_Point),(Ask+TPP*_Point),NULL);L=__LINE__;Print("############ open longo OL=1 L:",__LINE__);
cuando_fue=TimeCurrent();
}
//+------------------------------------------------------------------+
//| OPENING SHORTS |
//+------------------------------------------------------------------+
if(signal==1 && PositionsTotal()<1) {
trade.Sell(IL,NULL,Bid,(Bid+SLP*_Point),(Bid-TPP*_Point),NULL);L=__LINE__;Print("########### open corto OS=1 L:",__LINE__);
cuando_fue=TimeCurrent();
}
if (signal==1 && LT==1) {
CL=1;
}
if (signal==0 && ST==1) {
CS=1;
}
//esto lo hago por si llega se<EFBFBD>al de venta/compra con largo/corto abierto, lo cierro, a ver qu<EFBFBD> resultado da. Nada, mal en peor.
//+------------------------------------------------------------------+
//| CLOSING LONGS |
//+------------------------------------------------------------------+
if(CL==1)
{
for(int i=PositionsTotal()-1;i>=0; i--)
{
ulong ticket=PositionGetTicket(i);
PositionSelectByTicket(ticket);
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
{
// Print (" Por curiosidad cierro y el valor ticket es = ",ticket, " la position select por ticket es ",PositionSelectByTicket(ticket));
trade.PositionClose(ticket);
}
}
CL=0;
L=__LINE__;Print("CL=1 CONFIRMED. L:",__LINE__);
}
//+------------------------------------------------------------------+
//| CLOSING SHORTS |
//+------------------------------------------------------------------+
if(CS==1)
{
for(int i=PositionsTotal()-1;i>=0; i--)
{
ulong ticket2=PositionGetTicket(i);
PositionSelectByTicket(ticket2);
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
{
// Print (" Por curiosidad cierro y el valor ticket es = ",ticket2, " la position select por ticket es ",PositionSelectByTicket(ticket2));
trade.PositionClose(ticket2);
}
}
CS=0;
L=__LINE__;Print("CS=1 CONFIRMED. L:",__LINE__);
}
//+------------------------------------------------------------------+
//| Dashboard |
//+------------------------------------------------------------------+
Comment("<00> Singularity Partners 2024","\n",
//"CODE LINE:",L,"\n",
" punto de resistencia actual :",puntoderesistencia, " Punto de soporte: ",NormalizeDouble(soporte,_Digits)," VSMR: ",vsmr,"\n"
" el ask es :",Ask," El bid es :",Bid , " la ultima vela cerrada valia:",iClose(_Symbol, _Period, 1),"\n" ,
"Balance:",DoubleToString(Balance,0)," Equity:",DoubleToString(Equity,0)," Profit:",DoubleToString(Profit,0),"\n"
//"LT: ",LT," ST: ",ST," T:",PositionsTotal(),"\n",
// "CL:",CL," CS:",CS,"\n",
// " EMAFirst:",NormalizeDouble(EMAArray[EMAFirst],_Digits)," EMASecond:",NormalizeDouble(EMAArray[EMASecond],_Digits)," signal:",signal, "\n"
// "poporcion : ",proporcioncalculada*100 ," --- media proporcion ---> ",mediaproporciones
);
}
double calculaSoporte (int cuantasvelas)
{
double minimosdevela[];
ArrayResize(minimosdevela, cuantasvelas+1);
double minimo1=DBL_MAX,minimo2=DBL_MAX;
int velamin1=0,velamin2=0;
double p;
for (int numvela=1;numvela<=cuantasvelas;numvela++) {
minimosdevela[numvela]=iLow(_Symbol, _Period, numvela);
if (minimosdevela[numvela]<minimo1) {
minimo2=minimo1;
velamin2=velamin1;
minimo1=minimosdevela[numvela];
velamin1=numvela;
// Print (" la vela ",numvela," nueva 1<00> vela con valor ",velamax1);
}
else if (minimosdevela[numvela]<minimo2) {
velamin2=numvela;
minimo2=minimosdevela[numvela];
// Print (" la vela ",numvela," nueva 2<00> vela con valor ",velamax2);
}
}
p=(minimo1-minimo2)/(velamin1-velamin2);
if (iLow(_Symbol, _Period, 1)<(minimo1-p*velamin1) ) {
Print ("se rompi<00> el soporte ");
return (iLow(_Symbol, _Period, 1));
}
return(minimo1-p*velamin1);
}
//-----------------------------------------------
double calculaResistencia (int pcuantasvelas) {
double maximosdevela[];
ArrayResize(maximosdevela, pcuantasvelas+1);
double maximo1=-DBL_MAX,maximo2=-DBL_MAX;
int velamax1=0,velamax2=0;
double pendiente;
//Relleno el array
for (int numvela=1;numvela<=pcuantasvelas;numvela++) {
maximosdevela[numvela]=iHigh(_Symbol, _Period, numvela);
if (maximosdevela[numvela]>maximo1) {
maximo2=maximo1;
velamax2=velamax1;
maximo1=maximosdevela[numvela];
velamax1=numvela;
// Print (" la vela ",numvela," nueva 1<00> vela con valor ",velamax1);
}
else if (maximosdevela[numvela]>maximo2) {
velamax2=numvela;
maximo2=maximosdevela[numvela];
// Print (" la vela ",numvela," nueva 2<00> vela con valor ",velamax2);
}
}
pendiente=(maximo1-maximo2)/(velamax1-velamax2);
puntoderesistencia=maximo1-pendiente*velamax1;
if (iHigh(_Symbol, _Period, 1)<puntoderesistencia ) { //Deliverar sobre si interesa modificar los maximo1 y maximo2 cuando rompe resisten<EFBFBD>a
Print ("se rompi<00> la resistencia ");
dibujaLinea(iTime(_Symbol, PERIOD_CURRENT, velamax1),maximo1,iTime(_Symbol, PERIOD_CURRENT, 1),iHigh(_Symbol, _Period, 1));
return (iHigh(_Symbol, _Period, 1));
}
dibujaLinea(iTime(_Symbol, PERIOD_CURRENT, CUANTASVELAS),pendiente*CUANTASVELAS+puntoderesistencia,iTime(_Symbol, PERIOD_CURRENT, 1),pendiente+puntoderesistencia);
return (puntoderesistencia);
//Print ("LA funci<00>n es ",pendiente," * x + ",puntoderesistencia);
//Print ("CR<00>eme, el punto de resistencia actual es: ",puntoderesistencia);
}
void dibujaLinea (datetime time1, double price1,datetime time2, double price2) {
string line_name = "Resistencia";
// Comprobar si el objeto ya existe y eliminarlo
if(ObjectFind(0, line_name) >= 0)
ObjectDelete(0, line_name);
// Definir las coordenadas de la l<EFBFBD>nea
// datetime time1 = TimeCurrent(); // Fecha y hora del primer punto
// double price1 = Ask; // Precio del primer punto
//datetime time2 = TimeCurrent()-600; // Fecha y hora del segundo punto
// double price2 = puntoderesistencia; // Precio del segundo punto
// Crear la l<EFBFBD>nea de tendencia
ObjectCreate(0, line_name, OBJ_TREND, 0, time1, price1, time2, price2);
// Configurar las propiedades de la l<EFBFBD>nea
ObjectSetInteger(0, line_name, OBJPROP_COLOR, clrRed); // Color de la l<EFBFBD>nea
ObjectSetInteger(0, line_name, OBJPROP_WIDTH, 2); // Ancho de la l<EFBFBD>nea
ObjectSetInteger(0, line_name, OBJPROP_STYLE, STYLE_SOLID); // Estilo de la l<EFBFBD>nea
}