Skip to content

Commit

Permalink
Cleanup and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
golden-lucky-monkey committed Apr 14, 2022
1 parent d2d3859 commit 709c46e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ We counter-trade those deviations, and enter short/long positions based on trigg
on the orderbook delta
from a (10-20) period rolling bollinger band.

We are testing this with BTC-PERP on FTX, which has good liquidity and small spreads (and the best API I've seen).
We are testing this with BTC-PERP on FTX, which has good liquidity and small spreads (and FTX has the best API I've seen).
In principle, the scheme could be modified for lower liquidity pairs too, perhaps by adjusting the bollinger band width
and length for generating triggers.

Expand Down
2 changes: 1 addition & 1 deletion src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub(crate) fn read_settings(filepath: &str) -> SettingsFile {

/// Invert side i.e. buy -> sell, sell -> buy
pub(crate) fn invert_side(side: ftx::rest::Side) -> ftx::rest::Side {
return match side {
return match side {
ftx::rest::Side::Buy => ftx::rest::Side::Sell,
ftx::rest::Side::Sell => ftx::rest::Side::Buy
};
Expand Down
27 changes: 20 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ async fn main() {
future_name: String::from(&settings.market_name)
}
).await.unwrap();
let price_precision = helpers::convert_increment_to_precision(price_result.price_increment);
let size_precision = helpers::convert_increment_to_precision(price_result.size_increment);
let price_precision = helpers::convert_increment_to_precision(
price_result.price_increment);
let size_precision = helpers::convert_increment_to_precision(
price_result.size_increment);
let order_size = settings.order_size.round_dp(size_precision);

// Set up loop outer variables
Expand Down Expand Up @@ -183,7 +185,8 @@ async fn main() {
let (tp_price, sl_price) = order_handler::calculate_tp_and_sl(
price, order_side, settings.tp_percent, settings.sl_percent, price_precision);
log::info!(
"{:?} {:?} {} at {:?}. Take profit at {:?} ({:?}%) and stop loss at {:?} ({:?}%)",
"{:?} {:?} {} at {:?}. Take profit at {:?} ({:?}%) and \
stop loss at {:?} ({:?}%)",
current_side, order_size, settings.market_name, price, tp_price,
settings.tp_percent, sl_price, settings.sl_percent
);
Expand All @@ -197,9 +200,15 @@ async fn main() {
if open_position {
log::info!("Closing existing position...");
futures::executor::block_on(
order_handler::market_close_order(&api, &settings.market_name));
order_handler::market_close_order(
&api, &settings.market_name,
)
);
futures::executor::block_on(
order_handler::cancel_all_trigger_orders(&api, &settings.market_name));
order_handler::cancel_all_trigger_orders(
&api, &settings.market_name,
)
);
}

// TODO: Use Kelly criterion for order sizing
Expand Down Expand Up @@ -227,7 +236,8 @@ async fn main() {
order_size,
tp_price,
sl_price,
));
)
);

// If unable to place TP or SL, cancel all orders
// TODO: Market close position in event of failure
Expand All @@ -236,7 +246,10 @@ async fn main() {
let order_closed = futures::executor::block_on(
order_handler::market_close_order(&api, &settings.market_name));
let triggers_cancelled = futures::executor::block_on(
order_handler::cancel_all_trigger_orders(&api, &settings.market_name));
order_handler::cancel_all_trigger_orders(
&api, &settings.market_name,
)
);

if order_closed && triggers_cancelled {
continue;
Expand Down
21 changes: 9 additions & 12 deletions src/order_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ pub(crate) async fn place_market_order(
market_name: &str,
order_side: ftx::rest::Side,
order_size: rust_decimal::Decimal) -> bool {

let order= api.request(ftx::rest::PlaceOrder {
let order = api.request(ftx::rest::PlaceOrder {
market: std::string::String::from(market_name),
side: order_side,
price: None,
Expand All @@ -34,8 +33,7 @@ pub(crate) async fn place_market_order(
}
};

return order_success

return order_success;
}

/// Check if position is open on a market
Expand All @@ -44,10 +42,10 @@ pub(crate) async fn get_open_position(api: &ftx::rest::Rest, market_name: &str)

for position in positions {
if position.future == market_name {
return true
return true;
}
}
return false
return false;
}


Expand All @@ -57,7 +55,7 @@ pub(crate) async fn market_close_order(api: &ftx::rest::Rest, market_name: &str)

for position in positions {
if position.future == market_name {
let order_closed = api.request( ftx::rest::PlaceOrder {
let order_closed = api.request(ftx::rest::PlaceOrder {
market: String::from(market_name),
side: crate::helpers::invert_side(position.side),
price: None,
Expand All @@ -67,7 +65,7 @@ pub(crate) async fn market_close_order(api: &ftx::rest::Rest, market_name: &str)
ioc: false,
post_only: false,
client_id: None,
reject_on_price_band: false
reject_on_price_band: false,
}).await;

return match order_closed {
Expand All @@ -83,7 +81,7 @@ pub(crate) async fn market_close_order(api: &ftx::rest::Rest, market_name: &str)
}
}
log::warn!("No order open, cannot close");
return false
return false;
}

/// Cancel all open orders and trigger orders on FTX
Expand All @@ -99,12 +97,12 @@ pub(crate) async fn cancel_all_trigger_orders(api: &ftx::rest::Rest, market_name
Ok(o) => {
log::info!("Cancelled all trigger orders: {:?}", o);
true
},
}
Err(e) => {
log::error!("Unable to cancel orders Err: {:?}, panicking!", e);
false
}
}
};
}

/// Place take profit and stop loss orders
Expand Down Expand Up @@ -177,7 +175,6 @@ pub(crate) fn calculate_tp_and_sl(
tp_percent: rust_decimal::Decimal,
sl_percent: rust_decimal::Decimal,
price_precision: u32) -> (rust_decimal::Decimal, rust_decimal::Decimal) {

let div = rust_decimal::Decimal::from(100);

let (tp_price, sl_price) = match side {
Expand Down
8 changes: 4 additions & 4 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[cfg(test)]
mod test_helpers{
mod test_helpers {
use crate::helpers::{convert_increment_to_precision, read_settings, SettingsFile, write_to_csv};

#[test]
Expand All @@ -18,7 +18,7 @@ mod test_helpers{
rust_decimal::Decimal::from(10 as i64),
rust_decimal::Decimal::from(10 as i64),
&crate::helpers::Side::Sell,
1 as usize
1 as usize,
).unwrap();

// Verify the file, and delete it
Expand All @@ -37,7 +37,7 @@ mod test_helpers{
fn test_read_settings() {
// Create a test file
let filename = "test_read_settings.json";
let data = SettingsFile {
let data = SettingsFile {
market_name: "BTC-USD".to_string(),
time_delta: 1,
bb_period: 10,
Expand All @@ -47,7 +47,7 @@ mod test_helpers{
order_size: Default::default(),
tp_percent: Default::default(),
sl_percent: Default::default(),
write_to_file: false
write_to_file: false,
};
serde_json::to_writer_pretty(
&std::fs::File::create(filename).unwrap(), &data).unwrap();
Expand Down

0 comments on commit 709c46e

Please sign in to comment.