using System;
using System.Collections.Generic;
using TSLab.Script;
using TSLab.Script.Handlers;
using TSLab.Script.Optimization;
using TSLab.Script.Helpers;
namespace TSLab.Sistema
{
public class Sistema : IExternalScript
{
public bool bBuy, bSell;
public bool BuyAndSell(ISecurity source, int bar)
{
#region Введение переменных
bool bBuyAndSell = false; // Проверка наличия или отсутсвия модели
int n = 1; // Переменная для циклов
int x = 0; // Переменная для циклов
int otstup = 20; // Отступ для поиска точки 1
double dT1, dT2, dT3; // Уровни точек
int iT1, iT2, iT3; // Номера баров точек
#endregion
#region Поиск модели
while (x<=20)
{
// Ищем точку 3
if (source.LowPrices[bar-n]<=source.LowPrices[bar-x])
{
n = n + 1;
x = x + 1;
continue;
}
// Ищем точку 1
PoiskT1:
if (source.LowPrices[bar-x-otstup]>source.LowPrices[bar-x])
{
if (otstup>60)
{
break;
}
otstup = otstup + 1;
continue;
}
// Ищем точку 2
int ss = n + 1;
for (int pp = n + 1; pp<otstup+n; ++pp)
{
if (source.HighPrices[bar-pp]>source.HighPrices[bar-ss])
{
ss = pp;
}
}
// Теперь имеем ss - это номер бара точки 2, х - бара точки 3, x+otstup - бара точки 1
iT1 = bar-x-otstup;
iT2 = bar-ss;
iT3 = bar-x;
dT1 = source.LowPrices[iT1];
dT2 = source.HighPrices[iT2];
dT3 = source.LowPrices[iT3];
if (dT2-630<dT3)
{
n = n + 1;
x = x + 1;
continue;
}
if (dT2-1000<dT1)
{
otstup = otstup + 1;
goto PoiskT1; //Возвращаемся к поиску точки 1
}
if (0.37*dT2-dT1>dT3-dT1)
{
otstup = otstup + 1;
goto PoiskT1; //Возвращаемся к поиску точки 1
}
bBuyAndSell = true;
break;
}
#endregion
return bBuyAndSell; // Возвращаем ответ на вопрос о постановке стопа
}
public virtual void Execute(IContext ctx, ISecurity source)
{
bool LongCond;
int StartBar = 0;
for (int bar = StartBar; bar < source.Bars.Count; bar++)
{
LongCond = BuyAndSell(source, bar);
}
}
}
}