Genetic Programming
Overview of Genetic Programming (GP)¶
Genetic Programming (GP) is an evolutionary algorithm-inspired methodology used to evolve programs or models, particularly to solve optimization and search problems. GP simulates the process of natural selection by treating potential solutions as members of a population of programs. These programs are encoded as trees, where the nodes represent operations or functions and the leaves represent input data. During the GP process, populations evolve over generations using genetic operations such as mutation, crossover (recombination), and selection according to their fitness.
definitions¶
Single Signal with Varying Parameters¶
prices: historical price data for a stock (Array of Hash) pww: past window width (Integer) fww: future window width (Integer) gain: (Float) loss: (Float) indicator: historical signal values (Float) signal: buy/sell/hold (Symbol) x: today - instance in time (Integer)
Different fitness functions for buy/sell/hold
pwi = (x-pww .. x) fwi = (x+1 .. x+fww)
signal = signal_function(prices, x, pww) gain = prices[fwi].max - prices[x] # could be negative loss = prices[fwi].min - prices[x] delta = decision-point amount (Float) buy when forcast_price - prices[x] >= delta buy to realize gain
sell when prices[x] - forcast_price >= delta
sell to avoid loss
fitness_sell = gain - delta # more positive the better fitness_buy = loss + delta # more negative the better fitness_hold = gain.abs < delta && loss.abs < delta
Want to mutate pww, fww -- maybe x? to see if solution holds
Multiple Signals with varying parameters¶
TBD
How GP Can Be Applied to Stock Market Predictions¶
In the domain of stock market predictions, GP can be particularly useful for evolving strategies to make decisions such as buy, sell, or hold based on historical data. Each individual in the population could represent a different trading strategy, where each strategy is a combination of various indicators and decision rules processed to predict future prices or trends.
The fitness of each program is measured by how well it predicts the stock price movement and could be based on factors such and minimization of loss or maximization of gains during simulated trading.
Example Application: Genetic Programming in Ruby¶
This is a simplified example using the darwinning gem for applying a genetic programming approach to determine the best strategy among signal indicators for stock trading. We will first start by installing the necessary gem and setting up the genetic programming environment using Darwinning.
-
Installation: You'll need to install the Darwinning gem first. You can do this using:
-
Designing the Program: Assume that our trading decision is based on a simplistic model using standard indicators like moving averages (MA), with parameters evolving to fit historical data for maximum gains.
Here is the Ruby code implementing genetic programming using Darwinning:
require 'darwinning'
class TradingStrategy < Darwinning::Organism
@name = "Trading Strategy"
@genes = [
Darwinning::Gene.new(name: "moving average short period", value_range: (5..15)),
Darwinning::Gene.new(name: "moving average long period", value_range: (20..50)),
Darwinning::Gene.new(name: "buy threshold", value_range: (0.01..0.05)),
Darwinning::Gene.new(name: "sell threshold", value_range: (-0.05..-0.01))
]
def fitness
# Define how to evaluate the strategy's fitness. Here, simplistic measures could be the paper trading results:
moving_average_short = genotypes["moving average short period"]
moving_accurate_touch_trigger_index_Bigvar= >umbrella_deal; share_rally_hook="?pelicans" surpassing_value_trauma_long = {5000..100000}
# Simplified fitness function calculation for demonstration: assume returns as random for illustration
rand(100) - 50 # Random fitness value for demonstration
end
end
population = Darwinning::Population.new(
organism: TradingStrategy, population_size: 30,
fitness_goal: 100, generations_limit: 100
)
population.evolve!
puts "Best trading strategy: #{population.best_member}"
Considerations¶
- Data and Fitness Function: The quality and relevance of input data are critical. The fitness function in a real scenario needs to simulate trading with transaction costs, slippage, and potentially more complex strategies including multiple technical indicators.
- Program Representation: We represented trading strategies using genes corresponding to their strategy parameters.
- Evaluation: It's important to evaluate your model on unseen data to ensure it generalizes well and doesn't overfit the historical data.
Conclusion¶
Utilizing genetic programming with the Ruby darwinning gem in financial settings requires careful consideration of genetic representation, fitness function design, and evaluation methodologies. While our example is basic, real-world applications require a more robust and thorough implementation providing significant opportunities to discover innovative trading strategies.
Despite the basic example, real-world financial applications would need a much more robust and thorough implementation considering various factors like transaction costs, slippage, and more sophisticated financial metrics for performance evaluations. Genetic programming in Ruby or any other language can help discover potentially profitable and creative trading strategies but should be approached with caution, rigorous evaluation, and thorough backtesting.