

Created a cBot that uses the CRTIndicator to set stop loss and take profit levels.
The idea is to use the CRT levels (high, mid, low) of a previous candle (specified by CandleIndex) to set the stop loss and take profit for a new trade.
Steps:
- We will create a cBot that opens a trade (either buy or sell) based on some condition (which we will define as a parameter for testing).
- We will use the CRTIndicator to get the levels from a previous candle (the candle index is set by the user).
- For a buy trade, we will set the stop loss at the CRT low and take profit at the CRT high of the reference candle.
- For a sell trade, we will set the stop loss at the CRT high and take profit at the CRT low of the reference candle.
However, note that the CRTIndicator we have only calculates the levels for each candle and plots them.
We need to access the indicator from the cBot and get the values for the specific candle we are interested in.
Let's design the cBot:
Parameters:
- CandleIndex: which previous candle to use (0 = current, 1 = previous, etc.)
- TradeType: we can set a parameter to choose between Buy and Sell for testing, or we can use a signal (like a moving average crossover) to decide.
For simplicity, let's make a parameter to choose the trade type.
But note: In live trading, we would use some conditions to enter. We'll make a simple cBot that opens a trade on the current candle if there is no existing position.
We'll assume we are trading on the time frame of the chart.
Steps in the cBot:
- Check if we have a position open. If not, then consider opening.
- Get the CRTIndicator values for the specified CandleIndex (which is a past candle relative to current).
- For a Buy trade:
Stop Loss = CRT Low of the reference candle
Take Profit = CRT High of the reference candle - For a Sell trade:
Stop Loss = CRT High of the reference candle
Take Profit = CRT Low of the reference candle
However, note that the indicator calculates for each index. We are plotting the same values for every index (because we are using a fixed CandleIndex offset).
But in the cBot, we want to get the levels from the indicator at the current time (the most recent completed candle) for the specified CandleIndex.
Important: The indicator's Calculate method is called for each index, and it stores the values for the reference candle (index - CandleIndex) at the current index.
So if we want to get the CRT levels for a specific historical candle, we can simply read the indicator's output series at the index of that candle?
But note: the indicator outputs are aligned with the current index. Actually, the indicator is plotting the same value for every bar because we are setting:
CRTHigh[index] = crtHigh; // where crtHigh is the high of (index - CandleIndex) candle
So if we are at index 100 and CandleIndex=1, then we are getting the high of index 99 and plotting it at index 100.
This means that if we want the CRT levels for the candle that is CandleIndex bars ago, we can get the indicator output at the current bar (index) and that will give us the levels from CandleIndex bars ago.
But wait, in the cBot, we are going to run OnBar or OnTick. We are interested in the most recent completed bar (index = MarketSeries.Close.Count - 2) and the candle we want is (current index - CandleIndex).
Alternatively, we can change the indicator to output the levels at the same index as the reference candle, but then we would have to shift the plot by CandleIndex.
However, the current indicator is designed to plot the levels at the current bar from a previous bar. So if we set CandleIndex=1, then at each bar we are plotting the levels from the previous bar.
How we can use it in the cBot:
We will add the indicator to the cBot and then get the values for the most recent bar (index = MarketSeries.Close.Count - 1) and that will give us the levels from CandleIndex bars ago.
Example:
Current bar index = last bar (index = MarketSeries.Close.Count - 1)
Then the indicator output at this index (CRTHigh[MarketSeries.Close.Count-1]) is the high of (current index - CandleIndex) bar.
But note: the indicator's Calculate method is called for each historical bar and then for each new bar. So the output series for the last bar will have the value we want.
Let's code the cBot:
We'll have parameters:
[Parameter("Candle Index", DefaultValue = 1, MinValue = 0)]
public int CandleIndex { get; set; }
[Parameter("Trade Type", DefaultValue = TradeType.Buy)]
public TradeType SelectedTradeType { get; set; }
[Parameter("Volume (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double Volume { get; set; }
We'll also create an instance of the CRTIndicator.
Steps in OnBar:
if there is no position, then open a new position with the stop loss and take profit set from the indicator's output for the current bar (which corresponds to the levels from CandleIndex bars ago).
However, note: the indicator's output for the current bar (last bar) is set to the levels of (current bar index - CandleIndex). But when we are at the very beginning, we must ensure we have enough bars.