There are three limiting factors.
First, you must file it before you publish the idea yourself. Telling others in strict confidence is ok (provided that they abide by that agreement), but any other form of disclosure will potentially invalidate a later-filed patent application. If you sell an example of the information, then you have disclosed whatever can be reverse-engineered from that product. If you tell a potential customer how it works, then you have just disclosed everything you said. So you need to file before you give the game away.
You also need to file the application before other people disclose the same or similar idea. They are all beavering away, trying to get their own product on sale, trying to file their own patent applications. Their products can be cited against your application, to the extent that they can be reverse-engineered. Their patent applications can be cited against yours, with everything they disclose removed from the scope of what you can protect.
So filign sooner rather than later is good, right?
Not necessarily. As well as being new, the invention must also be sufficiently described. If you file too soon, then you may find out that you have missed out information that is essential to the proper working of the invention. Wait, and file it after you have done enough to be reasonably sure that it actually works.