Lab Assignment 2 [Genetic Algorithm]
Lab Assignment 2 [Genetic Algorithm]
Part 1 [7 points]
CSE422: Artificial Intelligence [C02]
You are developing an AI-driven cryptocurrency trading bot that optimizes its
trading strategy using a Genetic Algorithm (GA). The goal is to maximize profit
while minimizing risk by evolving key trading parameters such as:
● Stop-Loss (%) – The percentage at which a position is automatically closed
to prevent further loss.
● Take-Profit (%) – The percentage at which a position is closed to secure
profits.
● Trade Size (%) – The portion of available capital allocated per trade.
Your task is to implement a GA-based approach to find the optimal set of trading
parameters that generate the highest profit over a given set of historical price
movements.
● Stop-Loss: 2%
● Take-Profit: 5%
● Trade Size: 20% of capital per trade
[Note: We start our trading on Day-01 with $1000 capital and invest $200 since
the trade size is 20%. Then after incurring a loss of -$2.40, our capital reduces to
997.60 ($1000-$2.40). Then for the 2nd day we again start our trade with 20% of
the updated capital which is 997.60*20% = $199.52
Then on Day-5 we see the price change(%) is -2.5 which is greater than the
stop_loss threshold, so we cap the loss at 2% instead of 2.5%, thus the loss for
Day-05 is 201.39 * 0.02 = 4.03
On Day -8, we see the price change(%) is +5.8% which is greater than the
take-profit threshold, so we cap the profit at 5% instead of 5.8%, thus the profit for
Day-08 is 201.15*0.05 = 10.06]
Summary
Task Breakdown:
Trade Size (%) Percentage of capital allocated per trade 01% - 99%
Each chromosome is created with random values within the defined ranges.
The fitness function determines how profitable a strategy is over historical price
movements.
Mutation ensures genetic diversity and prevents the algorithm from getting stuck
in local optima.
Input
“Capital to Start With”: $1000
"historical_prices": [-1.2, 3.4, -0.8, 2.1, -2.5, 1.7, -0.3, 5.8, -1.1, 3.5],
"initial_population": [
{"stop_loss": 2, "take_profit": 5, "trade_size": 20}, {"stop_loss": 3, "take_profit": 7,
"trade_size": 30},
{"stop_loss": 1.5, "take_profit": 4, "trade_size": 25},
{"stop_loss": 2.5, "take_profit": 6, "trade_size": 15} ],
"generations": 10
Output
"best_strategy":
{ "stop_loss": 2.3, "take_profit": 6.2, "trade_size": 22 },
"Final_profit" : 12.5
Part 2 [3 points]
For this part randomly select two parents from the initial population of your
problem statement. Then perform a two-point crossover to generate two
children. The two points have to be chosen randomly, but it has to be made sure
the second point always comes after the first point.
For two points crossover, we have randomly chosen the following points:
1st point:- between index 2 and index 3
2nd point:- between index 6 and index 7
[In this part, you just need to iterate once and print the resultant offspring after
doing the crossover]
Part 3 [0 points]
In part 1, you selected parents through random sampling from the initial
population. Another advanced technique for parent selection is known as
Tournament Selection. Please take some time to research and understand this
method at home. Might be helpful in the near future!