Australian (ASX) Stock Market Forum

Adventures in AI

Joined
20 July 2021
Posts
8,474
Reactions
11,700
Within the next few years it will be driving most new cars
as a serial pedestrian , i look forward to ( my ) insurance claims , i think i will go for pain and suffering as it will be tough to prove the collision did the damage to ( previously ) damaged body parts
 
Joined
28 June 2019
Posts
1,070
Reactions
2,511
The issue of the black box nature is nothing new to AI, nor is it new to algo trading either. Most would consider what we do to be tinkering with an opaque box. Valid claims and nothing to ignore, though. AI is largely the application of statistical models to data to reach an outcome. I'm sure there is a proper (i.e. better) description but that is how I think of it. There are new quants at large firms every day who build elaborate statistical models to find an edge in the market. It's the reason why firms like to hire PhD's in maths, physics, or similar as they are familiar with this process.

We are hobbyists, though, and so we take a hobbyist approach. I think there is something to be gained.

Btw, I'd be happy to take a 15-24 CAGR if those are real results.
 
Joined
8 June 2008
Posts
11,107
Reactions
15,283
Not to mention future leaks.

It was encouraging to know that the model could learn to use future leaking data, even just one column of it and produce unrealistically good results. Just like AB.

Hiding part of the process: This is an interesting topic. My Neural Network has 100's of millions of connections and weights. While you can view internally the structure, there is no way to analyse or predict what's going on under the hood, you can only really measure the performance of the outputs. You can never know how it will react to any particular change in input.

How much would you trust it with your money ? .. Within the next few years it will be driving most new cars.
I bought a very cheap Chinese ice car 26k driveaway mg zst
When activated, it drives up to legal speed limit, slows down in curves and when following a car ahead, keeps me in the lane and follows that lane.
I could just not touch anything hands and or feet and happily cruise along.
After a few seconds, it detects if I do not hold the steering wheel and forces me to at least touch it..but yes...life on it.
I somewhat do not like this too much and prefer manual mode.
I think this is probably the same with systems especially if $ at play is high
 
Joined
8 June 2008
Posts
11,107
Reactions
15,283
The issue of the black box nature is nothing new to AI, nor is it new to algo trading either. Most would consider what we do to be tinkering with an opaque box. Valid claims and nothing to ignore, though. AI is largely the application of statistical models to data to reach an outcome. I'm sure there is a proper (i.e. better) description but that is how I think of it. There are new quants at large firms every day who build elaborate statistical models to find an edge in the market. It's the reason why firms like to hire PhD's in maths, physics, or similar as they are familiar with this process.

We are hobbyists, though, and so we take a hobbyist approach. I think there is something to be gained.

Btw, I'd be happy to take a 15-24 CAGR if those are real results.
The same reason I am not attracted in buying a trading black box system and following its instructions wo knowing the inner side.
But many are happy with that approach
And if results are here, who can blame them
 
Joined
29 June 2007
Posts
217
Reactions
335
The issue of the black box nature is nothing new to AI, nor is it new to algo trading either. Most would consider what we do to be tinkering with an opaque box. Valid claims and nothing to ignore, though. AI is largely the application of statistical models to data to reach an outcome. I'm sure there is a proper (i.e. better) description but that is how I think of it. There are new quants at large firms every day who build elaborate statistical models to find an edge in the market. It's the reason why firms like to hire PhD's in maths, physics, or similar as they are familiar with this process.

We are hobbyists, though, and so we take a hobbyist approach. I think there is something to be gained.

Btw, I'd be happy to take a 15-24 CAGR if those are real results.

All the large firms would have teams of people now. Even something as simple as institutions trying to buy $100 Million dollars of BHP. This can't be done in one hit and used to be outsourced, but now AI could do it for them in house. These are things we take for granted.

As hobbyists we have limited resources, time and money. I'd love to go and buy a $3K GPU (or 2 or 3) to run bigger networks faster, but until something is proven and i REALLY need it, it's a pipe dream. I also limit myself to a few hours a morning, then i need to drag myself away.

