|
| 1 | +using DigitexConnector; |
| 2 | +using DigitexConnector.EngineAPI; |
| 3 | +using DigitexConnector.Orders; |
| 4 | +using DigitexConnector.Trading; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Threading; |
| 8 | + |
| 9 | +namespace Example |
| 10 | +{ |
| 11 | + class IntervalAlgorithm : TradingAlgorithm |
| 12 | + { |
| 13 | + private int Interval = 10; |
| 14 | + private string SymbolName = "BTC/USD1"; |
| 15 | + private Symbol Smbl; |
| 16 | + private OrderBook OBook; |
| 17 | + |
| 18 | + private Timer TradingTimer; |
| 19 | + |
| 20 | + public IntervalAlgorithm(string host, string token, bool wss) |
| 21 | + : base(host, token, wss) |
| 22 | + { } |
| 23 | + |
| 24 | + public override void Prepare() |
| 25 | + { |
| 26 | + Smbl = SymbolsContainer.GetSymbol(SymbolName); |
| 27 | + SpotPriceUpdated += SpotPriceHandler; |
| 28 | + OrderBookUpdated += OrderBookHandler; |
| 29 | + OBook = TrackSymbol(Smbl); |
| 30 | + TradingTimer = new Timer((object obj) => PlaceOrder(), null, Interval * 1000, Interval * 1000); |
| 31 | + } |
| 32 | + |
| 33 | + private void SpotPriceHandler(OrderBook oBook) |
| 34 | + { |
| 35 | + if (oBook.Equals(OBook)) |
| 36 | + { |
| 37 | + // Do something for handle spot price changing. |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private void OrderBookHandler(OrderBook oBook) |
| 42 | + { |
| 43 | + if (oBook.Equals(OBook)) |
| 44 | + { |
| 45 | + // Do something for handle market depth changing. |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private void PlaceOrder() |
| 50 | + { |
| 51 | + bool positivePosition = Account.GetTraderInfo(Smbl).PositionType == DigitexWire.OrderPosition.Long; |
| 52 | + decimal price; |
| 53 | + DigitexWire.OrderSide side; |
| 54 | + |
| 55 | + if (positivePosition) |
| 56 | + { |
| 57 | + price = OBook.GetBestAskPrice() ?? OBook.AdjustedSpotPrice; |
| 58 | + side = DigitexWire.OrderSide.Sell; |
| 59 | + } |
| 60 | + else |
| 61 | + { |
| 62 | + price = OBook.GetBestBidPrice() ?? OBook.AdjustedSpotPrice; |
| 63 | + side = DigitexWire.OrderSide.Buy; |
| 64 | + } |
| 65 | + |
| 66 | + OrderBase order = PlaceOrderLimit(Smbl, side, 1, price, OrderStatusHandler, ErrorHandler); |
| 67 | + |
| 68 | + Console.WriteLine($"Place {order?.OrigClientId} - {(order is null ? "fail" : "success")}"); |
| 69 | + } |
| 70 | + |
| 71 | + private void OrderStatusHandler(OrderBase order) |
| 72 | + { |
| 73 | + // Do something for handle order status changing. |
| 74 | + |
| 75 | + Console.WriteLine($"Order {order.OrigClientId} is {order.Status}"); |
| 76 | + |
| 77 | + // Use OrigClientId for comparison of orders. |
| 78 | + } |
| 79 | + |
| 80 | + private void ErrorHandler(OrderBase order, ErrorCodes code) |
| 81 | + { |
| 82 | + // Do something for handle order errors. |
| 83 | + |
| 84 | + Console.WriteLine($"Error {code}, order {order.OrigClientId}"); |
| 85 | + } |
| 86 | + |
| 87 | + public override List<ModuleState> GetParams() |
| 88 | + { |
| 89 | + return new List<ModuleState>() |
| 90 | + { |
| 91 | + new ModuleState("interval", "10") |
| 92 | + }; |
| 93 | + } |
| 94 | + |
| 95 | + public override void SetAlgoArguments(Dictionary<string, string> algoArguments) |
| 96 | + { |
| 97 | + if (algoArguments.ContainsKey("interval")) { Interval = int.Parse(algoArguments["interval"]); } |
| 98 | + } |
| 99 | + |
| 100 | + public override void Stop() |
| 101 | + { |
| 102 | + TradingTimer.Dispose(); |
| 103 | + } |
| 104 | + |
| 105 | + public override void OnDispose() |
| 106 | + { |
| 107 | + TradingTimer?.Dispose(); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments