Skip to content

Commit e6c3456

Browse files
committed
update sell simulation to include stop loss and calculate amount to sell
Signed-off-by: MarcoMandar <malicemandar@gmail.com>
1 parent 530e16a commit e6c3456

File tree

1 file changed

+78
-5
lines changed

1 file changed

+78
-5
lines changed

packages/plugin-solana/src/providers/simulationSellingService.ts

+78-5
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class simulationSellingService {
6565
await this.executeSells(sellDecisions);
6666

6767
// Perform stop loss checks
68-
await this.performStopLoss();
68+
await this.performStopLoss(tokenPerformances);
6969
}
7070

7171
private decideWhenToSell(
@@ -75,27 +75,100 @@ export class simulationSellingService {
7575
console.log("Deciding when to sell and how much...");
7676
const decisions: SellDecision[] = [];
7777

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;
8088
decisions.push({ tokenPerformance: performance, amountToSell });
8189
});
8290

8391
return decisions;
8492
}
8593

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+
86110
private async executeSells(decisions: SellDecision[]) {
87111
console.log("Executing sell orders...");
88112
for (const decision of decisions) {
89113
console.log(
90114
`Selling ${decision.amountToSell} of token ${decision.tokenPerformance.tokenSymbol}`
91115
);
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);
93135
}
94136
}
95137

96-
private async performStopLoss() {
138+
private async performStopLoss(tokenPerformances: TokenPerformance[]) {
97139
console.log("Performing stop loss checks...");
98140
// 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+
}
99172
}
100173

101174
async updateSellDetails(

0 commit comments

Comments
 (0)