Вот получилось немного написать (с ошибками):
Click to reveal..

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);
}
}
}

}


На сколько я понял скрипт начинает выполняться со строки public virtual void Execute. При попытке старта скрипта (я понимаю что он ничего не делает кроме как обращается к функции) выдаеться ошибка:
Click to reveal..

15:51:59.45 120 System.ArgumentOutOfRangeException: Индекс за пределами диапазона. Индекс должен быть положительным числом, а его размер не должен превышать размер коллекции.
Имя параметра: index
в System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
в System.ThrowHelper.ThrowArgumentOutOfRangeException()
в System.Collections.Generic.List`1.get_Item(Int32 index)
в TSLab.Sistema.Sistema.BuyAndSell(ISecurity source, Int32 bar) в c:\Users\Webert\Documents\SharpDevelop Projects\a1\a1\Class1.cs:строка 34
в TSLab.Sistema.Sistema.Execute(IContext ctx, ISecurity source) в c:\Users\Webert\Documents\SharpDevelop Projects\a1\a1\Class1.cs:строка 98
в TSLab.User.Script.Execute(IContext context, ISecurity var0) в c:\Users\Webert\AppData\Local\TSLab\TSLab\temp\code22.cs:строка 39



Подскажите что не так? Некорректно обращаюсь к функции? Или в саймой функции допущена ошибка? (упрощать скрипт буду позже щас главное что бы он заработал).
P.S. Спосибо за помощь.