mirror of
https://github.com/KhizarImran/backtestingfx.git
synced 2026-07-27 20:17:44 +00:00
Initial commit: project strcture & initialisation
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
/target
|
||||
Cargo.lock
|
||||
### My claude md file
|
||||
CLAUDE.md
|
||||
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "backtestingfx"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
@@ -0,0 +1,57 @@
|
||||
use crate::types::{Position, Trade};
|
||||
|
||||
pub struct Broker{
|
||||
pub cash: f64,
|
||||
pub positions: Vec<Position>,
|
||||
pub trade_history: Vec<Trade>
|
||||
}
|
||||
|
||||
impl Broker {
|
||||
pub fn new(initial_cash: f64) -> self { // does not need &mut because it initialises something new
|
||||
Broker {
|
||||
cash: initial_cash,
|
||||
positions: Vec::new(),
|
||||
trade_history: Vec::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn buy(&mut self, price: f64, lot_size: f64, timestamp: i64) { // needs to modify the broker with new position. (.push works with the Vec::)
|
||||
self.positions.push(Position {
|
||||
entry_price: price,
|
||||
lot_size,
|
||||
is_long: true,
|
||||
entry_timestamp: timestamp
|
||||
});
|
||||
}
|
||||
|
||||
pub fn sell(&mut self, price: f64, lot_size: f64, timestamp: i64) { // needs to modify the broker with new position. (.push works with the Vec::)
|
||||
self.positions.push(Position {
|
||||
entry_price: price,
|
||||
lot_size,
|
||||
is_long: false,
|
||||
entry_timestamp: timestamp
|
||||
});
|
||||
}
|
||||
|
||||
pub fn close_all (&mut self, price:f64, timestamp: i64) { // Instead of .push it uses drain to calculate the close all positions
|
||||
for position in self.positions.drain(..) {
|
||||
let pnl = if position.is_long {
|
||||
(price - position.entry_price) * position.lot_size
|
||||
} else {
|
||||
(position.entry_price - price) * position.lot_size
|
||||
};
|
||||
self.cash += pnl;
|
||||
self.trade_history.push(Trade {
|
||||
entry_price: position.entry_price,
|
||||
exit_price: position.exit_price,
|
||||
lot_size: position.lot_size,
|
||||
is_long: position.is_long,
|
||||
pnl,
|
||||
entry_timestamp: position.entry_timestamp,
|
||||
exit_timestamp: timestamp
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
pub mod types;
|
||||
pub mod strategy;
|
||||
pub mod broker;
|
||||
pub mod engine;
|
||||
pub mod data;
|
||||
@@ -0,0 +1,7 @@
|
||||
use crate::types::Bar;
|
||||
use crate::broker::Broker;
|
||||
|
||||
pub trait Strategy {
|
||||
fn next(&mut self, bar: &Bar, broker: &mut Broker);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Bar { // Initialises the interface for the bar
|
||||
pub timestamp: i64,
|
||||
pub open: f64,
|
||||
pub high: f64,
|
||||
pub low: f64,
|
||||
pub close: f64,
|
||||
pub volume: f64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Position { // this is for the trading position
|
||||
pub entry_price: f64,
|
||||
pub lot_size: f64,
|
||||
pub is_long: bool,
|
||||
pub entry_timestamp: i64
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Trade { // the actual trade
|
||||
pub entry_price: f64,
|
||||
pub exit_price: f64,
|
||||
pub lot_size: f64,
|
||||
pub is_long: bool,
|
||||
pub pnl: f64,
|
||||
pub entry_timestamp: i64,
|
||||
pub exit_timestamp:i64
|
||||
}
|
||||
Reference in New Issue
Block a user