-3

I have some functions, as .txt files which are written using easy language and I need to extract data from those functions using python. as an example consider the following part.

code segment -

If MarketPosition = 0 and (EntriesToday(Date) < 1 or EndofSess) and
EntCondL 
then begin
    Buy("EnStop-L") NShares shares next bar at EntPrL stop;
end;

in here, I need to extract the parts

  • MarketPosition = 0
  • EntriesToday(Date) < 1
  • EndofSess
  • EntCondL

and identify =, < signs using python. thanks in advance.

cuzi
  • 978
  • 10
  • 21

2 Answers2

1

Here is an example for finding and splitting the text between if and then The result is a list of the individual elements: variables, brackets and the comparison operators.

code = """
If MarketPosition = 0 and (EntriesToday(Date) < 1 or EndofSess) and
EntCondL 
then begin
    Buy("EnStop-L") NShares shares next bar at EntPrL stop;
end;
"""

import re
words = re.split("\s+|(\(|\)|<|>|=|;)", code)

is_if = False
results = []
current = None
for token in words:
    if not token:
        continue
    elif token.lower() == "if":
        is_if = True
        current = []
    elif token.lower() == "then":
        is_if = False
        results.append(current)
    elif is_if:
        if token.isdecimal(): # Detect numbers
            try:
                current.append(int(token))
            except ValueError:
                current.append(float(token))
        else: # otherwise just take the string
            current.append(token)



print(results)

The result:

['MarketPosition', '=', 0, 'and', '(', 'EntriesToday', '(', 'Date', ')', '<', 1, 'or', 'EndofSess', ')', 'and', 'EntCondL']

I think it's easier to go from here (I don't in which form you need the data, for example do the brackets matter?)

cuzi
  • 978
  • 10
  • 21
1

i think you are looking for prefix and postfix of some operators
i suggest you to find these list of operators and use it's location to get prefix and postfix

Muhammad Yusuf
  • 563
  • 4
  • 20