Simple question onsyntax

Created at 08 May 2013, 10:18
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
IR

iRobot

Joined 17.02.2013

Simple question onsyntax
08 May 2013, 10:18


Sorry for newbie question, but I don't know jow to write command on current tick(bar). For example: I need a stoploss when last value touches (is below) ema20 (meaning it closes position right away). What I can achieve at the mom is that position is closed when last value is below ema20 in the previous bar (T-1). Time lag is to much for me at the mom.

 

Thanks for help.

 


@iRobot
Replies

cAlgo_Fanatic
14 May 2013, 11:19

You can close the position once the price crosses below the ema20. Below is an example that creates one position on robot start and if the ema20 crosses above the close the position is closed.

 

private ExponentialMovingAverage ema20;
private Position position;

// Source is the price (O-L-H-C) used in the ExponentialMovingAverage calculation
[Parameter]
public DataSeries Source { get; set; }

// Period of the ExponentialMovingAverage
[Parameter(DefaultValue = 20, MinValue = 1)]
public int Period { get; set; }

protected override void OnStart()
{
    // Initialize ExponentialMovingAverage Indicator
    ema20 = Indicators.ExponentialMovingAverage(Source, Period);
    Trade.CreateMarketOrder(TradeType.Buy, Symbol, 100000);
}

protected override void OnTick()
{

    // EMA is above price
    if (ema20.Result.HasCrossedAbove(MarketSeries.Close, 0))
    {
        Trade.Close(position);
    }
}


protected override void OnPositionOpened(Position openedPosition)
{
    position = openedPosition;
}




@cAlgo_Fanatic