373 lines
21 KiB
MQL5
373 lines
21 KiB
MQL5
#property copyright "Copyright 2023, M & Q Investment Group"
|
|
#property link "https://www.mql5.com"
|
|
#property version "1.00"
|
|
|
|
#include <trade/trade.mqh>
|
|
CTrade trade;
|
|
|
|
input int RangeMinutes = 5;
|
|
|
|
#define StreaksIndex 6
|
|
|
|
double ClosingValues[];
|
|
double ClosingDiffToLast[];
|
|
|
|
//Streakcounter Dim1: Streaks, Dim2: StreakValueIdentifier Dim3: 0 - Positiv, 1 - Negativ
|
|
int Streaks[StreaksIndex][StreaksIndex][2];
|
|
|
|
int Debugprint = 1;
|
|
|
|
//FUNCTION CHECK FOR NEW CANDLE
|
|
bool CheckfornewCandle (int CandleNumber)
|
|
{
|
|
|
|
static int LastCandleNumber;
|
|
|
|
bool IsNewCandle= false;
|
|
|
|
if (CandleNumber>LastCandleNumber)
|
|
{
|
|
|
|
IsNewCandle = true;
|
|
|
|
LastCandleNumber = CandleNumber;
|
|
|
|
Print("New Candle",
|
|
"\nLastOpenTime: ",iTime(_Symbol,PERIOD_CURRENT,1),
|
|
"\nLastOpen: ",iOpen(_Symbol,PERIOD_CURRENT,1),
|
|
"\nLastClose: ",iClose(_Symbol,PERIOD_CURRENT,1));
|
|
|
|
|
|
}
|
|
return IsNewCandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
int OnInit()
|
|
{
|
|
|
|
//INIT CLOSINGVALUES ARRAY
|
|
ArrayResize(ClosingValues,RangeMinutes + 1);
|
|
ArraySetAsSeries(ClosingValues,true);
|
|
int ClosingValuesSize = ArraySize(ClosingValues);
|
|
|
|
for(int i = 0; i < ClosingValuesSize; i++)
|
|
{
|
|
|
|
int Candleiterator = i + 1;
|
|
|
|
ClosingValues[i] = iClose(_Symbol,PERIOD_M1,Candleiterator);
|
|
|
|
}
|
|
|
|
//INIT DIFF ARRAY
|
|
ArrayResize(ClosingDiffToLast,RangeMinutes);
|
|
ArraySetAsSeries(ClosingDiffToLast,true);
|
|
int ClosingDiffToLastSize = ArraySize(ClosingDiffToLast);
|
|
|
|
for(int i = 0; i < ClosingDiffToLastSize; i++)
|
|
{
|
|
|
|
ClosingDiffToLast[i] = ClosingValues[i] - ClosingValues[i+1];
|
|
|
|
}
|
|
|
|
//INIT STREAKCOUNTER
|
|
ArrayFill(Streaks,0,ArraySize(Streaks),0);
|
|
int StreaksSize1D = ArraySize(Streaks) / StreaksIndex;
|
|
ClosingDiffToLastSize = ArraySize(ClosingDiffToLast);
|
|
|
|
if (ClosingDiffToLastSize < StreaksSize1D)
|
|
{
|
|
|
|
int StreakCounter = 0;
|
|
int CurrentStreakPos1Neg2Null0 = 0;
|
|
int StreakDepth;
|
|
|
|
for(int i = ClosingDiffToLastSize - 1 ; i > -1 ; i--)
|
|
{
|
|
|
|
//IDENTIFY DIRECTION
|
|
|
|
//DIRECTION POSITIVE
|
|
if(ClosingDiffToLast[i] > 0)
|
|
{
|
|
|
|
if (CurrentStreakPos1Neg2Null0 == 1)
|
|
{
|
|
StreakCounter = StreakCounter + 1;
|
|
}
|
|
else
|
|
{
|
|
StreakCounter = 1;
|
|
}
|
|
|
|
Streaks[StreakCounter][0][0] = Streaks[StreakCounter][0][0] + 1;
|
|
|
|
StreakDepth = Streaks[StreakCounter][0][0];
|
|
|
|
Streaks[StreakCounter][StreakDepth][0] = i + 1; // 1 ADDED SO ALL IDENTIFIERS ARE > 0 => SO ALL 0 = EMPTY
|
|
|
|
CurrentStreakPos1Neg2Null0 = 1;
|
|
|
|
}
|
|
|
|
//DIRECTION NEGATIVE
|
|
else if (ClosingDiffToLast[i] < 0)
|
|
{
|
|
|
|
if (CurrentStreakPos1Neg2Null0 == 2)
|
|
{
|
|
StreakCounter = StreakCounter + 1;
|
|
}
|
|
else
|
|
{
|
|
StreakCounter = 1;
|
|
}
|
|
|
|
Streaks[StreakCounter][0][1] = Streaks[StreakCounter][0][1] + 1;
|
|
|
|
StreakDepth = Streaks[StreakCounter][0][1];
|
|
|
|
Streaks[StreakCounter][StreakDepth][1] = i + 1; // 1 ADDED SO ALL IDENTIFIERS ARE > 0 => SO ALL 0 = EMPTY
|
|
|
|
CurrentStreakPos1Neg2Null0 = 2;
|
|
|
|
}
|
|
|
|
//DIRECTION 0
|
|
else if (ClosingDiffToLast[i] == 0)
|
|
{
|
|
|
|
CurrentStreakPos1Neg2Null0 = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
/*else
|
|
{
|
|
|
|
Print("Initialization failed. Range of Minutes is to high! Please Correct Input or modifiy StreakIndex value.");
|
|
return(INIT_FAILED);
|
|
|
|
}*/
|
|
for(int i = 0; i < ClosingDiffToLastSize; i++)
|
|
{
|
|
|
|
Print("Closingwert: ",NormalizeDouble(ClosingValues[i],_Digits),
|
|
"\n---Difference to last: ",NormalizeDouble(ClosingDiffToLast[i],_Digits));
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < StreaksIndex; i++)
|
|
{
|
|
|
|
Print("\n---StreakNeg: Streak",i," ", Streaks[i][0][1],"ID`s of Streak:", Streaks[i][1][1], Streaks[i][2][1], Streaks[i][3][1], Streaks[i][4][1], Streaks[i][5][1],
|
|
"\n---StreakPos: Streak",i," ", Streaks[i][0][0],"ID`s of Streak:", Streaks[i][1][0], Streaks[i][2][0], Streaks[i][3][0], Streaks[i][4][0], Streaks[i][5][0]);
|
|
|
|
}
|
|
return(INIT_SUCCEEDED);
|
|
}
|
|
|
|
void OnTick()
|
|
{
|
|
|
|
|
|
//CHECK FOR NEW CANDLE
|
|
int CandleNumber = Bars(_Symbol,_Period);
|
|
|
|
bool NewCandleAppeared;
|
|
|
|
NewCandleAppeared = CheckfornewCandle(CandleNumber);
|
|
|
|
|
|
//---- MAIN ---- NEW CANDLE => DO SOMETHING ---
|
|
if (NewCandleAppeared)
|
|
{
|
|
//GET LAST CLOSING
|
|
double NewLastClosing = iClose(_Symbol,PERIOD_M1,1);
|
|
|
|
//SHIFT DIFFERENCE ARRAYS
|
|
int ClosingValuesSize = ArraySize(ClosingValues);
|
|
ArrayCopy(ClosingValues, ClosingValues, 1, 0, ClosingValuesSize - 1);
|
|
|
|
int ClosingDiffToLastSize = ArraySize(ClosingDiffToLast);
|
|
ArrayCopy(ClosingDiffToLast, ClosingDiffToLast, 1, 0, ClosingDiffToLastSize - 1);
|
|
|
|
//INSERT NEW VALUES
|
|
ClosingValues[0] = NewLastClosing;
|
|
|
|
ClosingDiffToLast[0] = NewLastClosing - ClosingValues[1];
|
|
|
|
|
|
/*DEBUGPRINT
|
|
if(Debugprint == 1)
|
|
{
|
|
Print("---------------------------");
|
|
for(int i = 0; i < ClosingDiffToLastSize; i++)
|
|
{
|
|
|
|
Print(
|
|
"Closingwert: ",NormalizeDouble(ClosingValues[i],_Digits),
|
|
" Difference to last: ",NormalizeDouble(ClosingDiffToLast[i],_Digits));
|
|
|
|
}
|
|
Debugprint = 2;
|
|
}
|
|
*/
|
|
|
|
//SHIFT ARRAYS STREAK
|
|
ClosingDiffToLastSize = ArraySize(ClosingDiffToLast);
|
|
|
|
int CurrentCellValue;
|
|
|
|
//FIND AND DELETE LAST STREAK VALUE
|
|
for(int c = 0; c < StreaksIndex; c++)
|
|
{
|
|
for(int d = 0; d < StreaksIndex; d++)
|
|
{
|
|
for(int f = 0; f < 2; f++)
|
|
{
|
|
CurrentCellValue = Streaks[c][d][f];
|
|
|
|
if(CurrentCellValue == ClosingDiffToLastSize && d != 0)
|
|
{
|
|
Streaks[c][d][f] = 0;
|
|
Streaks[c][0][f] = Streaks[c][0][f] - 1;
|
|
Print("----Deleted Streaks",c,d,f,"---- Streak - 1: ",Streaks[c][0][f]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//SHIFT STREAK ARRAY IDENTIFIER
|
|
|
|
for(int i = 0; i < StreaksIndex; i++)
|
|
{
|
|
for(int j = 0; j < StreaksIndex; j++)
|
|
{
|
|
for(int k = 0; k < 2; k++)
|
|
{
|
|
CurrentCellValue = Streaks[i][j][k];
|
|
|
|
if(j != 0 && CurrentCellValue != 0)
|
|
{
|
|
Streaks[i][j][k] = Streaks[i][j][k] + 1;
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//ADD NEW VALUE
|
|
bool Filled = false;
|
|
|
|
if(ClosingDiffToLast[0] != 0)
|
|
{
|
|
for(int i = 0; i < StreaksIndex; i++)
|
|
{
|
|
for(int j = 0; j < StreaksIndex; j++)
|
|
{
|
|
for(int k = 0; k < 2; k++)
|
|
{
|
|
CurrentCellValue = Streaks[i][j][k];
|
|
|
|
//IDENTIFIY BEGINNING
|
|
if(i != 0 && CurrentCellValue == 2)
|
|
{
|
|
|
|
//FURTHER NEGATIVE STREAK
|
|
if(ClosingDiffToLast[0] < 0 && k == 1)
|
|
{
|
|
|
|
Streaks[i+1][0][k] = Streaks[i+1][0][k] + 1;
|
|
|
|
|
|
for(int l = 1; l < StreaksIndex; l++)
|
|
{
|
|
if(Streaks[i+1][l][k] == 0 && Filled == false)
|
|
{
|
|
Streaks[i+1][l][k] = 1;
|
|
Filled = true;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
//FURTHER POSITIVE STREAK
|
|
if(ClosingDiffToLast[0] > 0 && k == 0)
|
|
{
|
|
|
|
Streaks[i+1][0][k] = Streaks[i+1][0][k] + 1;
|
|
|
|
for(int l = 1; l < StreaksIndex; l++)
|
|
{
|
|
if(Streaks[i+1][l][k] == 0 && Filled == false)
|
|
{
|
|
Streaks[i+1][l][k] = 1;
|
|
Filled = true;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}//END IF IDENTIFIY BEGINNING
|
|
|
|
}
|
|
}
|
|
}//END FOR LOOPS
|
|
|
|
}//END IF THERE IS A STREAK/NEW STREAK
|
|
|
|
//IF NEW STREAK STARTS
|
|
if(ClosingDiffToLast[0] != 0)
|
|
{
|
|
int o;
|
|
if (ClosingDiffToLast[0] < 0) {o = 1;}
|
|
else {o = 0;}
|
|
|
|
for(int l = 1; l < StreaksIndex; l++)
|
|
{
|
|
if(Streaks[1][l][o] == 0 && Filled == false)
|
|
{
|
|
Streaks[1][l][o] = 1;
|
|
Streaks[1][0][o] = Streaks[1][0][o] + 1;
|
|
Filled = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
//DEGBUGGING TESTING PRINTING
|
|
|
|
for(int i = 0; i < ClosingDiffToLastSize; i++)
|
|
{
|
|
|
|
Print("Closingwert: ",NormalizeDouble(ClosingValues[i],_Digits),
|
|
"\n---Difference to last: ",NormalizeDouble(ClosingDiffToLast[i],_Digits));
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < StreaksIndex; i++)
|
|
{
|
|
|
|
Print("\n---StreakNeg: Streak",i," ", Streaks[i][0][1],"ID`s of Streak:", Streaks[i][1][1], Streaks[i][2][1], Streaks[i][3][1], Streaks[i][4][1], Streaks[i][5][1],
|
|
"\n---StreakPos: Streak",i," ", Streaks[i][0][0],"ID`s of Streak:", Streaks[i][1][0], Streaks[i][2][0], Streaks[i][3][0], Streaks[i][4][0], Streaks[i][5][0]);
|
|
|
|
}
|
|
|
|
}//---- END MAIN ---- NEW CANDLE => DO SOMETHING ---
|
|
// Indexierung funzt, aber Counter pro ding passt nicht
|
|
|
|
|
|
}
|