Originally Posted By: vito333
вопрос интересный

дайте код стандартного трейла,а? и доработаем



using System;
using System.Linq;
using TSLab.Script.Realtime;

// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
namespace TSLab.Script.Handlers
{
[HandlerName("Entry Price")]
[HandlerCategory("Position")]
[HandlerName("Trail Stop")]
[HandlerCategory("Position")]
public class TrailStop : IPosition2Double
{
[HandlerParameter(true, "1.5", Min = "0.1", Max = "5", Step = "0.1", Name = "Stop Loss")]
public double StopLoss { get; set; }

[HandlerParameter(true, "0.5", Min = "0.1", Max = "3", Step = "0.1", Name = "Trail Enable")]
public double TrailEnable { get; set; }

[HandlerParameter(true, "0.5", Min = "0.1", Max = "3", Step = "0.1", Name = "Trail Loss")]
public double TrailLoss { get; set; }

public double Execute(IPosition pos, int barNum)
{
if (pos == null)
{
return 0;
}
double stop;
var curProfit = pos.OpenMFEPct(barNum);
if (curProfit > TrailEnable)
{
double shift = (curProfit - TrailLoss) / 100;
stop = pos.EntryPrice * (1 + (pos.IsLong ? shift : -shift));
}
else
{
double shift = (0 - StopLoss) / 100;
stop = pos.EntryPrice * (1 + (pos.IsLong ? shift : -shift));
}
var lastStop = pos.GetStop(barNum);
if(lastStop == 0)
{
return stop;
}
return pos.IsLong ? Math.Max(stop, lastStop) : Math.Min(stop, lastStop);
}
}

[HandlerName("Trail Stop Abs")]
[HandlerCategory("Position")]
public class TrailStopAbs : IPosition2Double
{
[HandlerParameter(true, "150", Min = "10", Max = "500", Step = "5")]
public double StopLoss { get; set; }

[HandlerParameter(true, "50", Min = "10", Max = "500", Step = "5")]
public double TrailEnable { get; set; }

[HandlerParameter(true, "50", Min = "10", Max = "500", Step = "5")]
public double TrailLoss { get; set; }

public double Execute(IPosition pos, int barNum)
{
if (pos == null)
{
return 0;
}
double stop;
var curProfit = pos.OpenMFE(barNum) / pos.Shares;
if (curProfit > TrailEnable)
{
double shift = curProfit - TrailLoss;
stop = pos.EntryPrice + (pos.IsLong ? shift : -shift);
}
else
{
double shift = -StopLoss;
stop = pos.EntryPrice + (pos.IsLong ? shift : -shift);
}
var lastStop = pos.GetStop(barNum);
if (lastStop == 0)
{
return stop;
}
return pos.IsLong ? Math.Max(stop, lastStop) : Math.Min(stop, lastStop);
}
}


Отредактировано Lucky7 (Fri Feb 17 2012 03:40 PM)