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));
        }
        private void _updatePerformanceSummary(double profit, Tick tick)
        {
            _performanceSummary.Profit += profit;

              if (profit >= 0)
              {
            _performanceSummary.TotalWin += profit;
            if (profit > _performanceSummary.MaxWin)
              _performanceSummary.MaxWin = profit;

            _performanceSummary.NoWinPos++;
            if (_currPosSignal.Value == 1)
              _performanceSummary.NoWinLong++;
            else
              _performanceSummary.NoWinShort++;
              }
              else
              {
            _performanceSummary.NoLossPos++;
            _performanceSummary.TotalLoss += profit;
            if (profit < _performanceSummary.MaxLoss)
              _performanceSummary.MaxLoss = profit;
              }

              if (_currPosSignal.Value == 1)
            _performanceSummary.NoLong++;
              else
            _performanceSummary.NoShort++;

              _performanceSummary.TotalHoldSec += (tick.Time - _posOpenTime).Value.TotalSeconds;

              _performanceSummary.SharpeR = _calcSharpe();
        }
Example #3
0
 public int OnTick(Tick t, out bool isClosePos, out double risk, double sl)
 {
     return(_process_tick(_algo_p, t.TimeStr, t.Ask, t.Bid, t.Last,
                          (IntPtr)t.Volume, sl, out isClosePos, out risk,
                          _callbackInstance));
 }
        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 int OnTick(Tick t, out bool isClosePos, out double risk, double sl)
 {
   return _process_tick(_algo_p, t.TimeStr, t.Ask, t.Bid, t.Last,
     (IntPtr)t.Volume, sl, out isClosePos, out risk,
     _callbackInstance);
 }