Do I use writeIf to translate strings for external functions

The code creates a .html file in a directory and opens it with the output being data from a public API. In this case, binance. It is working provided that I manually specify the interval.

The intervals on the binance api are defined as 5m, 15m, 1h, 1d and 1w and in amibroker, we use in5minute, in15minute,inHourly,inDaily and inWeekly.

I am trying to define 'interv' and insert it into a string. I tried the IIF, but, that wanted a number. I tried the If Else, but I didn't manage to get that to work and on reading, I can see it is the probably wrong function for what I am trying to do anyway. I found the 'writeIf' function and thought that might get the job done, but it isn't. Here's my try;


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

where

interv = WriteIf( Interval() == inDaily, "1d",
                  WriteIf( Interval() == in5Minute, "5m",
                           WriteIf( Interval() == in15Minute, "15m",
                                    WriteIf( Interval() == inHourly, "1h",
                                            WriteIf( Interval() == inWeekly, "1w", "1d" ) ) ) ) );

However, when I change the interval in amibroker, the output in the developer console in Chrome always remains as the daily data (unless I manually change the string to 1w, of 5m, or whatever.

For better context of what I am trying to achieve;

interv = WriteIf( Interval() == inDaily, "1d",
                  WriteIf( Interval() == in5Minute, "5m",
                           WriteIf( Interval() == in15Minute, "15m",
                                    WriteIf( Interval() == inHourly, "1h",
                                            WriteIf( Interval() == inWeekly, "1w", "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 );
}

Thanks in advance for wisdoms.

Oops.

"Thanks in advance for wisdoms" is not part of the code.

@SpandexMan , you may want to try using an Exploration first to see what you are getting. If you play with time frame in the exploration and see what is happening, it might lead you to what is going on.

Why not just use a switch() statement?

switch(Interval())
{
   case inDaily: interv = "1d"; break;
   case in5Minute, interv = "5m"; break;
   // additional case statements here
}

@SpandexMan, you have already been given this exact solution by @Tomasz in this thread!

1 Like

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