// Support and Resistance indicator
function run()
{
//set(STEPWISE);
set(PLOTNOW);
StartDate = 20150101;
EndDate = 20170301;
BarPeriod = 1440;
LookBack = 100; //default - rising this gives more levels to start with
//BarZone = CET;
LifeTime = 1;
if(is(INITRUN)) printf("nnnnnn===============================");
static var SnR[100]; //array containing all price levels found -> needs to be dynamic in the future
static int n_SnR = 0; //integer needed to fill the support and resistance array
vars Price = series(price());
//vars Highs = series(priceHigh()); //maybe used later to determine the thickness of the support
//vars Lows = series(priceLow()); //and resistance levels
vars Open = series(priceOpen());
vars Close = series(priceClose());
var Factor = 0.5;
int Cycle = Factor*DominantPeriod(Price,20);
//ZigZag(Price,20*PIP,Cycle*Factor,BLUE); //replace 20*PIP with ATR to account for volatility and different timeframes...?
ZigZag(Price,3*ATR(20),Cycle*Factor,BLUE);
plot("rSlope", rSlope, NEW|SQUARE, BLACK);
plot("rPeak", rPeak, NEW|SQUARE, BLACK);
plot("rSign", rSign, NEW|SQUARE, BLACK);
plot("rLength", rLength, NEW|SQUARE, BLACK);
plot("Cycle", Cycle, NEW, BLACK);
vars RSign = series(rSign,2);
vars RPeak = series(rPeak,2);
if (RSign[0] != RSign[1]) //direction changed
{
enterLong(); ColorEquity = ColorDD = 0; //just to help the stepwise flag
//printf("ndirection changed!"); //debugging
int n = min(RPeak[1], LookBack-2); //prevent to look further back than LookBack, otherwise bullshit is happening like SnR[n_SnR] = 0 or SnR[n_SnR] = incredible high number
//printf("nnn is: %d", n); //debugging
//printf("n rPeak[1]-1: %.0f, rPeak[0]: %.0f", RPeak[1]-1, RPeak[0]);
while (n > 0)
{
if (rSign < 0) // looking for resistance
{
if (n == min(RPeak[1], LookBack-2)) //first loop - n still has it's initial value //store maximum of the oldest candle in the SnR array
{
//printf("nlooking for resistancenZigZag: %.5f", max(Close[rPeak], Open[rPeak]));
SnR[n_SnR] = max(Close[n], Open[n]);
}
SnR[n_SnR] = max(SnR[n_SnR], max(Close[n+1], Open[n+1])); //compare the subsequent candle and replace the old maximum if a higher price is found //ZigZag high if its higher (rise lookback!!!)
//printf("n%dth pot. Res.: %.5f, Max: %.5f", n, SnR[n_SnR], max(Close[n+1], Open[n+1]));
if (n == 1) //last loop
{
//printf("nresistance:%.5f, rPeak = %.5f", SnR[n_SnR], max(Close[rPeak], Open[rPeak]));
}
}
if (rSign > 0) // looking for support
{
if (n == min(RPeak[1], LookBack-2)) //first loop - n still has it's initial value //store min of the oldest candle in the SnR array
{
//printf("nlooking for supportnnZigZag: %.5f", max(Close[rPeak], Open[rPeak]));
SnR[n_SnR] = min(Close[n], Open[n]);
}
SnR[n_SnR] = min(SnR[n_SnR], min(Close[n+1], Open[n+1])); //compare the subsequent candle and replace the old minimum if a lower price is found //ZigZag low if its lower (rise lookback!!!)
//printf("n%dth pot. Sup.: %.5f, Min: %.5f", n, SnR[n_SnR], min(Close[n+1], Open[n+1]));
if (n == 1) //last loop
{
//printf("nsupport:%.5f, rPeak = %.5f", SnR[n_SnR], min(Close[rPeak], Open[rPeak]));
}
}
n--;
}
n_SnR++; //prepare to write into next slot in the SnR array
if(n_SnR > 99)
{
n_SnR = 0;
//as the S&R Array is still static, this will overwrite the oldest values to prevent errors.
}
}
int n;
for(n=0; n<100; n++)
{
if(SnR[n] != 0)
{
if(is(EXITRUN))
{
sortData(SnR, 100);
printf("n%dth Price level: %.5f", n, SnR[n]);
print(TO_CSV, "nPrice: %.5f", SnR[n]);
//I need to write something other than just the price
//Excel has problems with interpreting the numbers / komma is separator in Germany
//Fixing this with STRG + H (replace "Price: " with nothing) in Excel
}
plot("SnR", SnR[n], MAIN|SQUARE, RED); //plot only the recent level
//plot(strf("SnR_%d", n), SnR[n], MAIN|SQUARE, RED); //plot all levels in the array; behaving strange
}
}
}