openpondai/agents/price-trigger-bot
OpenTool app
1Branch0Tags
typescript
import type {
PriceTriggerActionSide,
PriceTriggerCondition,
PriceTriggerConfig,
PriceTriggerRule,
} from "../config";
import type { PriceTriggerRuleState } from "./state";
export type PriceTriggerTriggeredTarget = {
symbol: string;
side: PriceTriggerActionSide;
budgetUsd: number;
size: string;
price: string;
orderIds?: {
cloids: string[];
oids: string[];
};
orderResponse?: unknown;
};
export type PriceTriggerRuleResult = {
id: string;
sourceSymbol: string;
condition: PriceTriggerCondition;
threshold: number;
currentPrice: number;
lastObservedPrice: number | null;
alreadyExecuted: boolean;
executedAt: string | null;
triggered: boolean;
actionSide: PriceTriggerActionSide;
targets: PriceTriggerTriggeredTarget[];
};
export function evaluateRule(params: {
rule: PriceTriggerRule;
currentPrice: number;
previousState: PriceTriggerRuleState | null;
}) {
const { currentPrice, previousState, rule } = params;
const threshold = rule.threshold;
switch (rule.condition) {
case "above":
return currentPrice > threshold;
case "below":
return currentPrice < threshold;
case "crosses-above":
return (
previousState != null &&
previousState.lastObservedPrice <= threshold &&
currentPrice > threshold
);
case "crosses-below":
return (
previousState != null &&
previousState.lastObservedPrice >= threshold &&
currentPrice < threshold
);
}
}
export function resolveBudgetPerRule(
config: PriceTriggerConfig,
triggeredCount: number,
) {
if (triggeredCount <= 0) return 0;
const totalBudget = Math.min(
config.maxPerRunUsd,
config.amountUsd * triggeredCount,
);
return totalBudget / triggeredCount;
}