简体   繁体   English

创建具有可变逻辑和可变变量计数的 C# 算法

[英]Create C# algorithm with variable logic and variable variables count

I'm messing around with creating a trading "bot" for personal use and am trying to create a form to control the signals that are used, but am having trouble working out how to correctly process the data based on my form inputs because the logic to apply to the variables needs to be selected by the form, eg:我正在忙于创建一个供个人使用的交易“机器人”,并试图创建一个表单来控制所使用的信号,但是我无法根据我的表单输入正确处理数据,因为逻辑要应用于需要由表单选择的变量,例如:

在此处输入图片说明

In the screenshot above, I am using a basic algorithm which says:在上面的屏幕截图中,我使用了一个基本算法,它说:

Buy If 
    StochRSI < 20
        **AND** 
    (MACD is crossing on rise **OR** Awesome Oscillator Signals Buy) 
        **AND** 
    Hurst Exponent > 0.5

However, in this setup但是,在此设置中

在此处输入图片说明

I am saying:我正在说:

Buy If 
    (RSI < 30 **OR** StochRSI < 20) 
        **AND** 
    (MACD is crossing on rise **OR** Awesome Oscillator Signals Buy) 
        **AND** 
    Hurst Exponent > 0.5

The difference is only that I have added in the RSI indicator, but note that I might have wanted to use both RSI and StochRSI and write this:不同之处仅在于我添加了 RSI 指标,但请注意,我可能想要同时使用 RSI 和 StochRSI 并编写以下内容:

Buy If 
    RSI < 30 
        **AND** 
    StochRSI < 20
        **AND** 
    (MACD is crossing on rise **OR** Awesome Oscillator Signals Buy) 
        **AND** 
    Hurst Exponent > 0.5 

NOTE the brackets around the OR statements注意OR语句周围的括号

This form is submitting via AJAX to a server side method which processes the data and creates the charts and signals, the problem is that I can't figure out how to take the inputs and make a formula which correctly puts the OR operators into bracketed equations optionally according to the correct variables submitted by the form.此表单通过 AJAX 提交到服务器端方法,该方法处理数据并创建图表和信号,问题是我无法弄清楚如何获取输入并制作一个正确地将OR运算符放入括号方程的公式可选地根据表单提交的正确变量。

I have tried processing all the OR options first, but that's not using the right sequences.我曾尝试先处理所有的 OR 选项,但这没有使用正确的序列。 I am quite close using a setup in which I first list all the variables and incrementally check the logic required for the next signal and process, but that isn't always right.我使用的设置非常接近,在该设置中我首先列出所有变量并逐步检查下一个信号和过程所需的逻辑,但这并不总是正确的。

My method is currently writing this for the above example:我的方法目前正在为上面的例子编写这个:

Buy If 
    RSI < 30 
        **AND** 
    StochRSI < 20
        **AND** 
    MACD is crossing on rise
        **OR** 
    Awesome Oscillator Signals Buy
        **AND** 
    Hurst Exponent > 0.5 

Unless I am very much mistaken, this is close but not quite the correct formula (not brackets for the OR)除非我大错特错,这很接近但不是完全正确的公式(不是 OR 的括号)

Additionally, I want to be able to add indicators to this and ideally not have to write millions of lines of code to add them....此外,我希望能够为此添加指标,理想情况下不必编写数百万行代码来添加它们....

EDIT:编辑:

OK, so this question was closed due to lack of information, so I will try to improve...好的,所以这个问题由于缺乏信息而被关闭,所以我会努力改进......

The aims are identified above.目标已在上面确定。 I have created an "alogorithm" class which is serialised from the form fields in the screenshot and would create a set of values like this:我创建了一个“算法”类,该类从屏幕截图中的表单字段序列化,并将创建一组如下值:

BuyAlgorithm: 
    awesomeFollowingLogic: AND 
    hurstFollowingLogic: OR
    macdFollowingLogic: OR
    rsiFollowingLogic: AND
    stochRsiFollowingLogic: AND 
    useAwesomeTriggers: false
    useHurstExponent: true
    useMACDTriggers: true
    useRsiTriggers: false
    useStochRsiTriggers: false

Given a series of signals derived from the market data, such as:给定从市场数据得出的一系列信号,例如:

Signals:
    RSIsignal:               Buy/Ignore
    StochRSIsignal:          Buy/Ignore
    MACDSignal:              Buy/Ignore
    AwesomeOscillatorSignal: Buy/Ignore
    HurstExponentSignal:     Buy/Ignore

How can I take the variables above and use the logic defined by the properties such as rsiFollowingLogic etc to generate a Buy/Ignore signal ignoring those which are not to be taken into account, and compounding the formula correctly to use the OR AND and ANDNOT without writing millions of lines!!我怎样才能使用上面的变量并使用由rsiFollowingLogic等属性定义的逻辑来生成一个 Buy/Ignore 信号而忽略那些不被考虑的信号,并正确地复合公式以使用OR ANDANDNOT而不写了几百万行!!

Thanks for all the help, I managed to solve it myself with a JSFiddle which I have re-worked for C# in my main project.感谢所有的帮助,我设法用一个 JSFiddle 自己解决了这个问题,我在我的主项目中为 C# 重新工作过。

For those interested, this is a drag and drop basic algorithm builder:对于那些感兴趣的人,这是一个拖放式基本算法构建器:

https://jsfiddle.net/jhartnoll/ozeqv86w/ https://jsfiddle.net/jhartnoll/ozeqv86w/

The algorithm is basically a list of indicators like this:该算法基本上是一个指标列表,如下所示:

[
  [
    [
      "macd",
      "hurst"
    ],
    [
      "rsi"
    ]
  ]
]

The code to determine true/false based on an input of Buy or Sell is根据买入或卖出的输入确定真/假的代码是

function confirmAction(algorithm, indicators, side) {
  let algoSteps = []; // new List<bool>();
  algorithm.forEach(function(outerList) {
    let algoRows = []; //new List<bool>();
    outerList.forEach(function(innerList) {
      // check each row of the algorithm (this is an OR list, so set true if ANY of these are true) and add the row result to the rows list. 
      algoRows.push(innerList.filter(i => indicators[i] && indicators[i] == side).length > 0);
    });
    // this step should return true only if ALL the rows are true (each row is an AND operator whilst the values within the row are an OR operator) 
    algoSteps.push(algoRows.filter(i => !i).length == 0);
  });
  // now finally, return true if any of the steps are true, steps are an OR operator.
  // It would be quite simple to toggle this final step to be AND if required...?
  return algoSteps.filter(i => i).length > 0;
}

I have commented each stage to hopefully explain what the list iteration is doing.我已经评论了每个阶段,希望能解释列表迭代正在做什么。

This takes in input which is an object showing the "decisions" each indicator makes like this:这接受输入,它是一个对象,显示每个指标做出的“决定”,如下所示:

{
  "rsi": "hold",
  "stoch": "hold",
  "macd": "sell",
  "ao": "hold",
  "hurst": "sell"
}

Yes I know this isn't the best collection of indicators to use, it's just for testing the concept of developing the extendable algorithm.是的,我知道这不是最好的指标集合,它只是用于测试开发可扩展算法的概念。 The number of indicators input into this can be as many or as few as required.输入到其中的指标数量可以根据需要多或少。

Obviously the actual buy/sell indicators are generated in separate code, but for this example and testing in the JSFiddle you can simply set them in the top section of the fiddle to test the algo.显然,实际的买入/卖出指标是在单独的代码中生成的,但对于本示例和 JSFiddle 中的测试,您可以简单地将它们设置在 fiddle 的顶部以测试算法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM