Example #1
0
 public void FirstInit(double basePrice)
 {
     ErrorsCount = 0;
     BasePrice   = basePrice;
     if (IsDirectionBuy)
     {
         EnterPrice = ExTool.DecreasePrice(BasePrice, EnterDistance);
         ClosePrice = ExTool.IncreasePrice(EnterPrice, CloseDistance);
     }
     else
     {
         EnterPrice = ExTool.IncreasePrice(BasePrice, EnterDistance);
         ClosePrice = ExTool.DecreasePrice(EnterPrice, CloseDistance);
     }
 }
Example #2
0
        private void FirstInit(double avgPrice = -1)
        {
            LState.Reset();

            Ticker ticker = null;

            if (avgPrice == -1)
            {
                var task = Task.Run(() => LParam.Stock.GetMarketPrice(Param.Market, Print));
                ticker = task.Result;
            }

            if (LParam.IsLong)
            {
                double curPrice = avgPrice == -1 ? (double)ticker.Bid : avgPrice;
                LState.EnterLongPrice = ExTool.DecreasePrice(curPrice, LParam.EnterDistance);
            }

            if (LParam.IsShort)
            {
                double curPrice = avgPrice == -1 ? (double)ticker.Ask : avgPrice;
                LState.EnterShortPrice = ExTool.IncreasePrice(curPrice, LParam.EnterDistance);
            }
        }
Example #3
0
        private void OnUpdateTicker(Ticker ticker)
        {
            lock (LockLTicker)
            {
                LastTicker = ticker;
            }

            if (LState.PosDirection == 0 && LParam.IsLong && (double)ticker.Ask < LState.EnterLongPrice)   //enter Long
            {
                var tresult = CStrategyTrade.ExecuteByMarket(true, -1).Result;
                if (tresult != null)
                {
                    LState.PosDirection     = 1;
                    LState.ClosePositionSum = (double)tresult.BaseQty;
                    double avgPrice = (double)tresult.AveragePrice;
                    LState.ClosePositionPrice = ExTool.IncreasePrice(avgPrice, LParam.CloseDistance);
                    LState.StopPositionPrice  = ExTool.DecreasePrice(avgPrice, LParam.StopDistance);

                    Print(String.Format("Выполнено вход в позицию Long на суму: {0} {1} по цене: {2}. ({3} {4})",
                                        tresult.BaseQty, LParam.Market.BaseCurrency, avgPrice,
                                        tresult.MarketQty, LParam.Market.MarketCurrency));
                }
            }

            if (LState.PosDirection == 0 && LParam.IsShort && (double)ticker.Bid > LState.EnterLongPrice)  //enter Short
            {
                var tresult = CStrategyTrade.ExecuteByMarket(false, -1).Result;
                if (tresult != null)
                {
                    LState.PosDirection     = -1;
                    LState.ClosePositionSum = (double)tresult.BaseQty;
                    double avgPrice = (double)tresult.AveragePrice;
                    LState.ClosePositionPrice = ExTool.DecreasePrice(avgPrice, LParam.CloseDistance);
                    LState.StopPositionPrice  = ExTool.IncreasePrice(avgPrice, LParam.StopDistance);

                    Print(String.Format("Выполнено вход в позицию Short на суму: {0} {1} по цене: {2}. ({3} {4})",
                                        tresult.BaseQty, LParam.Market.BaseCurrency, avgPrice,
                                        tresult.MarketQty, LParam.Market.MarketCurrency));
                }
            }

            if (LState.PosDirection == 1 && (double)ticker.Bid > LState.ClosePositionPrice)     //close Long
            {
                var tresult = CStrategyTrade.ExecuteByMarket(false, LState.ClosePositionSum).Result;
                if (tresult != null)
                {
                    double avgPrice = (double)tresult.AveragePrice;
                    FirstInit(avgPrice);

                    Print(String.Format("Позиция Long закрылась на суму: {0} {1} по цене: {2}. ({3} {4})",
                                        tresult.BaseQty, LParam.Market.BaseCurrency, avgPrice,
                                        tresult.MarketQty, LParam.Market.MarketCurrency));
                }
            }

            if (LState.PosDirection == 1 && (double)ticker.Ask < LState.ClosePositionPrice)    //close Short
            {
                var tresult = CStrategyTrade.ExecuteByMarket(true, LState.ClosePositionSum).Result;
                if (tresult != null)
                {
                    double avgPrice = (double)tresult.AveragePrice;
                    FirstInit(avgPrice);

                    Print(String.Format("Позиция Short закрылась на суму: {0} {1} по цене: {2}. ({3} {4})",
                                        tresult.BaseQty, LParam.Market.BaseCurrency, avgPrice,
                                        tresult.MarketQty, LParam.Market.MarketCurrency));
                }
            }

            if (LState.PosDirection != 0 && LState.StopPositionPrice != 0)   //check Stop
            {
                if (LState.PosDirection == 1)
                {
                    if ((double)ticker.Ask < LState.ClosePositionPrice)  //close Long by stop
                    {
                        var tresult = CStrategyTrade.ExecuteByMarket(false, LState.ClosePositionSum).Result;
                        if (tresult != null)
                        {
                            double avgPrice = (double)tresult.AveragePrice;
                            FirstInit(avgPrice);

                            Print(String.Format("Позиция Long закрылась по стопу на суму: {0} {1} по цене: {2}. ({3} {4})",
                                                tresult.BaseQty, LParam.Market.BaseCurrency, avgPrice,
                                                tresult.MarketQty, LParam.Market.MarketCurrency));
                        }
                    }
                }

                if (LState.PosDirection == -1)
                {
                    if ((double)ticker.Bid > LState.ClosePositionPrice)  //close Short by stop
                    {
                        var tresult = CStrategyTrade.ExecuteByMarket(true, LState.ClosePositionSum).Result;
                        if (tresult != null)
                        {
                            double avgPrice = (double)tresult.AveragePrice;
                            FirstInit(avgPrice);

                            Print(String.Format("Позиция Short закрылась по стопу на суму: {0} {1} по цене: {2}. ({3} {4})",
                                                tresult.BaseQty, LParam.Market.BaseCurrency, avgPrice,
                                                tresult.MarketQty, LParam.Market.MarketCurrency));
                        }
                    }
                }
            }
        }