Context:
One of the great features that the market has API de Binance
is that it allows its users to set a custom order id ( also known as newClientOrderId
) for each trade they make on the market Binance Futures
, this feature becomes useful if someone wants to keep track of the trades being made. made following a particular trading strategy.
According to your Documentos oficiales
, the parameter that will be sent in the POST request to put the new command must be newClientOrderId
and its value must match this regular expression criteria:^[\.A-Z\: /a-z0-9_-]{1,36}$
Problem:
I wrote a simple random string function in Python that returns a random string based on 2 parameters which are a string and the length of the desired string to match, here it is:
import random, string
def randstr(s, length):
all_chars = string.ascii_lowercase
result = s
for i in range(length):
result += random.choice(all_chars)
return(result)
Tests
randstr('test1',5)
Out[14]: 'test1uljgp'
randstr('test1',2)
Out[15]: 'test1mo'
randstr('test1',5)
Out[16]: 'test1pbgjw'
So how do I know if the output of my randstr
custom function matches this regex criteria: ^[\.A-Z\:/a-z0-9_-]{1,36}$
? Knowing this, I will make sure that future developments related to this API continue to work non-stop.
You can always apply the method
search()
to check if a particular string satisfies a regular expression. This is applied after calling the functionrandstr()
Since the pattern already includes the match of the start and end of text, we use a
search()
instead ofmatch()
.show
produces: