How to trade multiple equities at once using Ibrokers in R - r

Using the Ibrokers account I know how to place a trade with one ticker which , in the example below I place a trade with "DAL"
library(IBrokers)
tws=twsConnect(clientId = 1,host = "localhost", port = 7497 )
contract=twsEquity(symbol = "DAL", exch = "SMART" )
order=twsOrder(action = "BUY", totalQuantity ="10", tif = "OPG" )
placeOrder(twsconn = tws, Contract = contract, Order = order)
However I am interested in trading multiple tickers at once for example how can I place an order to buy "DAL" and "AAL". How can I put multiple orders into IBrokers in R?

I doubt your code even works now. To place another order just place another order. Note that they must have separate ids and the ids have to increase every order.
library(IBrokers)
tws=twsConnect(clientId = 1,host = "localhost", port = 7497 )
id <- tws.reqids(1)
contract=twsEquity(symbol = "AAL", exch = "SMART" )
# give an order id
order=twsOrder(id, action = "BUY", totalQuantity ="10", tif = "OPG" )
placeOrder(twsconn = tws, Contract = contract, Order = order)
# increment id
id <- id+1
contract=twsEquity(symbol = "DAL", exch = "SMART" )
order=twsOrder(id,action = "BUY", totalQuantity ="10", tif = "OPG" )
placeOrder(twsconn = tws, Contract = contract, Order = order)
I don't think that code will work. The defaults for order look to be LMT with a price of 0. You may want to try orderType = "MKT". Check the documentation for IBrokers.
I would suggest using a different API, it doesn't look like you have used the R package much so it's not a big deal to switch. It probably won't work for much longer. I think it is unsupported and the API has changed since the last update.

Related

Data Manipulation: Dynamic observation Drop

