dexter
March 22, 2023, 8:23am
1
Pinakin is an API-based trading terminal that allows users to perform a variety of operations related to trading and analysis. In addition to supporting various brokers, Pinakin also integrates with popular third-party systems such as Sensibull, Chartink, Tradingview, and Screener.
This allows users to access a wide range of tools and features for trading and analysis, all within a single platform. Pinakin’s API-based architecture also provides flexibility and customization options for advanced users who want to build their own trading strategies or integrate with other systems.
Here is a quick insight on how multivariate global functions are handled as discussed in quant channel.
dexter
March 22, 2023, 8:23am
2
def positions(broker,first_name,last_name):
func = globals()[broker+"_positions"]
return func(first_name,last_name)
def iifl_positions(first_name,last_name):
print("hello1"+first_name+last_name)
def paisa_positions(first_name,last_name):
print("hello2"+first_name+last_name)
positions("iifl"," amit"," ghosh")
This is method 1
dexter
March 22, 2023, 8:23am
3
def iifl_positions(first_name, last_name):
print("Hello1 " + first_name + " " + last_name)
def paisa_positions(first_name, last_name):
print("Hello2 " + first_name + " " + last_name)
broker_functions = {
"iifl": iifl_positions,
"paisa": paisa_positions
}
def positions(broker, first_name, last_name):
if broker in broker_functions:
return broker_functions[broker](first_name, last_name)
else:
print("Broker not supported")
positions("iifl", "Amit", "Ghosh")
positions("paisa", "John", "Doe")
This is method 2
dexter
March 22, 2023, 8:25am
4
def iifl_positions(first_name, last_name):
print("Hello1 " + first_name + " " + last_name)
def paisa_positions(first_name, last_name):
print("Hello2 " + first_name + " " + last_name)
broker_functions = {
"iifl": iifl_positions,
"paisa": paisa_positions
}
def positions(broker, first_name, last_name):
if broker in broker_functions:
return broker_functions[broker](first_name, last_name)
else:
print("Broker not supported")
positions("iifl", "Amit", "Ghosh")
positions("paisa", "John", "Doe")
This is method 3.
But the drawback in these is, it can not handle optional arguments. For say, this following code will land in error.
import inspect
def iifl_positions(first_name, last_name, account_id="123"):
print("Hello1 " + first_name + " " + last_name + " " + account_id)
def paisa_positions(first_name, last_name, user_id):
print("Hello2 " + first_name + " " + last_name + " " + user_id)
broker_functions = {
"iifl": (iifl_positions, ["first_name", "last_name", "account_id"]),
"paisa": (paisa_positions, ["first_name", "last_name", "user_id"])
}
def positions(broker, **kwargs):
if broker in broker_functions:
func, arg_names = broker_functions[broker]
if set(kwargs.keys()) == set(arg_names):
kwargs = {arg_name: kwargs[arg_name] for arg_name in arg_names}
return func(**kwargs)
else:
print(f"Expected arguments for {broker}: {arg_names}")
else:
print(f"Broker {broker} not supported")
positions("iifl", first_name="Amit", last_name="Ghosh")
positions("paisa", first_name="John", last_name="Doe", user_id="456")
In this case I use inspect.signature
to get full list of parameters.
dexter
March 22, 2023, 9:04am
5
import inspect
def iifl_positions(first_name, last_name, account_id="123"):
print("Hello1 " + first_name + " " + last_name + " " + account_id)
def paisa_positions(first_name, last_name, user_id=None):
print("Hello2 " + first_name + " " + last_name + " " + str(user_id))
broker_functions = {
"iifl": (iifl_positions, ["first_name", "last_name", "account_id"]),
"paisa": (paisa_positions, ["first_name", "last_name", "user_id"])
}
def positions(broker, **kwargs):
if broker in broker_functions:
func, arg_names = broker_functions[broker]
sig = inspect.signature(func)
bound_args = sig.bind(**kwargs)
bound_args.apply_defaults()
kwargs = dict(bound_args.arguments)
if set(kwargs.keys()) == set(arg_names):
kwargs = {arg_name: kwargs[arg_name] for arg_name in arg_names}
return func(**kwargs)
else:
print(f"Expected arguments for {broker}: {arg_names}")
else:
print(f"Broker {broker} not supported")
positions("iifl", first_name="Amit", last_name="Ghosh")
positions("paisa", first_name="John", last_name="Doe", user_id="456")
positions("paisa", first_name="Jane", last_name="Doe")
The correct method. Now this becomes easier when you do multiprocess