avatarRayne Sanning

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

6469

Abstract

market dynamics. This is where feature engineering becomes invaluable. By transforming and synthesizing the raw data using established technical indicators, we can provide the model with enriched insights that could explain underlying market patterns and trends. Such enriched data can significantly improve the model’s ability to anticipate future price movements.</p><p id="07ea">For this forecast, we will utilize a couple of technical indicators for our feature engineering like RSI, MACD, Bollinger Bands, Parabolic SAR, and Stochastic Oscillator. Additionally, we introduce lag features to capture temporal dependencies, ensuring our model benefits from both current and historical contexts.</p><p id="f61d"><b>Let’s calculate the features we will utilize in this forecast</b></p><div id="d4ed"><pre><span class="hljs-comment"># Compute RSI</span> df[<span class="hljs-string">'momentum_rsi'</span>] = RSIIndicator(close=df[<span class="hljs-string">'Close'</span>]).rsi()

<span class="hljs-comment"># Compute MACD</span> macd = MACD(close=df[<span class="hljs-string">'Close'</span>]) df[<span class="hljs-string">'trend_macd'</span>] = macd.macd() df[<span class="hljs-string">'trend_macd_signal'</span>] = macd.macd_signal() df[<span class="hljs-string">'trend_macd_diff'</span>] = macd.macd_diff()

<span class="hljs-comment"># Compute Bollinger Bands</span> bollinger = BollingerBands(close=df[<span class="hljs-string">'Close'</span>]) df[<span class="hljs-string">'volatility_bbm'</span>] = bollinger.bollinger_mavg() df[<span class="hljs-string">'volatility_bbl'</span>] = bollinger.bollinger_lband() df[<span class="hljs-string">'volatility_bbh'</span>] = bollinger.bollinger_hband()

<span class="hljs-comment"># Compute Parabolic SAR</span> psar = PSARIndicator(high=df[<span class="hljs-string">'High'</span>], low=df[<span class="hljs-string">'Low'</span>], close=df[<span class="hljs-string">'Close'</span>]) <span class="hljs-comment"># Assuming you have 'High' and 'Low' columns in your df</span> df[<span class="hljs-string">'trend_psar'</span>] = psar.psar()

<span class="hljs-comment"># Compute Stochastic Oscillator</span> stochastic = StochasticOscillator(high=df[<span class="hljs-string">'High'</span>], low=df[<span class="hljs-string">'Low'</span>], close=df[<span class="hljs-string">'Close'</span>]) <span class="hljs-comment"># Assuming you have 'High' and 'Low' columns</span> df[<span class="hljs-string">'momentum_stoch'</span>] = stochastic.stoch() df[<span class="hljs-string">'momentum_stoch_signal'</span>] = stochastic.stoch_signal()

<span class="hljs-comment"># Create Lag Features</span> df[<span class="hljs-string">'Close_Lag1'</span>] = df[<span class="hljs-string">'Close'</span>].shift(<span class="hljs-number">1</span>)

<span class="hljs-comment"># Drop NaN values introduced due to lag features and indicators</span> df = df.dropna()

<span class="hljs-comment"># Define features and target</span> X = df[[<span class="hljs-string">'momentum_rsi'</span>, <span class="hljs-string">'trend_macd'</span>, <span class="hljs-string">'trend_macd_signal'</span>, <span class="hljs-string">'trend_macd_diff'</span>, <span class="hljs-string">'volatility_bbm'</span>, <span class="hljs-string">'volatility_bbl'</span>, <span class="hljs-string">'volatility_bbh'</span>, <span class="hljs-string">'trend_psar'</span>, <span class="hljs-string">'momentum_stoch'</span>, <span class="hljs-string">'momentum_stoch_signal'</span>, <span class="hljs-string">'Close_Lag1'</span>]] y = df[<span class="hljs-string">'Close'</span>]</pre></div><p id="712f">The above code is organizing the dataset <code>df</code> into input features and a target variable for our model. The input features, captured under <code>X</code>, consist of various the features we calculated on and previously defined. The target variable, denoted by <code>y</code>, is the <code>Close</code> column, representing the daily closing price of EUR/USD, which our model aims to predict based on the provided features.</p><p id="bdfb"><b>Model Initialization and Training</b></p><div id="a7d2"><pre><span class="hljs-comment"># Initialize the model</span> model = xgb.XGBRegressor( learning_rate=<span class="hljs-number">0.75</span>, n_estimators=<span class="hljs-number">200</span>, max_depth=<span class="hljs-number">5</span>, subsample=<span class="hljs-number">0.9</span>, colsample_bytree=<span class="hljs-number">0.8</span>, colsample_bylevel=<span class="hljs-number">0.8</span>, gamma=<span class="hljs-number">0</span>, min_child_weight=<span class="hljs-number">1</span> )

