[R-SIG-Finance] IBrokers Risk Management Backend Solution Help with twsPortfolioValue
JohnnyPaper
brad.saterfiel at gmail.com
Wed Feb 13 02:54:17 CET 2013
Hi Everyone,
Ok so I have pretty much thoroughly (at least to my knowledge) tested a
couple of ways to get this solution under control. However, I cannot seem
to get around the problems of the code executing more than 1 order which
seems very strange to me. As I have mentioned in earlier posts, this is
what I need to have manage the positions. So what I did was try and create
a function to manage a futures position (ES only), which can currently be
tried out in paper at this moment. Please, will someone just help me finish
up this solution so that I can move forward. I really am trying here. As
well, Jeff (if you read this) I have sent you an email at lemnica.
Here is the code.
tws<-twsConnect()
OrderStatesFutures<-function(twsConnection,StopVal,ProfitVal){
Vals<-twsPortfolioValue(reqAccountUpdates(twsConnection),zero.pos=FALSE)
if (nrow(Vals) != 0){
ManagementData<-matrix(0,nrow(Vals),8)
for(i in 1:nrow(Vals)){
if (substr(Vals[i,1],1,2) == "ES"){
FillPrice<-abs(Vals[i,4]/(Vals[i,6]))/50
}
ManagementData[i,1:4]<-cbind(toString(substr(Vals[i,1],1,2)),toString(Vals[i,2]),toString(abs(Vals[i,6])),FillPrice)
if (Vals[i,6] < 0){
ManagementData[i,5]<-round(FillPrice*(1+StopVal),digits=2)
ManagementData[i,6]<-round(FillPrice*(1-ProfitVal),digits=2)
ManagementData[i,7]<-("SHORT")
ManagementData[i,8]<-("BUY")
} else if (Vals[i,6] >0){
ManagementData[i,5]<-round(FillPrice*(1-StopVal),digits=2)
ManagementData[i,6]<-round(FillPrice*(1+ProfitVal),digits=2)
ManagementData[i,7]<-("LONG")
ManagementData[i,8]<-("SELL")
}
}
colnames(ManagementData)<-c("Stock","IBType","NumShares","FillPrice","StopPrice","ProfitTarget","Currently","ToClose")
return(ManagementData)
}
}
OSFutures<-OrderStatesFutures(tws,.00025,.00025)
while(TRUE){
Flag<-1
if ( nrow(OSFutures) != 0){
Ding1<-reqMktData(tws,
twsFUT("ES","GLOBEX","201303"),eventWrapper=eWrapper.data(1),snapshot=TRUE)
PositionMonitor<-cbind(OSFutures,Ding1[,7]);print(PositionMonitor)
for (i in 1:nrow(PositionMonitor)){
if( Flag == 1 & PositionMonitor[i,7] == "LONG" & (PositionMonitor[i,9] <=
PositionMonitor[i,5])){
IBrokers:::.placeOrder(tws,twsFUT(PositionMonitor[i,1],"GLOBEX","201303"),twsOrder(Offset,"SELL",PositionMonitor[i,3],"MKT"))
Flag<-2
Offset<-Offset + 1
#Sys.sleep(1)
#break
} else if ( Flag == 1 & PositionMonitor[i,7] == "LONG" &
(PositionMonitor[i,9] >= PositionMonitor[i,6])){
IBrokers:::.placeOrder(tws,twsFUT(PositionMonitor[i,1],"GLOBEX","201303"),twsOrder(Offset,"SELL",PositionMonitor[i,3],"MKT"))
Flag<-2
Offset<-Offset + 1
#Sys.sleep(1)
#break
} else if ( Flag == 1 & PositionMonitor[i,7] == "SHORT" &
(PositionMonitor[i,9] >= PositionMonitor[i,5])){
IBrokers:::.placeOrder(tws,twsFUT(PositionMonitor[i,1],"GLOBEX","201303"),twsOrder(Offset,"BUY",PositionMonitor[i,3],"MKT"))
Flag<-2
Offset<-Offset + 1
#Sys.sleep(1)
#break
} else if ( Flag == 1 & PositionMonitor[i,7] == "SHORT" &
(PositionMonitor[i,9] <= PositionMonitor[i,6])){
IBrokers:::.placeOrder(tws,twsFUT(PositionMonitor[i,1],"GLOBEX","201303"),twsOrder(Offset,"BUY",PositionMonitor[i,3],"MKT"))
Flag<-2
Offset<-Offset + 1
#Sys.sleep(1)
#break
}
#break
}
}
Sys.sleep(1)
OSFutures<-OrderStatesFutures(tws,.00025,.00025)
if(nrow(OSFutures) == 0){
break
}
}
I'd seriously like to thank anyone with some input to help me get this
solved.
Best to All,
Brad
--
View this message in context: http://r.789695.n4.nabble.com/IBrokers-Risk-Management-Backend-Solution-Help-with-twsPortfolioValue-tp4658101p4658357.html
Sent from the Rmetrics mailing list archive at Nabble.com.
More information about the R-SIG-Finance
mailing list