Added sounds on market order placement and profit state changes

This commit is contained in:
Giulio De Pasquale 2021-02-26 16:02:55 +00:00
parent a2cddead14
commit 5cd6f8d37c
8 changed files with 72 additions and 2 deletions

12
Cargo.lock generated
View File

@ -237,6 +237,17 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee2626afccd7561a06cf1367e2950c4718ea04565e20fb5029b6c7d8ad09abcf"
[[package]]
name = "ears"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e741c53078ec208f07c837fca26cd2624f959140c52268ee0677ad3b8e9b2112"
dependencies = [
"lazy_static",
"libc",
"pkg-config",
]
[[package]]
name = "encoding_rs"
version = "0.8.26"
@ -1168,6 +1179,7 @@ dependencies = [
"chrono",
"dotenv",
"dyn-clone",
"ears",
"fern",
"float-cmp",
"futures-retry",

View File

@ -23,3 +23,4 @@ futures-retry = "0.6"
tungstenite = "0.12"
tokio-tungstenite = "0.13"
dotenv = "0.15"
ears = "0.8.0"

BIN
sounds/smas-smb3_goal.wav Normal file

Binary file not shown.

BIN
sounds/smw2_boing.wav Normal file

Binary file not shown.

View File

@ -22,6 +22,7 @@ mod models;
mod strategy;
mod ticker;
mod tests;
mod sounds;
pub type BoxError = Box<dyn std::error::Error + Send + Sync>;

View File

@ -16,6 +16,7 @@ use crate::currency::SymbolPair;
use crate::events::{ActionMessage, ActorMessage, Event};
use crate::models::{ActiveOrder, OrderBook, OrderForm, OrderKind, OrderMetadata, Position, PriceTicker};
use crate::strategy::{TrailingStop, MarketEnforce, PositionStrategy};
use crate::sounds::{play_sound, MARKET_ORDER_PLACED_PATH};
pub type OptionUpdate = (Option<Vec<Event>>, Option<Vec<ActionMessage>>);
@ -519,6 +520,11 @@ impl OrderManager {
}
};
// play sound if Market order is placed
if let OrderKind::Market = order_form.kind() {
play_sound(MARKET_ORDER_PLACED_PATH);
}
// TODO: return valid messages and events!111!!!1!
Ok((None, None))
}

21
src/sounds.rs Normal file
View File

@ -0,0 +1,21 @@
use ears::{AudioController, Sound};
use log::error;
pub const MARKET_ORDER_PLACED_PATH: &str = "sounds/smas-smb3_goal.wav";
pub const LOSS_TO_BREAK_EVEN_PATH: &str = "sounds/smw2_boing.wav";
pub const MIN_PROFIT_SOUND_PATH: &str = "sounds/smw_1-up.wav";
pub const GOOD_PROFIT_SOUND_PATH: &str = "sounds/smw_power-up.wav";
pub fn play_sound(sound_path: &'static str) {
std::thread::spawn(move || {
match Sound::new(sound_path) {
Ok(mut sound) => {
sound.play();
while sound.is_playing() {}
}
Err(e) => { error!("Could not play {}: {}", sound_path, e); }
}
});
}

View File

@ -9,6 +9,8 @@ use crate::BoxError;
use crate::events::{ActionMessage, Event, EventKind, EventMetadata};
use crate::managers::OptionUpdate;
use crate::models::{ActiveOrder, OrderBook, OrderForm, OrderKind, OrderMetadata, Position, PositionProfitState, TradingFees};
use crate::models::PositionProfitState::{Critical, Loss};
use crate::sounds::{GOOD_PROFIT_SOUND_PATH, LOSS_TO_BREAK_EVEN_PATH, MIN_PROFIT_SOUND_PATH, play_sound};
/***************
* DEFINITIONS
@ -92,6 +94,29 @@ pub struct TrailingStop {
impl TrailingStop {
fn play_sound_on_state(prev_position: &Position, current_position: &Position) {
if prev_position.profit_state().is_none() {
return;
}
if current_position.profit_state().is_none() {
return;
}
let prev_state = prev_position.profit_state().unwrap();
let current_state = current_position.profit_state().unwrap();
// negative to positive
if let Loss | Critical = prev_state {
match current_state {
PositionProfitState::BreakEven => { play_sound(LOSS_TO_BREAK_EVEN_PATH); }
PositionProfitState::MinimumProfit => { play_sound(MIN_PROFIT_SOUND_PATH); }
PositionProfitState::Profit => { play_sound(GOOD_PROFIT_SOUND_PATH); }
_ => {}
}
}
}
fn print_status(&self, position: &Position) {
match self.stop_percentages.get(&position.id()) {
None => {
@ -181,7 +206,7 @@ impl Default for TrailingStop {
impl PositionStrategy for TrailingStop {
fn name(&self) -> String {
"Hidden Trailing Stop".into()
"Trailing Stop".into()
}
/// Sets the profit state of an open position
@ -220,6 +245,8 @@ impl PositionStrategy for TrailingStop {
if prev.profit_state() == Some(state) {
return (new_position, None, None);
}
TrailingStop::play_sound_on_state(&prev, &new_position);
}
None => return (new_position, None, None),
};
@ -320,6 +347,8 @@ impl PositionStrategy for TrailingStop {
return (position, None, None);
}
// if we get here we are with a profit/loss ration > 0.0
let mut messages = vec![];
// if a stop loss order was previously set,