I'm not convinced the results are stable enough yet to even consider trading it and it was never built with that in mind. There is no entry or exit mechanism, no market filter, stops or anything, just simple buy and hold for 1 week. Even so it appears to have a positive expectancy over the years, which is an excellent sign.

Still scratching the surface, there's a long way to go yet unfortunately.
 
Joined
29 June 2007
Posts
217
Reactions
335
Here are some findings from interrogating the network this morning.

I implemented the rolling window last night, and trained annual networks at 5 years of data, 2005 through to 2023. Running the model through the test years from 2005 the results exceeded the NDX index in almost every year since 2005. The exceptions were 2008 , 2011 and 2023 (so far). However, the results were worse than when i trained on 10Y of data .. :mad: . Back to 10Y rolling windows tonight.


When i looked at the outputs, there wasn't a whole lot of difference between the top and bottom for every stock, every day. basically it's predictions worst->best = 0.423 to 0.432 which is only 0.009 (2%) difference in values between best and worst, so there's not a great deal of definition there. There aren't the earth shattering "BUY ME" signals that one might hope for.

I think this means that the network converged to a value which is virtually the average of the market. Not quite what i hoped for, but probably not unexpected. Each set of input values produces roughly 50% up, 50% down results and no sets provide consistant up values. However that little 2% difference is enough to push expectancy into positive territory vs $NDX.

Flipping (buying the worst) does produce consistantly worse results over the years, so that sort of confirms that we MIGHT have a slight edge in there.
 
Joined
29 June 2007
Posts
217
Reactions
335
1697351245389.png

I retrained the annual networks and increased their dimensions considerably. I increased the network size from 70MB to 1GB. Most years benefitted with a massive increase in return.

The downside was 2008 MDD was a staggering 74%. No WAY is this acceptable. The index dropped 49%, but we still want to do much better than the index in both return and risk.

It's pretty clear that the AI has chosen high Alpha stocks with massive volatility to gain the best upside. In a falling market though that means being exposed to massive price swings into negative territory. Unfortunately it's doing exactly what i've asked it to do, pick the stocks that it thinks will go up the most in the next week, which in the long run it has ... AT A COST.

I still have some work to do on the environment that the AI exists in. Currently it suffers from survivorship bias. It also includes stocks that may not have been in the NDX at the time of testing. These 2 issues are next on the hit list.

Then i'll get to work on fixing 2008 !!!!
 
Joined
28 June 2019
Posts
1,070
Reactions
2,511
2008 doesn't look good there, but finished at 49% EOY. The -74% would not be tradeable. Your AI fund would likely be an 'experimental fund', that is higher risk and higher 'risk of ruin'. It's how I would think of it. You could limit total capital allocated to it, you could also implement a hard rule of shutting off at a certain MDD. The rule for turning back on would need some thought.

Your points on the AI taking on higher risk, higher volatility stocks to maximise profits reminded me of Modern Portfolio Theory/Markowitz model. I remember seeing a python library where someone had coded it up and was using it. It would take up high volatility stocks and balance it with low volatility stocks based on the math. It might give some inspiration for your work and see how some deal with the issues that come up with the markowitz model.

Edit: python library is here, https://pyportfolioopt.readthedocs.io/en/latest/index.html
 
Joined
29 June 2007
Posts
217
Reactions
335
2008 doesn't look good there, but finished at 49% EOY. The -74% would not be tradeable. Your AI fund would likely be an 'experimental fund', that is higher risk and higher 'risk of ruin'. It's how I would think of it. You could limit total capital allocated to it, you could also implement a hard rule of shutting off at a certain MDD. The rule for turning back on would need some thought.

Your points on the AI taking on higher risk, higher volatility stocks to maximise profits reminded me of Modern Portfolio Theory/Markowitz model. I remember seeing a python library where someone had coded it up and was using it. It would take up high volatility stocks and balance it with low volatility stocks based on the math. It might give some inspiration for your work and see how some deal with the issues that come up with the markowitz model.

