Skip to content

Commit

Permalink
Revert "refactor: reformat code"
Browse files Browse the repository at this point in the history
This reverts commit 310b159.
  • Loading branch information
dargmuesli committed Mar 8, 2021
1 parent 7c1f64f commit 1fd45ae
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 310 deletions.
3 changes: 1 addition & 2 deletions src/backtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,7 @@ async function run() {
console.log("starting backtest... ".green)
await backtest(lines)
console.log(
"Ending backtest... ".green +
parseInt((Date.now() - startBacktest) / 1000)
"Ending backtest... ".green + parseInt((Date.now() - startBacktest) / 1000)
)

process.exit(0)
Expand Down
193 changes: 79 additions & 114 deletions src/notifiers/gmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,133 +4,98 @@ const gmail_address = env.GMAIL_ADDRESS
const gmail_app_password = env.GMAIL_APP_PASSWORD

function createMailMessage(subject, html) {
return {
from: '"🐬 BVA " <no-reply@gmail.com>',
to: gmail_address,
subject,
text: html,
html,
}
return {
from: '"🐬 BVA " <no-reply@gmail.com>',
to: gmail_address,
subject,
text: html,
html,
}
}

function createSignalMessage(base, signal) {
const subject =
base +
" :: " +
signal.stratname +
" " +
signal.pair +
" " +
signal.price
const subject =
base + " :: " +
signal.stratname +
" " +
signal.pair +
" " +
signal.price;

const text = (signal.score ? "score: " + signal.score : "score: NA") + "\n"
const text =
(signal.score
? "score: " + signal.score
: "score: NA") + "\n";

return createMailMessage(subject, text)
return createMailMessage(subject, text)
}

module.exports = function () {
if (!env.USE_GMAIL) return {}

const mailTransport = require("nodemailer").createTransport(
`smtps://${encodeURIComponent(gmail_address)}:${encodeURIComponent(
gmail_app_password
)}@smtp.gmail.com`
)
if (!env.USE_GMAIL) return {};

function send(message) {
if (!env.USE_GMAIL) return
const mailTransport = require("nodemailer").createTransport(
`smtps://${encodeURIComponent(gmail_address)}:${encodeURIComponent(gmail_app_password)}@smtp.gmail.com`
)

if (typeof message === "string") {
message = createMailMessage("Tading Bot Message", message)
}
function send(message) {
if (!env.USE_GMAIL) return;

return mailTransport.sendMail(message).catch((error) => {
console.error(
"There was an error while sending the email ... trying again...",
error
)
setTimeout(() => {
mailTransport.sendMail(message).catch((error) => {
console.error(
"There was an error while sending the email: stop trying",
error
)
})
}, 2000)
})
if (typeof message === "string") {
message = createMailMessage("Tading Bot Message", message)
}

function notifyBuyToCoverSignal(signal) {
return send(
createSignalMessage(
"<i>BUY_SIGNAL :: BUY TO COVER SHORT TRADE</i>",
signal
)
)
}
function notifyBuyToCoverTraded(signal) {
return send(
createSignalMessage(
"<b>>> SUCCESS! BUY_SIGNAL :: BUY TO COVER SHORT TRADE</b>",
signal
)
)
}
function notifyEnterLongSignal(signal) {
return send(
createSignalMessage("<i>BUY_SIGNAL :: ENTER LONG TRADE</i>", signal)
)
}
function notifyEnterLongTraded(signal) {
return send(
createSignalMessage(
"<b>>> SUCCESS! BUY_SIGNAL :: ENTER LONG TRADE</b>",
signal
)
)
}
function notifyEnterShortSignal(signal) {
return send(
createSignalMessage(
"<i>SEL_SIGNAL :: ENTER SHORT TRADE</i>",
signal
)
return mailTransport
.sendMail(message)
.catch((error) => {
console.error(
"There was an error while sending the email ... trying again...", error
)
}
function notifyEnterShortTraded(signal) {
return send(
createSignalMessage(
"<b>>> SUCCESS! SELL_SIGNAL :: ENTER SHORT TRADE</b>",
signal
)
)
}
function notifyExitLongSignal(signal) {
return send(
createSignalMessage(
"<i>SELL_SIGNAL :: SELL TO EXIT LONG TRADE</i>",
signal
)
)
}
function notifyExitLongTraded(signal) {
return send(
createSignalMessage(
"<b>>> SUCCESS! SELL_SIGNAL :: SELL TO EXIT LONG TRADE</b>",
signal
)
)
}
setTimeout(() => {
mailTransport
.sendMail(message)
.catch((error) => {
console.error(
"There was an error while sending the email: stop trying", error
)
})
}, 2000)
})
}

return {
notifyBuyToCoverSignal,
notifyBuyToCoverTraded,
notifyEnterLongSignal,
notifyEnterLongTraded,
notifyEnterShortSignal,
notifyEnterShortTraded,
notifyExitLongSignal,
notifyExitLongTraded,
send,
}
function notifyBuyToCoverSignal(signal) {
return send(createSignalMessage("<i>BUY_SIGNAL :: BUY TO COVER SHORT TRADE</i>", signal));
}
function notifyBuyToCoverTraded(signal) {
return send(createSignalMessage("<b>>> SUCCESS! BUY_SIGNAL :: BUY TO COVER SHORT TRADE</b>", signal));
}
function notifyEnterLongSignal(signal) {
return send(createSignalMessage("<i>BUY_SIGNAL :: ENTER LONG TRADE</i>", signal));
}
function notifyEnterLongTraded(signal) {
return send(createSignalMessage("<b>>> SUCCESS! BUY_SIGNAL :: ENTER LONG TRADE</b>", signal));
}
function notifyEnterShortSignal(signal) {
return send(createSignalMessage("<i>SEL_SIGNAL :: ENTER SHORT TRADE</i>", signal));
}
function notifyEnterShortTraded(signal) {
return send(createSignalMessage("<b>>> SUCCESS! SELL_SIGNAL :: ENTER SHORT TRADE</b>", signal));
}
function notifyExitLongSignal(signal) {
return send(createSignalMessage("<i>SELL_SIGNAL :: SELL TO EXIT LONG TRADE</i>", signal));
}
function notifyExitLongTraded(signal) {
return send(createSignalMessage("<b>>> SUCCESS! SELL_SIGNAL :: SELL TO EXIT LONG TRADE</b>", signal));
}

return {
notifyBuyToCoverSignal,
notifyBuyToCoverTraded,
notifyEnterLongSignal,
notifyEnterLongTraded,
notifyEnterShortSignal,
notifyEnterShortTraded,
notifyExitLongSignal,
notifyExitLongTraded,
send,
}
}
43 changes: 18 additions & 25 deletions src/notifiers/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
const env = require("./../env")

module.exports = function (trading_pairs) {
const notifiers = []
if (env.USE_TELEGRAM) notifiers.push(require("./telegram")(trading_pairs))
if (env.USE_GMAIL) notifiers.push(require("./gmail")())
const notifiers = [];
if (env.USE_TELEGRAM)
notifiers.push(require('./telegram')(trading_pairs))
if (env.USE_GMAIL)
notifiers.push(require('./gmail')())

const notifyAllFor = (method, arg) =>
notifiers.forEach((n) => n[method] && n[method](arg))
const notifyAllFor = (method, arg) => notifiers.forEach(n => n[method] && n[method](arg));

return {
notifyBuyToCoverSignal: (signal) =>
notifyAllFor("notifyBuyToCoverSignal", signal),
notifyBuyToCoverTraded: (signal) =>
notifyAllFor("notifyBuyToCoverTraded", signal),
notifyEnterLongSignal: (signal) =>
notifyAllFor("notifyEnterLongSignal", signal),
notifyEnterLongTraded: (signal) =>
notifyAllFor("notifyEnterLongTraded", signal),
notifyEnterShortSignal: (signal) =>
notifyAllFor("notifyEnterShortSignal", signal),
notifyEnterShortTraded: (signal) =>
notifyAllFor("notifyEnterShortTraded", signal),
notifyExitLongSignal: (signal) =>
notifyAllFor("notifyExitLongSignal", signal),
notifyExitLongTraded: (signal) =>
notifyAllFor("notifyExitLongTraded", signal),
send: (message) => notifyAllFor("send", message),
}
}
return {
notifyBuyToCoverSignal: signal => notifyAllFor("notifyBuyToCoverSignal", signal),
notifyBuyToCoverTraded: signal => notifyAllFor("notifyBuyToCoverTraded", signal),
notifyEnterLongSignal: signal => notifyAllFor("notifyEnterLongSignal", signal),
notifyEnterLongTraded: signal => notifyAllFor("notifyEnterLongTraded", signal),
notifyEnterShortSignal: signal => notifyAllFor("notifyEnterShortSignal", signal),
notifyEnterShortTraded: signal => notifyAllFor("notifyEnterShortTraded", signal),
notifyExitLongSignal: signal => notifyAllFor("notifyExitLongSignal", signal),
notifyExitLongTraded: signal => notifyAllFor("notifyExitLongTraded", signal),
send: message => notifyAllFor("send", message),
}
}
73 changes: 16 additions & 57 deletions src/notifiers/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const TeleBot = require("telebot")
/**
* @type {TeleBot}
*/
let telBot
let telBot;

module.exports = function (trading_pairs) {
if (!env.USE_TELEGRAM) return publicMethods
if (!env.USE_TELEGRAM) return publicMethods;

telBot = new TeleBot(env.TELEGRAM_API_KEY)

Expand All @@ -20,93 +20,52 @@ module.exports = function (trading_pairs) {
return send(response)
})


telBot.on("start", () => {
send("Trader Bot started!")
})

telBot.start()
return publicMethods
return publicMethods;
}

function createSignalMessage(base, signal) {
let msg =
base +
" :: " +
signal.stratname +
" " +
signal.pair +
" " +
signal.price +
"\n"
msg += (signal.score ? "score: " + signal.score : "score: NA") + "\n"
let msg = base + " :: " + signal.stratname + ' ' + signal.pair + ' ' + signal.price + "\n"
msg += (signal.score ? "score: " + signal.score : 'score: NA') + "\n"
return msg
}

function send(message) {
if (!env.USE_TELEGRAM || !telBot) return
if (!env.USE_TELEGRAM || !telBot) return;

return telBot.sendMessage(env.TELEGRAM_RECEIVER_ID, message, {
parseMode: "html",
parseMode: "html"
})
}

function notifyBuyToCoverSignal(signal) {
return send(
createSignalMessage(
"<i>BUY_SIGNAL :: BUY TO COVER SHORT TRADE</i>",
signal
)
)
return send(createSignalMessage("<i>BUY_SIGNAL :: BUY TO COVER SHORT TRADE</i>", signal));
}
function notifyBuyToCoverTraded(signal) {
return send(
createSignalMessage(
"<b>>> SUCCESS! BUY_SIGNAL :: BUY TO COVER SHORT TRADE</b>",
signal
)
)
return send(createSignalMessage("<b>>> SUCCESS! BUY_SIGNAL :: BUY TO COVER SHORT TRADE</b>", signal));
}
function notifyEnterLongSignal(signal) {
return send(
createSignalMessage("<i>BUY_SIGNAL :: ENTER LONG TRADE</i>", signal)
)
return send(createSignalMessage("<i>BUY_SIGNAL :: ENTER LONG TRADE</i>", signal));
}
function notifyEnterLongTraded(signal) {
return send(
createSignalMessage(
"<b>>> SUCCESS! BUY_SIGNAL :: ENTER LONG TRADE</b>",
signal
)
)
return send(createSignalMessage("<b>>> SUCCESS! BUY_SIGNAL :: ENTER LONG TRADE</b>", signal));
}
function notifyEnterShortSignal(signal) {
return send(
createSignalMessage("<i>SELL_SIGNAL :: ENTER SHORT TRADE</i>", signal)
)
return send(createSignalMessage("<i>SELL_SIGNAL :: ENTER SHORT TRADE</i>", signal));
}
function notifyEnterShortTraded(signal) {
return send(
createSignalMessage(
"<b>>> SUCCESS! SELL_SIGNAL :: ENTER SHORT TRADE</b>",
signal
)
)
return send(createSignalMessage("<b>>> SUCCESS! SELL_SIGNAL :: ENTER SHORT TRADE</b>", signal));
}
function notifyExitLongSignal(signal) {
return send(
createSignalMessage(
"<i>SELL_SIGNAL :: SELL TO EXIT LONG TRADE</i>",
signal
)
)
return send(createSignalMessage("<i>SELL_SIGNAL :: SELL TO EXIT LONG TRADE</i>", signal));
}
function notifyExitLongTraded(signal) {
return send(
createSignalMessage(
"<b>>> SUCCESS! SELL_SIGNAL :: SELL TO EXIT LONG TRADE</b>",
signal
)
)
return send(createSignalMessage("<b>>> SUCCESS! SELL_SIGNAL :: SELL TO EXIT LONG TRADE</b>", signal));
}

const publicMethods = {
Expand Down
Loading

0 comments on commit 1fd45ae

Please sign in to comment.