[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Re: MIKE - SIMPLE WRITELINE JAVASCRIPT FOR AMIBROKER USE QUESTION


  • Date: Fri, 04 Dec 2009 07:17:55 -0000
  • From: "Mike" <sfclimbers@xxxxxxxxx>
  • Subject: [amibroker] Re: MIKE - SIMPLE WRITELINE JAVASCRIPT FOR AMIBROKER USE QUESTION

PureBytes Links

Trading Reference Links

Bert,

Your most recent version of code is still flawed.

1. You do not need to (should not?) declare x1, y1 inside your function (i.e. get rid of var x1, y1;). They are already implicitly declared by virtue of being named input parameters.

2. Your function is actually ignoring the input parameters and is instead using the global variables x, y. Change refernces to x, y in your function to refer instead to x1, y1 respectively.

3. For exception handling, I haven't done enough JScript/JavaScript to give an example. But, in general, check for null return values, empty strings, -1 return codes, etc. It will vary based on the methods being called. Upon finding an unexpected condition, you might show an error dialog and force an exit. You'd pretty much have to google around for method descriptions to find out what they return in error conditions, or even if they throw an exception for usage with try/catch.

Mike

--- In amibroker@xxxxxxxxxxxxxxx, "bistrader" <bistrader@xxx> wrote:
>
> Mike,
> 
> Thanks.  I understand all except for "7. Might want to check for successful return values from any of the functions that return them, and handle exceptions when value is not what was expected."  I understand concept but not how to do.  If have time, then provide feedback.
> 
> Here is the example with changes for others as well.
> 
> // Attempt to do the following.
> // 1. Set x to 5 and y to 8 for a function
> // 2. Create a file to write to called myoutput.txt
> // 3. Write the following to this file, where x is set to 5, y to 8.
> //      I pick x of y for today.
> //      This is my final pick.
> //      These are "My Winners" for the day.
> 
> // I am having problems writing My Winners in quotes.
> 
> // ----------------------------------------------------------------------------
> // Create AmiBroker object and get Analysis object
> // ----------------------------------------------------------------------------
> var AB, AA;
> var x, y;
> var WLEmpty_ExploreFile;
> 
> AB = new ActiveXObject("Broker.Application");
> AA = AB.Analysis;
> 
> // ----------------------------------------------------------------------------
> // Set x = 5, y = 8 for function; Define output file
> // ----------------------------------------------------------------------------
> x = 5;
> y = 8;
> 
> WLEmpty_ExploreFile = "C:\\Amibroker\\Temp\\myoutput2.txt"; 
> 
> VarAndQuotes(x,y);
> 
> AA.ShowWindow(0);
> 
> // ----------------------------------------------------------------------------
> // Function to create an txt where x and y change and with
> // some of the text in quotes
> // ----------------------------------------------------------------------------
> function VarAndQuotes(x1,y1)
> {
>     var fso, f, ts;
>     var x1, y1;
>     var ForReading = 1, ForWriting = 2, ForAppending = 8;
>     var TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0;
> 
>     var line1 = "I pick " + x + " of " + y + " for today."
>     var line2 = "This is my final pick.";
>     var line3 = 'These are "My Winners" for the day.'
> 
>     fso = new ActiveXObject("Scripting.FileSystemObject");
>     fso.CreateTextFile( WLEmpty_ExploreFile );
>     f = fso.GetFile(WLEmpty_ExploreFile);
>     ts = f.OpenAsTextStream(ForWriting, TristateUseDefault);
>     
>     ts.WriteLine ( line1 + "\n" + line2 );
>     ts.Write ( line3);// use Write and not WriteLine as want no CR
>     ts.Close( );
> }
> // The end
> --- In amibroker@xxxxxxxxxxxxxxx, "Mike" <sfclimbers@> wrote:
> >
> > Bert,
> > 
> > I'm not a JScript/JavaScript expert. But, here's what I see:
> > 
> > 1. Explicitly declaring your variables (i.e. using var keyword) is considered good practice. As such, you might want to consider using it consistently throughout your code. In your sample, you use it for some variables, but not for all (e.g. WLEmpty_Directory).
> > 
> > 2. The code might be easier to read if you just declared the complete file path in a single variable, as opposed to concatenating it togeather from composite strings. Unless you need the composite parts for reuse later, I'd just collapse them into a single variable. Personal preference.
> > 
> > 3. Same goes for the construction of your strings. No need to declare each segment as a variable if you don't plan to reuse them again later. Might be easier to read if you just concatenated it all at once
> > 
> > e.g.
> > var line1 = "I pick " + x + " of " + y + " for today.";
> > 
> > 4. I believe that you are mis-using the call method of your function. Do not write:
> > 
> > VarAndQuotes.call(x,y);
> > 
> > Just move the declaration of the function to before its usage then simply call the same as any other function
> > 
> > VarAndQuotes(x,y);
> > 
> > If I understand correctly, your current usage is actually changing the 'this' object upon which the function is being called, which is probably not what you intended.
> > 
> > 5. If you want to output quotes, you can probably escape them in the quoted string.
> > 
> > e.g.
> > var line3b = "\"My Winners\"";
> > 
> > 6. I see no reason for calling AB.RefreshAll(); If for whatever reason you do need this call as a result of other (not shown) code, then I would at least move it out of this function since it is unrelated to the function's purpose.
> > 
> > 7. Might want to check for successful return values from any of the functions that return them, and handle exceptions when value is not what was expected.
> > 
> > 8. File usage looks fine to me.
> > 
> > Mike
> > 
> > --- In amibroker@xxxxxxxxxxxxxxx, "bistrader" <bistrader@> wrote:
> > >
> > > Mike,
> > > I am having problems creating a txt file via JavaScript for AmiBroker use so I create the following simple example.  I want the txt file to be exactly the following, but can not get the quotes to be included.
> > > 
> > > I pick x of y for today.
> > > This is my final pick.
> > > These are "My Winners" for the day.
> > > 
> > > Here, x and y are variables and My Winners is in quotes.
> > > 
> > > Plus, I want to expand on this so would appreciate feedback on whether my approach to using a function, var, writeline and write, etc is a good way to approach writing what is needed to a file.
> > > 
> > > Here is my example code and thanks 
> > > **************************
> > > 
> > > // Attempt to do the following.
> > > // 1. Set x to 5 and y to 8 for a function
> > > // 2. Create a file to write to called myoutput.txt
> > > // 3. Write the following to this file, where x is set to 5, y to 8.
> > > //      I pick x of y for today.
> > > //      This is my final pick.
> > > //      These are "My Winners" for the day.
> > > 
> > > // I am having problems writing My Winners in quotes.
> > > 
> > > // ----------------------------------------------------------------------------
> > > // Create AmiBroker object and get Analysis object
> > > // ----------------------------------------------------------------------------
> > > var AB, AA;
> > > 
> > > AB = new ActiveXObject("Broker.Application");
> > > AA = AB.Analysis;
> > > 
> > > // ----------------------------------------------------------------------------
> > > // Set x = 5 and y = 8 for function
> > > // ----------------------------------------------------------------------------
> > > var x = 5;
> > > var y = 8;
> > > 
> > > WLEmpty_Directory = "C:\\Amibroker\\Temp\\";
> > > WLEmpty_ExploreFileName = "myoutput.txt";
> > > WLEmpty_ExploreFile = WLEmpty_Directory + WLEmpty_ExploreFileName; 
> > > 
> > > VarAndQuotes.call(x,y);
> > > 
> > > AA.ShowWindow(0);
> > > 
> > > // ----------------------------------------------------------------------------
> > > // Function to create an txt where x and y change and with
> > > // some of the text in quotes
> > > // ----------------------------------------------------------------------------
> > > function VarAndQuotes(x1,y1)
> > > {
> > >     var fso, f, ts;
> > >     var x1, y1;
> > >     var ForReading = 1, ForWriting = 2, ForAppending = 8;
> > >     var TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0;
> > > 
> > >     var line1a = "I pick ";// need to place 'x' after this
> > >     var line1b = " of ";// need to place 'y' after this.
> > >     var line1c = " for today.";
> > >     var line2 = "This is my final pick.";
> > >     var line3a = "These are ";
> > >     var line3b = "My Winners";// need to somehow place in double quotes
> > >     var line3c = "for the day.";// no carrage return after.
> > > 
> > >     fso = new ActiveXObject("Scripting.FileSystemObject");
> > >     fso.CreateTextFile( WLEmpty_ExploreFile );
> > >     f = fso.GetFile(WLEmpty_ExploreFile);
> > >     ts = f.OpenAsTextStream(ForWriting, TristateUseDefault);
> > >     
> > >     ts.WriteLine ( line1a + x + line1b + y + line1c );
> > >     ts.WriteLine ( line2 );
> > >     ts.Write ( line3a + " + line3b + " + line3c );// DOES NOT WORK
> > >     ts.Close( );
> > >     AB.RefreshAll();
> > > }
> > > 
> > > 
> > > 
> > > // Thanks for the help!
> > > 
> > > // Bert
> > >
> >
>




------------------------------------

**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

TO GET TECHNICAL SUPPORT send an e-mail directly to 
SUPPORT {at} amibroker.com

TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    amibroker-digest@xxxxxxxxxxxxxxx 
    amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/