예제 #1
0
        public TradeParameters GetTradeParameters(iTrenderMtApiService trenderMtApiService)
        {
            double currentprice = trenderMtApiService.GetCurrentPrice("EURUSD").Result;
            double atr          = trenderMtApiService.GetATR("EURUSD", MtApi.ENUM_TIMEFRAMES.PERIOD_M1, 14, 0).Result;

            return(new TradeParameters("EURUSD", 0.2, 0)
            {
            });
        }
        public async override Task <bool> ExcecuteTask(int TaskID)
        {
            //get parameters for task

            enableTrading = true;
            isTaskRunning = true;
            await _TrenderMtApiService.Connect();

            while (_TrenderMtApiService.GetConnectionState().Result != TrenderConnectionState.Failed)
            {
                int tradeID = 0;
                if (_TrenderMtApiService.GetConnectionState().Result == TrenderConnectionState.Connected)
                {
                    if (_TrenderMtApiService.isTradingEnabled().Result)
                    {
                        var datestring = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();

                        TradeParameters tradeParameters = _TradeService.GetTradeParameters(_TrenderMtApiService);
                        //close qualifying orders
                        //CloseOrders(tradeParameters);

                        TrenderTradeOperation tradeOperation = await _TrenderDowJonesService.GetTradeOperation(_TrenderMtApiService, symbol, timeframes, startpos, count);

                        double atr          = _TrenderMtApiService.GetATR(symbol, timeframes, 14, 0).Result;
                        double currentprice = _TrenderMtApiService.GetCurrentPrice(symbol).Result;
                        double ma           = _TrenderMtApiService.GetMA(symbol, timeframes, period, mashift, mamethod, appliedprice, shift).Result;
                        double takeprofit   = 0.0;
                        double stoploss     = 0.0;
                        TrenderTradeOperation maSentiment = TrenderTradeOperation.OpStayAside;

                        if (ma < currentprice)
                        {
                            maSentiment = TrenderTradeOperation.OpBuy;
                        }
                        else
                        {
                            maSentiment = TrenderTradeOperation.OpSell;
                        }
                        if (tradeOperation != maSentiment)
                        {
                            Console.WriteLine(datestring + " ,cannot trade against the MA/trend.");
                            await _TrenderMtApiService.DisableTrading();

                            continue;
                        }
                        //calculate stop loss and take profit
                        if (tradeOperation == TrenderTradeOperation.OpBuy)
                        {
                            takeprofit = currentprice + atr * 2;
                            stoploss   = currentprice - atr;
                        }
                        else
                        {
                            takeprofit = currentprice - atr * 2;
                            stoploss   = currentprice + atr;
                        }

                        switch (tradeOperation)
                        {
                        case TrenderTradeOperation.OpBuy:
                            var buy = _TrenderMtApiService.GetOrders().Result?.FirstOrDefault(s => s.Operation == TradeOperation.OP_BUY);
                            if (buy == null)
                            {
                                tradeID = await _TrenderMtApiService.OpBuy(tradeParameters.Symbol, tradeParameters.Volume, tradeParameters.Slippage, stoploss, takeprofit);
                            }
                            else
                            {
                                Console.WriteLine(datestring + " : cannot open more than 1 buy trade");
                            }

                            break;

                        case TrenderTradeOperation.OpSell:
                            var sell = _TrenderMtApiService.GetOrders().Result?.FirstOrDefault(s => s.Operation == TradeOperation.OP_SELL);
                            if (sell == null)
                            {
                                tradeID = await _TrenderMtApiService.OpSell(tradeParameters.Symbol, tradeParameters.Volume, tradeParameters.Slippage, stoploss, takeprofit);
                            }
                            else
                            {
                                Console.WriteLine(datestring + " : cannot open more than 1 sell trade");
                            }
                            break;

                        case TrenderTradeOperation.OpStayAside:

                            Console.WriteLine(datestring + " : No Trade:{0}", 0);
                            break;

                        default:
                            break;
                        }
                        await _TrenderMtApiService.DisableTrading();
                    }
                }

                if (!enableTrading)
                {
                    isTaskRunning = false;
                    return(true);
                }
            }
            isTaskRunning = false;
            return(false);
        }