using System;
using System.Collections.Generic;
using TSLab.Script;
using TSLab.Script.Handlers;
using TSLab.Script.Optimization;
using TSLab.Script.Helpers;
namespace TSLab.User
{
//using System;
//using TSLab.Script;
public class Script
{
private TSLab.Script.Handlers.Compress Compress_h = new TSLab.Script.Handlers.Compress();
private TSLab.Script.Handlers.Close Close_h = new TSLab.Script.Handlers.Close();
private TSLab.Script.Handlers.SMA Quick_h = new TSLab.Script.Handlers.SMA();
private TSLab.Script.Handlers.SMA Slow_h = new TSLab.Script.Handlers.SMA();
public TSLab.Script.Optimization.OptimProperty Quick_Period = new TSLab.Script.Optimization.OptimProperty(20, 10, 100, 5);
public TSLab.Script.Optimization.OptimProperty Compress_Shift = new TSLab.Script.Optimization.OptimProperty(0, 0, 60, 5);
public TSLab.Script.Optimization.OptimProperty Slow_Period = new TSLab.Script.Optimization.OptimProperty(20, 10, 100, 5);
public virtual void Execute(TSLab.Script.Handlers.IContext context, TSLab.Script.ISecurity Index, TSLab.Script.ISecurity Futures)
{
// Initialize 'Quick' item
this.Quick_h.Period = this.Quick_Period;
// Initialize 'Закрытие' item
this.Close_h.Context = context;
// Initialize 'Сжать' item
this.Compress_h.Interval = 10;
this.Compress_h.Shift = this.Compress_Shift;
// Make 'Сжать' item data
TSLab.Script.ISecurity IndexComp;
try
{
IndexComp = this.Compress_h.Execute(Index);
}
catch (System.ArgumentOutOfRangeException)
{
throw new TSLab.Script.ScriptException("Ошибка при вычислении блока \'Сжать\'. Индекс за пределам диапазона.");
}
// Make 'Закрытие' item data
System.Collections.Generic.IList<double> CloseComp = context.GetData("CloseComp", new string[] {
this.Compress_h.Interval.ToString(),
this.Compress_h.Shift.ToString()
}, delegate
{
try
{
return this.Close_h.Execute(IndexComp);
}
catch (System.ArgumentOutOfRangeException)
{
throw new TSLab.Script.ScriptException("Ошибка при вычислении блока \'Закрытие\'. Индекс за пределам диапазона.");
}
});
// Make 'Quick' item data
System.Collections.Generic.IList<double> Quick = context.GetData("Quick", new string[] {
this.Quick_h.Period.ToString(),
this.Compress_h.Interval.ToString(),
this.Compress_h.Shift.ToString()
}, delegate
{
try
{
return this.Quick_h.Execute(CloseComp);
}
catch (System.ArgumentOutOfRangeException)
{
throw new TSLab.Script.ScriptException("Ошибка при вычислении блока \'Quick\'. Индекс за пределам диапазона.");
}
});
// Initialize 'Slow' item
this.Slow_h.Period = this.Slow_Period;
// Make 'Slow' item data
System.Collections.Generic.IList<double> Slow = context.GetData("Slow", new string[] {
this.Slow_h.Period.ToString(),
this.Compress_h.Interval.ToString(),
this.Compress_h.Shift.ToString()
}, delegate
{
try
{
return this.Slow_h.Execute(CloseComp);
}
catch (System.ArgumentOutOfRangeException)
{
throw new TSLab.Script.ScriptException("Ошибка при вычислении блока \'Slow\'. Индекс за пределам диапазона.");
}
});
// Make 'log_long' item data
System.Collections.Generic.IList<bool> log_long;
try
{
int count = System.Math.Min(Slow.Count, Quick.Count);
bool[] list = new bool[count];
if ((Index.IsLastBarUsed == false))
{
count--;
}
for (int i = 1; (i < count); i++)
{
list[i] = Quick[i] >= Slow[i] & Quick[i - 1] < Slow[i - 1];
}
log_long = list;
}
catch (System.ArgumentOutOfRangeException)
{
throw new TSLab.Script.ScriptException("Ошибка при вычислении блока \'log_long\'. Индекс за пределам диапазона.");
}
// Make 'log_short' item data
System.Collections.Generic.IList<bool> log_short;
try
{
int count = System.Math.Min(Quick.Count, Slow.Count);
bool[] list = new bool[count];
if ((Index.IsLastBarUsed == false))
{
count--;
}
for (int i = 1; (i < count); i++)
{
list[i] = Quick[i] <= Slow[i] & Quick[i - 1] > Slow[i - 1];
}
log_short = list;
}
catch (System.ArgumentOutOfRangeException)
{
throw new TSLab.Script.ScriptException("Ошибка при вычислении блока \'log_short\'. Индекс за пределам диапазона.");
}
// =================================================
// Panels
// =================================================
// Make 'Главное' pane
TSLab.Script.IPane Главное_pane = context.CreatePane("Главное", 70, false);
Главное_pane.Visible = true;
// =================================================
// Handlers
// =================================================
TSLab.Script.IPosition L;
TSLab.Script.IPosition S;
log_long = IndexComp.Decompress(log_long, TSLab.DataSource.DecompressMethodWithDef.Default);
log_short = IndexComp.Decompress(log_short, TSLab.DataSource.DecompressMethodWithDef.Default);
// =================================================
// Trading
// =================================================
int barsCount = Index.Bars.Count;
if ((Index.IsLastBarUsed == false))
{
barsCount--;
}
for (int i = context.TradeFromBar; (i < barsCount); i++)
{
L = Futures.Positions.GetLastActiveForSignal("L");
S = Futures.Positions.GetLastActiveForSignal("S");
if ((L == null))
{
if (log_long[i])
{
Futures.Positions.BuyAtMarket(i + 1, 1, "L");
}
}
else
{
if ((L.EntryBarNum <= i))
{
if (log_short[i])
{
L.CloseAtMarket(i + 1, "Lx");
}
}
}
if ((S == null))
{
if (log_short[i])
{
Futures.Positions.SellAtMarket(i + 1, 1, "S");
}
}
else
{
if ((S.EntryBarNum <= i))
{
if (log_long[i])
{
S.CloseAtMarket(i + 1, "Sx");
}
}
}
}
// =================================================
// Charts
// =================================================
// Make 'Index' chart
TSLab.Script.IGraphList Главное_pane_Index_chart = Главное_pane.AddList(("Index"
+ (" ["
+ (Index.Symbol + "]"))), Index, TSLab.Script.CandleStyles.BAR_CANDLE, true, -16777216, TSLab.Script.PaneSides.RIGHT);
Index.ConnectSecurityList(Главное_pane_Index_chart);
Главное_pane.UpdatePrecision(TSLab.Script.PaneSides.RIGHT, Index.Decimals);
// Make 'Quick' chart
Quick = IndexComp.Decompress(Quick, TSLab.DataSource.DecompressMethodWithDef.Default);
TSLab.Script.IGraphList Главное_pane_Quick_chart = Главное_pane.AddList(((("Quick"
+ (" (" + this.Quick_h.Period))
+ ")")
+ (" ["
+ (IndexComp.Symbol + "]"))), Quick, TSLab.Script.ListStyles.LINE, -393216, TSLab.Script.LineStyles.SOLID, TSLab.Script.PaneSides.RIGHT);
Главное_pane.UpdatePrecision(TSLab.Script.PaneSides.RIGHT, IndexComp.Decimals);
// Make 'Slow' chart
Slow = IndexComp.Decompress(Slow, TSLab.DataSource.DecompressMethodWithDef.Default);
TSLab.Script.IGraphList Главное_pane_Slow_chart = Главное_pane.AddList(((("Slow"
+ (" (" + this.Slow_h.Period))
+ ")")
+ (" ["
+ (IndexComp.Symbol + "]"))), Slow, TSLab.Script.ListStyles.LINE, -15004712, TSLab.Script.LineStyles.SOLID, TSLab.Script.PaneSides.RIGHT);
Главное_pane.UpdatePrecision(TSLab.Script.PaneSides.RIGHT, IndexComp.Decimals);
// Make 'Futures' chart
TSLab.Script.IGraphList Главное_pane_Futures_chart = Главное_pane.AddList(("Futures"
+ (" ["
+ (Futures.Symbol + "]"))), Futures, TSLab.Script.CandleStyles.BAR_CANDLE, true, -4980736, TSLab.Script.PaneSides.LEFT);
Futures.ConnectSecurityList(Главное_pane_Futures_chart);
Главное_pane.UpdatePrecision(TSLab.Script.PaneSides.LEFT, Futures.Decimals);
}
}
}