private FuncAnalyzable(IEnumerable <TInput> inputs, Func <IReadOnlyList <TInput>, int, IReadOnlyList <decimal>, IAnalyzeContext <TInput>, decimal?> func, params decimal[] parameters) : this(inputs) { _func = func; Parameters = parameters; _ctx = new AnalyzeContext <TInput>(inputs); }
private BuySellRuleExecutor CreateBuySellRuleExecutor(IAnalyzeContext <IOhlcv> context, IFeeCalculator calculator, IDictionary <IEnumerable <IOhlcv>, decimal> assetCashMap, List <Transaction> transactions) { bool isPrevTransactionOfType(IEnumerable <Transaction> ts, IAnalyzeContext <IOhlcv> ctx, TransactionType tt) => ts.LastOrDefault(_t => _t.OhlcvList.Equals(ctx.BackingList))?.Type == tt; bool buyRule(IIndexedOhlcv ic) => !isPrevTransactionOfType(transactions, ic.Context, TransactionType.Buy) && _buyRule(ic); bool sellRule(IIndexedOhlcv ic) => transactions.Any() && !isPrevTransactionOfType(transactions, ic.Context, TransactionType.Sell) && _sellRule(ic); (TransactionType, IIndexedOhlcv)? outputFunc(IIndexedOhlcv ic, int i) { if (ic.Next == null) { return(null); } var type = (TransactionType)i; if (type.Equals(TransactionType.Buy)) { BuyAsset(ic, calculator, assetCashMap, transactions); } else { SellAsset(ic, calculator, assetCashMap, transactions); } return((TransactionType)i, ic); } return(new BuySellRuleExecutor(outputFunc, context, buyRule, sellRule)); }
private BuySellRuleExecutor CreateBuySellRuleExecutor(IAnalyzeContext <Candle> context, decimal premium, IDictionary <IEnumerable <Candle>, decimal> assetCashMap, List <Transaction> transactions) { Func <IEnumerable <Transaction>, IAnalyzeContext <Candle>, TransactionType, bool> isPreviousTransactionA = (ts, ctx, tt) => ts.LastOrDefault(_t => _t.Candles.Equals(ctx.BackingList))?.Type == tt; Predicate <IndexedCandle> buyRule = ic => !isPreviousTransactionA(transactions, ic.Context, TransactionType.Buy) && _buyRule(ic); Predicate <IndexedCandle> sellRule = ic => transactions.Any() && !isPreviousTransactionA(transactions, ic.Context, TransactionType.Sell) && _sellRule(ic); Func <IndexedCandle, int, (TransactionType, IndexedCandle)?> outputFunc = (ic, i) => { if (ic.Next == null) { return(null); } var type = (TransactionType)i; if (type.Equals(TransactionType.Buy)) { BuyAsset(ic, premium, assetCashMap, transactions); } else { SellAsset(ic, premium, assetCashMap, transactions); } return((TransactionType)i, ic); }; return(new BuySellRuleExecutor(outputFunc, context, buyRule, sellRule)); }
protected RuleExecutorBase( Func <TIndexed, int, TOutput> outputFunc, IAnalyzeContext <TInput> context, Predicate <TIndexed>[] rules) { _context = context ?? throw new ArgumentNullException(nameof(context)); OutputFunc = outputFunc ?? throw new ArgumentNullException(nameof(outputFunc)); Rules = rules ?? throw new ArgumentNullException(nameof(rules)); if (!rules.Any()) { throw new ArgumentException("You must have at least one rule to execute", nameof(rules)); } }
public SimpleRuleExecutor(IAnalyzeContext <IOhlcv> context, Predicate <IIndexedOhlcv> rule) : base((l, i) => l, context, new[] { rule }) { }
public SimpleRuleExecutor(IAnalyzeContext <Candle> context, Predicate <IndexedCandle> rule) : base((l, i) => l, context, new Predicate <IndexedCandle>[] { rule }) { }