using System; using sharp.tradebot; using sharp.json; using sharp.trading; using System.IO; using sharp.extensions; using sharp.json.attributes; using System.Globalization; namespace BigBot { [JSONClassPolicy( Policy = JSONPolicy.ATTRIBUTED)] public class BigBot : TradingBot { FileBackedJSONValue Setup; public BigBot() { } sharp.tradebot.TradeBotBalance balanceBase; sharp.tradebot.TradeBotBalance balanceMarket; [JSONField] public double CurrentMarketCosts; [JSONField] double lastCenterRate; [JSONField] double lastDT; [JSONField] public SmoothValue[] Gliders; [JSONField] public SmoothValue[] dT; [JSONField] public SmoothValue[] dTdT; [JSONField] public string currentOrderID; public override void Prepare() { base.Prepare(); Setup = new FileBackedJSONValue(Path.Combine(BaseDataPath, "setup.json")); if (Setup.CurrentValue == null){ Setup.CurrentValue = new BigBotSetup(); Setup.Save(); } if (this.dT.IsNull()) { initialize_dT(); } if (this.dTdT.IsNull()) { initialize_dTdT(); } TradingBotEnvironment.RegisterPeriodic(Worker,15); Log("Prepared"); } public override void Save(){ Setup.Save(); base.Save(); } public override void Unprepare() { TradingBotEnvironment.UnregisterPeriodic(Worker); Save(); Log("Unprepared"); base.Unprepare(); } private bool Check(){ balanceBase = getBalance(Setup.CurrentValue.BaseSymbol); balanceMarket = getBalance(Setup.CurrentValue.MarketSymbol); if (balanceBase.IsNull() || balanceMarket.IsNull()){ Log("check failed: balances not available"); return false; } return true; } private void CheckCurrentOrder(){ if (!currentOrderID.IsNull()){ Order order = getOrder(currentOrderID); if (!order.IsNull() && !order.IsOpen){ balanceBase.CurrentBalance -= order.PayedFees; CurrentMarketCosts += order.PayedFees; if (order.OrderType == OrderType.BUY){ CurrentMarketCosts += order.PayedPrice; balanceBase.CurrentBalance -= order.PayedPrice; balanceMarket.CurrentBalance += order.FilledVolume; } else { CurrentMarketCosts -= order.PayedPrice; balanceBase.CurrentBalance += order.PayedPrice; balanceMarket.CurrentBalance -= order.FilledVolume; } } } } private void Worker() { if (Setup.CurrentValue.Enabled && Check()) { Log("BigBot: Worker() called and enabled!"); Log("Next run..."); Market market = TradingBotEnvironment.TradingConnection.openMarket(Setup.CurrentValue.MarketSymbol, Setup.CurrentValue.BaseSymbol); OrderBook orderBook = market.getOrderBook(); orderBook.Refresh(); System.Tuple currentCheck = orderBook.getVolumePrice(Setup.CurrentValue.MarketCheckVolume); double centerPrice = (currentCheck.Item1 + currentCheck.Item2) / 2; double centerRate = centerPrice / Setup.CurrentValue.MarketCheckVolume; if (this.Gliders.IsNull()) { initializeGliders(centerRate); } else { foreach (SmoothValue glide in this.Gliders) { glide.Add(centerRate); } } double _dT = (((centerRate - lastCenterRate) / centerRate)) * 100.0; double _dTdT = (_dT - lastDT); foreach (SmoothValue dt in this.dT) { dt.Add(_dT); } foreach (SmoothValue dtdt in this.dTdT) { dtdt.Add(_dTdT); } double[] Indicators = new double[Gliders.Length - 1]; for (int n = 0; n < Indicators.Length; n++) { Indicators[n] = 1.0 - (this.Gliders[0].CurrentValue / this.Gliders[n + 1].CurrentValue); } lastCenterRate = centerRate; lastDT = _dT; Log("Current volumized Market Values: Ask: {0,11:####0.00000000} {2} Bid: {1,11:####0.00000000} {2}", currentCheck.Item1, currentCheck.Item2, Setup.CurrentValue.BaseSymbol); Log("Current volumized Market Spread: {0,6:##0.00}%", ((currentCheck.Item1 / currentCheck.Item2)-1) * 100); Log("Current volumized Center Rate: {0,11:####0.00000000} {1}", centerRate, Setup.CurrentValue.BaseSymbol); Log("Current indicators [ 0..3 ]: {0,6:##0.00}% / {1,6:##0.00}% / {2,6:##0.00}% / {3,6:##0.00}%", Indicators[0] * 100,Indicators[1] * 100,Indicators[2] * 100,Indicators[3] * 100); WriteHistory(); Save(); market.Close(); } } private void initializeGliders(double center) { double k = 1; this.Gliders = new SmoothValue[10]; for (int n = 0; n < this.Gliders.Length; n++) { this.Gliders[n] = new SmoothValue(k); this.Gliders[n].Set(center); k /= 10; } } private void initialize_dT() { double k = 1; this.dT = new SmoothValue[8]; for (int n = 0; n < this.dT.Length; n++) { this.dT[n] = new SmoothValue(k); k /= 2; } } private void initialize_dTdT() { double k = 1; this.dTdT = new SmoothValue[8]; for (int n = 0; n < this.dTdT.Length; n++) { this.dTdT[n] = new SmoothValue(k); k /= 2; } } public void WriteHistory() { FileStream s = new FileStream(Path.Combine(BaseDataPath, "gliders.csv"), FileMode.Append); StreamWriter w = new StreamWriter(s); foreach (SmoothValue glide in this.Gliders) { w.Write(glide.CurrentValue.ToString(CultureInfo.InvariantCulture)); w.Write('\t'); } w.WriteLine(); w.Close(); s = new FileStream(Path.Combine(BaseDataPath, "dt.csv"), FileMode.Append); w = new StreamWriter(s); foreach (SmoothValue dt in this.dT) { w.Write(dt.CurrentValue.ToString(CultureInfo.InvariantCulture)); w.Write('\t'); } w.WriteLine(); w.Close(); s = new FileStream(Path.Combine(BaseDataPath, "dtdt.csv"), FileMode.Append); w = new StreamWriter(s); foreach (SmoothValue dtdt in this.dTdT) { w.Write(dtdt.CurrentValue.ToString(CultureInfo.InvariantCulture)); w.Write('\t'); } w.WriteLine(); w.Close(); } } public class BigBotSetup : BasicBotSetup { public double MarketCheckVolume = 1; } }