Unfortunately, the police were within their rights to require a blood test. Every state has an implied consent statute which says you are deemed to have consented to a test if the police require it of you. To have refused would have been to cost you your license.
From there, if there is an intoxicant in your system, whether it is prescribed or not, the officer can charge you with a DUI. Drugs are not like alcohol when it comes to a DUI charge. That is, with alcohol, they can look at the levels and see if you are drunk. They do not use levels with drugs because the levels aren't meaningful in the same way. Any presence of an intoxicating drug would be sufficient to get you charged with a DUI.
That said, a valid prescription for the medication, along with a statement from your doctor that when taken as prescribed you would be able to drive safely, can frequently get a charge like this dismissed.
You have to keep in mind, however, that there is a big difference between what the state needs to arrest you and what they need to convict you. To arrest you, they only need probable cause -- a reasonable belief that your driving was impaired by your medication. Your slowness and poor balance plus the positive test would give the officer probable cause for the arrest.
In court, you/your lawyer would be able to present medical proof that it was your medical condition and not the effects of the tramadol that caused what looked like, but weren't, signs of intoxication. But the police don't have to take your word for that at the scene of an incident. They're entitled to arrest you if it looks reasonable to them that you may be intoxicated.
If you wish to go to trial on this case, you'd be able to challenge this at a pre-trial hearing, but hopefully, with proof from your doctor and a valid prescription, it can be dismissed before it ever has to come to that.