PDA

View Full Version : Virtual stock exchange.



T_Kill3r
13-08-2009, 05:43 PM
ey everyone.

Im currently in grade 11, and i take IT as you'd probably guess. This year for my practical assignment i have decided to create a virtual stock exchange.
Now the thing is, i need some help because i just can't find the logic to reach what im aiming for, and i was hoping for someone who could assist me in establishing this logic, which i wont have a problem converting to code. So my appeal isnt for you guys to give me the code for what im asking, just the framework or logic which i can base it on.
The school uses Delphi by the way.

I'll start by explaining what i have in mind so far.
The game will only make use of 5 companies in which the user can invest. A 1 year history of the share prices of each of the companies will be downloaded and be put into a database. The user will be given a starting amount and he will invest where he/she chooses.
Now, what i had in mind is, the program gets the date off the pc, and uses this date to determine how many days have past since the user last logged on, and then will insert the (random)data up untill this point. Im wondering what the easiest way will be to determine how many days have passed? With months varying from 30 to 31 days, and 28 in february, will the amount of days in each month have to be pre entered and then this used to calculate the days passed? or is there an easier method? And is it possible to work on an hourly basis, for users who dont wish to 'play' at such a slow pace?
Now the second issue im having trouble with is trends. The shares cant just randomly fluctuate because it would pretty much defeat the purpose of share investing and it would just be a matter of luck. What i though would be an easy way, would be to create a random number, and then use this number as the number of days the share will increase or decrease. And in this period of increasing or decreasing, the share will have a greater chance of increasing(or decreasing) until it reaches the end of the 'trend' and another random number is created. This way the share will follow a trend, but can still increase of decrease daily during the trend.
The other idea i had in mind was to calculate the trend of the last month, weak, and day, and after comparing the three, to give a chance of increase or decrease depending on the result. It will have a larger chance to follow the monthly trend, up until a point where the monthly increase/decrease becomes too large, and the chance of following the trend will decrease until the trend reaches equilibrium or shifts in an opposite direction.
I dont know which will work better, and i dont know if there is any other more effective or easier ways to do this.
Any help or suggestions will be greatly appreciated, whether regarding the above mentioned, or just the general idea of the program

Thanks in advance

herman.tulleken
13-08-2009, 06:19 PM
One very simple way to get trend-like behaviour is to run a cumulative sum of random numbers (that must be positive or negative).

something like this:


do forever:
k = random number between -1 and 1
current_value = current_value + k

