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

Add initial controller #22

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
47 changes: 47 additions & 0 deletions src/actors/controller.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use actix::{Actor, Addr, Context, Handler, Recipient};
use std::collections::HashMap;
use trade::Buy;

pub struct Controller {
scaling_coefficient: f64,
subscribers: Vec<Recipient<ControllerCommand>>,
}

impl Controller {
pub fn new(subscribers: Vec<Recipient<ControllerCommand>>) -> Self {
Self {
scaling_coefficient: 1.,
subscribers,
}
}
}

impl Actor for Controller {
type Context = Context<Self>;
}

impl Handler<PolicyDecision> for Controller {
type Result = ControllerCommand;

fn handle(
&mut self,
msg: PolicyDecision,
ctx: &mut Context<Self>,
) -> Self::Result {
let artificial_spread_coefficient;
match msg {
PolicyDecision::BuyAction => {
artificial_spread_coefficient = 1 / scaling_coefficient
}
PolicyDecision::SellAction => {
artificial_spread_coefficient = scaling_coefficient
}
PolicyDecision::HoldAction => artificial_spread_coefficient = 1,
}
for s in &self.subscribers {
s.do_send(ControllerCommand(artificial_spread_coefficient));
}

artificial_spread_coefficient
}
}
27 changes: 24 additions & 3 deletions src/policy_maker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use crate::actors::mid_price::MidPrice;

use crate::actors::mid_price::MidPriceResponse;
use crate::trade::{Buy, Hold, Sell};
use crate::util::{deserialize_from_str, MovingAverageMessage};
use crate::util::{
deserialize_from_str, ControllerCommand, MovingAverageMessage,
};
use actix::{Actor, Context, Handler, Message, MessageResult, Recipient};
use chrono::Utc;
use serde::Deserialize;
Expand Down Expand Up @@ -34,6 +36,7 @@ pub struct PolicyFrame {
true_price_gradient: f64,
moving_average_price: f64,
true_price: f64,
artificial_spread_coefficient: f64,
prev_decision: Option<PolicyDecision>,
}

Expand All @@ -56,6 +59,7 @@ impl PolicyMaker {
true_price_gradient: 0.0,
moving_average_price: 0.0,
true_price: 0.0,
artificial_spread_coefficient: 1.0,
prev_decision: None,
},
recipients,
Expand Down Expand Up @@ -139,6 +143,7 @@ impl Handler<MidPrice> for PolicyMaker {
prev_decision: self.frame.prev_decision.take(),
// TODO: update
moving_average_gradient: 0.,
artificial_spread_coefficient: 1.,
moving_average_price: 0.,
};
self.frame = frame;
Expand Down Expand Up @@ -168,15 +173,29 @@ impl Handler<MovingAverageMessage> for PolicyMaker {
}
}

impl Handler<ControllerCommand> for PolicyMaker {
type Result = f64;
fn handle(
&mut self,
msg: ControllerCommand,
_ctx: &mut Context<Self>,
) -> f64 {
self.frame.artificial_spread_coefficient = msg.0;
msg.0
}
}

fn should_buy(frame: &PolicyFrame) -> bool {
is_rising_trend(frame)
&& frame.moving_average_price < frame.true_price
&& frame.moving_average_price
< frame.true_price * frame.artificial_spread_coefficient
&& !(matches!(frame.prev_decision, Some(PolicyDecision::BuyAction(_))))
}

fn should_sell(frame: &PolicyFrame) -> bool {
is_downward_trend(frame)
&& frame.moving_average_price > frame.true_price
&& frame.moving_average_price
> frame.true_price * frame.artificial_spread_coefficient
&& !(matches!(frame.prev_decision, Some(PolicyDecision::SellAction(_))))
}

Expand Down Expand Up @@ -245,6 +264,7 @@ mod test {
true_price_gradient: 1.0,
moving_average_price: 10.0,
true_price: 20.0,
artificial_spread_coefficient: 1.0,
prev_decision: Some(PolicyDecision::SellAction(sell)),
};

Expand All @@ -270,6 +290,7 @@ mod test {
true_price_gradient: 1.0,
moving_average_price: 10.0,
true_price: 20.0,
artificial_spread_coefficient: 1.0,
prev_decision: Some(PolicyDecision::BuyAction(buy)),
};

Expand Down
4 changes: 4 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub struct Return(pub f64);
#[rtype(result = "f64")]
pub struct MovingAverageMessage(pub f64);

#[derive(Message)]
#[rtype(result = "f64")]
pub struct ControllerCommand(pub f64);

#[derive(Message)]
#[rtype(result = "Option<f64>")]
pub struct SharpeRatio(pub f64);
Expand Down