1

I have a text file with 2 lines

fyers.place_orders(token = token, data = {"symbol" : "NSE:SBIN-EQ","qty" : 1,"type" : 2,"side" : 1,"productType" : "INTRADAY","limitPrice" : 0,"stopPrice" : 0,"disclosedQty" : 0,"validity" : "DAY","offlineOrder" : "False","stopLoss" : 0,"takeProfit" : 0})
fyers.place_orders(token = token, data = {"symbol" : "BSE:SBIN-EQ","qty" : 1,"type" : 2,"side" : 1,"productType" : "INTRADAY","limitPrice" : 0,"stopPrice" : 0,"disclosedQty" : 0,"validity" : "DAY","offlineOrder" : "False","stopLoss" : 0,"takeProfit" : 0})

These are basically the command required to fire orders to the Fyers Api for trading.

So my goal is to connect to the api and run these lines one by one. Here is my code

import sys
import os
import copy
import math
from fyers_api import accessToken
from fyers_api import fyersModel

token = 'gAAAAABern0tlSRuURXn3v0FB8M4MZ7z2PEIEfuJzpxEgkPYaVkDSVZ9vVp60qRI-Sm711-g2H6fcZRJiEKKqXyzyD9OdvnfjBuNac3iB1MX18o5ufDMdHE=&user_id=DDDDDD'

is_async = False#(By default False, Change to True for asnyc API calls.)

fyers = fyersModel.FyersModel(is_async)

my_profile = fyers.get_profile(token=token)
print(my_profile)
my_funds = fyers.funds(token=token)
print(my_funds)

#Print lines one by one
signal_file = open("C:/Users/USERNAME/AppData/Roaming/MetaQuotes/Terminal/D190C39A3FEE6BA05C8C73D22E65170B/MQL4/Files/signals.txt","r")
for line in signal_file:
    y = line
    print(y)
signal_file.close()

However, it is not working. Please help.

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
J-Jillu
  • 65
  • 1
  • 6
  • What part of this should actually be executing what is in `line`? – Scott Hunter May 03 '20 at 18:06
  • when i say y=line it must execute the first line of the text file. it is working when i directly type y = fyers.place_orders(token = token, data = {"symbol" : "NSE:SBIN-EQ","qty" : 1,"type" : 2,"side" : 1,"productType" : "INTRADAY","limitPrice" : 0,"stopPrice" : 0,"disclosedQty" : 0,"validity" : "DAY","offlineOrder" : "False","stopLoss" : 0,"takeProfit" : 0}) into the program. However, when i call it from a file it doesnt work – J-Jillu May 03 '20 at 18:12
  • is_async = False#(By default False, Change to True for asnyc API calls.) fyers = fyersModel.FyersModel(is_async) my_profile = fyers.get_profile(token=token) print(my_profile) my_funds = fyers.funds(token=token) print(my_funds) y = fyers.place_orders(token = token,data = {"symbol" : "NSE:SBIN-EQ","qty" : 1,"type" : 2,"side" : 1,"productType" : "INTRADAY","limitPrice" : 0,"stopPrice" : 0,"disclosedQty" : 0,"validity" : "DAY","offlineOrder" : "False","stopLoss" : 0,"takeProfit" : 0}) it works when i do this. however i want the y to be each line from the text file – J-Jillu May 03 '20 at 18:14
  • 1
    that is not the actual token... i edited it. – J-Jillu May 03 '20 at 18:17
  • it is supposed to give me a code 200 response or a code 400 if something is wrong. however, my code is only printing the lines from the file and not executing it inside the program – J-Jillu May 03 '20 at 18:20
  • You need to provide a [mre]. What specifically is not working? It seems like the API is not even relevant. Please [edit] the question. BTW welcome to SO! Check out the [tour] and [ask] if you want more advice. – wjandrea May 03 '20 at 18:22
  • 1
    Oh, I just noticed you're trying to run a string. There's already a question about that: [How do I execute a string containing Python code in Python?](https://stackoverflow.com/q/701802/4518341) – wjandrea May 03 '20 at 18:25
  • Please [edit] your question instead of adding comments. Code is unreadable in comments. Comments may or may not be shown initially. Order is not preserved. Make it easy to help you by putting everything in the question. – Robert May 03 '20 at 18:27
  • Why do you think `y = line` will *execute* what is in `line`? – Scott Hunter May 03 '20 at 19:31

1 Answers1

0

You must run the code you load in the variable. Replace

y = line

with

y = eval(line)

Massimo
  • 3,171
  • 3
  • 28
  • 41