Home > Blockchain >  How do I prevent one function from repeating until another function's if condition is met and i
How do I prevent one function from repeating until another function's if condition is met and i

Time:08-04

I'm new to javascript and programming in general, and I have a question about a simple crypto trading signals bot which I'm making that posts signals to Twitter. It gets an updated asset price and RSI (relative strength index indicator) every 16 seconds as allowed by taAPI's free version, and tweets a Buy signal with the current price every time the RSI is below 30, and a Sell signal when the RSI is above 70. I would like the program to work as it does currently, but prevent one signal from repeating until the opposite signal is called first. For example, if the RSI is above 70 for multiple 16 second updates, I don't want it to repeat the tweetSell function more than one time; I want it to wait until the tweetBuy function is run again when the RSI is below 30. Essentially, the signal tweets should be alternating from Buy to Sell when each if statement's condition is met and vice versa, without multiple Buys and Sells consecutively, but I am unsure how to implement this. It may be a really simple solution, but I am stuck here at the moment.

Thanks

var axios = require('axios');
var price;

let var1 = setInterval(function(){ priceUpdater() }, 16000); 
function priceUpdater() {
    axios.get('https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd', {

      })
      .then(function (reply) {
        console.log("ETH Price: "   reply.data.ethereum.usd); 
        price = reply.data.ethereum.usd
      })
      .catch(function (error) {
        console.log(error.reply.data);
      }); 
    } 
let var2 = setInterval(function(){ rsiUpdater() }, 16000);

    function rsiUpdater() {
        axios.get('https://api.taapi.io/rsi', {
            params: {
              secret: "PLACEHOLDER_FOR_SECRET",
              exchange: "binance",
              symbol: "ETH/USDT",
              interval: "5m",
            }
          })
          .then(function (response) {
            console.log("ETH RSI: "   response.data.value);
            if (response.data.value > 70) {
                console.log("Sell ETH/USD at $"   price)
                const rwClient = require("./twitterClient.js");
                const tweetSell = async () => {
                try {
                    await rwClient.v2.tweet("Sell ETH/USD at $"   price)
                } catch (e) {
                    console.error(e)
                }
            }
            tweetSell()
            } else if (response.data.value < 30) {
                console.log("Buy ETH/USD at $"   price)
                const rwClient = require("./twitterClient.js");
                const tweetBuy = async () => {
                    try {
                        await rwClient.v2.tweet("Buy ETH/USD at $"   price)
                    } catch (e) {
                        console.error(e)
                    }
                }
            tweetBuy()

            } else {
                console.log("Hold")
            }
          })
          .catch(function (error) {
            console.log(error.response.data);
          });
    }

CodePudding user response:

You can use flags in your code to do that. These flags allow you to control your code very easily. What they do is make them enter your function depending on whether the flag is true or false. In your case you should create a flag that will be dedicated to maintaining the state of the buy / sell signal. Suppose that true indicates buy (Next signal would be sell) and phase would be sell (Next signal buy). So the code would look like this:

var axios = require('axios');
var price;
var buySellFlag = false;  //Set a default signal flag.

let var1 = setInterval(function () { priceUpdater() }, 16000);
function priceUpdater() {
    axios.get('https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd', {

    })
        .then(function (reply) {
            console.log("ETH Price: "   reply.data.ethereum.usd);
            price = reply.data.ethereum.usd
        })
        .catch(function (error) {
            console.log(error.reply.data);
        });
}
let var2 = setInterval(function () { rsiUpdater() }, 16000);

function rsiUpdater() {
    axios.get('https://api.taapi.io/rsi', {
        params: {
            secret: "PLACEHOLDER_FOR_SECRET",
            exchange: "binance",
            symbol: "ETH/USDT",
            interval: "5m",
        }
    })
        .then(function (response) {
            console.log("ETH RSI: "   response.data.value);
            if (response.data.value > 70 && buySellFlag) {     //Enter the if, if it's buy sell signal and last tweet was buy!
                console.log("Sell ETH/USD at $"   price)
                const rwClient = require("./twitterClient.js");
                const tweetSell = async () => {
                    try {
                        await rwClient.v2.tweet("Sell ETH/USD at $"   price)
                    } catch (e) {
                        console.error(e)
                    }
                }
                tweetSell()
                buysellFlag = false;    //Set that the sell tweet has already been sent

            } else if (response.data.value < 30 && !buySellFlag) {     //Enter the if, if it's buy sell signal and last tweet was Sell!
                console.log("Buy ETH/USD at $"   price)
                const rwClient = require("./twitterClient.js");
                const tweetBuy = async () => {
                    try {
                        await rwClient.v2.tweet("Buy ETH/USD at $"   price)
                    } catch (e) {
                        console.error(e)
                    }
                }
                tweetBuy()
                buysellFlag = true;    //Set that the buy tweet has already been sent
            } else {
                console.log("Hold")
            }
        })
        .catch(function (error) {
            console.log(error.response.data);
        });
}

Recomendations

  • You should use let instead of var because it's a good practice, scope problems are common and if you don't need to use var I wouldn't recommend you use it.
  • You can use arrow functions to get more readable code, but you don't have to :)

If you have any questions, do not hesitate to reply to this message.

  • Related