


# co.txt run's like this: R <co.txt> out.txt

library(IBrokers)

CO_INT_N_REV <- function(twsCon, eWrapper, timestamp, file, playback = 1, ...) {
	
	con <- twsCon[[1]]	
	traded_1 <- FALSE
	closed_1 <- FALSE
	traded_2 <- FALSE
	closed_2 <- FALSE
	traded_3 <- FALSE
	closed_3 <- FALSE
	traded_4 <- FALSE

	ihavetraded <- 'I HAVE NOT TRADED'
	while (TRUE) {

		socketSelect(list(con), FALSE, NULL)

		# get data
		
		curMsg <- readBin(con, character(), 1)
		if (!is.null(timestamp)) {
			processMsg(curMsg, con, eWrapper, format(Sys.time(), timestamp), file, ...)
		} else {
			processMsg(curMsg, con, eWrapper, timestamp, file, ...)
		}

		# extract data

		Symbols <- eWrapper$.Data$symbols 
		print(ihavetraded)
		print(c('Time', format(Sys.time())))

		Last_1_1 <- as.numeric(eWrapper$.Data$data[[1]][,5])
		Last_1_2 <- as.numeric(eWrapper$.Data$data[[2]][,5])
		Spread_1 <- Last_1_1 - Last_1_2
		print(c('LEI ',Last_1_1, 'WOR ', Last_1_2, 'Spread ', Spread_1, 'The boundaries are 4.46697721920205', 'lower 2.44602278079795',' mean is 3.4565'))
		Last_2_1 <- as.numeric(eWrapper$.Data$data[[3]][,5])
		Last_2_2 <- as.numeric(eWrapper$.Data$data[[4]][,5])
		Spread_2 <- Last_2_1 - Last_2_2
		print(c('QBE ',Last_2_1, 'AMP ', Last_2_2, 'Spread ', Spread_2, 'The boundaries are 13.3779288310757', 'lower 12.2030711689243',' mean is 12.7905'))
		Last_3_1 <- as.numeric(eWrapper$.Data$data[[5]][,5])
		Last_3_2 <- as.numeric(eWrapper$.Data$data[[6]][,5])
		Spread_3 <- Last_3_1 - Last_3_2
		print(c('QBE ',Last_3_1, 'AXA ', Last_3_2, 'Spread ', Spread_3, 'The boundaries are 12.1631661762672', 'lower 11.1048338237328',' mean is 11.634'))
		Last_4_1 <- as.numeric(eWrapper$.Data$data[[7]][,5])
		Last_4_2 <- as.numeric(eWrapper$.Data$data[[8]][,5])
		Spread_4 <- Last_4_1 - Last_4_2
		print(c('Existing Position: JBH ',Last_4_1, 'MYR ', Last_4_2, 'Spread ', Spread_4))
		
		# trade logic here 
		     
		# first any potentially new positions from watchlist

		if(!traded_1 && !is.na(Spread_1) && Spread_1 < 4.46697721920205 && Spread_1 > 0) { 

						
			num_shares <- 1000 / Last_1_1
			num_shares <- as(num_shares,'integer')
			print(c('1000',Last_1_1, num_shares))
			placeOrder(twsCon,  twsContract('', 'LEI', 'CFD', 'SNFE', '', '', '', 'AUD', '','','',NULL,NULL,0), twsOrder(reqIds(twsCon), 'SELL', num_shares, 'MKT'))
			num_shares2 <- 1000 / Last_1_2
			num_shares2 <- as(num_shares2,'integer')
			print(c('1000',Last_1_2, num_shares2))
			placeOrder(twsCon,  twsContract('', 'WOR', 'CFD', 'SNFE', '', '', '', 'AUD', '','','',NULL,NULL,0), twsOrder(reqIds(twsCon), 'BUY', num_shares2, 'MKT'))
			traded_1 <- TRUE
			print(c('I HAVE TRADED'))
			ihavetraded <- c('I HAVE TRADED SHORT. SELL',num_shares, 'LEI. BUY', num_shares2, 'WOR')
			Spread_dealt_at_1 <- Spread_1
			num_shares_1_1 <- num_shares
			num_shares_1_2 <- num_shares2
			
			}
		if(traded_1 && !closed_1 && !is.na(Spread_1) && Spread_1 > (Spread_dealt_at_1 * 1.05)  && Spread_1 > 0) { 


			placeOrder(twsCon,  twsContract('', 'LEI', 'CFD', 'SNFE', '', '', '', 'AUD', '','','',NULL,NULL,0), twsOrder(reqIds(twsCon), 'BUY', num_shares_1_1, 'MKT'))
			
			placeOrder(twsCon,  twsContract('', 'WOR', 'CFD', 'SNFE', '', '', '', 'AUD', '','','',NULL,NULL,0), twsOrder(reqIds(twsCon), 'SELL', num_shares_1_2, 'MKT'))
			closed_1 <- TRUE
			print(c('I HAVE TRADED'))
			ihavetraded <- c('I HAVE CLOSED A  SHORT. BUY',num_shares_1_1, 'LEI. SELL', num_shares_1_2, 'WOR')
			}
		if(!traded_1 && !closed_1 && !is.na(Spread_1) && Spread_1 < 3.4565 && Spread_1 > 0) { 
			placeOrder(twsCon,  twsContract('', 'LEI', 'CFD', 'SNFE', '', '', '', 'AUD', '','','',NULL,NULL,0), twsOrder(reqIds(twsCon), 'BUY', num_shares_1_1, 'MKT'))
			placeOrder(twsCon,  twsContract('', 'WOR', 'CFD', 'SNFE', '', '', '', 'AUD', '','','',NULL,NULL,0), twsOrder(reqIds(twsCon), 'SELL', num_shares_1_2, 'MKT'))
			traded_1 <- TRUE
			print(c('I HAVE TRADED'))
			ihavetraded <- c('I HAVE TRADED AT AVERAGE CLOSING SHORT. SELL', '', 'LEI. BUY', '', 'WOR')
					
			}
		if(!traded_2 && !is.na(Spread_2) && Spread_2 > 12.2030711689243 && Spread_2 > 0) { 
			
			 
			
			num_shares <- 1000 / Last_2_1
			num_shares <- as(num_shares,'integer')
			print(c('1000',Last_2_1, num_shares))
			placeOrder(twsCon,  twsContract('', 'QBE', 'CFD', 'SNFE', '', '', '', 'AUD', '','','',NULL,NULL,0), twsOrder(reqIds(twsCon), 'BUY', num_shares, 'MKT'))
			num_shares2 <- 1000 / Last_2_2
			num_shares2 <- as(num_shares2,'integer')
			print(c('1000',Last_2_2, num_shares2))
			placeOrder(twsCon,  twsContract('', 'AMP', 'CFD', 'SNFE', '', '', '', 'AUD', '','','',NULL,NULL,0), twsOrder(reqIds(twsCon), 'SELL', num_shares2, 'MKT'))
			traded_2 <- TRUE
			print(c('I HAVE TRADED'))
			ihavetraded <- c('I HAVE TRADED LONG. BUY', num_shares, 'QBE. SELL', num_shares2, 'AMP')
			Spread_dealt_at_2 <- Spread_2
			num_shares_2_1 <- num_shares
			num_shares_2_2 <- num_shares2
			}
		if(traded_2 && !closed_2 && !is.na(Spread_2) && Spread_2 < (Spread_dealt_at_2 / 1.05 )  && Spread_2 > 0) { 


			placeOrder(twsCon,  twsContract('', 'QBE', 'CFD', 'SNFE', '', '', '', 'AUD', '','','',NULL,NULL,0), twsOrder(reqIds(twsCon), 'SELL', num_shares_2_1, 'MKT'))

			placeOrder(twsCon,  twsContract('', 'AMP', 'CFD', 'SNFE', '', '', '', 'AUD', '','','',NULL,NULL,0), twsOrder(reqIds(twsCon), 'BUY', num_shares_2_2, 'MKT'))
			closed_2 <- TRUE
			print(c('I HAVE TRADED'))
			ihavetraded <- c('I HAVE CLOSED A LONG. SELL',num_shares_2_1, 'QBE. BUY', num_shares_2_2, 'AMP')
			}
		if(!traded_2 && !closed_2 && !is.na(Spread_2) && Spread_2 > 12.7905 && Spread_2 > 0) { 
			placeOrder(twsCon,  twsContract('', 'QBE', 'CFD', 'SNFE', '', '', '', 'AUD', '','','',NULL,NULL,0), twsOrder(reqIds(twsCon), 'SELL', num_shares_2_1, 'MKT'))
			placeOrder(twsCon,  twsContract('', 'AMP', 'CFD', 'SNFE', '', '', '', 'AUD', '','','',NULL,NULL,0), twsOrder(reqIds(twsCon), 'BUY', num_shares_2_2, 'MKT'))
			traded_2 <- TRUE
			print(c('I HAVE TRADED'))
			ihavetraded <- c('I HAVE TRADED AT AVERAGE CLOSING LONG. BUY', '', 'QBE. SELL', '', 'AMP')
								
			}
		if(!traded_3 && !is.na(Spread_3) && Spread_3 > 11.1048338237328 && Spread_3 > 0) { 
			
			 
			
			num_shares <- 1000 / Last_3_1
			num_shares <- as(num_shares,'integer')
			print(c('1000',Last_3_1, num_shares))
			placeOrder(twsCon,  twsContract('', 'QBE', 'CFD', 'SNFE', '', '', '', 'AUD', '','','',NULL,NULL,0), twsOrder(reqIds(twsCon), 'BUY', num_shares, 'MKT'))
			num_shares2 <- 1000 / Last_3_2
			num_shares2 <- as(num_shares2,'integer')
			print(c('1000',Last_3_2, num_shares2))
			placeOrder(twsCon,  twsContract('', 'AXA', 'CFD', 'SNFE', '', '', '', 'AUD', '','','',NULL,NULL,0), twsOrder(reqIds(twsCon), 'SELL', num_shares2, 'MKT'))
			traded_3 <- TRUE
			print(c('I HAVE TRADED'))
			ihavetraded <- c('I HAVE TRADED LONG. BUY', num_shares, 'QBE. SELL', num_shares2, 'AXA')
			Spread_dealt_at_3 <- Spread_3
			num_shares_3_1 <- num_shares
			num_shares_3_2 <- num_shares2
			}
		if(traded_3 && !closed_3 && !is.na(Spread_3) && Spread_3 < (Spread_dealt_at_3 / 1.05 )  && Spread_3 > 0) { 


			placeOrder(twsCon,  twsContract('', 'QBE', 'CFD', 'SNFE', '', '', '', 'AUD', '','','',NULL,NULL,0), twsOrder(reqIds(twsCon), 'SELL', num_shares_3_1, 'MKT'))

			placeOrder(twsCon,  twsContract('', 'AXA', 'CFD', 'SNFE', '', '', '', 'AUD', '','','',NULL,NULL,0), twsOrder(reqIds(twsCon), 'BUY', num_shares_3_2, 'MKT'))
			closed_3 <- TRUE
			print(c('I HAVE TRADED'))
			ihavetraded <- c('I HAVE CLOSED A LONG. SELL',num_shares_3_1, 'QBE. BUY', num_shares_3_2, 'AXA')
			}
		if(!traded_3 && !closed_3 && !is.na(Spread_3) && Spread_3 > 11.634 && Spread_3 > 0) { 
			placeOrder(twsCon,  twsContract('', 'QBE', 'CFD', 'SNFE', '', '', '', 'AUD', '','','',NULL,NULL,0), twsOrder(reqIds(twsCon), 'SELL', num_shares_3_1, 'MKT'))
			placeOrder(twsCon,  twsContract('', 'AXA', 'CFD', 'SNFE', '', '', '', 'AUD', '','','',NULL,NULL,0), twsOrder(reqIds(twsCon), 'BUY', num_shares_3_2, 'MKT'))
			traded_3 <- TRUE
			print(c('I HAVE TRADED'))
			ihavetraded <- c('I HAVE TRADED AT AVERAGE CLOSING LONG. BUY', '', 'QBE. SELL', '', 'AXA')
								
			}
		
		# then in relation to open positions closing rules for any stops and crossings of the moving average
		

		if(!traded_4 && !is.na(Spread_4) && Spread_4 > (15.55 * 1.05) && Spread_4 > 0) { 
			placeOrder(twsCon,  twsContract('', 'JBH', 'CFD', 'SNFE', '', '', '', 'AUD', '','','',NULL,NULL,0), twsOrder(reqIds(twsCon), 'BUY', 52, 'MKT'))
			placeOrder(twsCon,  twsContract('', 'MYR', 'CFD', 'SNFE', '', '', '', 'AUD', '','','',NULL,NULL,0), twsOrder(reqIds(twsCon), 'SELL', 280, 'MKT'))
			traded_4 <- TRUE
			print(c('I HAVE TRADED'))
			ihavetraded <- c('I HAVE TRADED CLOSING STOP LOSS SHORT. SELL', '52', 'JBH. BUY', '280', 'MYR')

		}
		if(!traded_4 && !is.na(Spread_4) && Spread_4 < 15.034 && Spread_4 > 0) { 
			placeOrder(twsCon,  twsContract('', 'JBH', 'CFD', 'SNFE', '', '', '', 'AUD', '','','',NULL,NULL,0), twsOrder(reqIds(twsCon), 'BUY', 52, 'MKT'))
			placeOrder(twsCon,  twsContract('', 'MYR', 'CFD', 'SNFE', '', '', '', 'AUD', '','','',NULL,NULL,0), twsOrder(reqIds(twsCon), 'SELL', 280, 'MKT'))
			traded_4 <- TRUE
			print(c('I HAVE TRADED'))
			ihavetraded <- c('I HAVE TRADED AT AVERAGE CLOSING SHORT. SELL', '52', 'JBH. BUY', '280', 'MYR')
		
		};

		
		
		
		
	}	
}
    
    
con = twsConnect(99)

reqMktData(con, list(twsEquity('LEI', 'ASX', 'AUD'),twsEquity('WOR', 'ASX', 'AUD'),twsEquity('QBE', 'ASX', 'AUD'),twsEquity('AMP', 'ASX', 'AUD'),twsEquity('QBE', 'ASX', 'AUD'),twsEquity('AXA', 'ASX', 'AUD'),twsEquity('JBH', 'ASX', 'AUD'),twsEquity('MYR', 'ASX', 'AUD')),  eventWrapper=eWrapper.data(8), CALLBACK=CO_INT_N_REV)

closeAllConnections()

