Skip to content

Process Strategy Method

This function is entry point of your strategy logic and here we put our strategy main decision logic.
ProcessStrategy method is called for every bar close or for each incoming tick depending on setup is for backtesting or realtime or dual mode.

For historical bar defined by TimeDataSeries, this method is called at least once for each bar and here programmers can evaluate a signal generation logic based on indicator or price actions.
The signal is order action and invoke by command like EnterLong, EnterShort, GoFlat etc.


Various action command supported by strategy

EnterLongAtMarket

Generates a buy market order to create a long position.

If current position is 0, then:

EnterLongAtMarket(10, "long signal due of sma crossover")
→ Position becomes 10

If current position is 10: → Command ignored (unless pyramiding enabled)

If current position is -12: → Becomes +10 (buy 22)


Order Execution Mode

StrategyConfigData strategyConfigData = new StrategyConfigData();
strategyConfigData.OrderExecutionAt = OrderExecution.CurrentBar_Close;

strategyConfigData.OrderExecutionAt = OrderExecution.NextBar_Open;

EnterShortAtMarket

EnterShortAtMarket(10, "short signal")

Other Commands

  • EnterLongAtPrice
  • EnterShortAtPrice
  • EnterLongAtLimit
  • EnterShortAtLimit
  • EnterLongAtStop
  • EnterShortAtStop
  • EnterLongAtStopLimit
  • EnterShortAtStopLimit
  • GoFlatAtMarket
  • GoFlatAtLimit
  • GoFlatAtPrice

Important Properties & Functions

Property / Function Description
CurrentIndex Current bar index
TimeDataSeries[index] Access bar data
BarValue('C', i) Price by type
Ref('C', -1) Previous value
RefX('C', 1) Forward reference
TimeNum() HHmmss format
DateNum() yyyyMMdd format
MarketPosition Current position
GetAverageEntryTradedPrice() Avg entry price
GetLastTradeEntryPrice() Last trade price
IsFirstBarOfDay() First bar
IsLastBarOfDay() Last bar
DayOfWeekNum() Week day
DayOfMonthNum() Month day
IsGapUp() Gap up
IsGapDown() Gap down
IsInside() Inside bar
IsRising() Rising
IsFalling() Falling
LLV() Lowest
HHV() Highest
StdDev() Std deviation
Sum() Sum
Average() Average
WeightedAverage() Weighted avg
TrueRange() Range
TypicalPrice() Typical price
BarsSince() Bars since condition
HighestSince() Highest since
LowestSince() Lowest since
ValueWhen() Value when
Lookup(DateTime) Lookup by datetime
Lookup(int) Lookup by timenum
Cross() Series crossover

Note

Most functions are also applicable to DoubleSeries and indicators.


Sum of Previous N Bars

Method 1

double sum = 0;
for (int iCnt = CurrentIndex; iCnt > CurrentIndex - Input_LengthPeriod; iCnt--)
{
    sum += Close(iCnt);
}

Method 2

double sum = 0;
for (int barsAgo = 0; barsAgo < Input_LengthPeriod; barsAgo++)
{
    sum += RefX('C', barsAgo);
}

Method 3

double sum = Sum('C', Input_LengthPeriod);

Store Values in DoubleSeries

private DoubleSeries _barRangeDoubleSeries = new DoubleSeries();

public override void ProcessStrategy()
{
    _barRangeDoubleSeries[CurrentIndex] = High(CurrentIndex) - Low(CurrentIndex);

    if (_barRangeDoubleSeries[CurrentIndex] > _barRangeDoubleSeries[CurrentIndex - 1])
    {
        EnterLongAtMarket(1);
    }

    double avgRange = _barRangeDoubleSeries.Average(10);
}

BarsSince Example

Func<int, bool> Evaluate = (barIndex) =>
    tdSeries[barIndex].Close > tdSeries[barIndex].Open;

int barIndexMatched = BarsSince(Evaluate, 1);
double closePrice = Close(barIndexMatched);

ValueWhen Example

Func<int, bool> Evaluate = (barIndex) =>
    tdSeries.TimeNum(barIndex) == 091600;

double closePrice = tdSeries.ValueWhen(Evaluate, 'C', 1);

HighestSince Example

Func<int, bool> Evaluate = (barIndex) => High(barIndex) > 50;

double highestVal = HighestSince(Evaluate, 'C', 2);

LowestSince Example

Func<int, bool> Evaluate = (barIndex) => High(barIndex) > 50;

double lowestVal = LowestSince(Evaluate, 'C', 2);