Writing and executing a python to Binance API file from AFL strings

I am trying to write a trading algorithm for Binance from Amibroker.

I'm not certain of the best way to approach this. Clearly I want it to be able to function and take in information about position sizes based on where my stop is, so I need it to be dynamic and have variables.

I am not certain how this would normally be done. Would the user normally use Amibroker to fully test and optimise their system, then simply translate that into something like python and run it outside of amibroker, or would the user normally use Amibroker dynamically in some way with Python (which I don't really know how to do yet).

My attempt;
I have successfully implemented the python code and have got the script communicating with the Binance API endpoints. I'm using the testnet API for now and I have created a file outside of this which python references, where the API keys are kept. It works when I launch it via command prompt or windows powershell. I actually get trades through on the testnet. The balances are not real (I wish!!!)
Here is the python code;

from binance.client import Client
import config
import time


client = Client(config.apiKey, config.apiSecret)
print("logged in")


excinf = client.get_exchange_info()
acc = client.get_account()

timez = str(excinf['timezone'])
print("Timezone =: "+timez)

bal = acc['balances']

print("Balances: ")
for b in bal:
  if float(b['free'])>0:
    print (b)

from binance.enums import *
order = client.create_order(
  symbol = 'ETHBUSD',
  side = SIDE_BUY,
  type = ORDER_TYPE_MARKET,
  quantity = 3)
print("************************")

time.sleep(3)
print("!!!!!MARKET ORDER!!!!!")
time.sleep(3)


print("Balances: ")
for b in bal:
  if float(b['free'])>0:
    print (b)

I get this in the command prompt.

Screen Shot 2022-08-07 at 6.39.52 pm

So, I have gone 1 step further and in AFL manipulating strings, I have used the fputs() and strformat() functions and written in afl a long string, such as this

fmkdir("C:\\Program Files\\AmiBroker\\Export\\");
path= "C:\\Program Files\\AmiBroker\\Export\\bin.py";
fh = fopen(path,"w");

if( fh )
{
    filecontent = StrFormat(
"from binance.client import Client"
+"\nimport config"
+"\n"
+"\n"
+"\nclient = Client(config.apiKey, config.apiSecret)"
+"\nprint("+"'logged in'"+")"
+"\n"
+"\n"+
"\nexcinf = client.get_exchange_info()"
+"\nacc = client.get_account()"
+"\n"
+"\ntimez = str(excinf['timezone'])"
+"\nprint("+"'Timezone =: '"+"+timez)"
+"\n"
+"\nbal = acc['balances']"
+"\n"
+"\nprint("+"'Balances: '"+")"
+"\nfor b in bal:"
+"\n  if float(b["+"'free'"+"])>0:"
+"\n    print (b)"
+"\n"
+"\nfrom binance.enums import *"
+"\norder = client.create_order("
+"\n  symbol = 'ETHBUSD',"
+"\n  side = SIDE_BUY,"
+"\n  type = ORDER_TYPE_MARKET,"
+"\n  quantity = 0.1,)"
+"\n"  
+"\nprint("+"'!!!!!MARKET ORDER!!!!!'"+")"
+"\n"  
+"\n"
+"\nprint("+"'Balances:'"+")"
+"\nfor b in bal:"
+"\n  if float(b['free'])>0:"
+"\n    print (b)"
                     );

    fputs( filecontent, fh );
    fclose( fh );
    ThreadSleep(1000);
    ShellExecute("cmd.exe","py bin.py","C:\\Program Files\\AmiBroker\\Export\\",1);
}

This creates the python file, and, if I manually execute the via the command prompt, I get the exactly same result as if I execute the file I wrote straight into python, and this is great. However, I cannot automate it. I end up with...
Screen Shot 2022-08-07 at 6.44.41 pm

So, I'm getting closer. I had the idea (while writing this post actually) that perhaps powershell might be a better way to approach it. So, I tried changing it windows powershell. That seems to do the necessary, but as soon as it has finished executing, the powershell window closes.

 ShellExecute("PowerShell.exe","py bin.py","C:\\Program Files\\AmiBroker\\Export\\",1);

So by adding;

+"\nimport time"

and

+"\ntime.sleep(10)"

to my string, it all seems to work nicely!! :grin:

So, I think I have solved it.

How would other people have approached this?

Are aware that Python can be run directly from AFL? See

Thanks for the pointer.

I was aware, but I’ll be honest, I didn’t really know how to use it. I have installed it, but even after reading the thread, I don’t really know what it actually does.

Would I call the function then write a Python script directly into the AFL formula editor, or am I more using it to reference files.py that exist outside Amibroker?

You don't need to write script each time you communicate with Binance. Instead, you should create a Python FUNCTION that accepts desired variables as parameters and call such function using AmiPy.

I'll get on that. I tried to make the amipy bridge work this morning but I'm running into a load of errors. I'be got the Python38.dll and Its in PATH, and I have AmiPy etc, but I'm getting error codes.

I wont paste them here, because I'd rather work through them and work out what is going on, but if I can't progress, I'll come back.
:face_in_clouds:

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.