Custom Array questions.
Hi everyone,
I am having an issue with some code which compiles and can be run however the array indexes are not giving me the numbers I want. To put it simply I am trying to loop back through the chart bars anad find highs and lows based on "BarsN" to the left and right and then store the highs and lows in an array.
When I go back to call the array and ask for index 0 and index 1 the numebrs I am getting do not match the objects I have created on the chart. Here is the ontick function, I have placed "*****" after the lines that I seem to be having the issue with.
Basically I want it to look back from time current to the first low/high and then again to the 2nd low/high and work out if a trend is forming...
Any help would be appreciated - Thanks.
int bars = iBars(_Symbol, PERIOD_CURRENT);
if (totalBars != bars) {
totalBars = bars;
Comment("DownTrend: ",isDownTrend,
"\nUpTrend: ",isUpTrend);
// Remove previous objects (if any)
for (int i = 0; i < currentIndex; i++) {
ObjectsDeleteAll(0,"HighCircle");
ObjectsDeleteAll(0,"LowCircle");
}
for (int i = 0; i < iBars(_Symbol, PERIOD_CURRENT); i++) {
if (i > BarsN && iHighest(_Symbol, HIGHLOWTRIGGERTIMEFRAME, MODE_HIGH, BarsN*2+1, i-BarsN) == i) {
double high = iHigh(_Symbol, HIGHLOWTRIGGERTIMEFRAME, i);
string objectName = "HighCircle" + IntegerToString(i);
ObjectCreate(0, objectName, OBJ_ARROW_UP, 0, iTime(_Symbol, PERIOD_CURRENT, i), high);
ObjectSetInteger(0, objectName, OBJPROP_COLOR, clrRoyalBlue);
ObjectSetInteger(0, objectName, OBJPROP_FILL, clrRoyalBlue);
// Store the high price in the array
highObjectPrices[currentIndex] = high;
currentIndex++;
// Reset the index if it exceeds 4 (since you want to store 5 highs and 5 lows)
if (currentIndex >= ArrSize) {
currentIndex = 0;
}
}
if (i > BarsN && iLowest(_Symbol, HIGHLOWTRIGGERTIMEFRAME, MODE_LOW, BarsN*2+1, i-BarsN) == i) {
double low = iLow(_Symbol, HIGHLOWTRIGGERTIMEFRAME, i);
string objectName = "LowCircle" + IntegerToString(i);
ObjectCreate(0, objectName, OBJ_ARROW_DOWN, 0, iTime(_Symbol, PERIOD_CURRENT, i), low);
ObjectSetInteger(0, objectName, OBJPROP_COLOR, clrRed);
ObjectSetInteger(0, objectName, OBJPROP_FILL, clrRed);
// Store the low price in the array
lowObjectPrices[currentIndex] = low;
currentIndex++;
// Reset the index if it exceeds 4 (since you want to store 5 highs and 5 lows)
if (currentIndex >= ArrSize) {
currentIndex = 0;
}
}
// Calculate the prices of the most recent high and low objects
double currentHigh = highObjectPrices[currentIndex];*****
double previousHigh = highObjectPrices[(currentIndex + ArrSize - 1) % ArrSize];*****
double currentLow = highObjectPrices[currentIndex]; *****
double previousLow = lowObjectPrices[(currentIndex + ArrSize - 1) % ArrSize]; *****
Print(currentHigh," ",previousHigh," ",currentLow," ",previousLow);
// Determine if there is a higher high and higher low
if (currentHigh > previousHigh && currentLow > previousLow) {
isUpTrend = true;
Print("Uptrend detected (higher high and higher low)");
} else {
isUpTrend = false;
Print("No uptrend");
}
// Determine if there is a lower high and lower low
if (currentHigh < previousHigh && currentLow < previousLow) {
isDownTrend = true;
Print("Downtrend detected (lower high and lower low)");
} else {
isDownTrend = false;
Print("No downtrend");
}
}
1
5 comments
Angus Knight
2
Custom Array questions.
Automated Traders Community
skool.com/automated-traders-community-7473
Let's collaborate to become better traders/programmers!
No spamming, no scamming!
Be kind to each other!
Leaderboard (30-day)
powered by