Backend/Database/GAnalysis.cpp
/*!
 * @brief calculates RSI
 * @details RSI calculation
 * @param input the stock prices in a vector
 * @param period the length of a period
 */
@brief calculates RSI
@details RSI calculation
@param input the stock prices in a vector
@param period the length of a period
@brief calculates RSI @details RSI calculation @param input the stock prices in a vector @param period the length of a period
std::vector<float> GAnalysis::RSI(const std::vector<float>& input, unsigned int period)
{
	std::vector<float> rsi_value;
	std::vector<float> gain_loss = GainLoss(input);
	unsigned int len = input.size();
	float gain = 0, loss = 0, avg_gain = 0, avg_loss = 0, prev_avg_gain, prev_avg_loss, rs;
	for (unsigned int j = 0; j < period; ++j)
	{
		if (gain_loss[j] <= 0)
			loss = loss + gain_loss[j];
		else
			gain = gain + gain_loss[j];
	}
	for (unsigned int i = 0; i < len - period; ++i)
	{
		if (i == 0 && loss != 0)
		{
			avg_gain = gain / period;
			avg_loss = -1 * loss / period; 
			rs = avg_gain / avg_loss;
			rsi_value.push_back(100 - (100 / (1 + rs)));
			prev_avg_gain = avg_gain;
			prev_avg_loss = avg_loss;
		}
		else if (gain_loss[i + period - 1] <= 0 &&
				 ((prev_avg_loss * (period - 1) - gain_loss[i + period - 1]) / period) != 0)
		{
			rs = (prev_avg_gain * (period - 1) / period) /
				 ((prev_avg_loss * (period - 1) - gain_loss[i + period - 1]) /
				  period); 
			rsi_value.push_back(100 - (100 / (1 + rs)));
			prev_avg_gain = prev_avg_gain * (period - 1) / period;
			prev_avg_loss = (prev_avg_loss * (period - 1) - gain_loss[i + period - 1]) / period;
		}
		else if (gain_loss[i + period - 1] > 0 && (prev_avg_loss * (period - 1) / period) != 0)
		{
			rs = ((prev_avg_gain * (period - 1) + gain_loss[i + period - 1]) / period) /
				 (prev_avg_loss * (period - 1) / period);
			rsi_value.push_back(100 - (100 / (1 + rs)));
			prev_avg_gain = (prev_avg_gain * (period - 1) + gain_loss[i + period - 1]) / period;
			prev_avg_loss = prev_avg_loss * (period - 1) / period;
		}
		else
			rsi_value.push_back(100);
	}
	return rsi_value;
}
std :: vector < float > GAnalysis :: RSI ( const std :: vector < float > & input , unsigned int period ) { std :: vector < float > rsi_value ; std :: vector < float > gain_loss = GainLoss ( input ) ; unsigned int len = input . size ( ) ; float gain = 0 , loss = 0 , avg_gain = 0 , avg_loss = 0 , prev_avg_gain , prev_avg_loss , rs ; for ( unsigned int j = 0 ; j < period ; ++ j ) { if ( gain_loss [ j ] <= 0 ) loss = loss + gain_loss [ j ] ; else gain = gain + gain_loss [ j ] ; } for ( unsigned int i = 0 ; i < len - period ; ++ i ) { if ( i == 0 && loss != 0 ) { avg_gain = gain / period ; avg_loss = -1 * loss / period ; rs = avg_gain / avg_loss ; rsi_value . push_back ( 100 - ( 100 / ( 1 + rs ) ) ) ; prev_avg_gain = avg_gain ; prev_avg_loss = avg_loss ; } else if ( gain_loss [ i + period - 1 ] <= 0 && ( ( prev_avg_loss * ( period - 1 ) - gain_loss [ i + period - 1 ] ) / period ) != 0 ) { rs = ( prev_avg_gain * ( period - 1 ) / period ) / ( ( prev_avg_loss * ( period - 1 ) - gain_loss [ i + period - 1 ] ) / period ) ; rsi_value . push_back ( 100 - ( 100 / ( 1 + rs ) ) ) ; prev_avg_gain = prev_avg_gain * ( period - 1 ) / period ; prev_avg_loss = ( prev_avg_loss * ( period - 1 ) - gain_loss [ i + period - 1 ] ) / period ; } else if ( gain_loss [ i + period - 1 ] > 0 && ( prev_avg_loss * ( period - 1 ) / period ) != 0 ) { rs = ( ( prev_avg_gain * ( period - 1 ) + gain_loss [ i + period - 1 ] ) / period ) / ( prev_avg_loss * ( period - 1 ) / period ) ; rsi_value . push_back ( 100 - ( 100 / ( 1 + rs ) ) ) ; prev_avg_gain = ( prev_avg_gain * ( period - 1 ) + gain_loss [ i + period - 1 ] ) / period ; prev_avg_loss = prev_avg_loss * ( period - 1 ) / period ; } else rsi_value . push_back ( 100 ) ; } return rsi_value ; }
@brief calculates RSI
 @details RSI calculation
 @param input the stock prices in a vector
 @param period the length of a period
@brief calculates RSI @details RSI calculation @param input the stock prices in a vector @param period the length of a period
// loss is a positive value
 // -gainloss[i+period] since loss is negative here
