Пример #1
0
        private void CalculateTradeParams(Position position)
        {
            _tradeindexer++;
            //string positionstr = Math.Round(position.FPL, 2).ToString();

            string price = FormatNumber(position.Price);
            string last = FormatNumber(position.Last);
            string fpl = FormatNumber(Math.Round(position.Trades, 2));
            string clPnl = FormatNumber(Math.Round(position.ClosePNL, 2));
            string comission = FormatNumber(Math.Round(position.Commission, 2));
            string posPNL = FormatNumber(Math.Round(position.PosPNL, 2));
            string margin = FormatNumber(Math.Round(position.Margin, 2));

            if (position.Operation == Operation.PNL)
            {
                _dayOpenPrice = position.Price;
                _dayClosePrice = position.Last;
                //_nextDayOpenPrice = position.Price;
                //_nextDayClosePrice = position.Last;
                last = " ";
                price = "";
            }

            var trValues = new ArrayList
                           {
                              null,
                               _dayindexer,
                               position.TimeOpen.ToString(DateFormatsManager.CurrentShortDateFormat + " HH:mm:ss"),
                               position.TimeClose.ToString(DateFormatsManager.CurrentShortDateFormat + " HH:mm:ss"),
                               position.Operation,
                               position.Price,
                               position.Last,
                               Math.Round(position.Trades, 2),
                               Math.Round(position.Commission, 2),
                               Math.Round(position.PosPNL, 2),
                               Math.Round(position.ClosePNL, 2),
                               position.Comment,
                           };

            DataRow row = _weeklyData.tableTrades.NewRow();

            row.ItemArray = trValues.ToArray();
            _weeklyData.tableTrades.Rows.Add(row);
            _weeklyData.AcceptChanges();
        }
Пример #2
0
        private void AddEndDayPnl(int isExistDayTrade, double summaryChangedProfit, double lastOrderPriceChange, double dayChangedPrice, Tick tick)
        {
            DateTime temp = new DateTime(tick.Time.Year, tick.Time.Month, tick.Time.Day, 17, 0, 0);
            if (temp > tick.Time) temp = temp.AddDays(-1);
            Position endDayPNL = new Position(parameters.Account, parameters.Symbol, -1, temp,
                                  Operation.PNL, 1, _startPrice, tick.Close, "", "", true);
            double _dailyCommision = -isExistDayTrade * _commission * _contractSize * _ZIM;
            endDayPNL.TimeClose = tick.Time;
            endDayPNL.Trades = summaryChangedProfit; //Trades
            endDayPNL.Commission = _dailyCommision;
            endDayPNL.PosPNL = dayChangedPrice;
            endDayPNL.ClosePNL = lastOrderPriceChange;
            trades.Add(endDayPNL);

            /*
             *
             *  _lastOrderPriceChange = (Math.Pow(bar.Close - _lastOrderPrice, 2)*_contractSize*_multiplier*
                                                    _pointValue)/2;
                    }

                    _summaryChangedProfit = _dayChangedPrice + _lastOrderPriceChange;
             * */
            Log("{0}    -    Was added summary profit - summary profit {1} = summarySqrValue ( {4} + {5}) - commission {3}", tick.Time, Math.Round(endDayPNL.Trades, 2), Math.Round(summaryChangedProfit, 2), Math.Round(endDayPNL.Commission, 2), dayChangedPrice, lastOrderPriceChange);
        }
Пример #3
0
 private void AddPremium(DateTime open)
 {
     Position startHolidayPremium = new Position(parameters.Account, parameters.Symbol, -1, open, Operation.Premium, 1, 0, 0, "", "", true);
     startHolidayPremium.Trades = _TV;
     startHolidayPremium.TimeClose = open;
     trades.Add(startHolidayPremium);
     Log("{0}    -    was added premium = {1}", startHolidayPremium.TimeOpen, _TV);
 }
