BullScript: Best Practices

Best Practices

As with many programming languages, BullScript can be used well or poorly. When writing your own scripts, we advise that you endeavour to write them in a clean, clear fashion for several reasons:

  1. Your scripts will be easier to understand by others.
  2. You will be more able to spot potential problems, or bugs, in your script.
  3. It will be easier for you to modify your script at a later date.
  4. If you intend on publishing your scripts, they will appear more professional.

Here are several guidelines that we suggest you follow when writing scripts:

  • Use descriptive names when naming your scripts and their inputs. For example, use “Smoothing periods” instead of “smooth” as an input.
  • Add inputs to your script, so that results can be easily customized without having to edit the script. Give a sensible default value for each input.
  • Specify maximum and minimum allowed values for inputs as appropriate. For example, an input that will be used to adjust the length of a moving average should have a minimum value of at least 1. Don’t specify a maximum value unnecessarily.
  • Use consistent capitalisation when entering function names and variable names. This will make your script more readable.
  • White space (spaces and new lines) can be used to make your program more readable. Use white space consistently. For example, put a space before and after operators (+, -, *, /, :=, etc…).
  • For complicated scripts, group sections of your script together that logically belong together. For example, list all of your inputs together at the top. Then do all of your calculations together. Finally return all of the results together.
  • Only assign one variable per line. For example: a:=O+H; b:=a*3; c:=b+D; should not all be given on a single line.
  • Remember to use attributes to add meaning to your script. For example, if an indicator logically belongs with the price, such as ma(typical(),5), then use the [target=Price] attribute.
  • When multiple results are being returned, use the name attribute to give each result a meaningful name. For example: “MACD” and “Signal”.
  • If you publish your script, use the citation attribute so other people will remember where they found the script. Include a web address if appropriate.
  • Use comments to describe any unusual steps in your script. However, a general description of your indicator should be put in a description attribute. Use proper grammar and punctuation for both comments and descriptions.
  • Assign intermediate results to variables, rather than including them inline. This is particularly important for complicated scripts. For example:

tmp := (O+H+L+C)/4;
ma(tmp,5,S) + ma(tmp,7,S);
is better than: ma((O+H+L+C)/4,5,S) + ma((O+H+L+C)/4,7,S); 

  • Use the BullScript functions to simplify your scripts where ever possible. For example:

use ma(C,n,S) instead of sum(C,n)/n when calculating a moving average. Or use typical instead of (H+L+C)/3.

  • Carefully test your scripts to ensure that they are working as expected. For example, you may accidentally enter sum(C,m) when you intended sum(C,n), but the mistake will only be revealed with testing if both m and n are in use.
  • Surround text attributes with quotes. [name=”Modified Moving Average”] rather than [name=Modified Moving Average] to make it clear when the value begins and ends.

Was this article helpful?

Related Articles