## 3 Proposed Methodology
### Trading Recommendation
Algorithm 1 shows the pseudo-code of the _OpenTransaction_ procedure, implementing the proposed trading algorithm that automates decisions on when to open and close operations based on the analysis of real and predicted prices. The algorithm receives the following parameters as input: a cryptocurrency \(C\); a time \(E\) beyond which the execution of the trading algorithm ends; the loss percentage \(LV\) beyond which to activate the stop-loss procedure; the percentage gain \(PV\) beyond which to activate the take-profit procedure; a number \(D\) that indicates the time window (in days) for data used by the prediction model; a time \(W\) to wait before opening a trading operation; \(DB\), the reference to the dataset containing aggregated social media and market information; \(LSTM\), the trained neural network model for predicting prices; and a sleep time \(S\) between one completed operation and the next. ```
1:procedureOpenTransaction(C,L,V,P,D,W,DB,LSTM,S)
2:\(op\leftarrow\) null
3:whilenow0\(<\) Edo
4:if\(op\) is nullthen
5:\(previsionData\gets DB.getLastDisyData(C,D)\)
6:\(p_{e}\gets LSTM.predict(C,previousData)\)
7:\(p_{e}\leftarrow\)getRelFree(C)
8:if\(i\) intersect(\(P,P_{e}\)) then
9:wait(\(W\)) \(\triangleright\) Safety waiting time
10:if\(P,>P_{e}\)then
11:\(op\leftarrow\)getCellOperation(\(C\))\(\triangleright\) Betting against (or shorting) cryptocurrency C.
12:else
13:\(op\leftarrow\)openBlyOperation(\(C\))\(\triangleright\) Betting in favor of (or going long on) cryptocurrency C.
14:endif
15:endif
16:else
17:if\(op.is\)Result(\(\|\)) then
18:ifhoursPredictedPriceGreaterThanReal(\(C\)) \(>\) W or checkStopLossOfTakeProfit(\(LV,PV\)) then
19:\(op.close()\)
20:endif
21:else
22:ifhoursRealProGreaterThanPredicted(\(C\)) \(>\) W or checkStopLossOfTakeProfit(\(LV,PV\)) then
23:\(op.close()\)
24:endif
25:endif
26:if\(ip.is\)Closed(\(\|\)) then
27:\(op.calculateProfit()\)
28:\(op\leftarrow\)null
29:endif
30:endif
31:endif
32:endwhile
33:return
34:endprocedure
```  
**Algorithm 1** Pseudocode of the trading algorithm. Given a reference cryptocurrency \(C\), the algorithm initializes a variable \(op\) as null, which will be used to represent the current trading operation (line 2). Then, it initiates a while loop that continues until the current time exceeds the specified duration \(E\) for trading (line 3). If the \(op\) variable is null, indicating no active trading operation, then the algorithm tries to start a new one. To this end, it calculates the predicted and real prices for the cryptocurrency. In particular, the predicted price is given by the LSTM model based on the provided last \(D\)-days historical data (lines 5-6), while the real price is gathered from market coin APIs (line 7). The algorithm checks whether the predicted price and real price intersect (line 8), indicating a potential trading opportunity. After waiting a safety time \(W\) (line 9), aiming to avoid price retracements, the algorithm decides the type of trading operation to perform. Price retracements represent temporary and relatively short corrections within a growth or decline trend of a specific asset. They are common phenomena in financial markets, including cryptocurrency markets, and can offer trading and investment opportunities. Specifically, if the real price is greater than the predicted price, it starts a sell operation (lines 10-11), betting that the price will decrease (shorting). Otherwise, it starts a buy operation (lines 12-13), betting that the price will rise (going long). If \(op\) is defined, which means a trading operation is active, the algorithm starts monitoring the prices of the cryptocurrency to establish whether the current operation should be closed (lines 17-25). In particular, it checks whether the hours in which the predicted price exceeded the real price (or vice versa) have exceeded a given waiting time of \(W\) or if the stop-loss/take-profit condition is met.