using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
/*
This code was created using the Algo Strategy Building Tool by ClickAlgo Limited
https://clickalgo.com/strategy-builder
This cBot does not guarantee any particular outcome or
profit of any kind. Use it at your own risk.
Created: 19/12/2024
Name: MACD crossover v3
MACD crossover
*/
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class AlgoBuilderBot : Robot
{
// user parameter settings
[Parameter("Risk % Balance", Group = "Protection", DefaultValue = 1, MinValue = 0.01, Step = 1)]
public double RiskPerTrade { get; set; }
[Parameter("Stop loss", Group = "Protection", DefaultValue = 0)]
public int StopLoss { get; set; }
[Parameter("Take profit", Group = "Protection", DefaultValue = 0)]
public int TakeProfit { get; set; }
[Parameter("Close on reverse", Group = "Protection", DefaultValue = true)]
public bool CloseOnReverse { get; set; }
// Indicator settings for bullish signals
[Parameter("Timeframe", DefaultValue = "m1", Group = "MACD Crossover Buy #1")]
public TimeFrame MacdTimeFrameBuy1 { get; set; }
[Parameter("Signal Periods", DefaultValue = 9, Group = "MACD Crossover Buy #1")]
public int MacdPeriodsBuy1 { get; set; }
[Parameter("MACD Lower", DefaultValue = -0.001, Group = "MACD Crossover Buy #1")]
public double MacdLowerBuy1 { get; set; }
[Parameter("Long Cycle", DefaultValue = 200, Group = "MACD Crossover Buy #1")]
public int MacdLongCycleBuy1 { get; set; }
[Parameter("Short Cycle", DefaultValue = 20, Group = "MACD Crossover Buy #1")]
public int MacdShortCycleBuy1 { get; set; }
// Indicator settings for bearish signals
[Parameter("Timeframe", DefaultValue = "m1", Group = "MACD Crossover Sell #1")]
public TimeFrame MacdTimeFrameSell1 { get; set; }
[Parameter("Signal Periods", DefaultValue = 9, Group = "MACD Crossover Sell #1")]
public int MacdPeriodsSell1 { get; set; }
[Parameter("MACD Upper", DefaultValue = 0.001, Group = "MACD Crossover Sell #1")]
public double MacdUpperSell1 { get; set; }
[Parameter("Long Cycle", DefaultValue = 200, Group = "MACD Crossover Sell #1")]
public int MacdLongCycleSell1 { get; set; }
[Parameter("Short Cycle", DefaultValue = 20, Group = "MACD Crossover Sell #1")]
public int MacdShortCycleSell1 { get; set; }
// declaring the standard bullish indicators
private MacdCrossOver _macd_Buy1;
// declaring the standard bearish indicators
private MacdCrossOver _macd_Sell1;
// declaring the market data variables for bullish signals.
private Bars _macdBars_Buy1;
// declaring the market data variables for bearish signals.
private Bars _macdBars_Sell1;
// Private fields.
private string StrategyName { get; set; }
// makes sure only 1 trade opens per signal
bool IsBullish { get; set; }
bool IsBearish { get; set; }
// Initialise variables when the cBot starts.
protected override void OnStart()
{
StrategyName = "MACD crossover v3";
// constructing the market data for the bullish indicators
_macdBars_Buy1 = MarketData.GetBars(MacdTimeFrameBuy1);
// constructing the market data for the bearish indicators
_macdBars_Sell1 = MarketData.GetBars(MacdTimeFrameSell1);
// constructing the BULLISH indicators
_macd_Buy1 = Indicators.MacdCrossOver(_macdBars_Buy1.ClosePrices, MacdLongCycleBuy1, MacdShortCycleBuy1, MacdPeriodsBuy1);
// constructing the BEARISH indicators
_macd_Sell1 = Indicators.MacdCrossOver(_macdBars_Sell1.ClosePrices, MacdLongCycleSell1, MacdShortCycleSell1, MacdPeriodsSell1);
}
// The onTick method is called on each price change.
protected override void OnTick()
{
}
// this method is called when the candle closes and is used to check the trade signals, execute trades and send alerts.
protected override void OnBarClosed()
{
TradeRulesBullish();
TradeRulesBearish();
}
// parent method to check rules and open bullish trade or send signal.
private void TradeRulesBullish()
{
// flag to open a trade if all rules true.
bool OpenBuyTrade = false;
if (IsBullishSignal())
{
OpenBuyTrade = true;
}
if (OpenBuyTrade)
{
if (!IsBullish)
{
if (!IsTradeOpen(TradeType.Buy))
{
OpenTrade(TradeType.Buy);
}
}
IsBullish = true;
if(CloseOnReverse)
{
CloseTrade(TradeType.Sell);
}
}
else
{
IsBullish = false;
}
}
// parent method to check rules and open bearish trade or send signal.
private void TradeRulesBearish()
{
// flag to open a trade if all rules true.
bool OpenSellTrade = false;
if (IsBearishSignal())
{
OpenSellTrade = true;
}
if (OpenSellTrade)
{
if (!IsBearish)
{
if (!IsTradeOpen(TradeType.Sell))
{
OpenTrade(TradeType.Sell);
}
}
IsBearish = true;
if(CloseOnReverse)
{
CloseTrade(TradeType.Buy);
}
}
else
{
IsBearish = false;
}
}
// returns true if all signals are bullish
private bool IsBullishSignal()
{
if (_macd_Buy1.MACD.LastValue !> _macd_Buy1.Signal.LastValue && _macd_Buy1.Signal.LastValue !< MacdLowerBuy1)
{
return false;
}
return true;
}
// returns true if all signals are bearish
private bool IsBearishSignal()
{
if (_macd_Sell1.MACD.LastValue !< _macd_Sell1.Signal.LastValue && _macd_Sell1.Signal.LastValue !> MacdUpperSell1)
{
return false;
}
return true;
}
private void OpenTrade(TradeType type)
{
// calculate volume from lots based on % of the account balance
var maxAmountRisked = Account.Equity * (RiskPerTrade / 100);
var volume = Symbol.NormalizeVolumeInUnits(maxAmountRisked / (StopLoss * Symbol.PipValue), RoundingMode.Down);
ExecuteMarketOrder(type, SymbolName, volume, StrategyName, StopLoss, TakeProfit);
}
// returns true if buy trade is open.
private bool IsTradeOpen(TradeType type)
{
var positions = Positions.FindAll(StrategyName, SymbolName, type);
if (positions.Count() > 0)
{
return true;
}
else return false;
}
// closes the trade with the opposite signal.
private void CloseTrade(TradeType type)
{
var positions = Positions.FindAll(StrategyName, SymbolName, type);
foreach (var position in positions)
{
position.Close();
}
}
}
}