<span class="hljs-comment"># Train the model</span> model.fit(X_train, y_train)</pre></div><p id="7ce9">Continuing from the previously discussed data preparation, this section of code dives into the model initialization and training phases using XGBoost. The <code>xgb.XGBRegressor()</code> initializes a regression model with specified hyperparameters to optimize the forecast. Key parameters include a learning rate of <code>0.75</code>, which determines the step size at each iteration while optimizing, <code>200</code> estimators or trees, and a maximum depth of <code>5</code> for each tree, among others. These hyperparameters play a role in controlling the model’s complexity and fit to the data.</p><p id="4e91">After initializing, the model is trained on the <code>X_train</code> and <code>y_train</code> datasets using the <code>fit</code> method. This step allows the model to learn the underlying patterns from the training data, preparing it to make future predictions on unseen data.</p><p id="95cf"><b>Performance Evaluation and Testing</b></p><div id="a823"><pre><span class="hljs-comment"># Predict on the test set</span> y_pred = model.predict(X_test)

<span class="hljs-comment"># Calculate performance metrics</span> mae = mean_absolute_error(y_test, y_pred) mse = mean_squared_error(y_test, y_pred) rmse = np.sqrt(mse)

<span class="hljs-built_in">print</span>(<span class="hljs-string">f"Mean Absolute Error: <span class="hljs-subst">{mae}</span>"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Mean Squared Error: <span class="hljs-subst">{mse}</span>"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Root Mean Squared Error: <span class="hljs-subst">{rmse}</span>"</span>)

y_train_pred = model.predict(X_train)</pre></div><p id="8c56">After training the

Options

model on the historical data we evaluate its performance on unseen or test data. Using the <code>predict</code> method of the trained model, predictions (<code>y_pred</code>) are generated for the test dataset <code>X_test</code>. Subsequently, to assess the accuracy and reliability of these predictions, various performance metrics are computed:</p><ul><li><b>The Mean Absolute Error (MAE)</b> provides an average magnitude of errors between predicted and actual values.</li><li><b>The Mean Squared Error (MSE) </b>squares these errors to emphasize larger discrepancies.</li><li><b>Root Mean Squared Error (RMSE) </b>is the square root of MSE, providing error in the same units as the original data.</li></ul><p id="c92b">These metrics are then printed for clear visibility. We concludes by also predicting on the training set (<code>X_train</code>) with <code>y_train_pred</code>, to further analyze and compare the model’s performance on both training and test datasets.</p><p id="44c0">The following output displays the performance metricswhich assess the accuracy of our model’s predictions:</p><div id="6743"><pre><span class="hljs-attribute">Mean</span> Absolute Error: <span class="hljs-number">0</span>.<span class="hljs-number">009141215039947168</span> <span class="hljs-attribute">Mean</span> Squared Error: <span class="hljs-number">0</span>.<span class="hljs-number">000303615460154008</span> <span class="hljs-attribute">Root</span> Mean Squared Error: <span class="hljs-number">0</span>.<span class="hljs-number">017424564848340058</span></pre></div><ul><li><b>Mean Absolute Error (MAE): </b>At 0.0091, it shows the model’s average absolute deviation from the actual values.</li><li><b>Mean Squared Error (MSE):</b> With a value of 0.0003036, it indicates the average squared error, emphasizing larger mistakes.</li><li><b>Root Mean Squared Error (RMSE):</b> At 0.0174, it provides the average error in the original unit, illustrating the typical magnitude of error.</li></ul><p id="7500">The relatively low values across these metrics suggest that the model has a good degree of accuracy in its predictions. The model appears to be reliably forecasting the target variable, depicted with minimal deviations in the forecasted data when compared to the actual data.</p><p id="be3b"><b>Data Visualization</b></p><div id="2b49"><pre><span class="hljs-comment"># Create a new DataFrame for visualization</span> viz_df = pd.DataFrame({<span class="hljs-string">'True'</span>: y_test, <span class="hljs-string">'Predicted'</span>: y_pred})

<span class="hljs-comment"># Concatenate the training data for a complete view</span> viz_df_train = pd.DataFrame({<span class="hljs-string">'True'</span>: y_train, <span class="hljs-string">'Predicted'</span>: y_train_pred}) viz_df = pd.concat([viz_df_train, viz_df])

