using System; using System.Collections.Generic; using System.Linq; using System.Text; using TSLab.Script; using TSLab.Script.Handlers; using TSLab.Script.Optimization; using TSLab.Script.Helpers; using TSLab.Script.Realtime; namespace TSLab.Script.Handlers { [HandlerName("PosSize")] [HandlerCategory("Indicators")] public class PosSize : BasePeriodIndicatorHandler { #region Handlers public TSLab.Script.Handlers.EstimatedMoney DepoLocal_h = new TSLab.Script.Handlers.EstimatedMoney(); private TSLab.Script.Handlers.ConstGen Margin_h = new TSLab.Script.Handlers.ConstGen(); private TSLab.Script.Handlers.ConstGen GO_h = new TSLab.Script.Handlers.ConstGen(); private TSLab.Script.Handlers.ConstGen Part_h = new TSLab.Script.Handlers.ConstGen(); private TSLab.Script.Handlers.ConstGen SKP_h = new TSLab.Script.Handlers.ConstGen(); private TSLab.Script.Handlers.ConstGen SP_h = new TSLab.Script.Handlers.ConstGen(); private TSLab.Script.Handlers.ConstGen Tolerance_h = new TSLab.Script.Handlers.ConstGen(); private TSLab.Script.Handlers.ConstGen StepSize_h = new TSLab.Script.Handlers.ConstGen(); private TSLab.Script.Handlers.ConstGen StepPrise_h = new TSLab.Script.Handlers.ConstGen(); #endregion public IList Execute(IContext ctx, ISecurity source, double SP_in, double SKP_in, double GO_in, double Part_in, double PriseTick_in, double PriseStepTick_in, bool isLong) { #region Переменные double LongPosSize = 0; //Размер позиции для Long double ShortPosSize = 0; //Размер позиции для Long double CurentDepoLocal; //Депозит текущий double CurentPrice = 0; //Текущая стоимость int barsCount = source.Bars.Count; //IList list = new double[barsCount]; var secRt = source as ISecurityRt; //Данные в реалтайм //Входные данные //Средняя прибыльность в день this.SP_h.Value = SP_in; // Make 'SP' item data System.Collections.Generic.IList SP = ctx.GetData("SP", new string[] { this.SP_h.Value.ToString(), source.Symbol }, delegate { return this.SP_h.Execute(ctx); }); //Средняя квадрата прибыльности this.SKP_h.Value = SKP_in; // Make 'SKP' item data System.Collections.Generic.IList SKP = ctx.GetData("SKP", new string[] { this.SKP_h.Value.ToString(), source.Symbol }, delegate { return this.SKP_h.Execute(ctx); }); //Доля гарантийного обеспечения в рублях if (secRt != null) { //Если реалтайм this.GO_h.Value = (double)secRt.FinInfo.BuyDeposit; //Гарантийное обеспечение фьючерса this.StepSize_h.Value = source.Tick; //Шаг изменения цены в пунктах this.StepPrise_h.Value = (double)source.FinInfo.StepPrice; //Стоимость одного шага цены в рублях } else { //Если симуляция this.GO_h.Value = GO_in; //Гарантийное обеспечение фьючерса this.StepSize_h.Value = PriseTick_in; //Шаг изменения цены в пунктах this.StepPrise_h.Value = (double)PriseStepTick_in; //Стоимость одного шага цены в рублях } // Make 'GO' item data System.Collections.Generic.IList GO = ctx.GetData("GO", new string[] { this.GO_h.Value.ToString(), source.Symbol }, delegate { return this.GO_h.Execute(ctx); }); //Доля системы в капитале this.Part_h.Value = Part_in; // Make 'Part' item data System.Collections.Generic.IList Part = ctx.GetData("Part", new string[] { this.Part_h.Value.ToString(), source.Symbol }, delegate { return this.Part_h.Execute(ctx); }); //Предел когда допускается округлять число лотов вверх // Initialize 'Tolerance' item this.Tolerance_h.Value = 0.1D; // Make 'Tolerance' item data System.Collections.Generic.IList Tolerance = ctx.GetData("Tolerance", new string[] { this.Tolerance_h.Value.ToString(), source.Symbol }, delegate { return this.Tolerance_h.Execute(ctx); }); #endregion #region Вычисление размера позиции //if(secRt.Positions.IsRealtime==true) //source.Positions.IsRealtime!=true //Достпуное плече this.Margin_h.Value = source.Margin; System.Collections.Generic.IList Margin = ctx.GetData("Margin", new string[] { this.Margin_h.Value.ToString(), source.Symbol }, delegate { return this.Margin_h.Execute(ctx); }); //Шаг изменения цены в пунктах // Make 'StepSize' item data System.Collections.Generic.IList StepSize = ctx.GetData("StepSize", new string[] { this.StepSize_h.Value.ToString(), source.Symbol }, delegate { return this.StepSize_h.Execute(ctx); }); //Стоимость одного шага цены в рублях // Make 'StepPrise' item data System.Collections.Generic.IList StepPrise = ctx.GetData("StepPrise", new string[] { this.StepPrise_h.Value.ToString(), source.Symbol }, delegate { return this.StepPrise_h.Execute(ctx); }); // IList nDepo = new List(0); IList nLongPosSize = new List(0); IList nShortPosSize = new List(0); // Make 'PunktPrise' item data System.Collections.Generic.IList PunktPrise = new double[barsCount]; // Make 'CurentPriseRub' item data System.Collections.Generic.IList CurentPriseRub = new double[barsCount]; // Make 'KelliLong' item data System.Collections.Generic.IList KelliLong = new double[barsCount]; // Make 'KelliShort' item data System.Collections.Generic.IList KelliShort = new double[barsCount]; // Make 'HalfKelliLong' item data System.Collections.Generic.IList HalfKelliLong = new double[barsCount]; // Make 'HalfKelliShort' item data System.Collections.Generic.IList HalfKelliShort = new double[barsCount]; // Make 'HalfKelliLongGO' item data System.Collections.Generic.IList HalfKelliLongGO = new double[barsCount]; // Make 'HalfKelliShortGO' item data System.Collections.Generic.IList HalfKelliShortGO = new double[barsCount]; // Make 'MarginLong' item data System.Collections.Generic.IList MarginLong = new double[barsCount]; // Make 'MarginShort' item data System.Collections.Generic.IList MarginShort = new double[barsCount]; for (int bar = 0; bar < barsCount; bar++) { if (secRt != null) { //Если реалтайм CurentDepoLocal = (double)secRt.EstimatedBalance; // Чистый баланс. Все обязательства учтены. (Колонка чистая стоимость) } else { //Если симуляция CurentDepoLocal = this.DepoLocal_h.Execute(source, bar);//Депозит текущий } CurentPrice = source.ClosePrices[bar]; //Текущая стоимость //nDepo.Add(CurentDepoLocal); PunktPrise[bar] = StepPrise[bar] / StepSize[bar]; CurentPriseRub[bar] = CurentPrice * PunktPrise[bar]; KelliLong[bar] = (100 * SP[bar] / SKP[bar]) + 0.5; KelliShort[bar] = (100 * SP[bar] / SKP[bar]) - 0.5; HalfKelliLong[bar] = KelliLong[bar] / 2; HalfKelliShort[bar] = KelliShort[bar] / 2; HalfKelliLongGO[bar] = HalfKelliLong[bar] / (CurentPriseRub[bar] / GO[bar]); HalfKelliShortGO[bar] = HalfKelliShort[bar] / (CurentPriseRub[bar] / GO[bar]); MarginLong[bar] = Math.Min(Margin[bar] + 1, HalfKelliLongGO[bar]); MarginShort[bar] = Math.Min(Margin[bar], HalfKelliShortGO[bar]); double tempL = CurentDepoLocal * Part[bar] * MarginLong[bar] / GO[bar]; double tempS = CurentDepoLocal * Part[bar] * MarginShort[bar] / GO[bar]; LongPosSize = (Math.Ceiling(tempL) - (tempL) < Tolerance[bar]) ? Math.Max(Math.Ceiling(tempL), 1) : Math.Max(Math.Truncate(tempL), 1); nLongPosSize.Add(LongPosSize); ShortPosSize = (Math.Ceiling(tempS) - (tempS) < Tolerance[bar]) ? Math.Max(Math.Ceiling(tempS), 1) : Math.Max(Math.Truncate(tempS), 1); nShortPosSize.Add(ShortPosSize); ctx.Log(bar.ToString() + ") CurentPrice:" + CurentPrice.ToString() + ", CurentDepoLocal:" + CurentDepoLocal.ToString() + ", HalfKelliLong:" + HalfKelliLong[bar].ToString() + ", HalfKelliLongGO:" + HalfKelliLongGO[bar].ToString() + ", MarginLong:" + MarginLong[bar].ToString() + ", tempL:" + tempL.ToString(), new Color()); } #endregion if (isLong) { return nLongPosSize; } else { return nShortPosSize; } } } [HandlerName("PosSizeA")] [HandlerCategory("Indicators")] public class PosSizeA : TSLab.Script.Handlers.IContextUses, TSLab.Script.Handlers.IValuesHandlerWithPrecalc, TSLab.Script.Handlers.IDoubleReturns { private TSLab.Script.Handlers.IContext context; #region Handlers public TSLab.Script.Handlers.EstimatedMoney DepoLocal_h = new TSLab.Script.Handlers.EstimatedMoney(); private TSLab.Script.Handlers.ConstGen Margin_h = new TSLab.Script.Handlers.ConstGen(); private TSLab.Script.Handlers.ConstGen GO_h = new TSLab.Script.Handlers.ConstGen(); private TSLab.Script.Handlers.ConstGen Part_h = new TSLab.Script.Handlers.ConstGen(); private TSLab.Script.Handlers.ConstGen SKP_h = new TSLab.Script.Handlers.ConstGen(); private TSLab.Script.Handlers.ConstGen SP_h = new TSLab.Script.Handlers.ConstGen(); private TSLab.Script.Handlers.ConstGen Tolerance_h = new TSLab.Script.Handlers.ConstGen(); private TSLab.Script.Handlers.ConstGen StepSize_h = new TSLab.Script.Handlers.ConstGen(); private TSLab.Script.Handlers.ConstGen StepPrise_h = new TSLab.Script.Handlers.ConstGen(); private TSLab.Script.Handlers.EstimatedMoney Depo_h = new TSLab.Script.Handlers.EstimatedMoney(); #endregion public TSLab.Script.Handlers.IContext Context { get { return this.context; } set { this.context = value; } } public double Execute(IContext ctx, ISecurity source, double SP_in, double SKP_in, double GO_in, double Part_in, double PriseTick_in, double PriseStepTick_in, bool isLong, int i) { #region Переменные double LongPosSize = 0; //Размер позиции для Long double ShortPosSize = 0; //Размер позиции для Long double CurentDepoLocal; //Депозит текущий double CurentPrice = 0; //Текущая стоимость int barsCount = source.Bars.Count; //IList list = new double[barsCount]; var secRt = source as ISecurityRt; //Данные в реалтайм //Входные данные //Средняя прибыльность в день this.SP_h.Value = SP_in; // Make 'SP' item data System.Collections.Generic.IList SP = ctx.GetData("SP", new string[] { this.SP_h.Value.ToString(), source.Symbol }, delegate { return this.SP_h.Execute(ctx); }); //Средняя квадрата прибыльности this.SKP_h.Value = SKP_in; // Make 'SKP' item data System.Collections.Generic.IList SKP = ctx.GetData("SKP", new string[] { this.SKP_h.Value.ToString(), source.Symbol }, delegate { return this.SKP_h.Execute(ctx); }); //Доля гарантийного обеспечения в рублях if (secRt != null) { //Если реалтайм this.GO_h.Value = (double)secRt.FinInfo.BuyDeposit; //Гарантийное обеспечение фьючерса this.StepSize_h.Value = source.Tick; //Шаг изменения цены в пунктах this.StepPrise_h.Value = (double)source.FinInfo.StepPrice; //Стоимость одного шага цены в рублях } else { //Если симуляция this.GO_h.Value = GO_in; //Гарантийное обеспечение фьючерса this.StepSize_h.Value = PriseTick_in; //Шаг изменения цены в пунктах this.StepPrise_h.Value = (double)PriseStepTick_in; //Стоимость одного шага цены в рублях } // Make 'GO' item data System.Collections.Generic.IList GO = ctx.GetData("GO", new string[] { this.GO_h.Value.ToString(), source.Symbol }, delegate { return this.GO_h.Execute(ctx); }); //Доля системы в капитале this.Part_h.Value = Part_in; // Make 'Part' item data System.Collections.Generic.IList Part = ctx.GetData("Part", new string[] { this.Part_h.Value.ToString(), source.Symbol }, delegate { return this.Part_h.Execute(ctx); }); //Предел когда допускается округлять число лотов вверх // Initialize 'Tolerance' item this.Tolerance_h.Value = 0.1D; // Make 'Tolerance' item data System.Collections.Generic.IList Tolerance = ctx.GetData("Tolerance", new string[] { this.Tolerance_h.Value.ToString(), source.Symbol }, delegate { return this.Tolerance_h.Execute(ctx); }); #endregion #region Вычисление размера позиции //if(secRt.Positions.IsRealtime==true) //source.Positions.IsRealtime!=true //Достпуное плече this.Margin_h.Value = source.Margin; System.Collections.Generic.IList Margin = ctx.GetData("Margin", new string[] { this.Margin_h.Value.ToString(), source.Symbol }, delegate { return this.Margin_h.Execute(ctx); }); //Шаг изменения цены в пунктах // Make 'StepSize' item data System.Collections.Generic.IList StepSize = ctx.GetData("StepSize", new string[] { this.StepSize_h.Value.ToString(), source.Symbol }, delegate { return this.StepSize_h.Execute(ctx); }); //Стоимость одного шага цены в рублях // Make 'StepPrise' item data System.Collections.Generic.IList StepPrise = ctx.GetData("StepPrise", new string[] { this.StepPrise_h.Value.ToString(), source.Symbol }, delegate { return this.StepPrise_h.Execute(ctx); }); // IList nDepo = new List(0); IList nLongPosSize = new List(0); IList nShortPosSize = new List(0); // Make 'PunktPrise' item data System.Collections.Generic.IList PunktPrise = new double[barsCount]; // Make 'CurentPriseRub' item data System.Collections.Generic.IList CurentPriseRub = new double[barsCount]; // Make 'KelliLong' item data System.Collections.Generic.IList KelliLong = new double[barsCount]; // Make 'KelliShort' item data System.Collections.Generic.IList KelliShort = new double[barsCount]; // Make 'HalfKelliLong' item data System.Collections.Generic.IList HalfKelliLong = new double[barsCount]; // Make 'HalfKelliShort' item data System.Collections.Generic.IList HalfKelliShort = new double[barsCount]; // Make 'HalfKelliLongGO' item data System.Collections.Generic.IList HalfKelliLongGO = new double[barsCount]; // Make 'HalfKelliShortGO' item data System.Collections.Generic.IList HalfKelliShortGO = new double[barsCount]; // Make 'MarginLong' item data System.Collections.Generic.IList MarginLong = new double[barsCount]; // Make 'MarginShort' item data System.Collections.Generic.IList MarginShort = new double[barsCount]; if (secRt != null) { //Если реалтайм CurentDepoLocal = (double)secRt.EstimatedBalance; // Чистый баланс. Все обязательства учтены. (Колонка чистая стоимость) } else { //Если симуляция CurentDepoLocal = this.DepoLocal_h.Execute(source, i);//Депозит текущий } CurentPrice = source.ClosePrices[i]; //Текущая стоимость //nDepo.Add(CurentDepoLocal); PunktPrise[i] = StepPrise[i] / StepSize[i]; CurentPriseRub[i] = CurentPrice * PunktPrise[i]; KelliLong[i] = (100 * SP[i] / SKP[i]) + 0.5; KelliShort[i] = (100 * SP[i] / SKP[i]) - 0.5; HalfKelliLong[i] = KelliLong[i] / 2; HalfKelliShort[i] = KelliShort[i] / 2; HalfKelliLongGO[i] = HalfKelliLong[i] / (CurentPriseRub[i] / GO[i]); HalfKelliShortGO[i] = HalfKelliShort[i] / (CurentPriseRub[i] / GO[i]); MarginLong[i] = Math.Min(Margin[i] + 1, HalfKelliLongGO[i]); MarginShort[i] = Math.Min(Margin[i], HalfKelliShortGO[i]); double tempL = CurentDepoLocal * Part[i] * MarginLong[i] / GO[i]; double tempS = CurentDepoLocal * Part[i] * MarginShort[i] / GO[i]; LongPosSize = (Math.Ceiling(tempL) - (tempL) < Tolerance[i]) ? Math.Max(Math.Ceiling(tempL), 1) : Math.Max(Math.Truncate(tempL), 1); //nLongPosSize.Add(LongPosSize); ShortPosSize = (Math.Ceiling(tempS) - (tempS) < Tolerance[i]) ? Math.Max(Math.Ceiling(tempS), 1) : Math.Max(Math.Truncate(tempS), 1); //nShortPosSize.Add(ShortPosSize); ctx.Log(i.ToString() + ") CurentPrice:" + CurentPrice.ToString() + ", CurentDepoLocal:" + CurentDepoLocal.ToString() + ", HalfKelliLong:" + HalfKelliLong[i].ToString() + ", HalfKelliLongGO:" + HalfKelliLongGO[i].ToString() + ", MarginLong:" + MarginLong[i].ToString() + ", tempL:" + tempL.ToString(), new Color()); #endregion if (isLong) { return LongPosSize; } else { return ShortPosSize; } } } }