Example 1: Multi-Phase DCA Exit with Trailing Stop
This strategy implements a common professional trading pattern:
Buy on a 15% dip from signal (but only for fresh tokens)
Take initial profits at 2x (sell 25%)
Take more profits at 5x (sell 50% of remaining)
Use trailing stop on final position (15% drop from peak)
class Strategy {
constructor() {
this.strategy = {
data: {
token: {
address: "",
creationTime: 0
},
priceTicks: []
},
steps: [
{
run: (entry, current) => this.waitForDipAndBuy(entry, current),
label: "Buy on 15% dip (tokens < 3 days old)"
},
{
run: (entry, current) => this.firstTakeProfit(entry, current),
label: "First take profit: 25% at 2x"
},
{
run: (entry, current) => this.secondTakeProfit(entry, current),
label: "Second take profit: 50% at 5x"
},
{
run: (entry, current) => this.trailingStopLoss(entry, current),
label: "Trailing stop: 15% from peak on remaining 37.5%"
}
],
context: {
// Will store: highestPrice, peakTimestamp, lastTrailUpdate
}
}
}
/**
* Step 1: Wait for 15% dip and buy
* Only buys tokens that are less than 3 days old
* This filters out older, potentially less volatile tokens
*/
waitForDipAndBuy(signalPrice, currentPrice) {
// Check token age first
const tokenData = this.strategy.data.token;
if (tokenData && tokenData.creationTime) {
const tokenAgeMs = Date.now() - tokenData.creationTime;
const tokenAgeDays = tokenAgeMs / (1000 * 60 * 60 * 24);
// Skip tokens older than 3 days - stay at this step forever (never buy)
if (tokenAgeDays > 3) {
// Don't buy, remain on this step indefinitely
return null;
}
}
// Check for 15% dip from signal price
const dipPercentage = (signalPrice - currentPrice) / signalPrice;
if (dipPercentage >= 0.15) {
// Price has dropped 15% or more - buy full position
return {
buy: true,
partToBuyInPercentage: 100
};
}
// Not enough dip yet, keep waiting
return null;
}
/**
* Step 2: First take profit at 2x
* Sells 25% of position when price doubles
* This locks in initial profits while maintaining exposure
*/
firstTakeProfit(entryPrice, currentPrice) {
const multiplier = currentPrice / entryPrice;
if (multiplier >= 2.0) {
// Price has doubled - take 25% profit
return {
sell: true,
partToSellInPercentage: 25
};
}
return null;
}
/**
* Step 3: Second take profit at 5x
* Sells 50% of REMAINING position (which is 37.5% of original)
* After this sell, 37.5% of original position remains
*/
secondTakeProfit(entryPrice, currentPrice) {
const multiplier = currentPrice / entryPrice;
if (multiplier >= 5.0) {
// Price is 5x - sell half of remaining position
return {
sell: true,
partToSellInPercentage: 50
};
}
return null;
}
/**
* Step 4: Trailing stop loss
* Tracks the highest price seen and sells if price drops 15% from peak
* This protects profits while allowing unlimited upside
*
* The remaining position is 37.5% of original:
* - Started with 100%
* - Sold 25% at 2x (75% remaining)
* - Sold 50% of that at 5x (37.5% remaining)
*/
trailingStopLoss(entryPrice, currentPrice) {
const context = this.strategy.context;
// Initialize highest price tracking on first execution
if (!context.highestPrice) {
context.highestPrice = currentPrice;
context.peakTimestamp = Date.now();
return null; // Just initialize, don't sell
}
// Update highest price if current price is higher
if (currentPrice > context.highestPrice) {
const oldPeak = context.highestPrice;
context.highestPrice = currentPrice;
context.peakTimestamp = Date.now();
context.lastTrailUpdate = Date.now();
// Calculate new multiplier from entry
const currentMultiplier = currentPrice / entryPrice;
// No action, just tracking new peak
return null;
}
// Calculate drop from peak
const dropFromPeak = (context.highestPrice - currentPrice) / context.highestPrice;
// If price has dropped 15% or more from peak, sell everything
if (dropFromPeak >= 0.15) {
const peakMultiplier = context.highestPrice / entryPrice;
const currentMultiplier = currentPrice / entryPrice;
// Sell remaining position (37.5% of original)
return {
sell: true,
partToSellInPercentage: 100
};
}
// Continue monitoring
return null;
}
}Strategy Breakdown
Position Sizing Math:
Initial: 100% position after buy
After Step 2: 75% remaining (sold 25%)
After Step 3: 37.5% remaining (sold 50% of 75%)
After Step 4: 0% remaining (sold 100% of 37.5%)
Example Trade Scenario:
Token detected at $0.10, age: 1 day old
Price drops to $0.085 (15% dip) → Buy 100%
Price rises to $0.17 (2x from $0.085) → Sell 25% (75% left)
Price rises to $0.425 (5x from $0.085) → Sell 37.5% (37.5% left)
Price peaks at $0.85 (10x from $0.085) → Track peak
Price drops to $0.7225 (15% from $0.85 peak) → Sell 37.5% (exit complete)
Total Profit Calculation:
Entry: $100 at $0.085
Exit 1: $25 × 2x = $50
Exit 2: $37.5 × 5x = $187.50
Exit 3: $37.5 × 8.5x = $318.75
Total Return: $556.25 (5.56x overall)
Last updated
