Exemplo n.º 1
0
        private void ProcessPrice(Price Price)
        {
            try {
                RaisePropertyChanged(() => DensityAverage);
                if (Visibility == Visibility.Hidden)
                {
                    return;
                }
                var digits = fw.GetDigits(pair);
                ShowSpread(Price);

                var account       = fw.GetAccount();
                var usableMargin  = account.UsableMargin;
                var avalibleTotal = usableMargin * Leverage;
                AmountToBuy  = TradesManagerStatic.GetLotstoTrade(account.Balance, Leverage, lotsToBuyRatio, fw.MinimumQuantity);
                AmountToSell = TradesManagerStatic.GetLotstoTrade(account.Balance, Leverage, lotsToSellRatio, fw.MinimumQuantity);
                var summary = fw.GetSummary() ?? new Order2GoAddIn.Summary();
                ShowAccount(account, summary);
                ShowSummary(summary, account);

                #region Trade (Open)
                var canBuy = chartingWindow.GoBuy && !chartingWindow.CloseBuy &&
                             (chartingWindow.CanTrade || summary.BuyLots > 0) &&
                             (Lib.GetChecked(chkCanBuy).Value || summary.BuyLots > 0);
                var canSell = chartingWindow.GoSell && !chartingWindow.CloseSell &&
                              (chartingWindow.CanTrade || summary.SellLots > 0) &&
                              (Lib.GetChecked(chkCanSell).Value || summary.SellLots > 0);
                if ((isAutoPilot || isAutoAdjust || summary.SellPositions + summary.BuyPositions > 0))
                {
                    var buyTradeDelta = tradeDelta * chartingWindow.DencityRatio;// *averageProfitCMA30_Sell / (averageProfitCMA30_Sell + averageProfitCMA30_Buy);
                    var takeProfitBuy = summary == null || summary.BuyPositions == 0 || chartingWindow.TakeProfitBuy < 0 ? chartingWindow.TakeProfitBuy
            : -chartingWindow.TakeProfitNet(chartingWindow.TakeProfitBuy, summary, true);
                    if ((isAutoPilot || summary.BuyPositions > 0) && canBuy /*&& lots > 0 && fw.CanTrade(true, buyTradeDelta)*/)
                    {
                        try {
                            var lots = chartingWindow.LotsToTradeBuy > 1000 ? chartingWindow.LotsToTradeBuy : chartingWindow.LotsToTradeBuy * AmountToBuy;
                            var l    = fw.CanTrade2(true, buyTradeDelta, lots, tradeDistanceUnisex);
                            lots = chartingWindow.LotsToTradeBuy > 1 && l < lots ? 0 : l;
                            if (lots > 0)
                            {
                                fw.FixOrderOpen(true, lots, takeProfitBuy, chartingWindow.StopLossBuy, chartingWindow.TradeInfo.ToString());
                            }
                        } catch (Order2GoAddIn.FXCoreWrapper.OrderExecutionException exc) {
                            Log = exc;
                        }
                    }
                    var sellTradeDelta = tradeDelta * chartingWindow.DencityRatio;// *averageProfitCMA30_Buy / (averageProfitCMA30_Sell + averageProfitCMA30_Buy);
                    var takeProfitSell = summary == null || summary.SellPositions == 0 || chartingWindow.TakeProfitSell < 0 ? chartingWindow.TakeProfitSell
            : -chartingWindow.TakeProfitNet(chartingWindow.TakeProfitSell, summary, false);
                    if ((isAutoPilot || summary.SellPositions > 0) && canSell /* && lots > 0 && fw.CanTrade(false, sellTradeDelta)*/)
                    {
                        try {
                            var lots = chartingWindow.LotsToTradeSell > 1000 ? chartingWindow.LotsToTradeSell : chartingWindow.LotsToTradeSell * AmountToSell;
                            var l    = fw.CanTrade2(false, sellTradeDelta, lots, tradeDistanceUnisex);
                            lots = chartingWindow.LotsToTradeSell > 1 && l < lots ? 0 : l;
                            if (lots > 0)
                            {
                                fw.FixOrderOpen(false, lots, takeProfitSell, chartingWindow.StopLossSell, chartingWindow.TradeInfo.ToString());
                            }
                        } catch (Order2GoAddIn.FXCoreWrapper.OrderExecutionException exc) {
                            Log = exc;
                        }
                    }

                    if (isAutoAdjust)
                    {
                        var stats = new DispatcherOperationStatus[] { DispatcherOperationStatus.Executing, DispatcherOperationStatus.Pending };
                        if (summary != null && summary.BuyPositions > 0 && takeProfitBuy > 0)
                        {
                            fw.FixOrderSetNetLimits(Math.Abs(takeProfitBuy), true);
                            //if( setLimitThreadBuy != null )
                            //  setLimitThreadBuy.Abort();
                            //setLimitThreadBuy = new Thread(delegate() { fw.FixOrder_SetNetLimits(Math.Abs(takeProfitBuy), true); }) { Priority = ThreadPriority.Lowest };
                            //setLimitThreadBuy.Start();
                        }
                        if (summary != null && summary.SellPositions > 0 && takeProfitSell > 0)
                        {
                            fw.FixOrderSetNetLimits(Math.Abs(takeProfitSell), false);
                            //if (setLimitThreadSell != null)
                            //  setLimitThreadSell.Abort();
                            //setLimitThreadSell = new Thread(delegate() { fw.FixOrder_SetNetLimits(Math.Abs(takeProfitSell), false); }) { Priority = ThreadPriority.Lowest };
                            //setLimitThreadSell.Start();
                        }
                    }
                }
                #endregion
            } catch (ThreadAbortException) {
            } catch (Exception exc) {
                Log = exc;
            }
        }