Created
May 24, 2017 18:37
-
-
Save elken/e5313b06f8bba4094eac8a0179c15316 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void RT_test() { | |
List<Putup> putupList = new ArrayList<>(); | |
for (String ticker : tickers) { | |
putupList.add(new Putup(ticker, PutupTypeEnum.LONGS, 13.37, AtrClassEnum.UU, false)); | |
} | |
List<Callable<BarPointGraph>> callableList = new ArrayList<>(); | |
callableList.addAll(putupList.stream().map(LoadRealtimeDataTask::new).collect(Collectors.toList())); | |
for (Putup putup : putupList) { | |
try { | |
BarPointGraph points = new BarPointGraph(); | |
points.setPutup(putup); | |
GlobalPoints.getInstance().addGraph(points); | |
BarPointGraph graph = new LoadRealtimeDataTask(putup).call(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package daytradertasks; | |
import daytrader.datamodel.BarPointGraph; | |
import daytrader.api.DaytraderController; | |
import daytrader.datamodel.Putup; | |
import java.util.concurrent.Callable; | |
/** | |
* Do the heavy lifting for realtime data | |
*/ | |
public class LoadRealtimeDataTask extends BaseTask implements Callable<BarPointGraph> { | |
/** | |
* Instantiates a new Load realtime data task. | |
* | |
* @param putup the putup | |
*/ | |
public LoadRealtimeDataTask(Putup putup) { | |
this.putup = putup; | |
} | |
@Override | |
public BarPointGraph call() throws Exception { | |
return DaytraderController.getInstance().run(this.putup); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Attempt the realtime request (not very safe yet) | |
* | |
* @param putup the putup | |
* @return the bar point graph | |
* @throws TWSException the tws exception | |
*/ | |
public BarPointGraph run(Putup putup) throws TWSException { | |
if (!isConnected) { | |
throw new NotLoggedInException(); | |
} | |
BarHandler handler = new BarHandler(putup); | |
Contract contract = createContract(putup); | |
controller().reqRealTimeBars(contract, Types.WhatToShow.TRADES, true, handler); | |
lock.lock(); | |
try { | |
TimeUnit.MILLISECONDS.sleep(100); | |
} catch (InterruptedException ex) { | |
logger.error(ex); | |
} finally { | |
lock.unlock(); | |
} | |
return handler.getBars(); | |
} | |
/** | |
* Wrapper for the various bar handler interfaces | |
*/ | |
public class BarHandler implements com.ib.controller.ApiController.IHistoricalDataHandler, com.ib.controller.ApiController.IRealTimeBarHandler { | |
/** | |
* Gets bars. | |
* | |
* @return the bars | |
*/ | |
public BarPointGraph getBars() { | |
return bars; | |
} | |
/** | |
* The Bars. | |
*/ | |
BarPointGraph bars; | |
/** | |
* The Putup. | |
*/ | |
Putup putup; | |
/** | |
* Dirty trick to handle the bars reactively | |
*/ | |
SimpleBooleanProperty isDataReady; | |
/** | |
* Instantiates a new Bar handler. | |
* | |
* @param putup the putup | |
*/ | |
public BarHandler(Putup putup) { | |
this.putup = putup; | |
this.bars = new BarPointGraph(); | |
} | |
/** | |
* Instantiates a new Bar handler. | |
* | |
* @param putup the putup | |
* @param isDataReady the is data ready | |
*/ | |
public BarHandler(Putup putup, SimpleBooleanProperty isDataReady) { | |
this(putup); | |
this.isDataReady = isDataReady; | |
} | |
/** | |
* TWS method called when a historic bar is returned | |
* @param bar The bar | |
* @param b Not sure tbh | |
*/ | |
@Override | |
public void historicalData(Bar bar, boolean b) { | |
bars.add(new BarPoint(bar)); | |
} | |
/** | |
* TWS method called when all the bars have been returned | |
*/ | |
@Override | |
public void historicalDataEnd() { | |
isDataReady.setValue(Boolean.TRUE); | |
} | |
/** | |
* TWS method called when a realtime bar is returned | |
* @param bar The bar | |
*/ | |
@Override | |
public void realtimeBar(Bar bar) { | |
show(bar.toString() + " " + putup.getTickerCode()); | |
GlobalPoints.getInstance().getGraphByTicker(putup.getTickerCode()).add(new BarPoint(bar)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment