Пример #1
0
 protected override void OnStart()
 {
     _filePath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\cAlgo\\cbotset\\";
     _fileName = _filePath + "cbotset.json";
     Print("fiName=" + _fileName);
     SetParams();
     if (_magnify != 1)
     {
         Print("Please choose the MACS_Magnify.");
         this.Stop();
     }
     _mac = Indicators.GetIndicator <MAC>(_resultperiods, _averageperiods, _sub);
     _mas = Indicators.GetIndicator <MAS>(_resultperiods, _averageperiods, _sub, _break);
     InitTimer1();
     _timer1.Start();
     Print("Done OnStart()");
 }
Пример #2
0
        protected override void OnStart()
        {
            _filePath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\cAlgo\\cbotset\\";
            _fileName = _filePath + "cbotset.json";
            Print("fiName=" + _fileName);
            SetParams();
            _istrade = !_stopTrade;
            Print("IsTrade: " + _istrade.ToString() + "-" + _istrade.GetType().ToString());

            if (_magnify != 1)
            {
                Print("Please choose the MACS_Magnify.");
                this.Stop();
            }
            Positions.Opened += OnPositionsOpened;
            Positions.Closed += OnPositionsClosed;
            _mac              = Indicators.GetIndicator <MAC>(_resultperiods, _averageperiods, _sub);
            _mas              = Indicators.GetIndicator <MAS>(_resultperiods, _averageperiods, _sub, _break);

            if (_openCross)
            {
                _abovecross = true;
                _belowcross = true;
                Print("abovecross: " + _abovecross.ToString());
                Print("belowcross: " + _belowcross.ToString());
            }
            else
            {
                _abovecross = false;
                _belowcross = false;
                Print("abovecross: " + _abovecross.ToString());
                Print("belowcross: " + _belowcross.ToString());
            }

            _risk = false;
            Print("risk: " + _risk.ToString());

            _h_abovelabel = "HAbove" + "-" + Symbol.Code + "-" + MarketSeries.TimeFrame.ToString();
            _h_belowlabel = "HBelow" + "-" + Symbol.Code + "-" + MarketSeries.TimeFrame.ToString();
            _t_abovelabel = "TAbove" + "-" + Symbol.Code + "-" + MarketSeries.TimeFrame.ToString();
            _t_belowlabel = "TBelow" + "-" + Symbol.Code + "-" + MarketSeries.TimeFrame.ToString();
            _init         = new OrderParams(null, Symbol, _initvolume, null, null, null, null, null, null, new System.Collections.Generic.List <double>
            {
            });

            #region Get Mark
            Position[] h_pos_above = this.GetPositions(_h_abovelabel);
            Position[] h_pos_below = this.GetPositions(_h_belowlabel);
            var        h_poss      = h_pos_above.Length == 0 ? h_pos_below : h_pos_above;
            if (h_poss.Length != 0)
            {
                foreach (var p in h_poss)
                {
                    var idx = p.Comment.IndexOf("M_") + 2;
                    if (!_h_marklist.Contains(p.Comment.Substring(idx, 13)))
                    {
                        _h_marklist.Add(p.Comment.Substring(idx, 13));
                    }
                }
            }
            if (_h_marklist.Count != 0)
            {
                foreach (var mar in _h_marklist)
                {
                    Print("h: " + mar);
                }
            }

            Position[] t_pos_above = this.GetPositions(_t_abovelabel);
            Position[] t_pos_below = this.GetPositions(_t_belowlabel);
            if (t_pos_above.Length != 0)
            {
                foreach (var p in t_pos_above)
                {
                    var idx = p.Comment.IndexOf("M_") + 2;
                    if (!_t_a_marklist.Contains(p.Comment.Substring(idx, 13)))
                    {
                        _t_a_marklist.Add(p.Comment.Substring(idx, 13));
                    }
                }
            }
            if (t_pos_below.Length != 0)
            {
                foreach (var p in t_pos_below)
                {
                    var idx = p.Comment.IndexOf("M_") + 2;
                    if (!_t_b_marklist.Contains(p.Comment.Substring(idx, 13)))
                    {
                        _t_b_marklist.Add(p.Comment.Substring(idx, 13));
                    }
                }
            }
            if (_t_a_marklist.Count != 0)
            {
                foreach (var mar in _t_a_marklist)
                {
                    Print("t_a: " + mar);
                }
            }
            if (_t_b_marklist.Count != 0)
            {
                foreach (var mar in _t_b_marklist)
                {
                    Print("t_b: " + mar);
                }
            }
            #endregion
            Print("Done OnStart()");
        }