Пример #4
0
        public override void Start(int startBarIndex)
        {
            InitializeMainParameters();

            int _isExistDayTrade = 0;

            if (ticks.Count == 0)
                return;
            double percent = ticks.Count/100;
            double current_tick = 0;

            for (int i = startBarIndex; i < ticks.Count; i++)
            {
                if (i >= current_tick)
                {
                    NotifyProgress();
                    current_tick += percent;
                }

                Tick tick = ticks[i];

                if (i == startBarIndex)
                {
                    Log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
                    Log("|                         NEW TEST                      |");
                    Log("|step 1 = {0}                step 2 = {1}               |", _step1, _step2);
                    Log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
                }

                Tick prevTick = null;
                if (i - 1 >= startBarIndex)
                    prevTick = ticks[i - 1];
                else
                {
                    Log("                                                         ");
                    Log("                          NEW DAY                        ");
                    Log("                                                         ");
                    _startPrice = tick.Close;
                    _fixedPrice = tick.Close;
                    //AddPremium(bar.Time);
                    _isExistDayTrade = trades.Count;
                    continue;
                }

                bool firstTickAfter17 = false;
                switch (tick.Time.DayOfWeek)
                {
                    case DayOfWeek.Sunday:
                    case DayOfWeek.Saturday:
                        {
                            firstTickAfter17 = false;
                            break;
                        }
                    case DayOfWeek.Monday:
                    case DayOfWeek.Tuesday:
                    case DayOfWeek.Wednesday:
                    case DayOfWeek.Thursday:
                    case DayOfWeek.Friday:
                        {
                            if (tick.Time.DayOfWeek == prevTick.Time.DayOfWeek)
                            {
                                if (tick.IntradayIndex >= 170000 && prevTick.IntradayIndex < 170000)
                                    firstTickAfter17 = true;
                                else
                                {
                                    firstTickAfter17 = false;
                                }

                            }
                            else
                            {
                                double _dayDiff = (tick.Time.Date - prevTick.Time.Date).TotalDays;
                                if (_dayDiff > 1)
                                {
                                    if (tick.Time.DayOfWeek == DayOfWeek.Monday && tick.IntradayIndex < 170000)
                                        firstTickAfter17 = false;
                                    else
                                        firstTickAfter17 = true;

                                    // GAP
                                    if (_lastFridayOperation == Operation.Buy && Math.Round(tick.Close - _fixedPrice, 2) < 0)
                                    {
                                        int contract = (int)(Math.Abs(tick.Close - _fixedPrice) / Math.Round(_step2, 2));
                                        if (contract >= 1)
                                        {
                                            contract = 1;
                                            _fixedPrice -= Math.Round(_step2, 2);
                                            contract += (int)(Math.Abs(tick.Close - _fixedPrice) / Math.Round(_step1, 2));
                                            if (contract > 1)
                                            {
                                                _fixedPrice -= (contract - 1) * Math.Round(_step1, 2);
                                                CreateOrder(Operation.Sell, tick, true, positions.Count + contract);
                                            }
                                        }
                                    }
                                    else
                                        if (_lastFridayOperation == Operation.Sell && Math.Round(_fixedPrice - tick.Close, 2) < 0)
                                        {
                                            int contract = (int)(Math.Abs(tick.Close - _fixedPrice) / Math.Round(_step2, 2));
                                            if (contract >= 1)
                                            {
                                                contract = 1;
                                                _fixedPrice += Math.Round(_step2, 2);
                                                contract += (int)(Math.Abs(tick.Close - _fixedPrice) / Math.Round(_step1, 2));
                                                if (contract > 1)
                                                {
                                                    _fixedPrice += (contract - 1) * Math.Round(_step1, 2);
                                                    CreateOrder(Operation.Buy, tick, true, positions.Count + contract);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            int _contract = (int)(Math.Abs(tick.Close - _fixedPrice) / Math.Round(_step1, 2));
                                            if (_contract > 1)
                                            {
                                                if (tick.Close > _fixedPrice)
                                                {
                                                    _fixedPrice += _contract * Math.Round(_step1, 2);
                                                    CreateOrder(Operation.Buy, tick, false, _contract);

                                                }
                                                else
                                                {
                                                    _fixedPrice -= _contract * Math.Round(_step1, 2);
                                                    CreateOrder(Operation.Sell, tick, false, _contract);
                                                }
                                                DispatchOrders(tick);
                                            }
                                        }
                                }
                                else
                                {
                                    if (tick.IntradayIndex < 170000)
                                        firstTickAfter17 = false;
                                    else
                                        firstTickAfter17 = true;
                                }
                            }
                            break;
                        }
                }

                if (firstTickAfter17)
                {
                    // END TRADING DAY
                    Trade(ref positions, tick);
                    positions.Clear();
                    orders.Clear();
                    _isExistDayTrade = trades.Count - _isExistDayTrade;

                    //PosPNL
                    if (_isExistDayTrade > 0)
                    {
                        double _dayChangedPrice = Math.Pow(tick.Close - _startPrice, 2)*_contractSize*_multiplier*_pointValue/2;

                        double _lastOrderPriceChange = 0.0;
                        if (_lastOrderPrice != 0.0)
                        {
                            _lastOrderPriceChange = Math.Pow(tick.Close - _lastOrderPrice, 2)*_contractSize*_multiplier*
                                                    _pointValue/2;
                        }

                        double _summaryChangedProfit = _dayChangedPrice + _lastOrderPriceChange;

                        AddEndDayPnl(_isExistDayTrade, _summaryChangedProfit, _lastOrderPriceChange, _dayChangedPrice, tick);
                    }
                    else
                    {
                        AddEndDayPnl(0, 0, 0, 0, tick);
                    }

                    // START TRADING DAY
                    //AddPremium(bar.Time);

                    _lastFridayOrderPrice = 0.0;
                    _lastOrderPrice = 0.0;
                    _startPrice = tick.Close;
                    _fixedPrice = tick.Close;
                    _isExistDayTrade = trades.Count;
                }

                // TRADING
                if (tick.Time.DayOfWeek == DayOfWeek.Saturday || tick.Time.DayOfWeek == DayOfWeek.Sunday)
                    continue;

                if (_lastOrderOperation == Operation.Buy && positions.Count != 0 && Math.Round(tick.Close - _fixedPrice, 2) < 0 && Math.Round(_fixedPrice - tick.Close, 2) >= Math.Round(_step2, 2))
                {
                    _fixedPrice -= Math.Round(_step2, 2);

                    CreateOrder(Operation.Sell, tick, true, positions.Count + 1);
                    if (tick.Time.DayOfWeek == DayOfWeek.Friday && tick.IntradayIndex >= 170000)
                    {
                        _lastFridayOrderPrice = _fixedPrice;
                        _lastFridayOperation = Operation.Sell;
                    }
                }
                else if (_lastOrderOperation == Operation.Sell && positions.Count != 0 && Math.Round(_fixedPrice - tick.Close, 2) < 0 && Math.Round(tick.Close - _fixedPrice, 2) >= Math.Round(_step2, 2))
                {
                    _fixedPrice += Math.Round(_step2, 2);

                    CreateOrder(Operation.Buy, tick, true, positions.Count + 1);
                    if (tick.Time.DayOfWeek == DayOfWeek.Friday && tick.IntradayIndex >= 170000)
                    {
                        _lastFridayOrderPrice = _fixedPrice;
                        _lastFridayOperation = Operation.Buy;
                    }
                }
                else
                {
                    if (Math.Round(tick.Close - _fixedPrice, 2) >= Math.Round(_step1, 2) && _lastOrderOperation != Operation.Sell)
                    {
                        CreateOrder(Operation.Buy, tick, false, 1);
                        if (tick.Time.DayOfWeek == DayOfWeek.Friday && tick.IntradayIndex >= 170000)
                        {
                            _lastFridayOrderPrice = _fixedPrice;
                            _lastFridayOperation = Operation.Buy;
                        }
                    }
                    else if (Math.Round(_fixedPrice - tick.Close, 2) >= Math.Round(_step1, 2) && _lastOrderOperation != Operation.Buy)
                    {
                        CreateOrder(Operation.Sell, tick, false, 1);
                        if (tick.Time.DayOfWeek == DayOfWeek.Friday && tick.IntradayIndex >= 170000)
                        {
                            _lastFridayOrderPrice = _fixedPrice;
                            _lastFridayOperation = Operation.Sell;
                        }
                    }
                }

                DispatchOrders(tick);
            }

            _isExistDayTrade = trades.Count - _isExistDayTrade;
            if (trades[trades.Count - 1].Operation != Operation.PNL)
            {
                if (_isExistDayTrade > 0)
                {
                    double _dayChangedPrice = Math.Pow(ticks[ticks.Count-1].Close - _startPrice, 2) * _contractSize * _multiplier * _pointValue / 2;

                    double _lastOrderPriceChange = 0.0;
                    if (_lastOrderPrice != 0.0)
                    {
                        _lastOrderPriceChange = Math.Pow(ticks[ticks.Count - 1].Close - _lastOrderPrice, 2) * _contractSize * _multiplier *
                                                _pointValue / 2;
                    }

                    double _summaryChangedProfit = _dayChangedPrice + _lastOrderPriceChange;

                    //AddEndDayPnl(_isExistDayTrade, _summaryChangedProfit, _lastOrderPriceChange, _dayChangedPrice, bars[bars.Count - 1]);

                    Position endDayPNL = new Position(parameters.Account, parameters.Symbol, -1, ticks[ticks.Count - 1].Time,
                      Operation.PNL, 1, _startPrice, ticks[ticks.Count - 1].Close, "", "", true);
                    double _dailyCommision = -_isExistDayTrade * _commission * _contractSize * _ZIM;
                    endDayPNL.TimeClose = ticks[ticks.Count - 1].Time;
                    endDayPNL.Trades = _summaryChangedProfit; //Trades
                    endDayPNL.Commission = _dailyCommision;
                    endDayPNL.PosPNL = _dayChangedPrice;
                    endDayPNL.ClosePNL = _lastOrderPriceChange;
                    trades.Add(endDayPNL);
                }
                else
                {
                    AddEndDayPnl(0, 0, 0, 0, ticks[ticks.Count - 1]);
                }
            }

            if (ticks[0].IntradayIndex < 170000)
            {
                Tick tick = ticks[0];
                AddPremium(new DateTime(tick.Time.Year, tick.Time.Month, tick.Time.Day, 17, 00, 00));
            }
            DateTime _premiumTime = new DateTime(ticks[0].Time.Year,
                                                ticks[0].Time.Month,
                                                ticks[0].Time.AddDays(1).Day,//bars[0].IntradayIndex < 170000 ? bars[0].Time.AddDays(-1).Day : bars[0].Time.Day,
                                                17, 0, 0);
            while (_premiumTime <= ticks[ticks.Count - 1].Time)
            {
                AddPremium(_premiumTime);
                _premiumTime = _premiumTime.AddDays(1);
            }

            if (ticks[ticks.Count - 1].IntradayIndex < 170000 && ticks[ticks.Count - 1].Time.Day > trades[trades.Count - 1].TimeOpen.Day)
            {
                Tick tick = ticks[ticks.Count - 1];
                AddPremium(new DateTime(tick.Time.Year, tick.Time.Month, tick.Time.Day, 17, 00, 00));
            }

            if (ticks[ticks.Count - 1].IntradayIndex > 170000 && ticks[ticks.Count - 1].Time.Day == trades[trades.Count - 1].TimeOpen.Day)
            {
                Tick tick = ticks[ticks.Count - 1];
                AddPremium(new DateTime(tick.Time.Year, tick.Time.Month, tick.Time.Day+1, 17, 00, 00));
            }

            for (int i = 0; i < trades.Count; i++)
            {
                if (!trades[i].IsPremium)
                {
                    trades[i].Trades *= -1 * _ZIM * _contractSize;
                }
            }

            Log("{0}", trades.Count);
        }