Hello,
I have trying to shoot BUY Orders from amibroker to my Broker using Python. I can place order for single symbol well, but when I use "Name() + EQ" in AFL , it is not working.
I am using ShellExecute() for this and it works for particular Stock if I specifically mention it in AFL as well as Python file, but does not work when using Name()+EQ (When I use NAME(), there is no -EQ so I have to put +EQ, please see codes)
I am attaching my AFL for BUY orders as well as Python (which shoots orders to broker terminal)
Please help me ! I am tryinig to do this all using AI coding I am not a coder. Its all trial and error !
Please guide me on this little problem
AFL for BUY orders
// BuyOrderParamTrigger.afl
SetTradeDelays(0, 0, 0, 0); // No delays
// Define the trigger parameter
BuyTrigger = ParamTrigger("Place Buy Order", "CLICK TO BUY");
// Buy signal based on trigger
Buy = BuyTrigger; // Buy when the trigger is clicked
Sell = 0; // No sell for this test
BuyPrice = Close; // Buy at close price
SetPositionSize(1, spsShares); // 1 share
// Get the current chart symbol and append -EQ
Symbol = Name() + "-EQ"; // e.g., "TCS-EQ"
// Execute Python script with the symbol as a parameter
if (LastValue(Buy)) {
PopupWindow("Sending Buy for: " + Symbol, "Debug", 5);
ShellExecute("d:\\Python\\python.exe d:\\amibrokme\\place_order.py " + "\"" + Symbol + "\"", "", "");
}
The following is Code for Python filename place_order.py
# place_order.py
import sys
from NorenRestApiPy.NorenApi import NorenApi
class FlatTradeApiPy(NorenApi):
def __init__(self):
NorenApi.__init__(self, host='https://piconnect.flattrade.in/PiConnectTP/', websocket='wss://piconnect.flattrade.in/PiConnectWSTp/', eodhost='https://web.flattrade.in/chartApi/getdata/')
def place_buy_order(symbol=None, qty=1):
# Get symbol from command-line argument if provided
if len(sys.argv) > 1:
symbol = sys.argv[1] # e.g., "TCS-EQ" from AmiBroker
else:
symbol = 'RELIANCE-EQ' # Default
print("Symbol received:", symbol) # Debug print
api = FlatTradeApiPy()
ret = api.set_session(userid='HIDDEN ON PURPOSE', password='HIDDEN ON PURPOSE!', usertoken='HIDDEN ON PURPOSE')
print("set_session response:", ret)
if isinstance(ret, dict):
if ret.get('stat') != 'Ok':
print("Session failed:", ret)
return None
elif isinstance(ret, bool):
if not ret:
print("Session failed: Returned False")
return None
else:
print("Unexpected set_session return type:", type(ret), ret)
return None
order = api.place_order(
buy_or_sell='B',
product_type='C',
exchange='NSE',
tradingsymbol=symbol,
quantity=qty,
discloseqty=0,
price_type='MKT',
price=0,
trigger_price=0,
retention='DAY',
remarks='AmiBroker Dynamic Symbol Test',
act_id='HIDDEN ON PURPOSEEEE'
)
return order
if __name__ == "__main__":
response = place_buy_order()
print("Order response:", response)
I am trying to use this Broker FLATTRADE and this is their API Documentation page...
Please guide me !