예제 #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.");
         this.Stop();
     }
     _mac = Indicators.GetIndicator <_Magnify_MAC>(_resultperiods, _averageperiods, _magnify, _sub);
     _mas = Indicators.GetIndicator <_Magnify_MAS>(_resultperiods, _averageperiods, _magnify, _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.");
                this.Stop();
            }
            Positions.Opened += OnPositionsOpened;
            Positions.Closed += OnPositionsClosed;
            _mac              = Indicators.GetIndicator <_Magnify_MAC>(_resultperiods, _averageperiods, _magnify, _sub);
            _mas              = Indicators.GetIndicator <_Magnify_MAS>(_resultperiods, _averageperiods, _magnify, _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());

            _abovelabel = "Above" + "-" + Symbol.Code + "-" + MarketSeries.TimeFrame.ToString();
            _belowlabel = "Below" + "-" + 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[] pos_above = this.GetPositions(_abovelabel);
            Position[] pos_below = this.GetPositions(_belowlabel);
            var        poss      = pos_above.Length == 0 ? pos_below : pos_above;
            if (poss.Length != 0)
            {
                foreach (var p in poss)
                {
                    var idx = p.Comment.IndexOf("M_") + 2;
                    if (!_marklist.Contains(p.Comment.Substring(idx, 13)))
                    {
                        _marklist.Add(p.Comment.Substring(idx, 13));
                    }
                }
            }
            if (_marklist.Count != 0)
            {
                foreach (var mar in _marklist)
                {
                    Print(mar);
                }
            }
            #endregion
            Print("Done OnStart()");
        }
예제 #3
0
        protected override void OnTick()
        {
            #region Parameter
            GetRisk();
            SetParams();
            if (_isChange)
            {
                _mac = Indicators.GetIndicator <_Magnify_MAC>(_resultperiods, _averageperiods, _magnify, _sub);
                _mas = Indicators.GetIndicator <_Magnify_MAS>(_resultperiods, _averageperiods, _magnify, _sub, _break);
            }
            var        cr        = _mac.Result.LastValue;
            var        ca        = _mac.Average.LastValue;
            var        sr        = _mas.Result.LastValue;
            var        sa        = _mas.Average.LastValue;
            Position[] pos_above = this.GetPositions(_abovelabel);
            Position[] pos_below = this.GetPositions(_belowlabel);
            var        poss      = pos_above.Length == 0 ? pos_below : pos_above;
            #endregion

            #region Cross
            if (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 (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)
            {
                //Risk
                if (_risk)
                {
                    if (poss.Length >= 2)
                    {
                        var first  = poss[0];
                        var second = poss[1];
                        var last0  = poss.OrderByDescending(p => p.EntryTime).ToArray()[0];
                        var last1  = 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;
                        }
                    }
                }
                if (pos_above.Length != 0)
                {
                    if (GetClose(_abovelabel))
                    {
                        if (sr <= _sub / 5)
                        {
                            this.closeAllLabel(_abovelabel);
                            _risk = false;
                            Print("risk: " + _risk.ToString());
                        }
                    }
                    else
                    {
                        if (sr <= 0)
                        {
                            this.closeAllLabel(_abovelabel);
                            _risk = false;
                            Print("risk: " + _risk.ToString());
                        }
                    }
                }
                if (pos_below.Length != 0)
                {
                    if (GetClose(_belowlabel))
                    {
                        if (sr >= -_sub / 5)
                        {
                            this.closeAllLabel(_belowlabel);
                            _risk = false;
                            Print("risk: " + _risk.ToString());
                        }
                    }
                    else
                    {
                        if (sr >= 0)
                        {
                            this.closeAllLabel(_belowlabel);
                            _risk = false;
                            Print("risk: " + _risk.ToString());
                        }
                    }
                }
            }
            #endregion

            #region Trade
            if (_istrade)
            {
                #region Open
                #region Above
                if (GetOpen() == "above")
                {
                    var volume = GetOpenVolume(GetOpen());
                    _init.TradeType = TradeType.Sell;
                    _init.Volume    = Symbol.NormalizeVolumeInUnits(volume, RoundingMode.ToNearest);
                    _init.Label     = _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}", 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     = _abovelabel;
                    _init.Comment   = "CR_" + string.Format("{0:000000}", Math.Round(cr)) + "<";
                    _init.Comment  += "BR_" + string.Format("{0:000}", GetBreak(_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}", pos_above.Length + 1) + "<";
                    _init.Comment  += "M_" + _mas.Mark + "<";
                    this.executeOrder(_init);
                }
                #endregion
                #region Below
                if (GetOpen() == "below")
                {
                    var volume = GetOpenVolume(GetOpen());
                    _init.TradeType = TradeType.Buy;
                    _init.Volume    = Symbol.NormalizeVolumeInUnits(volume, RoundingMode.ToNearest);
                    _init.Label     = _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}", 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     = _belowlabel;
                    _init.Comment   = "CR_" + string.Format("{0:000000}", Math.Round(cr)) + "<";
                    _init.Comment  += "BR_" + string.Format("{0:000}", GetBreak(_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}", 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" + pos_above.Length.ToString() + "\t\t" + GetClose(_abovelabel).ToString() + "\t\t" + pos_below.Length.ToString() + "\t\t" + GetClose(_belowlabel).ToString(), StaticPosition.TopLeft, Colors.Red);
            #endregion
        }