Skip to content

Commit

Permalink
add alpaca
Browse files Browse the repository at this point in the history
  • Loading branch information
ryqdev committed May 30, 2024
1 parent de903e2 commit 2ccd045
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 11 deletions.
61 changes: 59 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ egui_plot = "0.27.2"
eframe = "0.27.2"
ibapi = "0.4.3"
colored = "2.1.0"
tungstenite = {version="0.21.0", features=["native-tls"]}
url = "2.5.0"
serde_json = "1.0.117"
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ All in one trading engine
### Paper trading & live trading
- [x] Single live broker: IBKR
- [x] Live trading with single broker
- [ ] Async
- [ ] AsyncIO
- [ ] Websocket: Constant polling is not recommended to get real-time market data. Use websocket instead

## Demo
```shell
Expand Down
6 changes: 3 additions & 3 deletions src/cmds/backtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ impl Command for BackTestCommand {
async fn backtest(symbol: &str) -> Result<()> {
let mut green = Green::new()
.set_mode(GreenModeType::Backtest)
.add_broker(100_000.0)
.add_data_feed(symbol)
.add_strategy(SimpleStrategy{})
.set_broker(100_000.0)
.set_data_feed(symbol)
.set_strategy(SimpleStrategy{})
.build();

green.run();
Expand Down
10 changes: 10 additions & 0 deletions src/green/broker/alpaca/alpaca.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use url::Url;
use tungstenite::{connect, Message};
use serde_json;

const BASE_URL: &str = "wss://stream.data.alpaca.markets/v2/test";

fn main() {
let (mut socket, response) = connect(Url::parse(BASE_URL).unwrap()).expect("Can't connect");
println!("{:#?}", response);
}
1 change: 1 addition & 0 deletions src/green/broker/alpaca/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod alpaca;
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion src/green/broker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pub trait Broker{
}

pub mod live;
pub mod backtest;
pub mod backtest;
mod alpaca;
6 changes: 3 additions & 3 deletions src/green/green.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl Green {


impl GreenBuilder {
pub fn add_data_feed(&mut self, symbol: &str) -> &mut GreenBuilder{
pub fn set_data_feed(&mut self, symbol: &str) -> &mut GreenBuilder{
// TODO: refactor code
let contract = Contract {
symbol: "USD".to_owned(),
Expand All @@ -146,7 +146,7 @@ impl GreenBuilder {
self.mode = mode;
self
}
pub fn add_broker(&mut self, cash: f64) -> &mut GreenBuilder {
pub fn set_broker(&mut self, cash: f64) -> &mut GreenBuilder {
self.broker = match self.mode {
GreenModeType::Backtest => BackTestBroker{
cash: Vec::from([cash]),
Expand All @@ -159,7 +159,7 @@ impl GreenBuilder {
};
self
}
pub fn add_strategy(&mut self, strategy: SimpleStrategy) -> &mut GreenBuilder{
pub fn set_strategy(&mut self, strategy: SimpleStrategy) -> &mut GreenBuilder{
self.strategy = strategy;
self
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ async fn main() -> anyhow::Result<()> {
Some(("backtest", sub_m)) => Ok(BackTestCommand::handler(sub_m).await?),
Some(("paper-trading", sub_m)) => Ok(PaperTradingCommand::handler(sub_m).await?),
Some(("live-trading", sub_m)) => Ok(LiveTradingCommand::handler(sub_m).await?),
_ => Err(anyhow::Error::msg("Miss arguments. Please access Makefile to get instructions")),
_ => Err(anyhow::Error::msg("Miss arguments. Please open Makefile to get instructions")),
}
}

0 comments on commit 2ccd045

Please sign in to comment.