Edit: python library is here, https://pyportfolioopt.readthedocs.io/en/latest/index.html
Thanks for the link, there's a lot of information in there, lots to digest and some to implement. :xyxthumbs

When i implemented the 3 outputs for the 10,50,90 percentiles, i was hoping it would create it's own risk reward type scenario, but it's not quite doing that and it just chooses the path that leads to the best end, regardless of the pain. I am starting to understand how evil it really is.

I am only rewarding it for higher gains ( so to speak), so the model has no concept of bad trades or bad times. I need to introduce a punishment for bad trades to stop it taking on riskier trades, maybe with an exponential punishment .. bigger risk, much bigger slap. Hopefully that would smooth out both the equity curve and reduce the MDD as well.

FML .... I thought raising children was bad !!!
 
Joined
20 July 2021
Posts
8,474
Reactions
11,700
As hobbyists we have limited resources, time and money. I'd love to go and buy a $3K GPU (or 2 or 3) to run bigger networks faster, but until something is proven and i REALLY need it, it's a pipe dream.


maybe not , key if you want to trade ( or just analyze data ) is fast internet speeds , either to your broker/platform and/or your news feed ( live data or 'sensitive announcements )

unless you are trying to buy/sell large positions ( in smallish parcels ) that part is not hard a good platform/broker can do that , all you need is the software to identify targets and decide when to pull the trigger ( fairly quickly )

for example i might wake up tomorrow morning and decide i can grab some ILU at $7 , now without a fancy rig i can throw an order in premarket ( or even during the trading session )

all you need the AI to do FIRST is flag ILU ( or whatever you trading ) as a possible trade that day , now if you are setting trailing stops after buying that is a little more complicated do you trail at 3% or 5% for example

remember history only rhymes , it might not play out exactly the same next time ( so what kicks goals this year might be a trail of tears in 3 years time in the same scenario )

remember most computers are only fancy adding machines with a typewriter attached ( the first software spun out of print servers ))

very fast and colourful now , but until they think for themselves

Abacus V Modern Calculator (1967)​



this has been repeated over the decades ( by others ) the secret is at data entry speeds ( even on modern computers )