<span class="hljs-comment"># Plot the results</span> plt.figure(figsize=(<span class="hljs-number">14</span>, <span class="hljs-number">7</span>)) plt.plot(viz_df[<span class="hljs-string">'True'</span>], label=<span class="hljs-string">'True'</span>, color=<span class="hljs-string">'blue'</span>) plt.plot(viz_df[<span class="hljs-string">'Predicted'</span>], label=<span class="hljs-string">'Predicted'</span>, color=<span class="hljs-string">'red'</span>, alpha=<span class="hljs-number">0.7</span>) plt.title(<span class="hljs-string">'EUR/USD Forecast: True vs Predicted'</span>) plt.legend() plt.grid(<span class="hljs-literal">True</span>) plt.show()</pre></div><figure id="ce5c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*cLlTTCFRMZqUBQIUBeya0g.png"><figcaption></figcaption></figure><p id="62e5">The visual representation of the EUR/USD currency pair’s forecasted versus actual values offers an insightful glimpse into the model’s capabilities. The close alignment between the blue <code>True</code> line and the red <code>Predicted</code> line for most of the chart affirms the model’s strong predictive proficiency, especially given the low Mean Absolute Error (MAE) of 0.0091. The few areas where deviations occur resonate with the Root Mean Squared Error (RMSE) of 0.0174, indicating the average magnitude of error.</p><p id="ba85">Notably, the small segment towards the right end, where predictions seem to diverge slightly, underscores the challenges of exact currency forecasting. Nevertheless, the model, as depicted in the graph and corroborated by the performance metrics, has shown remarkable accuracy in capturing the nuances of the EUR/USD exchange rate’s movements.</p><h1 id="fe27">Conclusion</h1><p id="3036">In conclusion, this exploration into Forex forecasting has underscored the critical interplay between data preprocessing, feature engineering, and model selection. Through this model we found that XGBoost in predicting the EUR/USD currency pair stands out, demonstrating the algorithm’s robustness and adaptability. Finally, the precision showcased by our model reinforces XGBoost’s reputation as an efficient tool to forecast financial data.</p><p id="0141">Read more of my stories here:</p><div id="c60c" class="link-block"> <a href="https://algocraft.xyz/eur-usd-forecasting-simplified-an-lstm-users-guide-337ccdda6158"> <div> <div> <h2>EUR/USD Forecasting Simplified: an LSTM User’s Guide</h2> <div><h3>LSTM, or Long Short-Term Memory, is a specialized type of Recurrent Neural Network (RNN) designed to recognize patterns…</h3></div> <div><p>algocraft.xyz</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*EUIE-cUkt3x2AqBX5nUotA.jpeg)"></div> </div> </div> </a> </div><div id="a6a6" class="link-block"> <a href="https://algocraft.xyz/how-to-get-131-return-with-mean-reversion-trading-strategy-from-stock-selection-to-backtesting-c623870adf31"> <div> <div> <h2>How to Get a 131% Return with Mean Reversion Trading Strategy: From Stock Selection to Backtesting</h2> <div><h3>undefined</h3></div> <div><p>undefined</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*o95y-D4ETf1Geqx3)"></div> </div> </div> </a> </div></article></body>

Monday Mashup #29 part 3/3

Second Life Part III

Theon grows closer to the princess, defends her in a sword fight, and the story concludes.

Theon and Princess Roxanna. Image generated by Nightcafe AI under author’s direction.

Author’s note: This is the third and final part of the story of Theon, who is hit by a truck and transported to another world. If you missed the first part, you can read it here.

Theon was staggered to realize the size of the place. It was a genuine castle estate. The grounds, he was told, extended a full day’s ride to the south and west, and about a half day’s ride on the Eastern side of the castle. The north was protected by a large lake which was fed by a waterfall tumbling down from an impressive cliff face. A second lake was to be found in the south-western corner of the estate, but Theon was not feeling up to traveling that far. Additionally, he did not want to embarrass himself by revealing that he had never ridden a horse and wasn’t sure he would stay on.

The land they toured was sparsely populated. Rather than a village, Theon learned that the major settlement was further away and an ancient monarch had reserved the land surrounding the castle for agricultural and recreational purposes. The few people who lived there full-time were engaged in growing food for the castle as well as their own families and the neighbouring village. This also gave the royal family an additional layer of privacy and security.

