Trading View indicator to MT5

 
I am proud of myself with how far I have come with my coding knowledge without any experience and relying only on YouTube and Google for help. I have been trying to convert an indicator from Trading View Pine Script into a MT5 indicator and have learned so much along the way. I have learned how to plot graphs, when to use int, bools, and buffers for example. Creating the complex if statements are a bit of a challenge but I like a good challenge. I have so much further to go evident by the current issue I am having. I have come a long way but have an issue.

Here is my current issue: I am trying to recall the Boolean value from a previous bar. I know Boolean values are either true or false. Doesn't Pine Script treat them both ways, as both true/false and 1/0? I am thinking I should turn my bool variable into a int variable in MT5 and work with the numbers or can you store then recall Boolean true/false values from previous bars in MT5?



 

You have two solutions:

1/ Create two separate booleans and update them with every iteration.

bool var;
bool prev_var;

2/ You can create a buffer(which has to be double). The buffer keeps the boolean variable for every single bar in the history. Only thing is you set 1=true and 0=false.

buffer[0]==> var
buffer[1]==> prev_var
 
Yashar Seyyedin #:

You have two solutions:

1/ Create two separate booleans and update them with every iteration.

2/ You can create a buffer(which has to be double). The buffer keeps the boolean variable for every single bar in the history. Only thing is you set 1=true and 0=false.


That makes a lot of sense. I will give it a try. Thanks!

 
toddy333 #:

That makes a lot of sense. I will give it a try. Thanks!


In Trading View there is a function barssince that counts the amount of bars that the bool was true. Would I create a custom function in MT5 to do this? Would this work? 

condition = *

int countTrueBarscross() 

        {

         count = 0;

         int totalBars = Bars(_Symbol, _Period);

         

         for (int i = 0; i < totalBars; i++) {

            if(condition)  {

               count++;

                           } 

                                                }

         return count;

         }


 
toddy333 #:


In Trading View there is a function barssince that counts the amount of bars that the bool was true. Would I create a custom function in MT5 to do this? Would this work? 

int pine_barssince(double &conditionBuffer[], int index) 
{
         for (int i = index; i < Bars; i++) 
	{

            if(conditionBuffer[i]==1)
		return (i-index);

        }
    return -1;
}
This is not that simple... I prefer to keep the condition as a buffer(0=false, 1=true). Also you need to pass the index history because in the first call you need to calculate the historical values of indicator.
 
That is some seriously good progress for learning on the fly! Converting Pine Script can be a headache, but you're tackling the right issues. For your main problem of recalling the boolean, using a double buffer (0.0 for false, 1.0 for true) is definitely the cleaner, most "MQL5-friendly" way to track that condition across historical bars. You've basically got to convert everything that holds historical data into an MQL5 buffer. And yeah, for the barssince function, you have to custom-code that loop yourself. The logic provided by the other user (Yashar) looks like the perfect way to start building that custom function!