It's legal that's not a problem. The only snag that I've seen in these instances is that if you can't actually deliver the stock electronically or physically (the actual stock certificate)and the stock is still in your account the IRS may say if its still in your possession the trade has not occurred.
And, BTW, there is no use using the write off if you haven't made capital gasin in the same years. I'm not sure how long you can keep the losses on the books to reduce capital gains? Three years? Better ask a real accountant.