Notes for the development of bl-sh. (/home/zashi/projects/stocks/)


Currently test5 is not running with the second derivative, it's still using the first derivative. I've also doubled the stocks in the stock pool. Now it includes several retail companies, banks, energy/oil, and two bio-tech companies.

I've calculated some interesting numbers. The mean price of stocks in my stock pool is 36.24. Taking in to consideration that the amount of shares required for a 1 cent gain to equate to a total value gain of $11 is 1100. Multiplying our average stock value by 1100 we get $39,864. Thus, The “Critical Mass” for investing is right around $40,000. At $40,000 invested that means half the stocks in the stocks pool would gain the minimum $11 required to cover trading fees after gaining $0.01. The fluidity of transition between stocks is the key to continuous growth. Even with the highest valued stock in the stock pool at $85/share, with $40,000 invested a 3 cent gain is enough to meet the $11 minimum.

I should probably weight purchasing decisions based on price since the same amount of cash buys fewer shares when stock price is high. Perhaps do slope of percent gained rather than actual gains.

I could also perhaps eschew the minimum gain problem by dropping the requirement altogether and seeing if the increased fluidity makes up for the inevitable occasional loss.

Graph Data

Total Currency

This graph shows total currency in the test5 sim (all stock value + account balance). The data points are 5 minute intervals from every time bl-sh runs. BL-SH starts at 9:00am and stops at 4:00pm.

Percent Return

This graph shows percent return on the initial $10,000 invested in daily increments since test5 was started.

Pseudo Code


  • second derivative refers to the second derivative of stock price vs time (time is 5 min intervals).
  • stock pool is the pool of hand selected stocks that can be bought and sold by bl-sh.
  • owned stock is stock that has been purchased (one of the stocks from the stock pool)
  • total value is the amount of shares owned multiplied by current stock price. E.G. 10 shares of a stock with a price of $1 is $10.


Various calculations and values are performed prior to performing the trade logic. This pseudo code is really the trade logic. Supporting code has been left out for clarity's sake.

First sell phase: Of the stocks owned, do nothing if the stock has not gained in total value at least $13. If the stocks owned have gained more than $13, do nothing if stock has the highest derivative. If the stock has gained more than $13 and does not have the highest derivative, sell stock.

Second sell phase aka abandon ship clause: If owned stock price has dropped by more than 30% of purchase value with a negative derivative, and there is a well performing (well performed defined as having a derivative of more than 0.4) stock or stock price has dropped by more than 30% and all stocks have 0 or less derivatives sell all owned stock.

If money is available (at least $999), buy as many shares as possible (allow for trading fee) of stock with the highest derivative. Do not buy anything if no stock's derivative is greater than 0.

In this way, bl-sh is optimized to purchase well-performing stocks, but only if no money will be lost in the transaction. In a down-sliding market, bl-sh will protect assets by withdrawing, regardless of losses.

Implementation Details

My first implementation of the slope subroutine, which calculates the slope of stock prices, found the difference between all points and returned their average. I've since implemented a new slope subroutine which utilizes Least Squares Regression to calculate slope.

It works like this:

N: Number of data points
ΣX: Sum of all X values
ΣY: Sum of all Y values
ΣXY: Sum of the product of all Xs and Ys
ΣX2: Sum of X's squared
M: Slope

` M = ( N*ΣXY - ΣX*ΣY ) / (N*ΣX^2 - (ΣX)^2) `

10:20am 2012-02-03

I've split test3 into test4. Test3 is still running. Test4 is running with the percentage calculation turned off. A stock will sell when it's more than $13 over purchase value ($12 fee + $1 buffer) but not if its slope.

So far after 2 days of testing, Test4 is doing better than test3.

Possible planned improvement: start calculating second derivative and use THAT for decisions. I.E. decided to buy and sell depending on if the rate of change is positive or negative.

14:30am 2012-02-01

I dumped another $5,000 into the test3 simulation (for a total of $10,000) initial invested. So far so good. Test3 is placing buys and sales more frequently (and intelligently) than test2.

11:00am 2012-02-01

I have created branch test3 of bl-sh and retired the running of test2 which fizzled out. After running for over a month's time it ended with less principal than it started with.

The two improvement goals determined from test2 have been implemented in test3: stocks that have a lower value than the previous sale price or have never been bought before and are trending upward will be bought. The stock with the most positive slope is chosen.

There may be a need to remove the lower value than previous sale price restriction (ala inflation, everything is trending up).

The gains trigger has been nudged down to 0.5% plus the $6 trading fee.

bl-sh test3 uses a slope function to determine short term trends (based on the frequency in which bl-sh runs, in this case 1 data point collected every 5 minutes with a total of 3 data points retained, thus trending is based on rolling 15 minute intervals).

Same as with test2, an initial $5,000 have been “invested.” I increased the balance by $1000 then ran the program. I repeated this 5 times. The $6 trading few may be harming me too much for $1000 level investments. To go up enough to cover the trading fee at $1000 base principal investments may be too much. If I do not see results soon I may change to 3 initial buys of $5,000.

Additionally, the cool-down period has been eliminated. Given the current parameters and purchase algorithm it is at best unnecessary and at worst would prevent good purchases.

I am hopeful for positive results within a week, a month of positive results will be enough to move forward with implementing the USAA (bank backed) back-end.

10:00am 2012-01-18

bl-sh.pl has been under performing with the below parameters:

  • Ran every 5 minutes from market open to market close.
  • Sell all shares of a stock when it's value has gone up 2% + $6
  • If funds are available, buy as many stocks as possible if

This program is optimized to buy poor performing stocks.

I must alter the program to find not the stock that has gone down the most, but rather the stock that has gone down the most and has the most positive derivative.

The planned changes are as follows:

  • To do this I will change the $stock{stockname}{price} from a scalar to an array/list containing a history of price values. 3 values is what I'm leaning towards, perhaps as many as 5. Too many and I will get too much of a history for (what I hope to be) a short term purchase. Values will be shifted off, and pushed on to maintain order.
  • Nudge down the sale trigger. Originally calculations were done for purchases and sales done once a day. Idealistically on year at 2% compounded daily (on market days) would yield 265.2 times principal investment. Since I've adjusted to running every 5 minutes from market open (9:30am EST) to market close (4:00pm EST). Even with a sale trigger of 0.5%, the ideal return is 20,381.4 times principal. A 0.5% change has shown to be far more likely than a 2% change.

Eventually I'd like to take the derivative into account for sale-logic purposes.