stuff
This commit is contained in:
		
							parent
							
								
									2705e393df
								
							
						
					
					
						commit
						370a57dbb9
					
				
							
								
								
									
										70
									
								
								rustybot/src/events.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										70
									
								
								rustybot/src/events.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,70 @@ | |||||||
|  | use std::collections::HashMap; | ||||||
|  | use std::future::Future; | ||||||
|  | 
 | ||||||
|  | use tokio::task::JoinHandle; | ||||||
|  | use crate::BoxError; | ||||||
|  | 
 | ||||||
|  | enum SignalKind { | ||||||
|  |     ClosePosition, | ||||||
|  |     OpenPosition, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | struct EventMetadata { | ||||||
|  |     position_id: Option<u64>, | ||||||
|  |     order_id: Option<u64>, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | #[derive(PartialEq, Eq, Hash)] | ||||||
|  | enum EventKind { | ||||||
|  |     NewMinimum, | ||||||
|  |     NewMaximum, | ||||||
|  |     ReachedLoss, | ||||||
|  |     ReachedBreakEven, | ||||||
|  |     ReachedMinProfit, | ||||||
|  |     ReachedGoodProfit, | ||||||
|  |     ReachedMaxLoss, | ||||||
|  |     TrailingStopSet, | ||||||
|  |     TrailingStopMoved, | ||||||
|  |     OrderSubmitted, | ||||||
|  |     NewTick, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | struct Event { | ||||||
|  |     kind: EventKind, | ||||||
|  |     tick: u64, | ||||||
|  |     metadata: Option<EventMetadata>, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | impl Event { | ||||||
|  |     pub fn new(kind: EventKind, tick: u64, metadata: Option<EventMetadata>) -> Self { | ||||||
|  |         Event { | ||||||
|  |             kind, | ||||||
|  |             tick, | ||||||
|  |             metadata, | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     fn has_metadata(&self) -> bool { | ||||||
|  |         self.metadata.is_some() | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub struct Dispatcher { | ||||||
|  |     event_handlers: HashMap<EventKind, Box<dyn FnMut(String) -> JoinHandle<()>>> | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | impl Dispatcher { | ||||||
|  |     pub fn new() -> Self { | ||||||
|  |         Dispatcher { | ||||||
|  |             event_handlers: HashMap::new() | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub fn register_event_handler<F: 'static, Fut: 'static>(&mut self, event: EventKind, mut f: F) | ||||||
|  |         where | ||||||
|  |             F: FnMut(String) -> Fut, | ||||||
|  |             Fut: Future<Output = ()> + Send, | ||||||
|  |     { | ||||||
|  |         self.event_handlers.insert(event, Box::new(move |args| tokio::spawn(f(args)))); | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										3
									
								
								rustybot/src/pairs.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								rustybot/src/pairs.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,3 @@ | |||||||
|  | struct PairStatus { | ||||||
|  | 
 | ||||||
|  | } | ||||||
							
								
								
									
										29
									
								
								rustybot/src/positions.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								rustybot/src/positions.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,29 @@ | |||||||
|  | pub enum PositionState { | ||||||
|  |     Critical, | ||||||
|  |     Loss, | ||||||
|  |     BreakEven, | ||||||
|  |     MinimumProfit, | ||||||
|  |     Profit, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | impl PositionState { | ||||||
|  |     fn color(self) -> String { | ||||||
|  |         match self { | ||||||
|  |             PositionState::Critical | PositionState::Loss => { "red" } | ||||||
|  |             PositionState::BreakEven => { "yellow" } | ||||||
|  |             PositionState::MinimumProfit | PositionState::Profit => { "green" } | ||||||
|  |         }.into() | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // TODO: implement position in bitfinex API before completing this struct
 | ||||||
|  | pub struct PositionWrapper { | ||||||
|  |     position: String, | ||||||
|  |     net_profit_loss: f64, | ||||||
|  |     net_profit_loss_percentage: f64, | ||||||
|  |     state: PositionState | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
							
								
								
									
										21
									
								
								rustybot/src/ticker.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								rustybot/src/ticker.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,21 @@ | |||||||
|  | use tokio::time::{Duration, Instant}; | ||||||
|  | 
 | ||||||
|  | pub struct Ticker { | ||||||
|  |     duration: Duration, | ||||||
|  |     start_time: Instant, | ||||||
|  |     current_tick: u64, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | impl Ticker { | ||||||
|  |     fn new(duration: Duration) -> Self { | ||||||
|  |         Ticker { | ||||||
|  |             duration, | ||||||
|  |             start_time: Instant::now(), | ||||||
|  |             current_tick: 1, | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     fn inc(&mut self) { | ||||||
|  |         self.current_tick += 1 | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user