Пример #3
0
        protected override void OnTick()
        {
            #region Parameter
            GetRisk();
            SetParams();
            if (_isChange)
            {
                _mac = Indicators.GetIndicator <MAC>(_resultperiods, _averageperiods, _sub);
                _mas = Indicators.GetIndicator <MAS>(_resultperiods, _averageperiods, _sub, _break);
            }
            var        cr          = _mac.Result.LastValue;
            var        ca          = _mac.Average.LastValue;
            var        sr          = _mas.Result.LastValue;
            var        sa          = _mas.Average.LastValue;
            Position[] h_pos_above = this.GetPositions(_h_abovelabel);
            Position[] h_pos_below = this.GetPositions(_h_belowlabel);
            Position[] t_pos_above = this.GetPositions(_t_abovelabel);
            Position[] t_pos_below = this.GetPositions(_t_belowlabel);
            var        h_poss      = h_pos_above.Length == 0 ? h_pos_below : h_pos_above;
            #endregion

            #region Cross
            if (h_pos_above.Length == 0)
            {
                if (_abovecross == false)
                {
                    _abovecross = true;
                    Print("abovecross: " + _abovecross.ToString());
                }
            }
            else
            {
                if (sr > sa)
                {
                    if (_abovecross == false)
                    {
                        _abovecross = true;
                        Print("abovecross: " + _abovecross.ToString());
                    }
                }
            }
            if (h_pos_below.Length == 0)
            {
                if (_belowcross == false)
                {
                    _belowcross = true;
                    Print("belowcross: " + _belowcross.ToString());
                }
            }
            else
            {
                if (sr < sa)
                {
                    if (_belowcross == false)
                    {
                        _belowcross = true;
                        Print("belowcross: " + _belowcross.ToString());
                    }
                }
            }
            #endregion

            #region Close
            if (!_stopClose)
            {
                #region Risk
                if (_risk)
                {
                    if (h_poss.Length >= 2)
                    {
                        var first  = h_poss[0];
                        var second = h_poss[1];
                        var last0  = h_poss.OrderByDescending(p => p.EntryTime).ToArray()[0];
                        var last1  = h_poss.OrderByDescending(p => p.EntryTime).ToArray()[1];
                        if (last1.NetProfit < 0 && first.NetProfit + last0.NetProfit > 0)
                        {
                            this.ClosePosition(last0);
                            this.ClosePosition(first);
                            _risk = false;
                            Print("risk: " + _risk.ToString());
                            return;
                        }
                        else if (last1.NetProfit > 0)
                        {
                            this.ClosePosition(last0);
                            _risk = false;
                            Print("risk: " + _risk.ToString());
                            return;
                        }
                    }
                }
                #endregion
                //above
                if (t_pos_above.Length != 0)
                {
                    if (sr <= 0 && this.TotalProfits(_t_abovelabel) > 0)
                    {
                        this.closeAllLabel(_t_abovelabel);
                    }
                }
                if (h_pos_above.Length != 0)
                {
                    if (GetClose(_h_abovelabel))
                    {
                        if (sr <= _sub / 5)
                        {
                            this.closeAllLabel(_h_abovelabel);
                            this.closeAllLabel(_t_belowlabel);
                            _risk = false;
                            Print("risk: " + _risk.ToString());
                        }
                    }
                    else
                    {
                        if (sr <= 0)
                        {
                            this.closeAllLabel(_h_abovelabel);
                            this.closeAllLabel(_t_belowlabel);
                            _risk = false;
                            Print("risk: " + _risk.ToString());
                        }
                    }
                }
                //below
                if (t_pos_below.Length != 0)
                {
                    if (sr >= 0 && this.TotalProfits(_t_belowlabel) > 0)
                    {
                        this.closeAllLabel(_t_belowlabel);
                    }
                }
                if (h_pos_below.Length != 0)
                {
                    if (GetClose(_h_belowlabel))
                    {
                        if (sr >= -_sub / 5)
                        {
                            this.closeAllLabel(_h_belowlabel);
                            this.closeAllLabel(_t_abovelabel);
                            _risk = false;
                            Print("risk: " + _risk.ToString());
                        }
                    }
                    else
                    {
                        if (sr >= 0)
                        {
                            this.closeAllLabel(_h_belowlabel);
                            this.closeAllLabel(_t_abovelabel);
                            _risk = false;
                            Print("risk: " + _risk.ToString());
                        }
                    }
                }
            }
            #endregion

            #region Trade
            if (_istrade)
            {
                #region Open
                #region Above
                if (GetOpen() == "below_t")
                {
                    var volume = GetOpenVolume(GetOpen());
                    _init.TradeType = TradeType.Sell;
                    _init.Volume    = Symbol.NormalizeVolumeInUnits(volume, RoundingMode.ToNearest);
                    _init.Label     = _t_belowlabel;
                    _init.Comment   = "CR_" + string.Format("{0:000000}", Math.Round(cr)) + "<";
                    _init.Comment  += "BR_000" + "<";
                    _init.Comment  += "D_" + string.Format("{0:000}", _distance) + "<";
                    _init.Comment  += "S_" + string.Format("{0:000}", _sub) + "<";
                    _init.Comment  += "B_" + string.Format("{0:000}", _break) + "<";
                    _init.Comment  += "P_" + string.Format("{0:000}", 1) + "<";
                    _init.Comment  += "M_" + _mac.Mark + "<";
                    this.executeOrder(_init);
                }
                if (GetOpen() == "above")
                {
                    var volume = GetOpenVolume(GetOpen());
                    _init.TradeType = TradeType.Sell;
                    _init.Volume    = Symbol.NormalizeVolumeInUnits(volume, RoundingMode.ToNearest);
                    _init.Label     = _h_abovelabel;
                    _init.Comment   = "CR_" + string.Format("{0:000000}", Math.Round(cr)) + "<";
                    _init.Comment  += "BR_000" + "<";
                    _init.Comment  += "D_" + string.Format("{0:000}", _distance) + "<";
                    _init.Comment  += "S_" + string.Format("{0:000}", _sub) + "<";
                    _init.Comment  += "B_" + string.Format("{0:000}", _break) + "<";
                    _init.Comment  += "P_" + string.Format("{0:000}", h_pos_above.Length + 1) + "<";
                    _init.Comment  += "M_" + _mas.Mark + "<";
                    this.executeOrder(_init);
                    _abovecross = false;
                    Print("abovecross: " + _abovecross.ToString());
                }
                if (GetOpen() == "above_br" && _isbreak)
                {
                    var volume = GetOpenVolume(GetOpen());
                    _init.TradeType = TradeType.Sell;
                    _init.Volume    = Symbol.NormalizeVolumeInUnits(volume, RoundingMode.ToNearest);
                    _init.Label     = _h_abovelabel;
                    _init.Comment   = "CR_" + string.Format("{0:000000}", Math.Round(cr)) + "<";
                    _init.Comment  += "BR_" + string.Format("{0:000}", GetBreak(_h_abovelabel) + _distance) + "<";
                    _init.Comment  += "D_" + string.Format("{0:000}", _distance) + "<";
                    _init.Comment  += "S_" + string.Format("{0:000}", _sub) + "<";
                    _init.Comment  += "B_" + string.Format("{0:000}", _break) + "<";
                    _init.Comment  += "P_" + string.Format("{0:000}", h_pos_above.Length + 1) + "<";
                    _init.Comment  += "M_" + _mas.Mark + "<";
                    this.executeOrder(_init);
                }
                #endregion
                #region Below
                if (GetOpen() == "above_t")
                {
                    var volume = GetOpenVolume(GetOpen());
                    _init.TradeType = TradeType.Buy;
                    _init.Volume    = Symbol.NormalizeVolumeInUnits(volume, RoundingMode.ToNearest);
                    _init.Label     = _t_abovelabel;
                    _init.Comment   = "CR_" + string.Format("{0:000000}", Math.Round(cr)) + "<";
                    _init.Comment  += "BR_000" + "<";
                    _init.Comment  += "D_" + string.Format("{0:000}", _distance) + "<";
                    _init.Comment  += "S_" + string.Format("{0:000}", _sub) + "<";
                    _init.Comment  += "B_" + string.Format("{0:000}", _break) + "<";
                    _init.Comment  += "P_" + string.Format("{0:000}", 1) + "<";
                    _init.Comment  += "M_" + _mac.Mark + "<";
                    this.executeOrder(_init);
                }
                if (GetOpen() == "below")
                {
                    var volume = GetOpenVolume(GetOpen());
                    _init.TradeType = TradeType.Buy;
                    _init.Volume    = Symbol.NormalizeVolumeInUnits(volume, RoundingMode.ToNearest);
                    _init.Label     = _h_belowlabel;
                    _init.Comment   = "CR_" + string.Format("{0:000000}", Math.Round(cr)) + "<";
                    _init.Comment  += "BR_000" + "<";
                    _init.Comment  += "D_" + string.Format("{0:000}", _distance) + "<";
                    _init.Comment  += "S_" + string.Format("{0:000}", _sub) + "<";
                    _init.Comment  += "B_" + string.Format("{0:000}", _break) + "<";
                    _init.Comment  += "P_" + string.Format("{0:000}", h_pos_below.Length + 1) + "<";
                    _init.Comment  += "M_" + _mas.Mark + "<";
                    this.executeOrder(_init);
                    _belowcross = false;
                    Print("belowcross: " + _belowcross.ToString());
                }
                if (GetOpen() == "below_br" && _isbreak)
                {
                    var volume = GetOpenVolume(GetOpen());
                    _init.TradeType = TradeType.Buy;
                    _init.Volume    = Symbol.NormalizeVolumeInUnits(volume, RoundingMode.ToNearest);
                    _init.Label     = _h_belowlabel;
                    _init.Comment   = "CR_" + string.Format("{0:000000}", Math.Round(cr)) + "<";
                    _init.Comment  += "BR_" + string.Format("{0:000}", GetBreak(_h_belowlabel) + _distance) + "<";
                    _init.Comment  += "D_" + string.Format("{0:000}", _distance) + "<";
                    _init.Comment  += "S_" + string.Format("{0:000}", _sub) + "<";
                    _init.Comment  += "B_" + string.Format("{0:000}", _break) + "<";
                    _init.Comment  += "P_" + string.Format("{0:000}", h_pos_below.Length + 1) + "<";
                    _init.Comment  += "M_" + _mas.Mark + "<";
                    this.executeOrder(_init);
                }
                #endregion
                #endregion
            }
            #endregion

            #region Draw
            ChartObjects.DrawText("stop1", "fCross: " + "\t\tsClose: " + "\t\tsTrade: ", StaticPosition.TopLeft);
            ChartObjects.DrawText("stop2", "\t" + _openCross.ToString() + "\t\t" + _stopClose.ToString() + "\t\t" + _stopTrade.ToString(), StaticPosition.TopLeft, Colors.Red);
            ChartObjects.DrawText("Cross1", "\naCross: " + "\t\tbCross: " + "\t\trisk: ", StaticPosition.TopLeft);
            ChartObjects.DrawText("Cross2", "\n\t" + _abovecross.ToString() + "\t\t" + _belowcross.ToString() + "\t\t" + _risk.ToString(), StaticPosition.TopLeft, Colors.Red);
            ChartObjects.DrawText("Close1", "\n\naCount: " + "\t\taClose: " + "\t\tbCount: " + "\t\tbClose: ", StaticPosition.TopLeft);
            ChartObjects.DrawText("Close2", "\n\n\t" + h_pos_above.Length.ToString() + "\t\t" + GetClose(_h_abovelabel).ToString() + "\t\t" + h_pos_below.Length.ToString() + "\t\t" + GetClose(_h_belowlabel).ToString(), StaticPosition.TopLeft, Colors.Red);
            #endregion
        }