protected override void OnStart() { _filePath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\cAlgo\\cbotset\\"; _fileName = _filePath + "cbotset.json"; Print("fiName=" + _fileName); SetParams(); _mac = Indicators.GetIndicator <Oil_MAC>(_resultperiods, _averageperiods, _sub); _mas = Indicators.GetIndicator <Oil_MAS>(_resultperiods, _averageperiods, _sub, _break); InitTimer1(); _timer1.Start(); Print("Done OnStart()"); }
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 <Oil_MAC>(_resultperiods, _averageperiods, _sub); _mas = Indicators.GetIndicator <Oil_MAS>(_resultperiods, _averageperiods, _sub, _break); _xbrsymbol = MarketData.GetSymbol("XBRUSD"); _xtisymbol = MarketData.GetSymbol("XTIUSD"); 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" + "-" + "XBRXTI" + "-" + MarketSeries.TimeFrame.ToString(); _belowlabel = "Below" + "-" + "XBRXTI" + "-" + MarketSeries.TimeFrame.ToString(); _init = new OrderParams(null, null, null, 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()"); }
protected override void OnTick() { #region Parameter GetRisk(); SetParams(); if (_isChange) { _mac = Indicators.GetIndicator <Oil_MAC>(_resultperiods, _averageperiods, _sub); _mas = Indicators.GetIndicator <Oil_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[] pos_above = this.GetPositions(_abovelabel); Position[] pos_below = this.GetPositions(_belowlabel); var poss = pos_above.Length == 0 ? pos_below : pos_above; List <Position> poss_xbr = new List <Position>(); List <Position> poss_xti = new List <Position>(); if (poss.Length != 0) { foreach (var p in poss) { if (p.SymbolCode == _xbrsymbol.Code) { poss_xbr.Add(p); } if (p.SymbolCode == _xtisymbol.Code) { poss_xti.Add(p); } } poss_xbr.OrderBy(p => p.EntryTime); poss_xti.OrderBy(p => p.EntryTime); } #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_xbr.Count >= 2 && poss_xti.Count >= 2) { var first_xbr = poss_xbr[0]; var second_xbr = poss_xbr[1]; var first_xti = poss_xti[0]; var second_xti = poss_xti[1]; var last0_xbr = poss_xbr.OrderByDescending(p => p.EntryTime).ToArray()[0]; var last1_xbr = poss_xbr.OrderByDescending(p => p.EntryTime).ToArray()[1]; var last0_xti = poss_xti.OrderByDescending(p => p.EntryTime).ToArray()[0]; var last1_xti = poss_xti.OrderByDescending(p => p.EntryTime).ToArray()[1]; var first_net = first_xbr.NetProfit + first_xti.NetProfit; var second_net = second_xbr.NetProfit + second_xti.NetProfit; var last0_net = last0_xbr.NetProfit + last0_xti.NetProfit; var last1_net = last1_xbr.NetProfit + last1_xti.NetProfit; if (last1_net < 0 && first_net + last0_net > 0) { this.ClosePosition(last0_xbr); this.ClosePosition(last0_xti); this.ClosePosition(first_xbr); this.ClosePosition(first_xti); _risk = false; Print("risk: " + _risk.ToString()); return; } else if (last1_net > 0) { this.ClosePosition(last0_xbr); this.ClosePosition(last0_xti); _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.Symbol = _xbrsymbol; _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}", poss_xbr.Count + 1) + "<"; _init.Comment += "M_" + _mas.Mark + "<"; this.executeOrder(_init); if (this.LastResult.IsSuccessful) { _init.TradeType = TradeType.Buy; _init.Symbol = _xtisymbol; _init.Volume = _init.Volume; this.executeOrder(_init); } _abovecross = false; Print("abovecross: " + _abovecross.ToString()); } if (GetOpen() == "above_br" && _isbreak) { var volume = GetOpenVolume(GetOpen()); _init.TradeType = TradeType.Sell; _init.Symbol = _xbrsymbol; _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}", poss_xbr.Count + 1) + "<"; _init.Comment += "M_" + _mas.Mark + "<"; this.executeOrder(_init); if (this.LastResult.IsSuccessful) { _init.TradeType = TradeType.Buy; _init.Symbol = _xtisymbol; _init.Volume = _init.Volume; this.executeOrder(_init); } } #endregion #region Below if (GetOpen() == "below") { var volume = GetOpenVolume(GetOpen()); _init.TradeType = TradeType.Buy; _init.Symbol = _xbrsymbol; _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}", poss_xbr.Count + 1) + "<"; _init.Comment += "M_" + _mas.Mark + "<"; this.executeOrder(_init); if (this.LastResult.IsSuccessful) { _init.TradeType = TradeType.Sell; _init.Symbol = _xtisymbol; _init.Volume = _init.Volume; this.executeOrder(_init); } _belowcross = false; Print("belowcross: " + _belowcross.ToString()); } if (GetOpen() == "below_br" && _isbreak) { var volume = GetOpenVolume(GetOpen()); _init.TradeType = TradeType.Buy; _init.Symbol = _xbrsymbol; _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}", poss_xbr.Count + 1) + "<"; _init.Comment += "M_" + _mas.Mark + "<"; this.executeOrder(_init); if (this.LastResult.IsSuccessful) { _init.TradeType = TradeType.Sell; _init.Symbol = _xtisymbol; _init.Volume = _init.Volume; 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 }