Help save net neutrality! Learn more.

How would I Implement Trailing Stop

  • Mary-Anne Wolf

    Mary-Anne Wolf - 2010-02-28

    Arthur Torrey,
    an end user of JStock is my romantic partner.

    He would like a functionality to be added which
    I think he calls "Trailing Stop".
    I would like to add it for him, if I can.

    I know nothing about how JStock works yet.

    Here is how I think trailing stop could work:

    Each user can set a preference which I call
    This is a floating point number
    between 0.0 and 100.0.
    If the user does not set a preference,
    the default value is 80.0

    For each purchase of a set of stocks in a company
    there are the following values which can be set
    in what I call a trailingStop related record:
    boolean trailingStopEnabled (default value is false)
    floating point trailingStopPercent (default value is user's preference value)
    floating point historicHighPrice (default value is 0.0)
    floating point trailingStopPrice (default value is 0.0)

    I am thinking the floating point numbers could be BigDecimal.
    I am not sure, but I know declaring them as float
    could cause math bugs.

    If no record has been stored about a given
    stock purchase,
    each field is given its default value.

    When the price is updated
    of a stock that has been purchased,
    if the trailingStopEnabled flag on that purchase
    is true, then the following things happen:

    (since the default value of the flag is false,
    this can only apply if a trailingStop related
    record for the purchase exists)

    If the current price of the stock is higher
    than the historicHighPrice, then
    the historicHighPrice is set to the current price
    and the trailingStopPrice is set to
    the new historicHighPrice times the
    trailingStopPerCent for that purchase
    divided by 100.0.

    If the current price is less than or equal to
    the trailingStopPrice, then an alert is sent
    to the user telling them to sell the stock,
    and the trailingStopEnabled flag for that
    purchase is set to false.

    The user may edit any of
    the trailing stop information about a purchase,
    which will have the side effect of creating a
    record, and will need to do so at least once
    in order to enable trailingStop for a stock

    Would something like I describe fit with the
    current architecture of JStock?  If so, which
    parts of JStock should I learn about in order
    to figure out how to add this functionality?

    (I understand that I would need to agree to
    the license if I wanted to add new code.)



  • yccheok

    yccheok - 2010-03-02

    Oh, I thought JStock already has the similar feature, named "Fall Below" and "Rise Above" in Real-Time Info tab. Does that feature meet your romantic partner needs?

  • Mary-Anne Wolf

    Mary-Anne Wolf - 2010-03-02

    Thanks for responding.  As best as I understand, the "Fall Below" and "Rise Above" feature allows a user to give a constant number.  It does not allow the stop price to increase as the price of the stock increases.

    For example, if I were to buy Google stock at $100 per share, and the price of that stock moved to $122, then $101, then $147, then $134, then $200, if I had set the stop percent to 80%, then I would want the new stop price to be $160, so if the price went from $200 back down to $153 I would want to be told to sell.

    Since I don't know what the price will do when I buy, I don't know how to set the "Falls below" price.

    Is there something I missed in what "Falls below" can do?

  • yccheok

    yccheok - 2010-03-18

    I am new to trailing stop feature. Here are some of the question I would like to ask about.

    Say, we implement this trailing stop feature in JStock standalone application. Standalone application means this application is only running in your computer, and user may shut down it, or start it any time.

    (1) I buy a stock at $1.00
    I set the trailing stop to 10%
    At the end day, the last price is still $1.00.
    Hence, my stop price is $1.00 x 90% = $0.90

    (2) 2nd day, stock price goes up non-stop to $2.00
    The stop price will upgraded to $2.00 x 90% = $1.80
    Later, price drop to $1.80
    Sell signal generated, with profit gain $1.80 - $1.00 = $0.80

    But, let say that day user do not open JStock, he will lose all these information.

    (3) 3rd day, user open up JStock. That time, price is $1.50.
    Hence, stop price is upgraded from 1st day $0.90 to $1.35
    Price later drop to $1.35, user will sell it and profit gain is $1.35 - $1.00 = $0.35

    As you can see, instead of generating profit $0.80, user generates $0.35, just because he does not switch on JStock all day.

    So, does this trailing stop feature still make sense to implement in JStock? Or it is only suitable for all days running web based system?

    Trailing stop only effective if he keeps the system on and never turns off.

    Get what I am trying to say? ;)

  • Mary-Anne Wolf

    Mary-Anne Wolf - 2010-03-18

    Excellent question.

    I do not know whether JStock is designed to be run all the time or not.

    Arthur had a bad accident and is in the hospital,
    so I expect JStock is not his highest priority for some weeks.

  • yccheok

    yccheok - 2010-03-18

    :O I rather surprise to hear that

    May God bless Arthur, to recover and keep healthy very soon.