Can I use nested if-else statements to change a string?

I am learning how to build a system which will eventually communicate with Binance using .html in a browser.
The code creates a folder and in that folder, a new html file for the current symbol, then launches that file. The file contents are a XML http request. In this example, klines on the Binance API is the OHLC and vol data. The result is returned in the developer console of Chrome.

Line 57 I was able to use the Name() function as that is pretty standard. However, Binance uses a different nomenclature to Amibroker when referencing intervals. I am trying to define 'interv' depending on the current chart interval, but I always get returned the data for the first definition. So, for example;

{
	if(Interval(1)==in5Minute)
	interv = "5m";
	else
	{
		if(Interval(1)==in15minute)
		interv = "15m";
		else
		{


			if(Interval(1)==inhourly)
			interv = "1h";
			else	
			{

				if(Interval(1)==inDaily)
				interv = "1d";
				else
				{
				
					if(Interval(1)==inWeekly)
					interv = "1w";
					else
					{
						interv="1d";
					}
				}
			}
		}
	}
}						

would return the 5 minute data, regardless of whether a 15 min, 1 hour, 1 day or 1 week chart is displayed.

The whole code is here;

{
	if(Interval(1)==in5Minute)
	interv = "5m";
	else
	{
		if(Interval(1)==in15minute)
		interv = "15m";
		else
		{


			if(Interval(1)==inhourly)
			interv = "1h";
			else	
			{

				if(Interval(1)==inDaily)
				interv = "1d";
				else
				{
				
					if(Interval(1)==inWeekly)
					interv = "1w";
					else
					{
						interv="1d";
					}
				}
			}
		}
	}
}						




                                            

fmkdir("C:\\Program Files\\AmiBroker\\Export\\");
path= "C:\\Program Files\\AmiBroker\\Export\\" + Name() + ".html";
path2= "file:///C:/Program%20Files/AmiBroker/Export/"+ Name() + ".html";
fh = fopen(path,"w");

if( fh )
{
    filecontent = StrFormat(
                      "<html>"
                      + "\n<body>"
                      + "\n</body>"
                      + "\n<script>"

                      + "\nvar burl ='https://api1.binance.com';"

                      + "\nvar query = '/api/v3/klines';"

                      + "\nquery += '?symbol="
                      + Name() + "&interval="+interv+"&limit=1';"

                      + "\nvar url = burl + query;"

                      + "\nvar ourRequest = new XMLHttpRequest();"

                      + "\nourRequest.open('GET',url,true);"
                      + "\nourRequest.onload = function(){"
                      +	"\nconsole.log(ourRequest.responseText);"
                      + "\n}"
                      + "\nourRequest.send();"

                      + "\n</script>"
                      + "\n</html>;" );

    fputs( filecontent, fh );
    fclose( fh );
    ShellExecute( "chrome", "-auto-open-devtools-for-tabs" + " " + path2, "", 1 );
}

Isn't it easier just to use Interval(2) that would give you interval as TEXT

Yes you can use nested if else, or you could use switch:

switch( Interval(1) )
{
   case inDaily: 
           str = "1d";
           break;
   case inWeekly: 
           str = "1w";
           break;
   case inHourly: 
           str = "1h";
           break;
 // and so on....
  default:
           str = "unknown";
          break;
}

By the way in AFL, as in C, you don't need curly braces for nested if-else as dangling else bounds to last if, so you could just write sequence as this:

n = Interval(1);

if( n == in1Minute )
    str = "1m";
else 
if( n == inHourly )
	str = "1h";
else
if( n == inDaily )
	str = "1d";
else
if( n == inWeekly )
	str = "1w";

Please note that if you run under debugger, the code would use interval that DEPENDS on Preferences (with default being base time interval):

image

Ah, brilliant. Yeah, that makes sense. Thanks Tomasz. I was running the code using the afl verify syntax button which I am guessing is the same as running it in the debugger.

Then I started running it from the chart instead and started running into issues with it looping and opening hundreds of chrome tabs, but once I added

if ( Status("stocknum") == 0 )

and ran it from the analysis window instead, it made much more sense.

All good and working now! Thanks! You're a legend. :grinning:

Thanks

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.