Написал, как мне кажется, почти близко к оригиналу, нет только стопов, да и объемами робот не играется, но результаты абсолютно не впечатляют. Или я чего то упустил или стратегия себя изжила, либо на фондовом рынке не особо применима.
Что скажете?
Код: Выделить всё
function Initialize()
{
StrategyName = "Turtle";
AddParameter("PeriodVeryFast", 10, "Очень быстрый период", 1);
AddParameter("PeriodFast", 20, "Быстрый период", 1);
AddParameter("PeriodSlow", 55, "Медленный период", 1);
AddInput("Input1", Inputs.Candle, 60, true, "VTBR=МБ ЦК");
AddGlobalVariable("CurrentSystem", Types.Int, 0);
}
function OnUpdate()
{
var hf = MaxValue(Input1.Close, (int)PeriodFast, 1);
var lf = MinValue(Input1.Close, (int)PeriodFast, 1);
var hs = MaxValue(Input1.Close, (int)PeriodSlow, 1);
var ls = MinValue(Input1.Close, (int)PeriodSlow, 1);
var hvf = MaxValue(Input1.Close, (int)PeriodVeryFast, 1);
var lvf = MinValue(Input1.Close, (int)PeriodVeryFast, 1);
// RULE SYSTEM 1 Enter Long (20)
if ( (Input1.Close[0] > hf) && (CurrentPosition() <= 0) )
{
var rpl = RealizedPL(0);
if (rpl <= 0)
{
CurrentSystem = 1;
EnterLong();
}
}
// RULE SYSTEM 1 Enter Short (20)
if ( (Input1.Close[0] < lf) && (CurrentPosition() >= 0) )
{
var rpl = RealizedPL(0);
if (rpl < 0)
{
CurrentSystem = 1;
EnterShort();
}
}
// RULE SYSTEM 2 Enter Long (55)
if ( (Input1.Close[0] > hs) && (CurrentPosition() <= 0) )
{
CurrentSystem = 2;
EnterLong();
}
// RULE SYSTEM 2 Enter Short (55)
if ( (Input1.Close[0] < ls) && (CurrentPosition() >= 0) )
{
CurrentSystem = 2;
EnterShort();
}
// RULE SYSTEM 2 Exit Long (20)
if ( (CurrentPosition() > 0) && (Input1.Close[0] < lf) )
{
CurrentSystem = 0;
CloseLong();
}
// RULE SYSTEM 2 Exit short (20)
if ( (CurrentPosition() < 0) && (Input1.Close[0] > hf) )
{
CurrentSystem = 0;
CloseShort();
}
// RULE SYSTEM 1 Exit long (10)
if ( (CurrentPosition() > 0) && (Input1.Close[0] < lvf) )
{
CurrentSystem = 0;
CloseLong();
}
// RULE SYSTEM 1 Exit short (10)
if ( (CurrentPosition() < 0) && (Input1.Close[0] > hvf) )
{
CurrentSystem = 0;
CloseShort();
}
}