javascript - 如何设计正则表达式编码以将 % 添加到交易结果中。止盈、止损,甚至入场
问题描述
我对我的正则表达式(机器人)有疑问。我被困住了。我正在尝试在我的外汇信号上添加百分比。增加或减少我的“止盈”(TP)并增加或减少我的“止损”(SL)。
让我们想象一下信号是:
Signal :⬆️ BUY
Price ENTRY: 0.73534
TP1: 0.76639
SL: 0.73415
(简单吧?)我的最后一个正则表达式是:
/replacerx ⬆️ BUY SL[0-9] SLs/~[0-9]*/$0,3%/g
如果信号是“卖出”,那么它将是
Signal :⬇️ SELL
Price ENTRY: 0.73534
TP1: 0.76639
SL: 0.73415
我的正则表达式将是:
/replacerx ⬇️ SELL SL[0-9] SLs/~[0-9]*/$-0,3%/g
但是又失败了!
我在正则表达式中识别 (⬆️ BUY ) 或 (⬇️ SELL) 以及“SL”,因为当它是“SELL position”而不是“BUY”以及“TP”和其他时,百分比需要改变。接下来我可以尝试什么?
解决方案
也许是这样的:
const signalList = [
'Signal :⬆️ BUY Price ENTRY: 0.73534 TP1: 0.76639 SL: 0.73415',
'Signal :⬆️ BUY Price ENTRY: 0.73534 SL: 0.73415',
'Signal :⬆️ BUY Price ENTRY: 0.73534 TP1: 0.76639',
'Signal :⬇️ SELL Price ENTRY: 0.73534 TP1: 0.76639 SL: 0.73415',
'Signal :⬇️ SELL Price ENTRY: 0.73534 SL: 0.73415',
'Signal :⬇️ SELL Price ENTRY: 0.73534 TP1: 0.76639',
];
// // [https://regex101.com/r/OK9XKT/1/]
// const regXSignal =
// (/(?<signal>Signal\s*\:\W+)(?<action>BUY|SELL)(?<entry>\D+[\d.]+)(?<takeprofit>\s+TP\d*\:\s*[\d.]+)?(?<stoploss>\s+SL\d*\:\s*[\d.]+)?/);
// [https://regex101.com/r/OK9XKT/2/]
const regXSignal =
(/(Signal\s*\:\W+)(BUY|SELL)(\D+[\d.]+)(\s+TP\d*\:\s*[\d.]+)?(\s+SL\d*\:\s*[\d.]+)?/);
function substituteSignalValues(_, signal, action, entry, takeprofit, stoploss) {
const isSell = (action.toUpperCase() === 'SELL');
const isBuy = (action.toUpperCase() === 'BUY');
// takeprofit = (takeprofit && (
// (isSell && (takeprofit + ' + 7%')) ||
// (isBuy && (takeprofit + ' - 2%')) ||
// stoploss
// ));
debugger;
stoploss = (stoploss && (
(isSell && (stoploss + ' - 0,3%')) ||
(isBuy && (stoploss + ' + 0,3%')) ||
stoploss
));
const substitutes = [signal, action, entry];
if (takeprofit) {
substitutes.push(takeprofit);
}
if (stoploss) {
substitutes.push(stoploss);
}
return substitutes.join('');
}
console.log(
signalList.map(signal => signal.replace(regXSignal, substituteSignalValues))
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
编辑
就我目前对 OP 问题的理解而言,特别是对于如何replacerx
利用,将没有通用的正则表达式/方法来解决问题。一个人必须想出两个具体的表达方式,每个表达方式都针对一个SELL
或BUY
...的具体情况。
SELL
.../(.*SELL)(.*ENTRY\D+[\d.]+)(\s+TP\d*\D+[\d.]+)(\s+SL\d*\D+[\d.]+)/
- 比赛是
$1
...SELL
特定的子字符串$2
...ENTRY
特定的子字符串以数字值结尾(以后可以添加到一些自定义数据中)$3
...TP
(获利)特定子字符串以数字值结尾(稍后可以添加到一些自定义数据中)$4
...SL
(止损)特定子字符串以数字值结尾(稍后可以添加到一些自定义数据中)
- 比赛是
BUY
.../(.*BUY)(.*ENTRY\D+[\d.]+)(\s+TP\d*\D+[\d.]+)(\s+SL\d*\D+[\d.]+)/
- 比赛是
$1
...BUY
特定的子字符串$2
...ENTRY
特定的子字符串以数字值结尾(以后可以添加到一些自定义数据中)$3
...TP
(获利)特定子字符串以数字值结尾(稍后可以添加到一些自定义数据中)$4
...SL
(止损)特定子字符串以数字值结尾(稍后可以添加到一些自定义数据中)
- 比赛是
因此,OP 需要更换两次,一次为两个动作中的每一个SELL
或BUY
...
s.replacerx("(.*SELL)(.*ENTRY\D+[\d.]+)(\s+TP\d*\D+[\d.]+)(\s+SL\d*\D+[\d.]+)" "$1$2$3$4 - 0,3%")
s.replacerx("(.*BUY)(.*ENTRY\D+[\d.]+)(\s+TP\d*\D+[\d.]+)(\s+SL\d*\D+[\d.]+)" "$1$2$3$4 0,3%")
https://docs.junction.space/replace/#Replace_with_regular_expressions
推荐阅读
- fullcalendar - 完整日历 v5:过滤掉 Google 日历上的“忙碌”事件
- angular - Angular 12:TypeError:teardown.unsubscribe 不是函数
- pimcore - pimcore 中的静态路由不适用于多个参数
- ansible - ansible:自定义要使用的密码管理器
- python-3.x - 数据类 + sqlalchemy(使用 mapper_registry.mapped)无法实例化对象
- json - 如何通过`jq`在一行中打印多个值?
- c - CS50 过滤器:边缘我的代码工作正常,但无法通过 cs50 检查
- r - R 无法连接到 SQLite
- react-native - 使用 expo 安装带有 NPM 的包失败
- browser - 如何在 Allure 框架中禁用缓存?