Grades went down... what are my chances?

Hi everyone! I’m a high school student living outside the United States. In my country, high school lasts three years, uses bimester system and its grade scale is 0-100.

Well, something bad happened to me in the third semester, but first of all, let’s have an overview:
In my first year of HS, I was ranked as the second best student among 40 students (top 5%).
In my second year, I was ranked as the second best student among 21 (top 10%).

Well, this year my class has only 18 students. In the first bimester I was the second best student; in the second bimester I was the best. However, my grandmother died during the third bimester and because of that my grades went down and I will probably be the third best… I will try to score the high as I can in the fourth bimester, but still I cannot be ranked in the first place, maximum in the second (11.1%)… how does this fact affects my chances to get into an Ivy or Stanford?

I think I have good EC, such as:
-I have created a B2B startup that is still in the beginning, but it already has 2 customers (I’m taking a gap year next year because I’m moving to Brazil’s biggest city with some friends to try get some invest). I’m also studying on my own a lot about business and economics;
-I have three silver medals in Brazil’s Astronomy Olympiad and, this year, I was ranked as the best astronomy student in my state and 21th in Brazil);
-I also have a gold and silver medal in Brazil’s Geography Olympiad;
-A Honor Mention in Brazil’s Physics Olympiad (and was ranked as one of the best in my state)
-I was selected to attend Brazilian Space Camp in 2015
-I know several programming languages, such as Javascript, Java, PHP, Ruby, C, C++, Python, HTML, CSS, and I’ve studied everything on my own!
-I’ve won the Google Brazil’s app marathon with an app aimed for autistic people;
-I have several programming projects, such as a website to help students to get a higher score in the speaking section of the TOEFL; a website to organize in one place all of the tests of several scientific olympiads; and other smaller projects.
-I practice kung fu twice a week

All of them were very important to me and I’ve learned a lot from them

And I believe I can get great letters of recommendation, because I have a good relationship with my math and history teachers and they know me very well.