private void ButtonMarketOrder_Click(object sender, RoutedEventArgs e) { try { string instrument = ""; decimal amount = 0; CBotBase bot = null; EnmOrderDir dir = EnmOrderDir.Buy; if (IsValidInput( ComboMarketOrder, ComboInstrumentMarketOrder, TextBoxMarketOrderLot, ref dir, ref instrument, ref amount, ref bot )) { bot.ForceAddMarketOrder(instrument, dir, amount, bot.BotId); ShowMessageOK(); } else { return; } } catch (Exception exc) { MessageBox.Show("ButtonMarketOrder_Click"); } }
private void ProcessClosePos(int conId, byte[] arrMsgBody) { CCloseBotPosTrdMgr closePos = CUtilProto.DeserializeProto <CCloseBotPosTrdMgr>(arrMsgBody); CBotBase bot = _dealingServer.GetBotById(closePos.BotId); bot.ClosePositionOfInstrument(closePos.Instrument); }
public SettingsTableViewModel(CBotBase bot, ControlBotGUI botGUI) { try { _bot = bot; _botGUI = botGUI; int cnt = 0; //use only case when one instr if (_bot.SettingsBot.ListIsins.Count == 1) { string isin = bot.SettingsBot.ListIsins[0]; SetBorder(); AddNewSettingRecord("Instr:", isin); CTradingSettings ts = _bot.SettingsBot.TradingSettings[isin]; AddNewSettingRecord("Lot:", ts.Lot.ToString()); AddNewSettingRecord("Stoploss:", ts.StopLoss.ToString()); AddNewSettingRecord("TakeProfit:", ts.TakeProfit.ToString()); foreach (var v in bot.SettingsStrategy) { AddNewSettingRecord(v.Key, v.Value); } } } catch (Exception e) { throw; } }
private void BindBotControls(ControlBotGUI botGUI, CBotBase bot) { bot.GUIBot.DisposeGUIBotEvent += botGUI.OnDisposeGUIBot; botGUI.GUIBot = bot.GUIBot; botGUI.DataContext = bot; botGUI.EllipseEnabled.DataContext = bot; botGUI.TextEnabled.DataContext = bot; //botGUI.EllipseReady.DataContext = bot.GUIBot; botGUI.EllipseReady.DataContext = bot; botGUI.TextReady.DataContext = bot; botGUI.ButtonEnableBot.DataContext = bot; botGUI.ButtonDisableBot.DataContext = bot; botGUI.ButtonManualControl.DataContext = bot; botGUI.MonitorPos.ItemsSource = bot.GUIBot.MonitorPos; botGUI.DealingServer = Plaza2Connector; bot.GUIBot.MonitorPos.CollectionChanged += botGUI.OnMonitorPosCollectionChanged; botGUI.Orders.ItemsSource = bot.GUIBot.Orders; bot.GUIBot.Orders.CollectionChanged += botGUI.OnMonitorOrdersCollectionChanged; bot.GUIBot.DisposeGUIBotEvent += botGUI.OnDisposeGUIBot; botGUI.ButtonPosLog.Id = bot.BotId; botGUI.ButtonPosLog.BotOperations = Plaza2Connector; botGUI.ButtonDisableBot.Id = bot.BotId; botGUI.ButtonDisableBot.BotOperations = Plaza2Connector; botGUI.ButtonEnableBot.Id = bot.BotId; botGUI.ButtonEnableBot.BotOperations = Plaza2Connector; botGUI.ButtonLoadBot.Id = bot.BotId; botGUI.ButtonLoadBot.BotOperations = Plaza2Connector; botGUI.ButtonManualControl.Id = bot.BotId; botGUI.ButtonManualControl.BotOperations = Plaza2Connector; BindBotButtons(ref botGUI.ButtonPosLog, bot); BindBotButtons(ref botGUI.ButtonDisableBot, bot); BindBotButtons(ref botGUI.ButtonEnableBot, bot); BindBotButtons(ref botGUI.ButtonUnloadBot, bot); //botGUI.StopBot _vmSTab = new SettingsTableViewModel(bot, botGUI); CUtil.SetBinding(bot.GUIBot, "BotState", botGUI.BotSate, ControlSettingsDataBlock.SettingValueTextProperty); botGUI.EvntLoadBot += OnBotLoad; }
private void OnBotLoad(ControlBotGUI botGUI, long botId) { CBotBase bot = DealingServer.GetBotById(botId); if (bot != null) { BindBotControls(botGUI, bot); } }
private void OnBotLoad(ControlBotGUI botGUI, long botId) { CBotBase bot = Plaza2Connector.GetBotById(botId); if (bot != null) { BindBotControls(botGUI, bot); } }
public CTradeTimer(string name, CBotBase bot, EnmBotEventCode evCode, double expTimeMS) { m_timer = new Timer(expTimeMS); m_bot = bot; m_name = name; m_timer.Elapsed += new ElapsedEventHandler(OnTimerExpired); m_evBot = evCode; }
private void ButtonLimitOrder_Click(object sender, RoutedEventArgs e) { try { string instrument = ""; decimal amount = 0; CBotBase bot = null; EnmOrderDir dir = EnmOrderDir.Buy; if (IsValidInput( ComboBoxLimitOrder, ComboInstrumentLimitOrder, TextBoxLimOrderLot, ref dir, ref instrument, ref amount, ref bot )) { if (TextBoxPrice.Text == null || TextBoxPrice.Text == "") { MessageBox.Show("Ошибка. Цена не задана"); return; } else { decimal price; try { price = Convert.ToDecimal(TextBoxPrice.Text); } catch (Exception excpt) { MessageBox.Show("Ошибка. Некорректная цена"); return; } bot.ForceAddOrder(instrument, price, dir, amount); ShowMessageOK(); } } else { return; } } catch (Exception exc) { MessageBox.Show("ButtonLimitOrder_Click"); } }
/// <summary> /// Generates user orders for traders /// /// Call from processRawOrdLogStruct /// </summary> public void EnqueueUserOrdersUpdate(int traderId, CBotBase bb) { int connId = -1; lock (_dictBotIdConnId) { //update 2017-02-07 if (!_dictBotIdConnId.ContainsKey(traderId)) { return; } connId = _dictBotIdConnId[traderId]; } if (connId != -1) { CUserOrdersUpdate ob = new CUserOrdersUpdate(); ob.MonitorOrders = new Dictionary <string, Dictionary <long, COrder> >(); lock (bb.MonitorOrdersAll) { foreach (var v in bb.MonitorOrdersAll) { string isin = v.Key; ob.MonitorOrders[isin] = new Dictionary <long, COrder>(); foreach (var t in v.Value) { long orderId = t.Key; COrder ord = t.Value; ob.MonitorOrders[isin][orderId] = (COrder)ord.Copy(); } } } _bqTraderData.Add(new CTradingData { ConnId = connId, Data = (object)ob, Event = enmTradingEvent.UserOrdersUpdate } ); //SendDataToClients(ob, enmTradingEvent.UserOrdersUpdate, "", connId); _dbg.DBGUserordersUpdate(traderId, ob); } }
public void EnqueueUpdatUserPositionsMonitor(CBotBase bb, int traderId) { int connId = -1; lock (_dictBotIdConnId) { if (!_dictBotIdConnId.ContainsKey(traderId)) { return; } connId = _dictBotIdConnId[traderId]; } if (connId != -1) { CUserPosMonitorUpdate userPosUpdate = new CUserPosMonitorUpdate(); userPosUpdate.MonitorUserPos = new Dictionary <string, CUserPos>(); lock (bb.MonitorPositionsAll) { foreach (var v in bb.MonitorPositionsAll) { string isin = v.Key; userPosUpdate.MonitorUserPos[isin] = new CUserPos { Amount = v.Value.Amount, AvPos = v.Value.AvPos }; } } _bqTraderData.Add(new CTradingData { Data = (object)userPosUpdate, Event = enmTradingEvent.UserUpdatePositionMonitor, ConnId = connId } ); _dbg.DBGUpdUSerPosMon(traderId, userPosUpdate); } }
private bool IsPairedBotOrderAdded() { CBotBase bot = _dealingServer.GetBotById(6); if (bot.MonitorOrdersAll != null) { lock (bot.MonitorOrdersAll) { if (bot.MonitorOrdersAll.ContainsKey(m_isin) && bot.MonitorOrdersAll[m_isin].Count > 0 && bot.MonitorOrdersAll[m_isin].First().Value.Isin == m_isin && bot.MonitorOrdersAll[m_isin].First().Value.Amount == m_parLot) { return(true); } } } return(false); }
private void BindBotButtons(ref ButtonBot butBot, CBotBase bot) { butBot.Id = bot.BotId; butBot.BotOperations = DealingServer; }
/* private object GeSettingsSupervisor(XmlNode node) * { * * XmlNodeList xnList = node.SelectNodes("StrategySettings/limits/limit"); * CBotSupervisorSettings bss = new CBotSupervisorSettings(); * foreach (XmlNode lim in xnList) * { * string isin = lim.Attributes["isin"].Value.ToString(); * * bss.DictInstrumentLimits[isin] = new CBotSupervisorInstrumentLimits(); * bss.DictInstrumentLimits[isin].MaxOpenedPos = Convert.ToInt32(lim.Attributes["MaxOpenedPos"].Value.ToString()); * bss.DictInstrumentLimits[isin].MaxLossVM = Convert.ToDecimal(lim.Attributes["MaxLossVM"].Value.ToString()); * } * * return (object)bss; * * } */ public void CreateOneBotFromConfig(Dictionary <string, object> botConfig, List <Dictionary <string, object> > lstBotsInstrumentsConfig, int num, ref List <CBotBase> listBots, bool needSynchroOnLoad) { try { Log("Start CreateOneBotFromConfig"); string type = (string)botConfig["type"]; bool bEnabled = Convert.ToBoolean(botConfig["Enabled"].ToString()); bool bExternal = Convert.ToBoolean(botConfig["External"].ToString()); bool bNeedTFAnalyzer = Convert.ToBoolean(botConfig["NeedTFAnalyzer"].ToString()); decimal maxLossVMClosedTotal = Convert.ToDecimal(botConfig["MaxLossVMClosedTotal"]); int StockExhId = Convert.ToInt16(botConfig["StockExchId"]); System.Xml.XmlDocument document = new System.Xml.XmlDocument(); document.LoadXml((string)botConfig["StrategySettings"]); XmlNode xmlNLStrategySettings = document.SelectSingleNode("StrategySettings"); Dictionary <string, string> dictSettingsStrategy = new Dictionary <string, string>(); foreach (XmlNode nd in xmlNLStrategySettings) { dictSettingsStrategy[nd.Name] = nd.InnerText; } enmStrategysCode code = m_dictBotNameCode[type]; string botName = GenerateBotName(type, num); Dictionary <string, CBotLimits> dictBotIsinLimits = new Dictionary <string, CBotLimits>(); Dictionary <string, CTradingSettings> dictBotIsinTradeSettings = new Dictionary <string, CTradingSettings>(); var res = lstBotsInstrumentsConfig.FindAll(a => (int)a["BotId"] == num); if (res != null) { List <string> listIsins = new List <string>(); foreach (var instConf in res) { string instrument = (string)instConf["Instrument"]; dictBotIsinLimits[instrument] = new CBotLimits((int)instConf["MaxSendOrderRuntime"], (int)instConf["MaxPosition"], (int)instConf["MaxAddedOrder"], (decimal)instConf["MaxLossVM"]); long SL = 0; long TP = 0; int lot = 0; if (instConf["StopLoss"] != null) { SL = Convert.ToInt32(instConf["StopLoss"]); } if (instConf["TakeProfit"] != null) { TP = Convert.ToInt32(instConf["TakeProfit"]); } if (instConf["Lot"] != null) { lot = Convert.ToInt32(instConf["Lot"]); } dictBotIsinTradeSettings[instrument] = new CTradingSettings(SL, TP, lot); listIsins.Add(instrument); } CSettingsBot settingsBot = new CSettingsBot(code, bEnabled, listIsins, dictBotIsinLimits, dictBotIsinTradeSettings, bExternal, bNeedTFAnalyzer, maxLossVMClosedTotal); object stratSettings = null; settingsBot.StrategySettings = stratSettings; CBotBase bot = null; if (!bExternal) { if (code == enmStrategysCode.StrategyTest) { bot = new CBotTester(num, botName, settingsBot, dictSettingsStrategy, _dealingServer); } else if (code == enmStrategysCode.StrategyTesterPos) { bot = new CBotTesterPos(num, botName, settingsBot, dictSettingsStrategy, _dealingServer); } else if (code == enmStrategysCode.StrategyHighLowContra) { bot = new CBotHighLowContra(num, botName, settingsBot, dictSettingsStrategy, _dealingServer); } else if (code == enmStrategysCode.StrategyTesterCrossFirst) { bot = new CBotTesterCrossFirst(num, botName, settingsBot, dictSettingsStrategy, _dealingServer); } else if (code == enmStrategysCode.StrategyTesterCrossSecond) { bot = new CBotTesterCrossSecond(num, botName, settingsBot, dictSettingsStrategy, _dealingServer); } else if (code == enmStrategysCode.StrategyTesterLimits) { bot = new CBotTesterLimits(num, botName, settingsBot, dictSettingsStrategy, _dealingServer); } else if (code == enmStrategysCode.StrategyTrader) { bot = new CBotTrader(num, botName, settingsBot, dictSettingsStrategy, _dealingServer); } Log("Created bot num=" + num + " botName=" + botName); //listBots.Last().Start(); } else { // if (code == enmStrategysCode.StrategyTesterExternal) // bot = CreateExternalBot(num, type, botName, settingsBot, dictSettingsStrategy, typeof(CBotTesterExternal).Assembly); } if (bot != null) { listBots.Add(bot); _dealingServer.DictBots[bot.BotId] = bot; bot.Start(); if (needSynchroOnLoad) { bot.SynchronizeOnBotReload(); } //listBots.Last().Start(); } Log("End CreateOneBotFromConfig"); ; } /* * XmlNodeList xnList = botXML.SelectNodes("isins/isin"); * * * List<string> listIsins = new List<string>(); * Dictionary<string, CBotLimits> dictBotIsinLimits = new Dictionary<string, CBotLimits>(); * Dictionary<string, CTradingSettings> dictBotIsinTradeSettings = new Dictionary<string, CTradingSettings>(); * foreach (XmlNode lst in xnList) * { * string isin = lst.InnerText; * listIsins.Add(isin); * * dictBotIsinLimits[isin] = new CBotLimits(lst.Attributes["MaxSendOrderRuntime"].Value.ToString(), lst.Attributes["MaxPosition"].Value.ToString(), * lst.Attributes["MaxAddedOrder"].Value.ToString(), lst.Attributes["MaxLossVM"].Value.ToString()); * * * long SL = 0; * long TP = 0; * int lot = 0; * if (lst.Attributes["StopLoss"] != null) * SL = Convert.ToInt32((lst.Attributes["StopLoss"].Value.ToString())); * * if (lst.Attributes["TakeProfit"] != null) * TP = Convert.ToInt32((lst.Attributes["TakeProfit"].Value.ToString())); * * if (lst.Attributes["Lot"] != null) * lot = Convert.ToInt32((lst.Attributes["Lot"].Value.ToString())); * * * dictBotIsinTradeSettings[isin] = new CTradingSettings(SL, TP, lot); * * } * * object stratSettings = null; * //if (type == "Supervisor") stratSettings = GeSettingsSupervisor(botXML); * * * * XmlNode xmlNLStrategySettings = botXML.SelectSingleNode("StrategySettings"); * Dictionary<string, string> dictSettingsStrategy = new Dictionary<string, string>(); * foreach (XmlNode nd in xmlNLStrategySettings) * dictSettingsStrategy[nd.Name] = nd.InnerText; * * * * CSettingsBot settingsBot = new CSettingsBot(code, bEnabled, listIsins, dictBotIsinLimits, * dictBotIsinTradeSettings, bExternal, bNeedTFAnalyzer,maxLossVMClosedTotal); * settingsBot.StrategySettings = stratSettings; * //--- exp ------ * * * CBotBase bot = null; * * //TO DO refactor it * //----------- * if (!bExternal) * { * if (code == enmStrategysCode.StrategyTest) * bot = new CBotTester(num, botName, settingsBot, dictSettingsStrategy, _dealingServer); * else if (code == enmStrategysCode.StrategyTesterPos) * bot = new CBotTesterPos(num, botName, settingsBot, dictSettingsStrategy, _dealingServer); * else if (code == enmStrategysCode.StrategyHighLowContra) * bot = new CBotHighLowContra(num, botName, settingsBot, dictSettingsStrategy, _dealingServer); * else if (code == enmStrategysCode.StrategyTesterCrossFirst) * bot = new CBotTesterCrossFirst(num, botName, settingsBot, dictSettingsStrategy, _dealingServer); * else if (code == enmStrategysCode.StrategyTesterCrossSecond) * bot = new CBotTesterCrossSecond(num, botName, settingsBot, dictSettingsStrategy, _dealingServer); * else if (code == enmStrategysCode.StrategyTesterLimits) * bot = new CBotTesterLimits(num, botName, settingsBot, dictSettingsStrategy, _dealingServer); * else if (code == enmStrategysCode.StrategyTrader) * bot = new CBotTrader (num, botName, settingsBot, dictSettingsStrategy, _dealingServer); * * Log("Created bot num="+num+" botName="+botName); * //listBots.Last().Start(); * } * else * { * // if (code == enmStrategysCode.StrategyTesterExternal) * // bot = CreateExternalBot(num, type, botName, settingsBot, dictSettingsStrategy, typeof(CBotTesterExternal).Assembly); * * } * * if (bot != null) * { * listBots.Add(bot); * _dealingServer.DictBots[bot.BotId] = bot; * bot.Start(); * if (needSynchroOnLoad) * bot.SynchronizeOnBotReload(); * //listBots.Last().Start(); * * } * Log("End CreateOneBotFromConfig"); */ } catch (Exception e) { Error("CBotFactory.CreateOneBotFromConfig", e); } }
private bool IsValidInput(ComboBox comboBoxBuySell, ComboBox comboboxInstrument, TextBox textBoxVolume, ref EnmOrderDir dir, ref string instrument, ref decimal lotInput, ref CBotBase bot) { int index = comboboxInstrument.SelectedIndex; //TODO normal window if (index < 0) { MessageBox.Show("Ошибка. Не выбран инструмент."); return(false); } instrument = comboboxInstrument.Items[index].ToString(); if (textBoxVolume.Text == null || textBoxVolume.Text == "") { MessageBox.Show("Ошибка. Объем не задан"); return(false); } try { //2018-01-03 to make possible use bitfinex //lotInput = Convert.ToInt16(textBoxVolume.Text); //2018-03-13 remove as using decimals //lotInput = _dealingServer.GetGUITradeSystemVolume(instrument, textBoxVolume.Text); lotInput = Convert.ToDecimal(textBoxVolume.Text); if (lotInput <= 0) { MessageBox.Show("Ошибка.Объем должен быть положительным"); return(false); } } catch (Exception excp) { MessageBox.Show("Ошибка. Некорректный объем"); return(false); } if (IsPossibleToSendCommand()) { try { //TODO make correct cast ! bot = (CBotBase)_dealingServer.DictBots[_botId]; /*OrderDirection*/ if (comboBoxBuySell.SelectedIndex == 0) { dir = EnmOrderDir.Buy; } else { dir = EnmOrderDir.Sell; } // int lot = Convert.ToInt32(textBoxVolume.Text); return(true); } catch (Exception ex) { OnManualTradingError(ex); } } return(false); }
private void BindBotButtons(ref ButtonBot butBot, CBotBase bot) { butBot.Id = bot.BotId; butBot.BotOperations = Plaza2Connector; }