however once you get the computer talking to the broker/platform ( needing communication elsewhere the computer grabs the advantage
 
Joined
28 June 2019
Posts
1,070
Reactions
2,511
Thanks for the link, there's a lot of information in there, lots to digest and some to implement. :xyxthumbs

When i implemented the 3 outputs for the 10,50,90 percentiles, i was hoping it would create it's own risk reward type scenario, but it's not quite doing that and it just chooses the path that leads to the best end, regardless of the pain. I am starting to understand how evil it really is.

I am only rewarding it for higher gains ( so to speak), so the model has no concept of bad trades or bad times. I need to introduce a punishment for bad trades to stop it taking on riskier trades, maybe with an exponential punishment .. bigger risk, much bigger slap. Hopefully that would smooth out both the equity curve and reduce the MDD as well.

FML .... I thought raising children was bad !!!

Yea, there's a lot of info there. It's been on the list of things to look at for the future. It's a long list unfortunately, lol.

I thought I had seen a tutorial where they did just that, and had punishments for risk that was too high. But it's been a while and I can't remember where that tutorial was so can't verify. Rewards for higher returns will just result in higher volatility, which you are seeing.

I'm too much of a n00b, but like the link I sent you there may be some credence is having an aim for sharpe/sortino or annualised volatility instead of max gain. Or maybe that'll just turn it into the markowitz model, but in AI form?
 
Joined
29 June 2007
Posts
217
Reactions
335
maybe not , key if you want to trade ( or just analyze data ) is fast internet speeds , either to your broker/platform and/or your news feed ( live data or 'sensitive announcements )

unless you are trying to buy/sell large positions ( in smallish parcels ) that part is not hard a good platform/broker can do that , all you need is the software to identify targets and decide when to pull the trigger ( fairly quickly )

for example i might wake up tomorrow morning and decide i can grab some ILU at $7 , now without a fancy rig i can throw an order in premarket ( or even during the trading session )

all you need the AI to do FIRST is flag ILU ( or whatever you trading ) as a possible trade that day , now if you are setting trailing stops after buying that is a little more complicated do you trail at 3% or 5% for example

remember history only rhymes , it might not play out exactly the same next time ( so what kicks goals this year might be a trail of tears in 3 years time in the same scenario )

remember most computers are only fancy adding machines with a typewriter attached ( the first software spun out of print servers ))

very fast and colourful now , but until they think for themselves

Abacus V Modern Calculator (1967)​



this has been repeated over the decades ( by others ) the secret is at data entry speeds ( even on modern computers )

however once you get the computer talking to the broker/platform ( needing communication elsewhere the computer grabs the advantage

This bit i have covered. I have an AWS server in singapore that trades with IB in Hong Kong and some custom software to do real time trading. Current order execution is between 5mS to 20mS from me placing a trade to IB sending confirmation or order being placed.

The decision to use AWS was less about speed though and more about reliability and security.

I have a server in the garage that i was running it all through, but the number of attempts to hack into it was ridiculous, thousands and thousands a day. Also power outages, internet outages, breakdowns ( servers break down a lot) led me to just outsource it. AWS is awesome, After locking the access down at the router level i don't have any attempts at all to get in EVER, no hardware issues to deal with etc.

Real Time Intraday trading would be the ultimate goal, but for now I'm learning and evaluating to see where things lead.
 
Joined
29 June 2007
Posts
217
Reactions
335
Yea, there's a lot of info there. It's been on the list of things to look at for the future. It's a long list unfortunately, lol.

I thought I had seen a tutorial where they did just that, and had punishments for risk that was too high. But it's been a while and I can't remember where that tutorial was so can't verify. Rewards for higher returns will just result in higher volatility, which you are seeing.

I'm too much of a n00b, but like the link I sent you there may be some credence is having an aim for sharpe/sortino or annualised volatility instead of max gain. Or maybe that'll just turn it into the markowitz model, but in AI form?
It's a tricky one. I don't want to diminish the ability to pick high returning stocks in sideways or upward markets, just the ability to decide when to apply it (Not in downward markets).

I am going to add index data into the models input, that extra dimension might allow it to learn good or bad periods in the overall market and make adjustments to it's algorithm. However the more dimensions i add the less it will learn or generalise.

I like your idea about the sharpe ratio, so i might try that as a target instead of momentum and see if that behaves better in weaker markets or gives a smoother equity curve.

Looking Forward: I will need to create a market sentiment model to gauge the direction the market is going. It could be something as simple as just the index, but i will try adding things such as gold, bonds, oil etc. The output from the sentiment model can be fed into the market price models OR the agent can decide which market model to use based on the sentiment model.

Things are getting deep now !!!!
 
Joined
28 June 2019
Posts
1,070
Reactions
2,511
It's a tricky one. I don't want to diminish the ability to pick high returning stocks in sideways or upward markets, just the ability to decide when to apply it (Not in downward markets).

I am going to add index data into the models input, that extra dimension might allow it to learn good or bad periods in the overall market and make adjustments to it's algorithm. However the more dimensions i add the less it will learn or generalise.

I like your idea about the sharpe ratio, so i might try that as a target instead of momentum and see if that behaves better in weaker markets or gives a smoother equity curve.

Looking Forward: I will need to create a market sentiment model to gauge the direction the market is going. It could be something as simple as just the index, but i will try adding things such as gold, bonds, oil etc. The output from the sentiment model can be fed into the market price models OR the agent can decide which market model to use based on the sentiment model.

Things are getting deep now !!!!

Your market gauge will be interesting. the 'strength' of its ranking for market (i.e. strong bear, strong bull, weak bear, weak bull, sideways, etc.) could be a weighting factor for allocated funds. Perhaps that's getting too deep into things, but if the model is pursuing a long strategy only and it's a strong bull market, then you'd want full allocation. If it's a weak bull market, then maybe 0.4 * alloc would be the way to go? And so on.

THe market sentiment could be a different model. The outputs of that model could then be used by your trading model. It may be best to develop them independently of each other.
 
Joined
29 June 2007
Posts
217
Reactions
335
Your market gauge will be interesting. the 'strength' of its ranking for market (i.e. strong bear, strong bull, weak bear, weak bull, sideways, etc.) could be a weighting factor for allocated funds. Perhaps that's getting too deep into things, but if the model is pursuing a long strategy only and it's a strong bull market, then you'd want full allocation. If it's a weak bull market, then maybe 0.4 * alloc would be the way to go? And so on.

THe market sentiment could be a different model. The outputs of that model could then be used by your trading model. It may be best to develop them independently of each other.

Definitely a separate model. One thought is to have an agent that would take the signals from various NN's. Market sentiment, price rise, reversal, breakout etc and make the best decision and allocation based on these factors. That's a long way off yet though.

Keeping each NN doing an individual task is probably the best for performance and being modular. It also means they can be trained in parallel.

===========================================

2 big changes today
- Implemented the Nasdaq 100 Current and Past and Stock In Index checking in both training and testing,
- Modified the Input Scaling to something that seems more logical to me

Removing the survivorship bias will mimic more closely what real world trading would do. Doing it though has been detrimental to the prediction performance, even after retraining :confused:. Guess that was to be expected.

The input scalers were implemented using tensorFlow's inbuilt functions, which i feel lost some importance when scaled individually. The custom scaler hopefully will retain some of the information.
 
Joined
29 June 2007
Posts
217
Reactions
335
Really good thread.
I was learning Python to try something similar. Very slow process so far.

I believe those that get in early will be able to enjoy profits until the rest catch on.

You don't really need to know to much of python, this is actually my first Python project. I've spent a lot of years programming in c/c++ though.

Understanding programming concepts, Some Machine Learning concepts and some basics in Python is really what you need and chatGPT can fill in the blanks. The nitty gritty stuff you learn along the way (I hope).
 
Joined
29 June 2007
Posts
217
Reactions
335

LSTM (Long Short-Term Memory) Layers:​

  1. Type: Recurrent Neural Network (RNN) layer.
  2. Purpose: To process sequences and remember patterns over long sequences. They are specifically designed to avoid long-term dependency issues.
  3. Structure: Consists of memory cells that are connected through gates (input, forget, and output). This structure allows LSTMs to maintain a state across sequences, effectively "remembering" patterns over time.
  4. Applications: Primarily used for time series forecasting, sequence-to-sequence tasks like language translation, and any application where the order and context of input data over time is important.
  5. Characteristics: Can be computationally expensive due to their recurrent nature. They are more complex in structure than Dense layers.

Dense (Fully Connected) Layers:​

  1. Type: Standard neural network layer.
  2. Purpose: To perform a linear transformation of its inputs, followed by an activation function. It doesn't have any memory of previous inputs.
  3. Structure: Every neuron in a Dense layer is connected to every neuron in the previous layer (hence "fully connected"). The layer applies a weight matrix, bias, and then typically a non-linear activation function.
  4. Applications: Can be used in any neural network. Often seen as the last layer(s) in convolutional neural networks (CNNs) and also used in traditional multi-layer perceptrons (MLPs).
  5. Characteristics: Simpler in structure and function compared to LSTMs. Doesn't maintain any internal state or memory of past inputs.

Key Differences:​

  • State: LSTMs maintain an internal state that allows them to process and remember sequences, while Dense layers don't have this capability.
  • Complexity: LSTMs are more complex due to their gate and cell mechanisms, whereas Dense layers are simpler linear layers.
  • Use Cases: LSTMs are specifically designed for sequence data, while Dense layers are general-purpose layers used in various types of networks.
  • Connections: In Dense layers, each neuron is connected to every neuron in the previous and next layers. In contrast, LSTMs have a more intricate structure with gates and cell states.
 
Joined
29 June 2007
Posts
217
Reactions
335
My mission today is to convert my Simple NN to an LTSM.

I have done this before, but i know that training is slower and memory requirements are higher !!!


While i won't be following this tutorial, It could be a good reference to help others understand and or follow.
 
Top