2/25/2011

Trend Following.3 - Programming (Ruby)

Using a simple programming language Ruby to do the analysis. We can verify the results (Entry and Exit) with Excel. A free book of Ruby is at SapphireSteel Software.

A simple program to find
1.the entry and exit
2.the max drawdown
3.the return of each Entry/Exit pair
4.the total return

Strategy:
1. If Crossover(+), then Entry.
2. If Crossover(-), then Exit.
3. If status is Entry and Max Drawdown is over than the stop loss limit, then Exit.


The process:
1. Assign the data array for Stock Price and Filter(moving average parameters)
2. Set up the Stop Loss ratio
3. Define state variables such as current status (in Entry or Exit)
4. Loop through each data (stock price)
      a.Calculate Filtered data
      b.If the status is in "Entry"
           Calculate the Drawdown and update the Maxdrawdown if necessary.
           If Maxdrawdown is over the stop loss limit
              Change status to "Exit"
              Calculate total return.
              Print out results.
           End If
         End If
      c.If Crossover happens
           If Up
               Change status to "Entry"
               Set Entry Price
               Reset Maxdrawndown 
           Else
               If status is in "Entry"
                  Change status to "Exit"
                  Calculate total return.
                  Print out results.
               End If
           End If
        End If
       d.Update status variables.
   End Loop
5. Print out the total return.

No comments :