On the subsequent days of healing from being hit by the truck, Theon often left the castle for walks with the princess. During these excursions, he learned more about Princess Roxanna than he had ever known about Roxy. He discovered that she loved the water and took every opportunity to swim, alternating between the two lakes on the estate. She liked to experiment and test boundaries, but was never cruel and she treated the castle’s servants the same way she treated visiting nobles and her extended family members. She had no living siblings and intended to take the crown when her father was ready to give it up. The way she talked about this eventuality, with a mix of gravitas and excitement over new ideas, let Theon know she would be a good leader.

Weeks passed and although Theon still didn’t know if he was in a coma, he thought about his old life less and less. By this time, he had grown comfortable with his new body. Raised by a single mother with no one else to confide in and a very open older sister, he had been privy to candid conversations about being a woman in the world that most men only heard about when they had overstepped and their wives laid out some truth bombs for them. Although he had never given a lot of thought to gender, he felt oddly calm about still thinking about himself as a man while having others view him as a woman. In the end, he mused, wasn’t everyone just human? He felt grateful that he could still move through the world, regardless of what genitalia his body had. Grateful that he wasn’t trapped in a hospital bed, perhaps in pain, perhaps permanently disabled.

He didn’t tell Roxanna about their past life working at the club together, but he did talk to her about his fascination with sword-fighting and medieval weaponry. At home it was considered extra nerdy to participate in live-action roleplaying, but in this world those skills came in handy as he practiced with knights, the castle’s version of a security team. As he regained his strength, he also explored the capabilities of this new body. He was smaller than he had been as a man. Though he had to choose a lighter sword in place of the mighty broadsword his caveman brain was drawn to, he was also quicker on his feet, more agile. And this served him well in matches against the men who challenged him.

And so Theon settled into a new life. A good life. He found that, regardless of bodies, he cared more for the princess every day, though he sensed there was still a part of her that saw him as a guest who might decide to leave.

It took several more weeks of bonding before Princess Roxanna trusted him enough to reveal her fears and the darker parts of her seemingly-charmed life. They were drinking wine in her chambers and the alcohol had loosened her tongue enough for her to admit that her father was stepping down as king. His health was declining and he wanted to smooth the transition as much as possible while he was still alive to do so. Few people knew, but her coronation was looming and she was scared.

“But your father will still be here to advise you even after you take the crown,” Theon reasoned, “and you know what to do, you’ve been preparing your whole life.” His thinking was that her fear related to her ability to lead the kingdom, but Princess Roxanna shook her head. That wasn’t it. Then she told him why she didn’t have a mother anymore. She had been taken, Roxanna said. This was during a period of war she had been too young to remember. An enemy kingdom had kidnapped the queen to use as a bargaining chip, to give themselves the upper hand in the conflict. The king, her father, had been willing to negotiate to keep his love safe, but something had gone wrong and the queen had been killed.

A tear slid down Princess Roxanna’s cheek as she looked at Theon across the bed. “As a princess, I’m protected by the king’s power and influence. As a queen, I’m vulnerable. Always a potential target. I don’t want my mother’s fate to become mine.”

Grief pooled in Theon’s chest, just under his ribcage. He could understand the princess’s pain, knowing she would never see her mother again, as he wasn’t sure he would ever see his again. When he looked at her, slightly drunk, he saw Roxy. He thought of her tending bar at the club and the many losers he had thrown out for harassing her. A surge of masculine protective instinct raced through him and he slid towards her on the bed.

“Listen,” he said, waiting until she made eye contact, “I am not leaving you.”

Her tears were sliding out faster now, streaking down her chin and dripping onto the bedclothes.

“I’m serious!” He said, “I’ll be your personal bodyguard! You know I’m pretty good with a sword. I’ll even stay here in your chambers with you. For as long as you want.”

When she lunged at him, it caught both of them off balance and the hug ended up being more of a horizontal cuddle. Theon was afraid to breathe at first. This wasn’t the body he was most used to and he didn’t know how it was going to respond to having the girl he loved lying on top of it. He could feel the heat of her thigh flush against his where her nightgown had ridden up. He could feel the soft curves of her body molding to fit his. Her slight weight on his breasts wasn’t uncomfortable like he thought it might be. Instead, they made space for her. Welcomed her. Enveloped her. His heart beat in time with hers.

When she finally shifted off of him, he realized he had been holding his breath.

“Ok,” she said in a quiet voice, wiping tears off her face. “Stay with me.”

Air whooshed out of Theon’s lungs, then in again. Relief. Sweet relief that she would trust him with this.

