Beispiel #1
0
        private bool CanRebalance()
        {
            MT4Ctrl1.GetAccountInfomation();
            MT4Ctrl2.GetAccountInfomation();

            if (MT4Ctrl1.AccountMargin == 0 && MT4Ctrl2.AccountMargin == 0)
            {
                if (MT4Ctrl1.AccountEquity != MT4Ctrl2.AccountEquity)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            double dMarginPercent1 = MT4Ctrl1.AccountEquity / MT4Ctrl1.AccountMargin * 100;
            double dMarginPercent2 = MT4Ctrl2.AccountEquity / MT4Ctrl2.AccountMargin * 100;

            if (dMarginPercent1 <= dLowLimitMargin || dMarginPercent2 <= dLowLimitMargin)
            {
                return(true);
            }

            return(false);
        }
Beispiel #2
0
 private void m_timerUpdateQuote_Tick(object sender, EventArgs e)
 {
     MT4Ctrl1.GetAccountInfomation();
     MT4Ctrl1.GetOpenOrders();
     MT4Ctrl2.GetAccountInfomation();
     MT4Ctrl2.GetOpenOrders();
     UpdatedCurrentQuote();
 }
Beispiel #3
0
        private void DoOpenOrder()
        {
            MT4Ctrl1.GetAccountInfomation();
            MT4Ctrl2.GetAccountInfomation();
            int    nAccountLeverage1         = MT4Ctrl1.GetAccountLeverage();
            int    nAccountLeverage2         = MT4Ctrl2.GetAccountLeverage();
            double dSymbolTradeContractSize1 = MT4Ctrl1.GetSymbolTradeContractSize(strCurrentSymbol);
            double dSymbolTradeContractSize2 = MT4Ctrl2.GetSymbolTradeContractSize(strCurrentSymbol);

            if (nAccountLeverage1 == 0 || nAccountLeverage2 == 0 || dSymbolTradeContractSize1 == 0 || dSymbolTradeContractSize2 == 0)
            {
                return;
            }

            double dMarginPerLot1 = dSymbolTradeContractSize1 / (double)nAccountLeverage1 + MT4Ctrl1.dCommissionPerLot;
            double dMarginPerLot2 = dSymbolTradeContractSize2 / (double)nAccountLeverage2 + MT4Ctrl2.dCommissionPerLot;

            double dAvailableMarginToOpen1, dAvailableMarginToOpen2;

            //Get AvailableMarginToOpen of MT4-1
            if (MT4Ctrl1.AccountMargin == 0)
            {
                dAvailableMarginToOpen1 = MT4Ctrl1.AccountEquity / dMarginTarget * 100;
            }
            else
            {
                double dMarginPercent1 = MT4Ctrl1.AccountEquity / MT4Ctrl1.AccountMargin * 100;
                if (dMarginPercent1 <= dMarginTarget)
                {
                    return;
                }

                double dAvailableMarginToOpenPercent1 = dMarginPercent1 - dMarginTarget;
                dAvailableMarginToOpen1 = MT4Ctrl1.AccountEquity * dAvailableMarginToOpenPercent1;
            }
            //Get AvailableMarginToOpen of MT4-2
            if (MT4Ctrl2.AccountMargin == 0)
            {
                dAvailableMarginToOpen2 = MT4Ctrl2.AccountEquity / dMarginTarget * 100;
            }
            else
            {
                double dMarginPercent2 = MT4Ctrl2.AccountEquity / MT4Ctrl2.AccountMargin * 100;
                if (dMarginPercent2 <= dMarginTarget)
                {
                    return;
                }

                double dAvailableMarginToOpenPercent2 = dMarginPercent2 - dMarginTarget;
                dAvailableMarginToOpen2 = MT4Ctrl1.AccountEquity * dAvailableMarginToOpenPercent2;
            }

            double dLotToOpen1 = dAvailableMarginToOpen1 / dMarginPerLot1;
            double dLotToOpen2 = dAvailableMarginToOpen2 / dMarginPerLot2;
            double dLotToOpen  = 0;

            if (dLotToOpen1 < dLotToOpen2)
            {
                dLotToOpen = dLotToOpen1 / nSplitCnt;
            }
            else
            {
                dLotToOpen = dLotToOpen2 / nSplitCnt;
            }

            dLotToOpen = (double)Math.Round(dLotToOpen * 100f) / 100f;


            //Open Order
            if ((MT4Ctrl1.bIsWithSwap && MT4Ctrl2.bIsWithSwap) || (MT4Ctrl1.bIsWithSwap && MT4Ctrl2.bIsWithSwap))
            {
                m_lblState.Text = "Two Account are not Matched!";
                return;
            }

            if (MT4Ctrl1.bIsWithSwap && !MT4Ctrl2.bIsWithSwap)
            {
                if (MT4Ctrl1.dSwapBuy > 0 && MT4Ctrl1.dSwapSell < 0)
                {
                    for (int i = 0; i < nSplitCnt; i++)
                    {
                        int nMagicCode = DateTime.Now.Millisecond;
                        MT4Ctrl1.OpenOrder(strCurrentSymbol, TradeOperation.OP_BUY, dLotToOpen, nSlippage1, nMagicCode);
                        MT4Ctrl2.OpenOrder(strCurrentSymbol, TradeOperation.OP_SELL, dLotToOpen, nSlippage2, nMagicCode);
                    }
                }
                else
                {
                    for (int i = 0; i < nSplitCnt; i++)
                    {
                        int nMagicCode = DateTime.Now.Millisecond;
                        MT4Ctrl1.OpenOrder(strCurrentSymbol, TradeOperation.OP_SELL, dLotToOpen, nSlippage1, nMagicCode);
                        MT4Ctrl2.OpenOrder(strCurrentSymbol, TradeOperation.OP_BUY, dLotToOpen, nSlippage2, nMagicCode);
                    }
                }
            }

            if (!MT4Ctrl1.bIsWithSwap && MT4Ctrl2.bIsWithSwap)
            {
                if (MT4Ctrl2.dSwapBuy > 0 && MT4Ctrl2.dSwapSell < 0)
                {
                    for (int i = 0; i < nSplitCnt; i++)
                    {
                        int nMagicCode = DateTime.Now.Millisecond;
                        MT4Ctrl1.OpenOrder(strCurrentSymbol, TradeOperation.OP_SELL, dLotToOpen, nSlippage1, nMagicCode);
                        MT4Ctrl2.OpenOrder(strCurrentSymbol, TradeOperation.OP_BUY, dLotToOpen, nSlippage2, nMagicCode);
                    }
                }
                else
                {
                    for (int i = 0; i < nSplitCnt; i++)
                    {
                        int nMagicCode = DateTime.Now.Millisecond;
                        MT4Ctrl1.OpenOrder(strCurrentSymbol, TradeOperation.OP_BUY, dLotToOpen, nSlippage1, nMagicCode);
                        MT4Ctrl2.OpenOrder(strCurrentSymbol, TradeOperation.OP_SELL, dLotToOpen, nSlippage2, nMagicCode);
                    }
                }
            }
        }
Beispiel #4
0
        private async void TryReBalance()
        {
            if (!MT4Ctrl1.bAvailable || !MT4Ctrl2.bAvailable || MT4Ctrl1.bDemoAccount || MT4Ctrl2.bDemoAccount)
            {
                return;
            }

            MT4Ctrl1.GetAccountInfomation();
            MT4Ctrl2.GetAccountInfomation();

            double dNewBalance = (MT4Ctrl1.AccountEquity + MT4Ctrl2.AccountEquity) / 2;
            double dAmount     = MT4Ctrl1.AccountEquity - dNewBalance;

            if (dAmount > 0)
            {
                accountFrom = MT4Ctrl1.AccountNumber.ToString();
                accountTo   = MT4Ctrl2.AccountNumber.ToString();
            }
            else
            {
                accountFrom = MT4Ctrl2.AccountNumber.ToString();
                accountTo   = MT4Ctrl1.AccountNumber.ToString();
            }
            dAmount = Math.Abs(dAmount);
            dAmount = (double)Math.Round(dAmount * 100f) / 100f;


            RunOnUiThread(() =>
            {
                m_lblState.Text = "Rebalancing Now ...";
            });

            var result = await Execute(() => AccountRebalancer.startRebalance(
                                           10, UAGTRADE_Username, UAGTRADE_Password, accountFrom, accountTo, nWithrawRetryCount, nDepositeRetryCount));

            string strState = "";

            if (result == 0)
            {
                strState = "Rebalancing Success!";
            }
            else if (result == -1)
            {
                strState = "Login Failed!";
            }
            else if (result == -2)
            {
                strState = "MT4 To Wallet Failed!";
            }
            else if (result == -3)
            {
                strState = "Wallet To MT4 Failed!";
            }
            else if (result == -4)
            {
                strState = "Error Happend!";
            }

            if (result == -5)
            {
                strState = "";
            }
            else
            {
                strState += "   ( WithrawRetryCount:" + AccountRebalancer.GetWithrawRetryCnt().ToString() + "     DepositeRetryCount:" + AccountRebalancer.GetDepositeCnt().ToString() +
                            "     DateTime:" + DateTime.Now.ToString() + "     Period:" + AccountRebalancer.GetPeriod().ToString() + "s )";
            }

            RunOnUiThread(() =>
            {
                m_lblState.Text = strState;
            });
        }