It won't work the way that you think it works. Basically, you cannot make this work out in your favor without the willing participation of the people who's money you would be taking.
The play that is illegal is where you buy a bunch of stock quietly, then deliberately mislead others into buying lots and lots of that stock to drive the price up (pump), and then sell all your shares before everyone figures out that you were misleading them (dump). You can't do this without the willing participation of the losers, that's why you need to mislead them. That's wrong and illegal, called "pump and dump."
If you try to do this without misleading anyone, it could sound like this:
Suppose that you buy a bunch of stock (and it would have to be a BUNCH) in order to drive the price up. Well, as you're buying it, it will go up, so each new batch will be a little more expensive for you than the last. Remember, you need to buy LOTS, so you can't think that you'll be able to just buy it all at one price in one transaction. It doesn't work that way.
Anyway, now the price is much higher than when you started, so you decide to sell. But once again you can't just sell it all at once in one large transaction at the higher price. You have to sell it in batches. And as you do the price will fall. You'll get a little less for each batch as you sell them. Ultimately, you'll be right back where you started, plus or minus a bit. Congratulations! You pumped-and-dumped yourself!