use trading_maid::prelude::*;
async fn my_strategy(cx: &Context<'_>) -> anyhow::Result<()> {
let body_size = (cx.open - cx.close).abs();
let upper_shadow_size = (cx.high - cx.open).abs();
let open_short_condition =
cx.open > cx.close && upper_shadow_size >= body_size * 2.0 && body_size >= 300.0;
if cx.get_position("BTCUSDT").await?.is_none() && open_short_condition {
println!("place order: {}", t2s(cx.time));
let take_profit_price = cx.open - upper_shadow_size;
let stop_price = cx.open + upper_shadow_size;
cx.cancel_all_order("BTCUSDT").await?;
cx.sell("BTCUSDT", 0.01).await?;
cx.buy_limit_reduce_only("BTCUSDT", take_profit_price, 0.01)
.await?;
cx.buy_trigger_market_reduce_only("BTCUSDT", stop_price, 0.01)
.await?;
}
Ok(())
}
#[tokio::main]
async fn main() {
let path = get_or_download("BTCUSDT/1m", 12).await.unwrap();
let data_source_1m = DataSource::from_file_metadata(
path,
Metadata {
symbol: "BTCUSDT".to_string(),
level: Level::Minute1,
min_size: 0.01,
min_notional: 0.0,
tick_size: 0.1,
maker_fee: 0.0002,
taker_fee: 0.0005,
maintenance: 0.004,
},
)
.unwrap();
let exchange = LocalExchange::new(data_source_1m.clone())
.cash(10000.0)
.leverage(10)
.slippage(0.0);
let mut engine = Engine::new(exchange.clone(), my_strategy);
if let Err(v) = engine.run("BTCUSDT", Level::Hour1).await {
println!("{:#?}", v);
}
let history_position = exchange.get_history_position_list("BTCUSDT").await.unwrap();
let history_order = exchange.get_history_order_list("BTCUSDT").await.unwrap();
let summary = summarize(&history_position);
println!("history summary: {:#?}", summary);
let data_source_1h = data_source_1m.resample(Level::Hour1).unwrap();
open_in_browser(
[data_source_1h, data_source_1m],
history_position,
history_order,
)
.unwrap();
}