public TradescriptAlert(string name, SymbolItem symbol, string script, IList <Bar> history) { _Alert = new Modulus.TradeScript.Alert(); _Alert.License = TradeScript.TradeScriptKey; _Alert.Symbol = symbol.Symbol; _Alert.AlertName = name; _Alert.AlertScript = script; TradeScript.AppendData(_Alert, history); _Alert.OnScriptError += alert_OnError; _Alert.OnAlert += alert_OnAlert; }
public void RunBacktest(BacktestGetRequest aRequest) { var errorString = TradeScript.ValidateBuySell(aRequest.BuyScript, aRequest.SellScript); if (aRequest.BuyScript == string.Empty || aRequest.SellScript == string.Empty) { errorString = "Buy and sell script can not be empty"; } if (errorString != string.Empty) { var response = new BacktestGetResponse { Id = aRequest.Id, Error = errorString, User = aRequest.User }; PushResponses(aRequest.User.ID, new ResponseMessage[] { response }); return; } var selection = new HistoryParameters(aRequest.Id, aRequest.Symbol, aRequest.Periodicity, aRequest.Interval, aRequest.BarsCount); IDataFeed aDataFeed = GetDataFeedByName(aRequest.Symbol.DataFeed); if (aDataFeed != null) { GetHistoryCtx aCtx = GetHistoryCtx(selection, aDataFeed.TimeZoneInfo); aCtx.Request = new HistoryRequest(selection); aCtx.DataFeed = aDataFeed; ThreadPool.QueueUserWorkItem(o => { aDataFeed.GetHistory(aCtx, (ctx, bars) => { string backtestOutput = TradeScript.Backtest(aRequest.BuyScript, aRequest.SellScript, bars); var response = new BacktestGetResponse { Id = aRequest.Id, User = aRequest.User, Symbol = aRequest.Symbol, Information = TradeScript.ExtractBacktestInformation(backtestOutput), Trades = TradeScript.ExtractBacktestTrades(backtestOutput) }; PushResponses(aRequest.User.ID, new ResponseMessage[] { response }); }); }); } }
private void SubscribeAlert(AlertSubscribeRequest aRequest) { var errorString = TradeScript.Validate(aRequest.Script); if (aRequest.Script == string.Empty) { errorString = "Script can not be empty"; } if (errorString != string.Empty) { var response = new AlertSubscribeResponse { Id = aRequest.Id, Error = errorString, AlertName = aRequest.AlertName, Alert = new Alert { Name = aRequest.Name, Symbol = aRequest.Symbol }, User = aRequest.User }; PushResponses(aRequest.User.ID, new ResponseMessage[] { response }); return; } var selection = new HistoryParameters(aRequest.Id, aRequest.Symbol, aRequest.Periodicity, aRequest.Interval, aRequest.BarsCount); IDataFeed aDataFeed = GetDataFeedByName(aRequest.Symbol.DataFeed); if (aDataFeed != null) { GetHistoryCtx aCtx = GetHistoryCtx(selection, aDataFeed.TimeZoneInfo); aCtx.Request = new HistoryRequest(selection); aCtx.DataFeed = aDataFeed; ThreadPool.QueueUserWorkItem(o => { aDataFeed.GetHistory(aCtx, (ctx, bars) => { Level1Subscribers subscribers = null; var symbolItem = new SymbolItem() { DataFeed = aRequest.Symbol.DataFeed, Exchange = aRequest.Symbol.Exchange, Symbol = aRequest.Symbol.Symbol, Type = aRequest.Symbol.Type }; lock (m_Level1SubscribersBySymbols) { if (!m_Level1SubscribersBySymbols.TryGetValue(aRequest.Symbol, out subscribers)) { subscribers = new Level1Subscribers(); subscribers.Subscribers = new List <string>(); subscribers.AlertSubscribers = new List <AlertSubscription>(); m_Level1SubscribersBySymbols.Add(aRequest.Symbol, subscribers); } } AlertSubscription alert = new AlertSubscription() { Id = aRequest.Id, AlertName = aRequest.AlertName, Symbol = symbolItem, Name = aRequest.Name, Periodicity = aRequest.Periodicity, Interval = aRequest.Interval, Script = aRequest.Script, UserSessionId = aRequest.User.ID, Login = aRequest.User.Login, CalculationType = aRequest.CalculationType }; alert.InitAlert(bars); lock (subscribers.AlertSubscribers) { subscribers.AlertSubscribers.Add(alert); } aDataFeed.Subscribe(aRequest.Symbol); }); }); } }