закрытие позиции
Добавлено: 14 авг 2016, 10:15
здравствуйте подскажите пожалуйста можно ли прописать в роботе что бы позиция закрывалась через минуту? спасибо


Неофициальный форум клиентского терминала Альфа-Инвестиции (Альфа-Директ 4). Обсуждение терминала, обмен опытом, разработка скриптов индикаторов и стратегий.
https://www.alfadirect4.ru/
Код: Выделить всё
/**ClosePositionMinute
Developed by evge;**/
function Initialize()
{
StrategyName = "ClosePositionMinute";
AddParameter("M", 1, "", 1);
AddInput("Input1", Inputs.Candle, -15, true, "SBER=МБ ЦК");
LongLimit = 1;
ShortLimit = -1;
AddGlobalVariable("OpenPosition", Types.Int, 0);
}
function OnUpdate()
{
if (CurrentPosition() != 0 && OpenPosition == 0 )
OpenPosition = CurrentIndex - 1;
if (CurrentPosition() == 0 && OpenPosition != 0)
OpenPosition = 0;
/// ПРАВИЛО 1
if ( (Input1.Open > Input1.Close) && (CurrentPosition() == 0) )
{
EnterLong();
}
/// ПРАВИЛО 2
TimeSpan iM = BarTime() - BarTime(CurrentIndex - OpenPosition);
if ( (CurrentPosition() != 0) && iM.TotalMinutes >= M || iM.TotalMinutes < 0)
{
ClosePosition();
}
}
Код: Выделить всё
OpenPosition = CurrentIndex;
Код: Выделить всё
OpenPosition = CurrentIndex - 1;
Код: Выделить всё
{#OptVar1 1;1;10;1}
// Eager Beaver 2 with PeekChecker
// by DrKoch 2003-09
{$I 'Gaussian'}
{$I 'PeekChecker'}
var BAR, P: integer;
var z: float;
var exit_f: float;
var ema_per, atr_per: integer;
var ma, C: float;
var exit_limit, exit_dist: float;
var uptrend: boolean;
var timeout: integer;
// Parameter
ema_per := 130; // period of MA
atr_per := 6;
exit_f := 0.6; // factor * Range for exit
timeout := 1;
// Graphics
EnableTradeNotes( false, false, true );
PlotSeries( GaussianSeries( #Close, ema_per, 4), 0, #red, #Thin );
DrawLabel( 'Gaussian( Close, ' + IntToStr(ema_per) + ')', 0 );
// Trading System
StartPeekCheck(1, 0);
InstallTimeBasedExitR( timeout );
for Bar := 30 to BarCount() - 1 do begin
ApplyAutoStopsR( Bar );
// intermediate values
z := ATR(Bar, atr_per);
C := PriceCloseR(Bar); // most recent close
ma := Gaussian(Bar, #Close, ema_per, 4); // moving average
uptrend := (C > ma); // what is the trend?
exit_dist := z * exit_f; // exit price rel. close
if uptrend then exit_limit := C + exit_dist
else exit_limit := C - exit_dist;
// Exit Rules
if ActivePositionCount() > 0 then begin
for p:= 0 to PositionCount -1 do begin
if not PositionActive( p ) then continue;
if PositionLong(p) then SellAtLimitR( Bar+1, exit_limit, p, 'Sell')
else CoverAtLimitR(Bar+1, exit_limit, p, 'Cover');
end;
end;
// Entry Rules
// enter a new position at Open of each Bar
if uptrend then BuyAtMarketR(Bar+1, '' )
else ShortAtMarketR(Bar+1, '' );
end;