Ejemplo n.º 1
0
        void OpenTrade(FXW fw, string pair, bool buy, int lots, double limit, double stop, Trade masterTrade)
        {
            string serverTradeID            = masterTrade.Id;
            Func <string, bool> tradeExists = id => fw.GetTrade(serverTradeID) != null;

            if (!tradeExists(serverTradeID))
            {
                if (masterTradesPending.Contains(serverTradeID) || masterTradesSynced.Contains(serverTradeID))
                {
                    return;
                }
                masterTradesPending.Add(serverTradeID);
                PendingOrder po = null;
                Action <object, RequestEventArgs> reqiesFailedAction = (s, e) => {
                    if (po != null && e.RequestId == po.RequestId)
                    {
                        masterTradesPending.Remove(serverTradeID);
                        po  = null;
                        Log = new Exception(e.Error);
                    }
                };
                Action <Order> orderRemovedAvtion = order => {
                    var o = order.FixStatus;
                };
                Action <object, ErrorEventArgs> errorAction = (s, e) => {
                    if (serverTradeID == e.Remark)
                    {
                        masterTradesPending.Remove(serverTradeID);
                        po  = null;
                        Log = e.Error;
                    }
                };
                var rfh = new EventHandler <RequestEventArgs>(reqiesFailedAction);
                var orh = new OrderRemovedEventHandler(orderRemovedAvtion);
                var erh = new EventHandler <ErrorEventArgs>(errorAction);
                try {
                    fw.RequestFailed += rfh;
                    fw.OrderRemoved  += orh;
                    fw.Error         += erh;
                    po = fw.FixOrderOpen(pair, buy, lots, limit, stop, serverTradeID);
                    //if (po != null)          pendingTrade.GetUnKnown().ErrorMessage = "Waiting for " + po.RequestId;
                    var done = SpinWait.SpinUntil(
                        () => {
                        Thread.Sleep(100);
                        return(masterTradesPending.Contains(serverTradeID) && po != null && tradeExists(serverTradeID));
                    },
                        TimeSpan.FromSeconds(secondsToWaitForTrade));
                    if (tradeExists(serverTradeID))
                    {
                        masterTradesSynced.Add(serverTradeID);
                    }
                } catch (Exception exc) { Log = exc; } finally {
                    masterTradesPending.Remove(serverTradeID);
                    fw.RequestFailed -= rfh;
                    fw.OrderRemoved  -= orh;
                    fw.Error         -= erh;
                }
            }
            OpenTradeSchedulers.Remove(serverTradeID);
        }
Ejemplo n.º 2
0
        public void GetOrders()
        {
            var    orders = o2g.GetOrders("");
            string orderID = "", tradeID = "";
            var    toc   = o2g.Desk.GetTimeout(o2g.Desk.TIMEOUT_COMMON);
            var    pair  = "USD/JPY";
            var    price = o2g.GetOffers().First(o => o.Pair == pair).Ask;

            o2g.FixOrderOpen("USD/JPY", true, 1000, price + o2g.InPoints(pair, 15), price - o2g.InPoints(pair, 15), "Dimok");
            var t = new Thread(() => Thread.Sleep(5000));

            t.Start();
            t.Join();
            if (tradeID != "")
            {
                o2g.FixOrdersClose(tradeID);
            }
            t = new Thread(() => Thread.Sleep(5000));
            ListCollectionView List = new ListCollectionView(orders);
        }
Ejemplo n.º 3
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;
            }
        }