internal void Start(StrategyContext strategyContext) { _strategy = _strategyFactory.GetStrategy( strategyContext.StrategyName, async(trades, candle) => { //TODO: move messages to on sell event await _telegram.SendMessageAsync($"{strategyContext.Market.ToUpperInvariant()} Buy signal @ {candle.Close} Will buy? {!trades.Any() || trades[trades.Count - 1].Direction == TradeDirection.Sell}"); return(!trades.Any() || trades[trades.Count - 1].Direction == TradeDirection.Sell); }, async(trades, candle) => { await _telegram.SendMessageAsync($"{strategyContext.Market.ToUpperInvariant()} Sell Signal @ {candle.Close} Will sell? {trades.Any() && trades[trades.Count - 1].Direction == TradeDirection.Buy}"); return(trades.Any() && trades[trades.Count - 1].Direction == TradeDirection.Buy); }); var cancellationToken = _cancellationTokenSource.Token; Task.Run(() => ExecuteStrategy(strategyContext, cancellationToken), cancellationToken); // For now I dont use websockts because too fast updates, maybe I should only take into consideration closed candles for signals? // Maybe add 2 modes and test how they perform in the market //candles.ForEach(c => _candles.Add(c.OpenTime, c)); //var subscription = new BinanceKLineSubscription() //{ // Symbol = strategyContext.Market.ToLowerInvariant(), // Interval = "1h" //}; //await _binanceApi.ConnectToWebSockets(subscription, OnCandleUpdate, cancellationToken); }
static bool TestStrategyPattern() { Console.WriteLine("TESTING THE STRATEGY DESIGN PATTERN: "); var factory = new StrategyFactory(); IStrategy strategy = factory.GetStrategy(StrategyEnum.Algorithms.Algorithm1); strategy.ExecuteAlgorithm("value 1"); //Output //Executing Algorithm 1 for input: value 1 strategy = factory.GetStrategy(StrategyEnum.Algorithms.Algorithm2); strategy.ExecuteAlgorithm("value 1"); //Output //Executing Algorithm 2 for input: value 1 return(true); }
static void Main(string[] args) { var strategyName = GetParam(args, "-r"); var fileName = GetParam(args, "-f"); IStrategy strategy = StrategyFactory.GetStrategy(strategyName); string[] answers = GetAnswers(fileName); new Chat(strategy, answers).Start(); }
public void DefaultItemsShouldDecreaseSellInAndQuality() { var item = new Item { Name = "+5 Dexterity Vest", SellIn = 10, Quality = 20 }; var strategy = StrategyFactory.GetStrategy(item); strategy.UpdateQuality(); Assert.Equal(item.SellIn, 9); Assert.Equal(item.Quality, 19); Assert.InRange(item.Quality, 0, 50); }
public void SulfurasDoesNotAlterSellInNorQuality() { var item = new Item { Name = "Sulfuras, Hand of Ragnaros", SellIn = 0, Quality = 80 }; var strategy = StrategyFactory.GetStrategy(item); strategy.UpdateQuality(); Assert.Equal(item.SellIn, item.SellIn); Assert.Equal(item.Quality, item.Quality); Assert.Equal(item.Quality, 80); }
public void AgedBrieShouldDecreaseSellInAndIncreaseQuality() { var item = new Item { Name = "Aged Brie", SellIn = 2, Quality = 0 }; var strategy = StrategyFactory.GetStrategy(item); strategy.UpdateQuality(); Assert.Equal(item.SellIn, 1); Assert.Equal(item.Quality, 1); Assert.InRange(item.Quality, 0, 50); }
public void ConjuredShouldDecreaseQualityTwiceAsFast() { var item = new Item { Name = "Conjured Mana Cake", SellIn = 3, Quality = 6 }; var strategy = StrategyFactory.GetStrategy(item); strategy.UpdateQuality(); Assert.Equal(item.SellIn, 2); Assert.Equal(item.Quality, 4); Assert.InRange(item.Quality, 0, 50); }
public Task StoreAsync <TDocument, TId, TContent>(Collection <TDocument, TId, TContent> collection) where TDocument : Document <TContent, TId>, new() where TContent : class { var commands = collection .GetModifiedDocuments() .Select(doc => StrategyFactory.GetStrategy(collection, doc)) .Select(s => s.GetCommand()); var commandsString = string.Join(";", commands); // TODO execute commands return(Task.CompletedTask); }
public void BackStagePassesShouldIncreaseQuality3TimesAsMuchWhenSellIn5OrLess() { var item = new Item { Name = "Backstage passes to a TAFKAL80ETC concert", SellIn = 5, Quality = 20 }; var strategy = StrategyFactory.GetStrategy(item); strategy.UpdateQuality(); Assert.Equal(item.SellIn, 4); Assert.Equal(item.Quality, 23); Assert.InRange(item.Quality, 0, 50); }
public void BackStagePassesShouldMakeQuality0WhenSellInOrLess() { var item = new Item { Name = "Backstage passes to a TAFKAL80ETC concert", SellIn = 0, Quality = 20 }; var strategy = StrategyFactory.GetStrategy(item); strategy.UpdateQuality(); Assert.Equal(item.SellIn, -1); Assert.Equal(item.Quality, 0); Assert.InRange(item.Quality, 0, 50); }
public void BackStagePassesShouldDecreaseSellInAndIncreaseQuality() { var item = new Item { Name = "Backstage passes to a TAFKAL80ETC concert", SellIn = 15, Quality = 20 }; var strategy = StrategyFactory.GetStrategy(item); strategy.UpdateQuality(); Assert.Equal(item.SellIn, 14); Assert.Equal(item.Quality, 21); Assert.InRange(item.Quality, 0, 50); }
private bool SetStrategy() { strategyFactory = StrategyFactory.GetInstance(); string formattenFileTypeExtension = fileType.Substring(0, 1).ToUpper() + fileType.Substring(1); Type t = Type.GetType("CircuitSimulator.Strategy." + formattenFileTypeExtension + "FileReaderStrategy"); if (t == null) { return(false); } strategyFactory.AddStrategyType(formattenFileTypeExtension, t); IFileReaderStrategy concretStrategy = strategyFactory.GetStrategy(formattenFileTypeExtension); strategy = concretStrategy; return(true); }
public TOutput Copy <TOutput, TInput>(TOutput output, TInput input) where TInput : class where TOutput : class { Guard.NotNull(input, nameof(input)); Guard.NotNull(output, nameof(output)); foreach (var propertyInfo in input.GetType().GetProperties()) { var copInfo = GetCopPropertyInfo(propertyInfo, input); if (copInfo is null) { continue; // No Copy attribute at all } var strategy = StrategyFactory.GetStrategy(copInfo); var context = new ExecutionContext(copInfo, input, output, propertyInfo); strategy.Execute(context); } return(output); }
private async Task RunTestAsync(string market, string candleSize, string strategyName) { var candles = (await _api.GetCandles(market, candleSize, 1000)).ToList(); var strategy = _strategyFactory.GetStrategy(strategyName); await strategy.Execute(candles); var sb = new StringBuilder(); if (strategy.Trades.Any()) { decimal totalProfit = strategy.Trades.Last(t => t.Direction == TradeDirection.Sell).VolumeEur; sb.AppendLine($"Start-End Date:{candles.First().OpenDate} - {candles.Last().OpenDate} {(candles.First().OpenDate - candles.Last().OpenDate).Days} days"); sb.AppendLine($"InitialInvestment: {strategy.InitialInvestment}"); sb.AppendLine($"Pnl: {totalProfit.Round(2)} ({MathExtensions.PercentageDifference(strategy.InitialInvestment, totalProfit)}%)"); sb.AppendLine($"Trades: "); for (int i = 0; i < strategy.Trades.Count; i++) { var p = strategy.Trades[i]; if (p.Direction == TradeDirection.Buy) { var percentage = i > 0 ? MathExtensions.PercentageDifference(strategy.Trades[i - 1].Volume, p.Volume) : 0; sb.AppendLine($"{p.TradeDate}: {p.Direction} >> {Math.Round(p.VolumeEur, 2)}EUR * {p.Price.Round(2)} = {p.Volume.Round(2)}??? ({percentage.Round(2)}%)"); } if (p.Direction == TradeDirection.Sell) { var percentage = i > 0 ? MathExtensions.PercentageDifference(strategy.Trades[i - 1].VolumeEur, p.VolumeEur) : 0; sb.AppendLine($"{p.TradeDate}: {p.Direction} >> {strategy.Trades[i - 1].Volume.Round(2)}??? @ {p.Price.Round(2)} = {Math.Round(p.VolumeEur, 2)}EUR ({percentage.Round(2)}%)"); } } } else { sb.AppendLine("No trades executed"); } await _telegram.SendMessageAsync(sb.ToString()); }
private void btnCalculate_Click(object sender, EventArgs e) { int Age = 0; double Weight = 0, Height = 0; if (!CheckInput(ref Age, ref Weight, ref Height)) { return; } Human human = new Human() { Age = Age, Weight = Weight, Height = Height, Gender = (GenderType)cbGender.SelectedIndex, Activity = (ActivityType)cbActivity.SelectedIndex, Goal = (Goal)cbGoal.SelectedIndex, isHighintensity = ckIntensity.Checked, isLabor = ckLabor.Checked }; try { TdeeStrategy strategy = StrategyFactory.GetStrategy(human); StringBuilder sb = new StringBuilder(); sb.Append($"計算結果為Tdee:{strategy.TDEE}\r\n"); sb.Append($"碳水化合物:{strategy.Nutrituon.Carbon}\r\n"); sb.Append($"蛋白質:{strategy.Nutrituon.Protein}\r\n"); sb.Append($"脂肪:{strategy.Nutrituon.Fat}\r\n"); MessageBox.Show(sb.ToString()); // SendMail(human, strategy.Nutrituon, strategy.TDEE); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
public void SimulateAllGames(List <string> gameStates) { List <double> turnCounts = new List <double>(); Console.WriteLine($"Preparing to run {gameStates.Count} simulations:"); foreach (var gameState in gameStates) { var map = Map.Deserialize(gameState); var strategy = StrategyFactory.GetStrategy(); turnCounts.Add(SimulateGame(map, strategy)); Console.Write("."); } Console.WriteLine(); Console.WriteLine("All simulations complete!"); var statistics = new DescriptiveStatistics(turnCounts); Console.WriteLine($"Average number of turns required: {statistics.Mean}"); Console.WriteLine($"Minimum number of turns required: {statistics.Minimum}"); Console.WriteLine($"Maximum number of turns required: {statistics.Maximum}"); Console.WriteLine($"Standard Deviation: {statistics.StandardDeviation}"); Console.WriteLine($"Skewness (0 represents a normal distribution): {statistics.Skewness}"); }
static void Main(string[] args) { var myFtpStrategy = StrategyFactory.GetStrategy <FtpStrategy, FtpConfig>(new FtpConfig { Uri = "my url" }); }
public void When_getting_the_strategy_to_use() { var chosenStrategy = StrategyFactory.GetStrategy(); Assert.That(chosenStrategy, Is.InstanceOf <NaiveBattleshipStrategy>()); }
public override void Execute() { Strategy = StrategyFactory.GetStrategy(name); Console.WriteLine($"Как советовать так все чатлане. Использую стратегию: {Strategy}"); }