如果达到,等待下一个命令

时间:2021-04-17 15:04:13

标签: c#

我正在尝试实现一个我的机器人可以实现的功能

  1. 等待第一个函数为真但尚未执行但只是触发它开始等待
  2. 等待 second 为真,然后执行
  3. 无论第一个是真还是假,仍然在先前实现的状态下执行

此时我只能在两者都为真的情况下才能执行它。

我现在把它写成:

if (rsi.Result.LastValue < LRL && Bars.ClosePrices[index] > EnterAndExitMA.Result[index]) 打开(TradeType.Buy);

但我知道 && 使它需要同时实现。我不知道如何将其转换为上述步骤。

    using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class OversoldOverboughtMACross : Robot
    {
        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Source", Group = "RSI")]
        public DataSeries Source { get; set; }

        [Parameter("Label", DefaultValue = "MAType")]
        public string Label { get; set; }

        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

        [Parameter("Enter And Exit Period", DefaultValue = 55)]
        public int EnterAndExitPeriod { get; set; }

        [Parameter("Periods", Group = "RSI", DefaultValue = 55)]
        public int Periods { get; set; }

        [Parameter("Lower RSI Level", Group = "RSI", DefaultValue = 35, MinValue = 0, MaxValue = 50)]
        public int LRL { get; set; }

        [Parameter("Upper RSI Level", Group = "RSI", DefaultValue = 65, MinValue = 50, MaxValue = 100)]
        public int URL { get; set; }


        private RelativeStrengthIndex rsi;
        private MovingAverage EnterAndExitMA;

        protected override void OnStart()
        {
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
            EnterAndExitMA = Indicators.MovingAverage(Source, EnterAndExitPeriod, MAType);
        }

        protected override void OnBar()
        {
            int index = Bars.Count - 2;
            Exit(index);

            {
                {
                    if (rsi.Result.LastValue < LRL && Bars.ClosePrices[index] > EnterAndExitMA.Result[index])
                        Open(TradeType.Buy);
                    else if (rsi.Result.LastValue > URL && Bars.ClosePrices[index] < EnterAndExitMA.Result[index])
                        Open(TradeType.Sell);
                }
            }
        }

        private void Open(TradeType tradeType)
        {
            var position = Positions.Find(Label, SymbolName, tradeType);
            var volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);

            if (position == null)
                ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, Label, null, null);
        }

        private void Exit(int index)
        {
            var positions = Positions.FindAll(Label);

            foreach (var position in positions)

                if ((position.TradeType == TradeType.Buy && rsi.Result.LastValue > URL && Bars.ClosePrices[index] < EnterAndExitMA.Result[index]) || (position.TradeType == TradeType.Sell && rsi.Result.LastValue < LRL && Bars.ClosePrices[index] > EnterAndExitMA.Result[index]))

                    ClosePosition(position);
        }
    }
}

0 个答案:

没有答案