Milktrader

Iterating Until Convergence

Wednesday, July 28, 2010

My 4th C Program

#include <stdio.h>  

int main (void) {

        double  O,H,L,C;
        double typical ;

        scanf("%lf %lf %lf %lf", &O, &H, &L, &C);

        typical = (H+L+C)/3;

        printf("The Day's Typical Price Was: %.2lf  \n", typical); 

        return 0;
}    

A little progress at a time. Instead of just taking inputs and creating re-arranged outputs as I did in My First C program, this version manipulates input data with the formula that calculates the day's typical price and then returns the value to the output. I've also graduated from terminal input and terminal output. The next step is to get data from a file that I previously typed, and then output data to a new file.

First, let's get the above code into a file, which we will call pivot4.c. From command line:

milktrader@ubuntu:~/Dropbox/desk.mySimpleC/trunk$  vim pivot4.c

This opens up a new file and calls it pivot4.c. Type in the above code and then, after getting back to Normal mode in vim, type:

:x

This will simultaneously save and close the file. And bring us back to our command line. For readability, I'm only showing the command line from $ sign for the rest of this blog. We want an input file called data4 so we type:

$ vim data4

And we type the following (OHLC SPX prices for July 27, 2010) into the file:

1117.36 1120.78 1109.78 1113.84

Again, we save and close the file, which sends us back to the command line where the fun begins. First, let's compile our program with the following:

$ gcc pivot4.c -o pivot4

Now we assign the input (data4) and output (just invent a name as the file will be automatically created) to our executable with the following:

$ ./pivot4 < data4 > answer4

To check our output, we type the following:

$ vim answer4 

When we open this file, we find what we've been looking for. Our daily pivot for July 28, 2010. Now that's a trading application you can use. 

Best practices require us to save our work so we first add the files we created to our svn repo with this:

$ svn add pivot4 pivot4.c data4 answer4

And then we commit them with the following:

$ svn commit -m "our fourth C program"

Now it would be really cool if we could avoid typing in our data4 file next time and just get it from Yahoo or something, thus avoiding the possibility of typing errors and reducing stress on our fingers, wrists and eyeballs.


Note: if you'd rather not do the above exercise and you really want to know the answer, the file outputs The Day's Typical Price was: 1114.80. 





Monday, July 26, 2010

Bought the VIX

Quite frankly, I'm a little ambivalent about this trade. I made it nevertheless with my judgment being that we are topping now with a little too much fearlessness. I bought AUG 20 call in VIX. The option is naked long, and expires in about 3 weeks. The premium I ponied up is $5.90. I normally despise the outright purchase of long options as either speculation or hedge, but this is the VIX product, which settles to cash and is more a play on a move in volatility than a move in price action.

I believe the VIX is oversold at these levels. Will it take more than three weeks to flush out this sentiment? Possibly. For this reason I'm going small and not going "all in" on long options. My break-even for my ITM put option is simple to calculate. In about three weeks, when the VIX future (to which VIX options are keyed) settle on their appropriately weird Wednesday, I need VIX to be at $20 (option strike) plus the premium I shelled out ($5.90) for a grand total of $25.90. Anything more is profit, anything less is loss up to a maximum risk of $5.90.

Friday, July 23, 2010

My First C Program

Before I sat down to read Programming In C by Stephen Kochan, I had to set the table. This is no small matter for someone who was using Excel and Windows just a few short months ago. I wanted to do things proper, so I installed Ubuntu on my laptop via the VMware Player. I am going to learn C and I'm going to do it on a linux machine. Part of learning to program requires writing and compiling programs that the author provides for examples. Keeping track of these can become a nightmare without a system. I chose to learn the svn utility to serve this purpose. Also, I needed some basic command-line skills to get me around linux, so I got some resources to help me along there as well. And to do command line programming, you need a compiler and text editor. Okay, no bigs. I decided to learn VIM because it requires no mouse and is itself programmable (loving it, by the way). The compiler comes with Ubuntu and is called gcc.

After some fits and starts, I got myself all set to go with being able to write a simple example program in VIM, compile it with gcc, run it from command line (there is a ./ trick that took a while to figure out) and then commit it to a repository that I previously set up in my Dropbox account. Now it was a matter of going through Kochan's book and compiling all his example code. I've almost finished the book and probably will finish it within a week. After getting through pointers, it's basically knowledge acquisition and not conceptual grasping any more.

I will use C to turbo-charge some R functions that I have planned. And I will use C as a building block to learning C#, which I plan on using quit a bit in deploying neural network solutions to market prediction.

It may not seem like much, this first program of mine. But actually, I'm quite pleased. It actually works. Here is what we want to do. Enter price data from the terminal and have the program return the Open, High, Low and Close in that order. Sort of like an echo. It's a 'knock, knock, anybody there?' sort of program. But the potential is there for more functional things to come.

// Here we will randomly enter the OHLC data manually from the terminal
// And essentially spit it out in a new order, that being OHLC

#include <stdio.h>

int main (void) {
      
 float  O,H,L,C;

 printf("Enter the High of the Day: \n");
 scanf("%f", &H);
 
 printf("Enter the Low of the Day: \n");
 scanf("%f", &L);
 
 printf("Enter the Open of the Day: \n");
 scanf("%f", &O);
 
 printf("Enter the Close of the Day: \n");
 scanf("%f", &C);

 printf("The OHLC data is: %.2f, %.2f,  %.2f,  %.2f  \n", O,H,L,C); 

 return 0;

}















Feel free to copy this code and compile it yourself. Admittedly it's not very exciting. It did take me a while to get here though, and the fun is just starting. Markets, beware.






Wednesday, July 21, 2010

Random Trade System: A Thought Experiment

The programming and setup of this experiment will take some effort on my part, but it is not impossible nor insurmountable by any means. Eventually I will report the results. For now, let me detail the experiment.

--1 Choose a tradeable product (SPY, for instance)
--2 Enter Long at the Open on Day n (n=1)
--3 Set Target at 1 ATR
--4 Set Stop at 3 ATR
--5 Close trade based on whichever occurs first.
--6 Record PnL and trade duration
--7 Enter Long at the Open on Day n=2
--8 Repeat steps 3-6 (ie, target, stop, close trade, record results)
--9 Repeat steps 7,8 for m Days (take 10 years worth for starters)
--10 Repeat steps 2-9 for Shorts.

Once the data is in on the experiment, the results can be analyzed. Does the long side make money or lose money and what is its expectancy? Same question for the short side. This data on its own doesn't do much more than answer the question of whether a monkey can make money in the markets. But this is not the point of the thought experiment. We're not out to prove that "Ha, ha, a monkey can make more money than a professional money manager." This may or may not be true. We don't really care though. We care about the behavior and nature of a risk-based trading system. And a new way of thinking about benchmarks.

Note 1: in real life, you would need p number of long accounts and q number of short accounts so as to not interfere with existing trades.

Note 2: this data is particularly well-suited to a Monte Carlo (scramble the card deck) simulation. I can't wait.