public void OnTick(Tick tick, int signal, bool isClosePos, double riskLev)
        {
            PerformanceTick pTick = new PerformanceTick()
                              {
                                Time = tick.Time,
                                Last = tick.Last,
                                Balance = _balance,
                                Equity = _balance,
                                IsBalanceUpdated = false,
                                CurrentSignal = _currPosSignal,
                                CurrentPosOpenRate = _posOpenRate,
                                CurrentPosOpenTime = _posOpenTime,
                                IsPosClosed = false
                              };

              if (_currPosSignal.HasValue)
              {
            int dollar = _currRiskLev.HasValue && _currRiskLev > 0 ? Convert.ToInt32(30 / (_currRiskLev * 4.5)) : _lotSize;
            //int dollar = riskLev > 0 ? Convert.ToInt32(30 / (riskLev * 6)) : _lotSize;
            int size = (int) (dollar/1000)*1000;
            double profit = _calcProfit(_currPosSignal.Value, size, _posOpenRate,
              _currPosSignal == -1 ? tick.Ask : tick.Bid);
            double margin = _lotSize/(_currPosSignal == 1 ? _posOpenRate : 1)/_leverage;

            var equityOnTick = _balance + profit + margin;
            pTick.Equity = equityOnTick;

            //close current position
            if (isClosePos)
            {
              _balance = equityOnTick;
              pTick.Balance = _balance;
              pTick.IsBalanceUpdated = true;
              pTick.IsPosClosed = true;

              _profits.Add(profit);
              _updatePerformanceSummary(profit, tick);
              DispatcherHelper.CheckBeginInvokeOnUI(() => Messenger.Default.Send(_performanceSummary));

              _currPosSignal = null;
              _currRiskLev = null;
              _posOpenRate = -1;
              _posOpenTime = null;
            }
              }
              else
              {
            //open new position
            if (Math.Abs(signal) == 1)
            {
              _posOpenRate = signal == 1 ? tick.Ask : tick.Bid;
              _posOpenTime = tick.Time;
              _currPosSignal = signal;
              _currRiskLev = riskLev;

              double margin = _lotSize/(_currPosSignal == 1 ? _posOpenRate : 1)/_leverage;
              _balance = _balance - margin;
              pTick.Balance = _balance;
              pTick.IsBalanceUpdated = true;
            }
              }

              //only update graph each x hours
              if (pTick.Time.Minute == 0 || pTick.Time.Hour % 4 == 0)
            DispatcherHelper.CheckBeginInvokeOnUI(() => Messenger.Default.Send(pTick));
        }
        public void OnTick(Tick tick, int signal, bool isClosePos, double riskLev)
        {
            PerformanceTick pTick = new PerformanceTick()
            {
                Time               = tick.Time,
                Last               = tick.Last,
                Balance            = _balance,
                Equity             = _balance,
                IsBalanceUpdated   = false,
                CurrentSignal      = _currPosSignal,
                CurrentPosOpenRate = _posOpenRate,
                CurrentPosOpenTime = _posOpenTime,
                IsPosClosed        = false
            };

            if (_currPosSignal.HasValue)
            {
                int dollar = _currRiskLev.HasValue && _currRiskLev > 0 ? Convert.ToInt32(30 / (_currRiskLev * 4.5)) : _lotSize;
                //int dollar = riskLev > 0 ? Convert.ToInt32(30 / (riskLev * 6)) : _lotSize;
                int    size   = (int)(dollar / 1000) * 1000;
                double profit = _calcProfit(_currPosSignal.Value, size, _posOpenRate,
                                            _currPosSignal == -1 ? tick.Ask : tick.Bid);
                double margin = _lotSize / (_currPosSignal == 1 ? _posOpenRate : 1) / _leverage;

                var equityOnTick = _balance + profit + margin;
                pTick.Equity = equityOnTick;

                //close current position
                if (isClosePos)
                {
                    _balance               = equityOnTick;
                    pTick.Balance          = _balance;
                    pTick.IsBalanceUpdated = true;
                    pTick.IsPosClosed      = true;

                    _profits.Add(profit);
                    _updatePerformanceSummary(profit, tick);
                    DispatcherHelper.CheckBeginInvokeOnUI(() => Messenger.Default.Send(_performanceSummary));

                    _currPosSignal = null;
                    _currRiskLev   = null;
                    _posOpenRate   = -1;
                    _posOpenTime   = null;
                }
            }
            else
            {
                //open new position
                if (Math.Abs(signal) == 1)
                {
                    _posOpenRate   = signal == 1 ? tick.Ask : tick.Bid;
                    _posOpenTime   = tick.Time;
                    _currPosSignal = signal;
                    _currRiskLev   = riskLev;

                    double margin = _lotSize / (_currPosSignal == 1 ? _posOpenRate : 1) / _leverage;
                    _balance               = _balance - margin;
                    pTick.Balance          = _balance;
                    pTick.IsBalanceUpdated = true;
                }
            }

            //only update graph each x hours
            if (pTick.Time.Minute == 0 || pTick.Time.Hour % 4 == 0)
            {
                DispatcherHelper.CheckBeginInvokeOnUI(() => Messenger.Default.Send(pTick));
            }
        }