On the way back to his own rooms to collect a sword, his senses were flooded with Roxanna. With each breath, he inhaled the remembered scent of her hair, he exhaled the syllables of her name, his body was warm in all the places it had been touching hers. He knew it hadn’t been sexual for her. But it had been a moment of emotional vulnerability, of genuine connection. And he wondered if maybe, just maybe…

A scream shattered his fantasy and he knew it was the princess. Theon started sprinting, straining to make it back to her chambers before it was too late. He was acutely aware that his body wasn’t responding the way he expected it to when he ran. He wasn’t as fast as he remembered, these legs were not as well-muscled, his lungs didn’t expand as far into his chest, and what an inconvenience to have breasts that heaved with every step.

He wasn’t going to make it. He was going to be too late. The princess would be lost. It would be all his fault.

With that agonizing thought, he burst through the doors of her bedchamber to find Roxanna cowering on the floor. He took in the scene quickly. Shattered glass indicated that the balcony window was the point of entry. The assassin was cloaked all in black. He retreated to a corner at Theon’s appearance, making his own assessment of the princess’ would-be rescuer. A small knife lay just out of Roxanna’s reach, blood dripping off of it. She had gotten a piece of her attacker but he was still coming. Blood oozed through her thin nightgown just below her ribs. He had gotten a piece of her too. Roxanna cupped a hand over the wound and crawled behind the bed, leaving the sword fight to Theon.

“Death to the Queen! Usurper of power!” Roared the assassin as he charged at Theon. Parrying and dancing out of reach, Theon began to worry in earnest that this unknown man would kill both of them. He feinted left, trying to find an opening. Success, the tip of the sword sliced into the assassin’s thigh. It wasn’t enough! The man rallied and came after Theon with renewed fury. A gash opened up in Theon’s bicep as the assassin’s blade found it’s target. Frantically scanning the room for a way out, Theon’s eyes lighted on the small dagger Roxanna had abandoned in the middle of the room. He scrambled to reach it, and after several more minutes of fighting, he managed to plunge the dagger into the assassin’s heart at the same time as the man’s sword found its way between his ribs. Darkness descended.

Theon kept his eyes closed for a minute, knowing that he was awake, just not sure in which world he might find himself. Or which body, for that matter.

He finally opened his eyes and was relieved to see a familiar face.

“Roxy,” he whispered, still groggy.

“Well, these days, I go by Queen Roxanna, but I suppose since you saved my life and all you’ve earned the right to call me… Roxy” She hesitated on the name as if it still felt foreign to her. Too informal to belong to a queen.

He tried to sit up, but she wouldn’t let him. He lifted his hands so he could see his fingers, swept them down his body, probing, inquiring, then sighed. Still a girl then. Alive. And still a girl.

Queen Roxanna climbed onto the bed and tucked her body against his.

“Theon,” she murmured, voice hushed, “I was worried you were going to leave me after all.”

“Never,” he said confidently, “this is where I’m meant to be.”

Epilogue:

Theon stayed in his new world with Queen Roxanna, their trusted servant, Sienna, and an assortment of knights. As more time passed, Theon’s memory of his past life as head of security at a nightclub faded more and more. Sometimes he was a man in his dreams, but those were just dreams and by definition they blurred the lines of reality. He wasn’t completely sure that he hadn’t been born into this world. He wasn’t completely sure that he hadn’t always been in this female body.

Theon remained a close adviser to Queen Roxanna as she grew into a kind and just leader. They were often called upon to settle disputes between neighbouring kingdoms as theirs was the only one headed by two women and they could often offer a unique perspective.

Queen Roxanna never married. Through her long and peaceful rule, there was no king, just the Queen and her confidante. This caused rumours, of course. Especially on the nights that they shared the queen’s bedchambers. But, who is to say what truly happens between two parts of a whole when the rest of the world has been shut out? Perhaps they preferred to conference alone and the queen’s bedchambers were the most secure place for a meeting of the minds. Perhaps Theon remained the queen’s personal bodyguard and that required proximity during the most sinister times of the night. It is also possible that they indulged in physical intimacy together. Again, who is to say whether this took the form of hugs and snuggles or whether they brought each other to the heights of sexual pleasure with kisses, and sighs, and gentle caresses. For most of the kingdom it didn’t matter. There are many ways to have a beautiful relationship.

Thanks for reading! Tagging Jonathon Sawyer once again so that you can count my mashup points for this section.

Tally box: 
The estate has two lakes (1 point each = 2 pts)
Someone tries to assassinate Princess Roxanna and also it's assumed that
a coronation took place at some point and she's now the queen. (2 pts)
Fiction
Fantasy
Medieval
Relationships
Recommended from ReadMedium