Description
This bot use MACD Cross Over
1. Signal-line crossover
2. Zero crossover
using cAlgo.API;
using cAlgo.API.Indicators;
using System;
using System.Linq;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class NdnghiaMACD : Robot
{
private MacdCrossOver _MACD;
[Parameter("Initial Volume Percent", DefaultValue = 1, MinValue = 0)]
public double InitialVolumePercent { get; set; }
[Parameter("Period", DefaultValue = 9)]
public int Period { get; set; }
[Parameter("Long Cycle", DefaultValue = 26)]
public int LongCycle { get; set; }
[Parameter("Short Cycle", DefaultValue = 12)]
public int ShortCycle { get; set; }
[Parameter("Stop Loss", DefaultValue = 100)]
public int StopLoss { get; set; }
[Parameter("Take Profit", DefaultValue = 100)]
public int TakeProfit { get; set; }
[Parameter("Signal-line crossover true:if Signal-line crossover false: Zero crossover", DefaultValue = true)]
public bool IsSignalLineCrossover { get; set; }
protected override void OnStart()
{
_MACD = Indicators.MacdCrossOver(LongCycle, ShortCycle, Period);
}
protected override void OnBar()
{
var volumne = Math.Floor(Account.Balance * 10 * InitialVolumePercent / 1000) * 1000;
if (IsSignalLineCrossover)
{
if (_MACD.MACD.Last(2) < _MACD.Signal.Last(2) && _MACD.MACD.Last(1) > _MACD.Signal.Last(1))
{
var position = Positions.Find("NdnghiaMACD");
if (position != null && position.TradeType == TradeType.Sell)
{
position.Close();
}
ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volumne, "NdnghiaMACD", 0, 0);
// ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volumne, "NdnghiaMACD", StopLoss, TakeProfit);
}
if (_MACD.MACD.Last(2) > _MACD.Signal.Last(2) && _MACD.MACD.Last(1) < _MACD.Signal.Last(1))
{
var position = Positions.Find("NdnghiaMACD");
if (position != null && position.TradeType == TradeType.Buy)
{
position.Close();
}
ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volumne, "NdnghiaMACD", 0, 0);
// ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volumne, "NdnghiaMACD", StopLoss, TakeProfit);
}
}
//Zero cross over
else
{
if (_MACD.MACD.Last(1) > 0 && _MACD.MACD.Last(2) < 0)
{
//up
ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volumne, "NdnghiaMACD", StopLoss, TakeProfit);
}
else if (_MACD.MACD.Last(1) < 0 && _MACD.MACD.Last(2) > 0)
{
//Down
ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volumne, "NdnghiaMACD", StopLoss, TakeProfit);
}
}
}
}
}
nghiand.amz
Joined on 06.07.2020
- Distribution: Free
- Language: C#
- Trading platform: cTrader Automate
- File name: NdnghiaMACD.algo
- Rating: 0
- Installs: 3153
- Modified: 13/10/2021 09:54
Warning! Running cBots downloaded from this section may lead to financial losses. Use them at your own risk.
Note that publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section, please use the Copyright Infringement Notification form to submit a claim.
Comments
Log in to add a comment.
DA
Nguyen, for some reason the bot does not set TP and SL, at least not for small values below 10. Could you look into this? I think your bot has a lot of potential and i would love to use but it's useless without TP and SL being set by the bot.
VI
It works only on Forex and not in CFD
Somebody can tell me how can I make this EA just with Long or short. What I hould add to the code?
CA
Doesnt do anything when trying to backtest it
nevermind….. i fixed it.