@@ -65,7 +65,7 @@ export class simulationSellingService {
65
65
await this . executeSells ( sellDecisions ) ;
66
66
67
67
// Perform stop loss checks
68
- await this . performStopLoss ( ) ;
68
+ await this . performStopLoss ( tokenPerformances ) ;
69
69
}
70
70
71
71
private decideWhenToSell (
@@ -75,27 +75,100 @@ export class simulationSellingService {
75
75
console . log ( "Deciding when to sell and how much..." ) ;
76
76
const decisions : SellDecision [ ] = [ ] ;
77
77
78
- tokenPerformances . forEach ( ( performance ) => {
79
- const amountToSell = performance . balance * 0.1 ;
78
+ tokenPerformances . forEach ( async ( performance ) => {
79
+ const tokenProvider = new TokenProvider (
80
+ performance . tokenAddress ,
81
+ this . walletProvider
82
+ ) ;
83
+ const sellAmount = await this . amountToSell (
84
+ performance . tokenAddress ,
85
+ tokenProvider
86
+ ) ;
87
+ const amountToSell = sellAmount . sellAmount ;
80
88
decisions . push ( { tokenPerformance : performance , amountToSell } ) ;
81
89
} ) ;
82
90
83
91
return decisions ;
84
92
}
85
93
94
+ async amountToSell ( tokenAddress : string , tokenProvider : TokenProvider ) {
95
+ // To Do: Implement logic to decide how much to sell
96
+ //placeholder
97
+ const processedData : ProcessedTokenData =
98
+ await tokenProvider . getProcessedTokenData ( ) ;
99
+ const prices = await this . walletProvider . fetchPrices ( null ) ;
100
+ const solPrice = prices . solana . usd ;
101
+ const tokenBalance = this . trustScoreDb . getTokenBalance ( tokenAddress ) ;
102
+
103
+ const sellAmount = tokenBalance * 0.1 ;
104
+ const sellSol = sellAmount / parseFloat ( solPrice ) ;
105
+ const sellValueUsd = sellAmount * processedData . tradeData . price ;
106
+
107
+ return { sellAmount, sellSol, sellValueUsd } ;
108
+ }
109
+
86
110
private async executeSells ( decisions : SellDecision [ ] ) {
87
111
console . log ( "Executing sell orders..." ) ;
88
112
for ( const decision of decisions ) {
89
113
console . log (
90
114
`Selling ${ decision . amountToSell } of token ${ decision . tokenPerformance . tokenSymbol } `
91
115
) ;
92
- // To Do sell logic
116
+ // update the sell details
117
+ const sellDetails = {
118
+ sell_amount : decision . amountToSell ,
119
+ sell_recommender_id : null ,
120
+ } ;
121
+ const sellTimeStamp = new Date ( ) . toISOString ( ) ;
122
+ const tokenProvider = new TokenProvider (
123
+ decision . tokenPerformance . tokenAddress ,
124
+ this . walletProvider
125
+ ) ;
126
+ const sellDetailsData = await this . updateSellDetails (
127
+ decision . tokenPerformance . tokenAddress ,
128
+ decision . tokenPerformance . recommenderId ,
129
+ sellTimeStamp ,
130
+ sellDetails ,
131
+ true ,
132
+ tokenProvider
133
+ ) ;
134
+ console . log ( "Sell order executed successfully" , sellDetailsData ) ;
93
135
}
94
136
}
95
137
96
- private async performStopLoss ( ) {
138
+ private async performStopLoss ( tokenPerformances : TokenPerformance [ ] ) {
97
139
console . log ( "Performing stop loss checks..." ) ;
98
140
// To Do: Implement stop loss logic
141
+ // check if the token has dropped by more than 50% in the last 24 hours
142
+ for ( const performance of tokenPerformances ) {
143
+ const tokenProvider = new TokenProvider (
144
+ performance . tokenAddress ,
145
+ this . walletProvider
146
+ ) ;
147
+ const processedData : ProcessedTokenData =
148
+ await tokenProvider . getProcessedTokenData ( ) ;
149
+ if ( processedData . tradeData . trade_24h_change_percent < - 50 ) {
150
+ const sellAmount = performance . balance ;
151
+ const sellSol = sellAmount / 100 ;
152
+ const sellValueUsd = sellAmount * processedData . tradeData . price ;
153
+ const sellDetails = {
154
+ sell_amount : sellAmount ,
155
+ sell_recommender_id : null ,
156
+ } ;
157
+ const sellTimeStamp = new Date ( ) . toISOString ( ) ;
158
+ const sellDetailsData = await this . updateSellDetails (
159
+ performance . tokenAddress ,
160
+ performance . recommenderId ,
161
+ sellTimeStamp ,
162
+ sellDetails ,
163
+ true ,
164
+ tokenProvider
165
+ ) ;
166
+ console . log (
167
+ "Stop loss triggered. Sell order executed successfully" ,
168
+ sellDetailsData
169
+ ) ;
170
+ }
171
+ }
99
172
}
100
173
101
174
async updateSellDetails (
0 commit comments