connected actor messages results with actual actor results (duh!)
This commit is contained in:
parent
bb5d1328d6
commit
b02554778e
@ -223,18 +223,15 @@ impl PositionManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn handle_message(&mut self, msg: ActorMessage) -> Result<(), BoxError> {
|
pub async fn handle_message(&mut self, msg: ActorMessage) -> Result<(), BoxError> {
|
||||||
match msg.message {
|
let (events, messages) = match msg.message {
|
||||||
Message::Update { tick } => {
|
Message::Update { tick } => self.update(tick).await?,
|
||||||
self.update(tick).await?;
|
_ => (None, None),
|
||||||
|
|
||||||
msg.respond_to
|
|
||||||
.send((None, None))
|
|
||||||
.map_err(|_| BoxError::from("Could not send message."))?;
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(())
|
Ok(msg
|
||||||
|
.respond_to
|
||||||
|
.send((events, messages))
|
||||||
|
.map_err(|_| BoxError::from("Could not send message."))?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update(&mut self, tick: u64) -> Result<OptionUpdate, BoxError> {
|
pub async fn update(&mut self, tick: u64) -> Result<OptionUpdate, BoxError> {
|
||||||
@ -284,6 +281,7 @@ impl PositionManager {
|
|||||||
.insert(self.current_tick(), pos_post_tick.clone());
|
.insert(self.current_tick(), pos_post_tick.clone());
|
||||||
self.active_position = Some(pos_post_tick);
|
self.active_position = Some(pos_post_tick);
|
||||||
|
|
||||||
|
println!("Message: {:?}", messages);
|
||||||
return Ok((events, messages));
|
return Ok((events, messages));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -392,21 +390,19 @@ impl OrderManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn handle_message(&mut self, msg: ActorMessage) -> Result<(), BoxError> {
|
pub async fn handle_message(&mut self, msg: ActorMessage) -> Result<(), BoxError> {
|
||||||
match msg.message {
|
let (events, messages) = match msg.message {
|
||||||
Message::Update { .. } => {
|
Message::Update { .. } => self.update().await?,
|
||||||
self.update().await?;
|
|
||||||
}
|
|
||||||
Message::ClosePosition { position_id } => self.close_position(position_id).await?,
|
Message::ClosePosition { position_id } => self.close_position(position_id).await?,
|
||||||
_ => {}
|
_ => (None, None),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(msg
|
Ok(msg
|
||||||
.respond_to
|
.respond_to
|
||||||
.send((None, None))
|
.send((events, messages))
|
||||||
.map_err(|_| BoxError::from("Could not send message."))?)
|
.map_err(|_| BoxError::from("Could not send message."))?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn close_position(&mut self, position_id: u64) -> Result<(), BoxError> {
|
pub async fn close_position(&mut self, position_id: u64) -> Result<OptionUpdate, BoxError> {
|
||||||
info!("Closing position #{}", position_id);
|
info!("Closing position #{}", position_id);
|
||||||
|
|
||||||
debug!("Retrieving open orders, positions and current prices...");
|
debug!("Retrieving open orders, positions and current prices...");
|
||||||
@ -511,18 +507,18 @@ impl OrderManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok((None, None))
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: finish me
|
// TODO: finish me
|
||||||
pub async fn update(&self) -> Result<OptionUpdate, BoxError> {
|
pub async fn update(&self) -> Result<OptionUpdate, BoxError> {
|
||||||
trace!("\tUpdating {} order manager.", self.pair);
|
trace!("\tUpdating {} order manager.", self.pair);
|
||||||
|
//
|
||||||
let (open_orders, opt_open_positions) = tokio::join!(
|
// let (open_orders, opt_open_positions) = tokio::join!(
|
||||||
self.client.active_orders(&self.pair),
|
// self.client.active_orders(&self.pair),
|
||||||
self.client.active_positions(&self.pair)
|
// self.client.active_positions(&self.pair)
|
||||||
);
|
// );
|
||||||
let (_open_orders, _opt_open_positions) = (open_orders?, opt_open_positions?);
|
// let (_open_orders, _opt_open_positions) = (open_orders?, opt_open_positions?);
|
||||||
|
|
||||||
Ok((None, None))
|
Ok((None, None))
|
||||||
}
|
}
|
||||||
@ -604,6 +600,7 @@ impl PairManager {
|
|||||||
messages.merge(opt_pos_messages);
|
messages.merge(opt_pos_messages);
|
||||||
messages.merge(opt_order_messages);
|
messages.merge(opt_order_messages);
|
||||||
|
|
||||||
|
println!("Total messages: {:?}", messages);
|
||||||
// TODO: to move into Handler?
|
// TODO: to move into Handler?
|
||||||
if let Some(messages) = messages {
|
if let Some(messages) = messages {
|
||||||
for m in messages {
|
for m in messages {
|
||||||
|
@ -2,6 +2,7 @@ use std::collections::HashMap;
|
|||||||
use std::fmt::{Debug, Formatter};
|
use std::fmt::{Debug, Formatter};
|
||||||
|
|
||||||
use dyn_clone::DynClone;
|
use dyn_clone::DynClone;
|
||||||
|
use log::info;
|
||||||
|
|
||||||
use crate::events::{Event, EventKind, EventMetadata, Message};
|
use crate::events::{Event, EventKind, EventMetadata, Message};
|
||||||
use crate::models::{
|
use crate::models::{
|
||||||
@ -68,10 +69,10 @@ pub struct TrailingStop {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TrailingStop {
|
impl TrailingStop {
|
||||||
const BREAK_EVEN_PERC: f64 = 0.01;
|
const BREAK_EVEN_PERC: f64 = 0.1;
|
||||||
const MIN_PROFIT_PERC: f64 = TrailingStop::BREAK_EVEN_PERC + 0.03;
|
const MIN_PROFIT_PERC: f64 = TrailingStop::BREAK_EVEN_PERC + 0.2;
|
||||||
const GOOD_PROFIT_PERC: f64 = TrailingStop::MIN_PROFIT_PERC * 2.5;
|
const GOOD_PROFIT_PERC: f64 = TrailingStop::MIN_PROFIT_PERC * 2.5;
|
||||||
const MAX_LOSS_PERC: f64 = -0.5;
|
const MAX_LOSS_PERC: f64 = -2.25;
|
||||||
|
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
TrailingStop {
|
TrailingStop {
|
||||||
@ -88,14 +89,14 @@ impl TrailingStop {
|
|||||||
|
|
||||||
if let Some(profit_state) = position.profit_state() {
|
if let Some(profit_state) = position.profit_state() {
|
||||||
let profit_state_delta = match profit_state {
|
let profit_state_delta = match profit_state {
|
||||||
PositionProfitState::MinimumProfit => Some(Self::MIN_PROFIT_PERC),
|
PositionProfitState::MinimumProfit => Some(0.2),
|
||||||
PositionProfitState::Profit => Some(Self::GOOD_PROFIT_PERC),
|
PositionProfitState::Profit => Some(0.1),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(profit_state_delta) = profit_state_delta {
|
if let Some(profit_state_delta) = profit_state_delta {
|
||||||
println!(
|
println!(
|
||||||
"PL%: {} | Delta: {}",
|
"PL%: {:0.2} | Delta: {}",
|
||||||
position.pl_perc(),
|
position.pl_perc(),
|
||||||
profit_state_delta
|
profit_state_delta
|
||||||
);
|
);
|
||||||
@ -230,14 +231,18 @@ impl PositionStrategy for TrailingStop {
|
|||||||
|
|
||||||
// let's check if we surpassed an existing stop percentage
|
// let's check if we surpassed an existing stop percentage
|
||||||
if let Some(existing_stop_percentage) = self.stop_percentages.get(&position.id()) {
|
if let Some(existing_stop_percentage) = self.stop_percentages.get(&position.id()) {
|
||||||
if existing_stop_percentage <= &position.pl_perc() {
|
if &position.pl_perc() <= existing_stop_percentage {
|
||||||
|
info!("Stop percentage surpassed. Closing position.");
|
||||||
return (position, None, Some(vec![close_message]));
|
return (position, None, Some(vec![close_message]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.update_stop_percentage(&position);
|
self.update_stop_percentage(&position);
|
||||||
|
|
||||||
println!("Stop percentages: {:?}", self.stop_percentages);
|
println!(
|
||||||
|
"Stop percentage: {:0.2}",
|
||||||
|
self.stop_percentages.get(&position.id()).unwrap()
|
||||||
|
);
|
||||||
|
|
||||||
(position, None, None)
|
(position, None, None)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user