I am working on credit line assimilation data.
df_01 <- data.frame(
user_id = c(1,1,1,1,1,1,1,1,1,2,2),
var_type = c("withdraw","repaid","repaid","withdraw","repaid","repaid","repaid","withdraw","repaid","withdraw","repaid"),
withdraw_id = c("u1_w1","u1_w1","u1_w1","u1_w2","u1_w2","u1_w2","u1_w2","u1_w3","u1_w3","u2_w1","u2_w1"),
repaid_id = c("","u1_w1_r1","u1_w1_r2","","u1_w2_r1","u1_w2_r2","u1_w2_r3","","u1_w3_r1","","u2_w2_r1"),
amt = c(-50,30,20,-60,20,30,10,-40,50,-30,10),
credit_limit = c(100,100,100,100,100,100,100,100,100,50,50),
running_balance = c(50,80,100,40,60,90,100,60,110,40,40),
new_credit_limit = c(50,50,50,50,50,50,50,50,50,40,40),
new_running_balance = c(0,30,50,-10,NA,NA,NA,10,60,0,40),
drop_obs_flag = c(0,0,0,1,1,1,1,0,0,0,0)
)
here
var_type: has two types i.e withdraw, repaid
repaid id is associated with withdraw_id
we are trying to see, if we alloted different credit line to users, how will it affect our business
Objective: as per new credit line, if withdraw amount is greater than, previous new running balance for a user_id, we have to drop all rows associated with that withdraw_id. i.e drop_obs_flag
remarks_1: if the earlier withdraw obs is dropped, then we have to check the next withdraw id (it's in loop) for that user_id,
remarks_2: last 2 columns, new_running_balance and drop_obs_flag is output variable

i can't do makemigrations in django 1.11 (fields.E312)

class Player(models.Model):
name = models.CharField(max_length = 256)
number = models.IntegerField()
age = models.IntegerField()
is_captain = models.BooleanField(default = False)
class Injuries(models.Model):
player = models.ForeignKey(Player, Player.name)
team = models.ForeignKey(Team)
Below are my suggestions, as per my capability.
As per the documentation,
A many-to-one relationship. Requires two positional arguments: the
class to which the model is related and the on_delete option.
See the reference here. So the first positional argument is the class which is related by Foreign Key and the second position argument is on_delete. You should define it as models.CASCADE or as appropriate for your app.
Thus in this case the second positional argument is "Player.name". I think you have to first replace it with models.CASCADE.
Thus change the code from what is below
class Injuries(models.Model):
player = models.ForeignKey(Player, Player.name)
team = models.ForeignKey(Team)
to the one below
class Injuries(models.Model):
player = models.ForeignKey(Player, on_delete=models.CASCADE)
team = models.ForeignKey(Team)
The foreign key is set to the primary key of the related model. In this case (since you have not defined it), it will be player_id that is automatically assigned by django. If you want it to force it to use the "name" column from the Player model, then you have to set "unique=True" in the name field of the Player model. Also notice the use of to_field='name' option.
In such a case the changed code will be as below.
class Player(models.Model):
name = models.CharField(max_length = 256, unique=True)
number = models.IntegerField()
age = models.IntegerField()
is_captain = models.BooleanField(default = False)
class Injuries(models.Model):
player = models.ForeignKey(Player, to_field='name', on_delete=models.CASCADE)
team = models.ForeignKey(Team)
Hopefully it will work. If there is still an error, please let me know.

Peoplesoft Learning Management - First Time Pass Rate

We are working on PeopleSoft ELM 9.2 and I'm not finding what I need in any of the OOB queries so I'm attempting to build one. We need to get a "First Time Pass Rate" score, or the average of how many people pass a learning course on their first try. Ideally we would have the data for each attempt so that we could also see how many attempts a particular person or course has prior to getting a passing score.
Our setup includes SCORM 1.2 modules that pass off a score to the LMS for verification that a passing score was received. The closest I've been able to come so far is to get a "Pass/Fail" score but either my query unions are incorrect or the data is incorrect as it returns extraneous data that is not relevant to the other data returned or (if I force distinct values) it returns sporadically accurate data. Below is the SQL that I'm using if it helps.
Has anyone tried to build this kind of query before and how did you do it? :)
SELECT DISTINCT D1X.XLATLONGNAME, D.LM_ORGANIZATION_ID, D.LM_ORG_DESCR,
D.LM_HR_EMPLID, TO_CHAR(D.LM_HIRE_DT,'YYYY-MM-DD'), D.FIRST_NAME,
D.LAST_NAME, C.LM_CS_LONG_NM, C.LM_ACT_CD, A.LM_LC_LONG_NM, A.LM_LC_ID,
B12X.XLATLONGNAME, TO_CHAR(B.LM_COMPL_DT,'YYYY-MM-DD'), B.LM_ENRLMT_ID,
E15X.XLATLONGNAME
FROM PS_LM_LC A, PS_LM_ENRLMT B LEFT OUTER JOIN PSXLATITEM B12X ON
B12X.FIELDNAME='LM_STTS' AND B12X.FIELDVALUE=B.LM_STTS AND B12X.EFF_STATUS =
'A' AND B12X.EFFDT = (SELECT MAX(EFFDT) FROM PSXLATITEM TB WHERE
TB.FIELDNAME=B12X.FIELDNAME AND TB.FIELDVALUE=B12X.FIELDVALUE AND
TB.EFF_STATUS = 'A' AND TB.EFFDT <= TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-
DD'),'YYYY-MM-DD') ), PS_LM_ACT_CI_VW C, PS_LM_PERS_DTL_VW D LEFT OUTER JOIN
PSXLATITEM D1X ON D1X.FIELDNAME='LM_ACTIVE' AND D1X.FIELDVALUE=D.LM_ACTIVE
AND D1X.EFF_STATUS = 'A' AND D1X.EFFDT = (SELECT MAX(EFFDT) FROM PSXLATITEM
TB WHERE TB.FIELDNAME=D1X.FIELDNAME AND TB.FIELDVALUE=D1X.FIELDVALUE AND
TB.EFF_STATUS = 'A' AND TB.EFFDT <= TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-
DD'),'YYYY-MM-DD') ), PS_LM_ENR_LC_BL_VW E LEFT OUTER JOIN PSXLATITEM E15X
ON E15X.FIELDNAME='LM_PASS_STTS' AND E15X.FIELDVALUE=E.LM_PASS_STTS AND
E15X.EFF_STATUS = 'A' AND E15X.EFFDT = (SELECT MAX(EFFDT) FROM PSXLATITEM TB
WHERE TB.FIELDNAME=E15X.FIELDNAME AND TB.FIELDVALUE=E15X.FIELDVALUE AND
TB.EFF_STATUS = 'A' AND TB.EFFDT <= TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-
DD'),'YYYY-MM-DD') )
WHERE ( A.LM_ACT_ID = B.LM_ACT_ID
AND A.LM_ACT_ID = C.LM_ACT_ID
AND D.LM_PERSON_ID = B.LM_PERSON_ID
AND D.BUSINESS_UNIT IN ('00340','00235')
AND B.LM_ENRL_DT BETWEEN TO_DATE(:1,'YYYY-MM-DD') AND TO_DATE(:2,'YYYY-MM-DD')
AND D.LM_HR_EMPLID = :3
AND D.LM_ACTIVE = :4
AND B.LM_STTS = 'COMP'
AND A.LM_LC_ID = E.LM_LC_ID
AND E.LM_PASS_STTS IN ('FAIL','PASS'))

Query matched 0 results, verify your query

I'm trying to develop an dashboard in Excel, using R studio.
I have now the code:
session01 <- Init(start.date = '2015-02-01',
end.date = '2015-04-01',
dimensions = 'ga:date,ga:userType',
metrics = 'ga:sessions',
sort = 'ga:date',
segments = NULL,
max.results = 10000,
start.index = NULL,
table.id = 'ga:XXXXXXX')
session01 <- QueryBuilder(session01)
report02 <- GetReportData(session01, oauth_token_LO)
However, it get this message:
> report02 <- GetReportData(session01, oauth_token_LO)
Your query matched 0 results. Please verify your query using the Query Feed Explorer and re-run it.
Error in GetDataFeed(query.uri) :
no loop for break/next, jumping to top level
Does anybody know how to solve this?

Shiny: BigQuery Fails when user selects "All" value

I am trying to use a BigQuery query to populate plots in Shiny. The query includes input values from the ui using selectInput. If the user selects a value that exists in the DB, such as year is 2014, the query works correctly, however, I would like the user to also be able to select "All." "All" should be a selection of all values, however, I am not sure how to express that in the query using selectInput.
server.r
data1 <- eventReactive(input$do_sql, {
bqr_auth(token = NULL, new_user = FALSE, verbose = FALSE)
query = paste('select month, event, partner_name, sum(f0_) from [dataset.table] where year =',input$year1,' and partner_name = \"',input$partner_name,'\"
GROUP by 1,2,3
ORDER by 1 asc
LIMIT 10000', sep="")
bqr_query(projectId, datasetId, query, maxResults =2000)
})
ui.r
(
selectInput("year1",
"Year:",
c("All",2014,2015
))
),
(
selectInput("partner_name",
"Partner:",
c("All",
unique(as.character(data5$partner_name))))
You should slightly change the query you are constructing
So, currently you have
SELECT month, event, partner_name, SUM(f0_)
FROM [dataset.table]
WHERE year = selected_year
AND partner_name = "selected_partner_name"
GROUP BY 1,2,3
ORDER BY 1 ASC
LIMIT 10000
with respectively:
selected_year --> input$year1
selected_partner_name --> input$partner_name
Instead, you should construct below query
SELECT month, event, partner_name, SUM(f0_)
FROM [dataset.table]
WHERE (year = selected_year OR "selected_year" = "All")
AND (partner_name = "selected_partner_name" OR "selected_partner_name" = "All")
GROUP BY 1,2,3
ORDER BY 1 ASC
LIMIT 10000
I am not shiny user at all - so excuse my syntax - below is just my
guess with regard of implementing above suggestion
query = paste('SELECT month, event, partner_name, sum(f0_)
FROM [dataset.table]
WHERE (year =',input$year1,' OR "All" ="',input$year1,'")
AND (partner_name = \"',input$partner_name,'\" OR "All" = \"',input$partner_name,'\")
GROUP by 1,2,3
ORDER by 1 asc
LIMIT 10000', sep="")
Mikhail's solution worked perfectly for character variables, but numerics didn't work correctly. I decided to use a character date range instead of the year numeric I originally used. Thanks.

Resources