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.
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...
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!!
So, I think I have solved it.
How would other people have approached this?