Здесь позиция будет закрываться с шагом даже если цена ходит в обратную сторону от последнего шага, но при этом есть условие, что это частичное закрытие будет только при положительном CurrentPL()
Код: Выделить всё
function Initialize()
{
StrategyName = "SuperTrendParts2";
AddParameter("PeriodATR", 4, "", 1);
AddParameter("PeriodCCI", 39, "", 1);
AddParameter("K", 1, "", 1);
AddParameter("TP", 1, "TakeProfit %", 1);
AddParameter("TPLot", 500, "Lot TakeProfit", 1);
AddInput("Input1", Inputs.Candle, 60, true, "SBER=МБ ЦК");
LongLimit = 5000;
ShortLimit = -5000;
AddChartIndicator("MY.SuperTrend", new Dictionary <string, string>{{"PeriodATR", "PeriodATR"},{"PeriodCCI", "PeriodCCI"},{"K", "K"}});
AddGlobalVariable("N", Types.Int, 0);
AddGlobalVariable("Last", Types.Double, 0);
}
function OnUpdate()
{
// evge 08.10.2019 https://alfadirect4.ru
var I = Input1;
var ST = MY.SuperTrend(Input1, PeriodATR, PeriodCCI, K);
var CP = CurrentPosition();
/// ПРАВИЛО 1
if ( ST["U"][1] > 0 && N >= 0)
{
EnterLong();
Last = I.Close[0];
N = -1;
}
/// ПРАВИЛО 2
if ( ST["D"][1] > 0 && N <= 0)
{
EnterShort();
Last = I.Close[0];
N = 1;
}
if (CP > 0 && I.Close[0] > Last * (1 + TP * 0.01)) { CloseLong(TPLot); Last = I.Close[0]; }
if (CP > 0 && I.Close[0] < Last * (1 - TP * 0.01) && CurrentPLper() > 0) { CloseLong(TPLot); Last = I.Close[0]; }
if (CP < 0 && I.Close[0] < Last * (1 - TP * 0.01)) { CloseShort(TPLot); Last = I.Close[0]; }
if (CP < 0 && I.Close[0] > Last * (1 + TP * 0.01) && CurrentPLper() > 0) { CloseShort(TPLot); Last = I.Close[0]; }
}