Many times automated strategies need to consider information from different timeframes before making a trading decision. Fortunately, cTrader provides plenty of tools that allow algos to easily access data from multiple timeframes. In this video and its corresponding article, we will explain how you can use these tools to create an effective cBot.
We will start by declaring the necessary parameters for a moving average. We will need the period, the timeframe, and the moving average type.
12345678
[Parameter("MA 1 Period", DefaultValue = 14, Group = "Moving Averages")]publicintMA1Period{get;set;}[Parameter("MA 1 Timeframe", DefaultValue = "Hour", Group = "Moving Averages")]publicTimeFrameMA1Timeframe{get;set;}[Parameter("MA 1 Type", Group = "Moving Averages")]publicMovingAverageTypeMA1Type{get;set;}
Then, we can define the moving average.
1
privateMovingAverage_ma1;
We can initialise the moving average in the OnStart() method. We will use the MarketData.GetBars() method to get the bars of the first moving average and pass them to the indicator constructor. GetBars() method is a very handy method for getting bar data for any timeframe and any symbol you need.
At this point, we can implement the trading logic. Our strategy will hold a buy position when all moving averages are rising and a sell position when moving averages are falling. Here is the code for all buy-side logic.
Last but not least, we should backtest our new cBot. We can see that the cBot enters positions when the moving averages are synchronised to the same direction and stays out of the market when each indicator points to a different direction.
We hope that you found this video and article helpful. Feel free to ask any questions at the bottom of this video and subscribe to be updated when we publish a new video.