This value will generally deviate far from the starting point (it's a bit unintuitive why).

Here is an example of a typical run.

http://code-spot.co.za/blog/wp-content/uploads/2009/08/randcumsum.png

You can scale the random numbers to give you more/less granularity.

You can also scale them in relation to the time run - this way you can control how far it will deviate, and simulate equilibrium. (You can then reset this value at random times to create stock shocks!)


i = 0
do forever:
i = i + 1
k = random number between -1 and 1
k = k / i
current_value = current_value + k

Here is how that looks

http://code-spot.co.za/blog/wp-content/uploads/2009/08/randcumsum2.png

Again, you can scale values to control the effect.

1D Perlin noise is also an option, although this will always stay between two values, and it is more complicated to implement (and also a bit too organic, I think).

See here http://freespace.virgin.net/hugo.elias/models/m_perlin.htm

Hope it helps,

ht

Nandrew
13-08-2009, 06:31 PM
Oh, wow, kudos on a classy response!

BlackHawk
15-08-2009, 09:37 AM
Oh, wow, kudos on a classy response!

Seconded. I was trying to think of a way to do the trend simulation and random numbers never even entered my mind. Excellent bit to remember!

Cleric
15-08-2009, 10:56 AM
May I just recommend that you don't use the real date to show a move. A game where you make a decision once a day would kind suck. Either model it as live trade (think how iNet displays), or make it a case of clicking "next day" to add numbers.

If you need any equity theory, give me a shout, I work with this stuff daily.

T_Kill3r
15-08-2009, 06:34 PM
Thanks for the help guys, i really appreciate.
and wow herman, thanks for the effort man.

Good point cleric, i should have given it more thought. thanks for the suggestion.

Oh and just another question, whats the easiest way of getting an excel spreadsheet into an access database?

herman.tulleken
17-08-2009, 09:37 AM
You should be able to import it directly. Can't remember how it works with the old Access; with 2007 you go to the External Data tab, click on the Excel button, fill in the details (where the file is, how to import it, etc.) and you're done.

ht

AndrewJ
19-08-2009, 01:23 PM
MS Access 2003:
From the horse's mouth:
http://office.microsoft.com/en-us/access/HA010546501033.aspx

Other:
http://www.ofzenandcomputing.com/zanswers/926
http://databases.about.com/aa123100a.htm

T_Kill3r
06-10-2009, 05:03 PM
hey again.
I just have a programming issue right now. I have a database with 2 table, one has the daily share price of each company, the other has the day to day fluctuation of each share price. now i have the data in the share price table, now im making a mini program to take that data and fill in the daily fluctuations table.
heres the coding

var
Price1, Price2, k : integer;
change : real;
begin
for k := 1 to 260 do
begin
ADO.SQL.Text := 'SELECT * FROM DailySharePrice where Day = ' + IntToStr(k);
Price1 := ADO.FieldByName('PriceMTN').AsInteger;
ADO.SQL.Text := 'SELECT * FROM DailySharePrice where Day = ' + IntToStr(k+1);
Price2 := ADO.fieldbyname('PriceMTN').AsInteger;
Change := ((Price1 - Price2) / Price1) * (-100);
ADO.SQL.Text := 'UPDATE DailyFluctuations SET PriceChangeMTN = ' + FloatToStr(Change) + ' WHERE Day = ' + IntToStr(k+1);

end;
end;

only problem is, during runtime it tells me ADO : Field 'PriceMTN' not found, although it is there and the program is linked to the database and table, so i dont know what the problem is. if anyone can help or tell me whats wrong, id really appreciate it cuz im stuck right now =/

Evil_Toaster
06-10-2009, 05:54 PM
I'm not familiar with Delphi's ADO class, but wouldn't you need to call some sort of execute function to retrieve the data?

Also, what is the class name of that ADO variable?

Evil_Toaster
06-10-2009, 06:26 PM
Assuming that's a TADOQuery, try something like this:

ADO.SQL.Text := 'SELECT * FROM DailySharePrice where Day = ' + IntToStr(k);
ADO.Open();
ADO.First(); // Assumes at least 1 row was returned. Line below will fail if there isn't one.
Price1 := ADO.FieldByName('PriceMTN').AsInteger;
ADO.Close();

ADO.SQL.Text := 'SELECT * FROM DailySharePrice where Day = ' + IntToStr(k+1);
ADO.Open();
ADO.First();
Price2 := ADO.fieldbyname('PriceMTN').AsInteger;
Change := ((Price1 - Price2) / Price1) * (-100);
ADO.Close();

ADO.SQL.Text := 'SELECT * FROM DailyFluctuations where Day = ' + IntToStr(k+1);
ADO.Edit();
ADO.First();
ADO.fieldbyname('PriceChangeMTN').Value = FloatToStr(Change);
ADO.Post();
ADO.Close();

I'm really not sure if the above will actually work without running it, but from doing some Googling on the matter and checking the Delphi help, that seems to be approximately how you'd do it...

That ADO component seems really nasty to use. Surely there's some better SQL components in the newer versions of Delphi?

T_Kill3r
06-10-2009, 07:16 PM
hey thanks toaster =D i swapped some lines around, added 2 or 3, and it worked like a charm. thanks a million man

Evil_Toaster
06-10-2009, 10:09 PM
No problem, good to hear it (mostly) worked. ;)

T_Kill3r
11-10-2009, 04:12 PM
Another problem. sorry to bother again

Im want to display the profit/loss of the user in terms of each company. Now the problem i have, if the user bought x amount of shares on day 5 at a value of R5 each, and y amount of shares on day 20 at a value of R6, how do i work out the users profit/loss in terms of this company a few days later? will i have to handle it as two seperate calculations? but then what happens when the user sells z amount of shares and then sells the other half a few days later. i cant wrap my mind around how to solve this. any help will be appreciated

The Dash
11-10-2009, 07:10 PM
Another problem. sorry to bother again

Im want to display the profit/loss of the user in terms of each company. Now the problem i have, if the user bought x amount of shares on day 5 at a value of R5 each, and y amount of shares on day 20 at a value of R6, how do i work out the users profit/loss in terms of this company a few days later? will i have to handle it as two seperate calculations? but then what happens when the user sells z amount of shares and then sells the other half a few days later. i cant wrap my mind around how to solve this. any help will be appreciated

Unfortunately this type of financial math just plain sucks.

Start by drawing a timeline to represent all the information. That usually helps me.

I would do it thus:

Work out the value of the users shares at the time of calculation. This would be the number of shares*share worth. To purchase or sell shares, just increase or decrease the number of shares the user has.
This will give the actual value of all the users shares.

In a separate calculation, keep track of the amount the player has spent purchasing shares, and the amount received from selling them.

Profit/Loss is then worked out as:
x=[Amount used to purchase shares]
y=[Amount received from sale of shares]
z=[Current share value]

Profit/Loss = z - (x-y)

IE: if the actual value of the shares is more than the amount the player has spent purchasing them, they have made a loss.