Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PriceChanger #20

Merged
merged 3 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ clap = { version = "4.4.8", features = ["derive"] }
serde_json = "1.0.113"
log = "0.4.20"
futures-util = "0.3.30"
RustQuant = "0.0.45"
3 changes: 3 additions & 0 deletions src/behaviors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ use serde::{Deserialize, Serialize};

pub mod deployer;
pub mod pool_admin;
pub mod price_changer;
pub mod token_admin;

use deployer::Deployer;
use pool_admin::PoolAdmin;
use price_changer::PriceChanger;
use token_admin::TokenAdmin;

#[derive(Behaviors, Debug, Serialize, Deserialize)]
pub enum Behaviors {
Deployer(Deployer),
TokenAdmin(TokenAdmin),
PoolAdmin(PoolAdmin),
PriceChanger(PriceChanger),
}
117 changes: 117 additions & 0 deletions src/behaviors/price_changer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use std::{fmt, sync::Arc};

use anyhow::Result;
use arbiter_core::middleware::ArbiterMiddleware;
use arbiter_engine::messager::{Message, Messager};
use ethers::types::H160;
use RustQuant::{
models::*,
stochastics::{process::Trajectories, *},
};

use super::*;
use crate::bindings::liquid_exchange::LiquidExchange;

#[derive(Serialize, Deserialize)]
pub struct PriceChanger {
#[serde(skip)]
pub params: OrnsteinUhlenbeckParams,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future we can make this generic and have a ProcessType trait. Then impl that on the different param types.


#[serde(skip)]
#[serde(default = "trajectory_default")]
ts0yu marked this conversation as resolved.
Show resolved Hide resolved
pub current_chunk: Trajectories,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this name?


#[serde(skip)]
pub client: Option<Arc<ArbiterMiddleware>>,

cursor: usize,
value: f64,
}

fn trajectory_default() -> Trajectories {
Trajectories {
times: Vec::new(),
paths: Vec::new(),
}
}

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct OrnsteinUhlenbeckParams {
mu: f64,
sigma: f64,
theta: f64,
}

impl fmt::Debug for PriceChanger {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", serde_json::to_string(self).unwrap())
}
}

#[derive(Debug, Serialize, Deserialize)]
pub struct PriceUpdate {
liquid_exchange: H160,
}

impl PriceChanger {
/// Public constructor function to create a [`PriceChanger`] behaviour.
pub fn new(initial_value: f64, params: OrnsteinUhlenbeckParams) -> Self {
let ou = OrnsteinUhlenbeck::new(params.mu, params.sigma, params.theta);

// Chunk our price trajectory over 100 price points.
let current_chunk = ou.euler_maruyama(initial_value, 0.0, 100.0, 100_usize, 1_usize, false);

Self {
params,
current_chunk,
cursor: 0,
client: None,
value: initial_value,
}
}
}

#[async_trait::async_trait]
impl Behavior<Message> for PriceChanger {
async fn startup(
&mut self,
_client: Arc<ArbiterMiddleware>,
_messager: Messager,
) -> Result<Option<EventStream<Message>>> {
Ok(None)
}

async fn process(&mut self, event: Message) -> Result<ControlFlow> {
let ou = OrnsteinUhlenbeck::new(self.params.mu, self.params.sigma, self.params.theta);

let query: PriceUpdate = match serde_json::from_str(&event.data) {
Ok(query) => query,
Err(_) => {
eprintln!("Failed to deserialize the event data into a PriceUpdate");
return Ok(ControlFlow::Continue);
}
};

if self.cursor >= 99 {
self.cursor = 0;
self.value = self.current_chunk.paths.clone()[0][self.cursor];
self.current_chunk =
ou.euler_maruyama(self.value, 0.0, 100.0, 100_usize, 1_usize, false);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use seedable

}

let liquid_exchange =
LiquidExchange::new(query.liquid_exchange, self.client.clone().unwrap());

let price = self.current_chunk.paths.clone()[0][self.cursor];

liquid_exchange
.set_price(ethers::utils::parse_ether(price)?)
.send()
.await?
.await?;

self.cursor += 1;

Ok(ControlFlow::Continue)
}
}
Loading