If you’ve been in your house for a while, you likely have built up some equity — the difference between what you still owe on your current mortgage and your home’s value. You can use equity for practically anything, including funding stock purchases or other investments.

When the stock market is doing well and mortgage interest rates are low, you may wonder if refinancing and pulling the equity out of your home to invest in stocks is a wise choice. Investing can pay off, but it’s also inherently risky.

So, when is a cash-out refinance to fund your investments a good strategy, and when is it a mistake? Let’s look at things to consider.

A cash-out refinance has two main benefits. It turns the equity you have in your home into cash, and your new mortgage loan could come with a lower interest rate, a lower monthly payment or possibly both.

Investing in stocks can be profitable, but comes with no guarantee of gains. It’s possible you could invest your equity and lose your entire investment. And remember, while your investments may generate money, you’ll also be paying interest on the home equity you borrowed.

Whether you want to buy a second home or an investment property to rent out and generate income, using the equity from a cash-out refinance can save you from dipping into your savings to fund the purchase. Because real estate tends to see less erratic swings than the stock market, your investment property will likely gain in value over time, with less risk of your investment losing value.

And while it’s easier to buy stocks than real estate, there are more potential tax benefits from investing in real estate.