Higher Timeframe filter on a cBot.
Higher Timeframe filter on a cBot.
15 Mar 2023, 11:56
Greetings,
Am playing around with an EMA crossover bot but I’d like to include a higher-time frame filter to it so that it only takes trades in the lower time frame that are in agreement with higher timeframe signals (i.e., go long when the fast EMA crosses above the slow EMA and the H2 fast EMA is also above its slow EMA)
Below is my code which has an error "Error CS1501:No overload for method "HasCrossedAbove" takes 1 argument."
Any help will be much appreciated.
The original code is not mine but from CTrader’s “Sample Trend cBot”
Regards.
// -------------------------------------------------------------------------------------------------
//
// This code is a cTrader Automate API example.
//
// This cBot is intended to be used as a sample and does not guarantee any particular outcome or
// profit of any kind. Use it at your own risk.
//
// All changes to this file might be lost on the next application update.
// If you are going to modify this file please make a copy using the "Duplicate" command.
//
// The "Sample Trend cBot" will buy when fast period moving average crosses the slow period moving average and sell when
// the fast period moving average crosses the slow period moving average. The orders are closed when an opposite signal
// is generated. There can only by one Buy or Sell order at any time.
//
// -------------------------------------------------------------------------------------------------
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class SampleTrendcBot4EMAVSEMA : Robot
{
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("MA Type", Group = "Moving Average")]
public MovingAverageType MAType { get; set; }
[Parameter("Source", Group = "Moving Average")]
public DataSeries SourceSeries { get; set; }
[Parameter("Slow Periods", Group = "Moving Average", DefaultValue = 22)]
public int SlowPeriods { get; set; }
[Parameter("Fast Periods", Group = "Moving Average", DefaultValue = 11)]
public int FastPeriods { get; set; }
[Parameter("Slow Periods H2", Group = "Moving Average", DefaultValue = 22)]
public int SlowPeriodsH2 {get; set;}
[Parameter("Fast Periods H2", Group = "Moving Average", DefaultValue = 11)]
public int FastPeriodsH2 {get; set;}
[Parameter(DefaultValue = 0.0)]
public double TakeProfit { get; set; }
[Parameter(DefaultValue = 0.0)]
public double StopLoss { get; set; }
private MovingAverage slowMa;
private MovingAverage fastMa;
private Bars H2;
private MovingAverage SlowMaH2;
private MovingAverage FastMaH2;
private const string label = "Sample Trend cBot";
protected override void OnStart()
{
fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
H2 = MarketData.GetBars(TimeFrame.Hour2);
FastMaH2 = Indicators.MovingAverage (H2.ClosePrices,FastPeriodsH2, MAType);
SlowMaH2 = Indicators.MovingAverage(H2.ClosePrices, SlowPeriodsH2, MAType);
}
protected override void OnTick()
{
var position = Positions.Find("MyLabel");
if (position !=null && position.GrossProfit >10)
{
ClosePosition(position);
Stop();
}
var longPosition = Positions.Find(label, SymbolName, TradeType.Buy);
var shortPosition = Positions.Find(label, SymbolName, TradeType.Sell);
var currentSlowMa = slowMa.Result.Last(0);
var currentFastMa = fastMa.Result.Last(0);
var previousSlowMa = slowMa.Result.Last(1);
var previousFastMa = fastMa.Result.Last(1);
if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa &&
FastMaH2.Result.HasCrossedAbove (SlowMaH2.Result) & longPosition == null)
{
if (shortPosition != null)
ClosePosition(shortPosition);
ExecuteMarketOrder(TradeType.Buy, SymbolName, VolumeInUnits, label);
}
else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa &&
FastMaH2.Result.HasCrossedBelow(SlowMaH2.Result) && shortPosition == null)
{
if (longPosition != null)
ClosePosition(longPosition);
ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, label);
}
}
private double VolumeInUnits
{
get { return Symbol.QuantityToVolumeInUnits(Quantity); }
}
}
}
Replies
j_matu
16 Mar 2023, 09:31
( Updated at: 21 Dec 2023, 09:23 )
RE:
PanagiotisChar said:
Hi there,
HasCrossedAbove and HasCrossedBelow take two arguments, not one. You also need to set the periods
Need help? Join us on Telegram
Need premium support? Trade with us
Thanks Panagiotis for your time and help. Unfortunately, am still stuck and have no idea how to move forward. How do I give it two arguments and set the periods?
Please do forgive me but am very new at programming and still learning.
Thanks, and much appreciated.
@j_matu
PanagiotisChar
16 Mar 2023, 08:34
Hi there,
HasCrossedAbove and HasCrossedBelow take two arguments, not one. You also need to set the periods
Aieden Technologies
Need help? Join us on Telegram
Need premium support? Trade with us
@PanagiotisChar