Description
I downloaded a simple Macd crossover algo that was free because I'm trying to learn. Everything seems fine but what do I know right? It compiles but places no trades.
Please help. Code below. I also cut out the trading hours thing, just to be sure it wasn't somehow interfering. There was also a reference to Trade.Executing or something similar that was deprecated and had to be removed. The original file sans changes is attached.
Thanks a million
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class MACDMarketTimerV2 : Robot
{
[Parameter("Sentiment: Buy", DefaultValue = true)]
public bool Buy { get; set; }
[Parameter("Sentiment: Sell", DefaultValue = true)]
public bool Sell { get; set; }
[Parameter("MME Slow", Group = "MA", DefaultValue = 16)]
public int mmeSlow { get; set; }
[Parameter("MME Fast", Group = "MA", DefaultValue = 12)]
public int mmeFast { get; set; }
[Parameter("Source", Group = "RSI")]
public DataSeries Source { get; set; }
[Parameter("Periods", Group = "RSI", DefaultValue = 19)]
public int Periods { get; set; }
// [Parameter("Start Hour", DefaultValue = 10.0)]
// public double StartTime { get; set; }
// [Parameter("Stop Hour", DefaultValue = 12.0)]
// public double StopTime { get; set; }
[Parameter(" Period", Group="MACD",DefaultValue = 9)]
public int Period { get; set; }
[Parameter(" Long Cycle",Group="MACD", DefaultValue = 26)]
public int LongCycle { get; set; }
[Parameter(" Short Cycle",Group="MACD", DefaultValue = 12)]
public int ShortCycle { get; set; }
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("Stop Loss ", DefaultValue = 100)]
public int StopLoss { get; set; }
[Parameter("Take Profit", DefaultValue = 100)]
public int TakeProfit { get; set; }
private MovingAverage i_MA_slow;
private MovingAverage i_MA_fast;
private RelativeStrengthIndex rsi;
// private DateTime _startTime;
// private DateTime _stopTime;
private MacdCrossOver macd;
private double volumeInUnits;
protected override void OnStart()
{
i_MA_slow = Indicators.MovingAverage(Bars.ClosePrices, mmeSlow, MovingAverageType.Exponential);
i_MA_fast = Indicators.MovingAverage(Bars.ClosePrices, mmeFast, MovingAverageType.Exponential);
rsi = Indicators.RelativeStrengthIndex(Source, Periods);
macd=Indicators.MacdCrossOver(LongCycle, ShortCycle, Period);
volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
{
// _startTime = Server.Time.Date.AddHours(StartTime);
// _stopTime = Server.Time.Date.AddHours(StopTime);
// Print("Start Time {0},", _startTime);
// Print("Stop Time {0},", _stopTime);
}
}
protected override void OnBar()
{
var MACDLine = macd.MACD.Last(1);
var PrevMACDLine = macd.MACD.Last(2);
var Signal = macd.Signal.Last(1);
var PrevSignal= macd.Signal.Last(2);
//var currentHours = Server.Time.TimeOfDay.TotalHours;
// bool tradeTime = StartTime < StopTime
// ? currentHours > StartTime && currentHours < StopTime
// : currentHours < StopTime || currentHours > StartTime;
// if (!tradeTime)
// return;
if (rsi.Result.LastValue > 25 && rsi.Result.LastValue < 70)
{
if ((MACDLine > Signal && PrevMACDLine <PrevSignal && default==Sell) && (i_MA_fast.Result.LastValue > i_MA_slow.Result.LastValue))
{
ExecuteMarketOrder( TradeType.Buy ,SymbolName,volumeInUnits, "MACDMarketTimerV2,RSI,MACD",StopLoss,TakeProfit);
}
else if ( (MACDLine < Signal && PrevMACDLine >PrevSignal && default== Buy)&(i_MA_fast.Result.LastValue < i_MA_slow.Result.LastValue))
{
var result = ExecuteMarketOrder( TradeType.Sell ,SymbolName,volumeInUnits, " MACDMarketTimerV2,RSI,MACD",StopLoss,TakeProfit);
if (result.Error == ErrorCode.NoMoney)
Stop();
}
}
}
protected override void OnStop()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class MACDMarketTimerV2 : Robot
{
[Parameter("Sentiment: Buy", DefaultValue = true)]
public bool Buy { get; set; }
[Parameter("Sentiment: Sell", DefaultValue = true)]
public bool Sell { get; set; }
[Parameter("MME Slow", Group = "MA", DefaultValue = 16)]
public int mmeSlow { get; set; }
[Parameter("MME Fast", Group = "MA", DefaultValue = 12)]
public int mmeFast { get; set; }
[Parameter("Source", Group = "RSI")]
public DataSeries Source { get; set; }
[Parameter("Periods", Group = "RSI", DefaultValue = 19)]
public int Periods { get; set; }
[Parameter("Start Hour", DefaultValue = 10.0)]
public double StartTime { get; set; }
[Parameter("Stop Hour", DefaultValue = 12.0)]
public double StopTime { get; set; }
[Parameter(" Period", Group="MACD",DefaultValue = 9)]
public int Period { get; set; }
[Parameter(" Long Cycle",Group="MACD", DefaultValue = 26)]
public int LongCycle { get; set; }
[Parameter(" Short Cycle",Group="MACD", DefaultValue = 12)]
public int ShortCycle { get; set; }
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("Stop Loss ", DefaultValue = 100)]
public int StopLoss { get; set; }
[Parameter("Take Profit", DefaultValue = 100)]
public int TakeProfit { get; set; }
private MovingAverage i_MA_slow;
private MovingAverage i_MA_fast;
private RelativeStrengthIndex rsi;
private DateTime _startTime;
private DateTime _stopTime;
private MacdCrossOver macd;
private double volumeInUnits;
protected override void OnStart()
{
i_MA_slow = Indicators.MovingAverage(Bars.ClosePrices, mmeSlow, MovingAverageType.Exponential);
i_MA_fast = Indicators.MovingAverage(Bars.ClosePrices, mmeFast, MovingAverageType.Exponential);
rsi = Indicators.RelativeStrengthIndex(Source, Periods);
macd=Indicators.MacdCrossOver(LongCycle, ShortCycle, Period);
volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
{
_startTime = Server.Time.Date.AddHours(StartTime);
_stopTime = Server.Time.Date.AddHours(StopTime);
Print("Start Time {0},", _startTime);
Print("Stop Time {0},", _stopTime);
}
}
protected override void OnBar()
{
var MACDLine = macd.MACD.Last(1);
var PrevMACDLine = macd.MACD.Last(2);
var Signal = macd.Signal.Last(1);
var PrevSignal= macd.Signal.Last(2);
if (Trade.IsExecuting) return;
var currentHours = Server.Time.TimeOfDay.TotalHours;
bool tradeTime = StartTime < StopTime
? currentHours > StartTime && currentHours < StopTime
: currentHours < StopTime || currentHours > StartTime;
if (!tradeTime)
return;
{
if (rsi.Result.LastValue > 25 && rsi.Result.LastValue < 70)
{
if ((MACDLine > Signal & PrevMACDLine <PrevSignal & default==Sell) & (i_MA_fast.Result.LastValue > i_MA_slow.Result.LastValue))
{
ExecuteMarketOrder( TradeType.Buy ,SymbolName,volumeInUnits, "MACDMarketTimerV2,RSI,MACD",StopLoss,TakeProfit);
}
else if ( (MACDLine < Signal & PrevMACDLine >PrevSignal && default== Buy)&(i_MA_fast.Result.LastValue < i_MA_slow.Result.LastValue))
{
ExecuteMarketOrder( TradeType.Sell ,SymbolName,volumeInUnits, " MACDMarketTimerV2,RSI,MACD",StopLoss,TakeProfit);
}
}
}
}
protected override void OnStop()
{
}
}
}
shane.scott.pub
Joined on 22.01.2023
- Distribution: Free
- Language: C#
- Trading platform: cTrader Automate
- File name: MACD Market Timer V2.algo
- Rating: 0
- Installs: 779
- Modified: 22/01/2023 21:57
Comments
Hi All, can i know for the Start Hour and Stop Hour are refer to which country GMT?
Thank you.
Wow, cool post. I'd like to write like this too - taking time and real hard work to make a great article... but I put things off too much and never seem to get started. Thanks though. 신용카드 현금화
This is highly informatics, crisp and clear. I think that everything has been described in systematic manner so that reader could get maximum information and learn many things. Natrium Pentobarbital Kaufen
but the idea of this bot is just to choose one side of the trend if you select sell and buy it will not open orders.
Thanks for the contribution, the idea is to make mistakes and learn. We helped each other, and we came up with something better.
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class MACDMarketTimerV2 : Robot
{
[Parameter("Label", Group = "Label", DefaultValue = " MACDMarketTimerV2,RSI,MACD")]
public string Label { get; set; }
[Parameter("Sentiment: Buy", Group = "Basic Setup", DefaultValue = true)]
public bool Buy { get; set; }
[Parameter("Sentiment: Sell", Group = "Basic Setup", DefaultValue = true)]
public bool Sell { get; set; }
[Parameter("Max Trades", Group = "Basic Setup", DefaultValue = 1, MinValue = 1)]
public int TradeCount { get; set; }
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("Stop Loss ", Group = "Protection", DefaultValue = 100)]
public int StopLoss { get; set; }
[Parameter("Take Profit", Group = "Protection", DefaultValue = 100)]
public int TakeProfit { get; set; }
[Parameter("Start Hour", Group = "Market Time", DefaultValue = 10.0)]
public double StartTime { get; set; }
[Parameter("Stop Hour", Group = "Market Time", DefaultValue = 12.0)]
public double StopTime { get; set; }
[Parameter("MME Slow", Group = "MA", DefaultValue = 16)]
public int MmeSlow { get; set; }
[Parameter("MME Fast", Group = "MA", DefaultValue = 12)]
public int MmeFast { get; set; }
[Parameter("Source", Group = "RSI")]
public DataSeries Source { get; set; }
[Parameter("Periods", Group = "RSI", DefaultValue = 19)]
public int Periods { get; set; }
[Parameter(" Period", Group="MACD",DefaultValue = 9)]
public int Period { get; set; }
[Parameter(" Long Cycle",Group="MACD", DefaultValue = 26)]
public int LongCycle { get; set; }
[Parameter(" Short Cycle",Group="MACD", DefaultValue = 12)]
public int ShortCycle { get; set; }
private MovingAverage i_MA_slow;
private MovingAverage i_MA_fast;
private RelativeStrengthIndex rsi;
private DateTime Starttime;
private DateTime Stoptime;
private MacdCrossOver macd;
private double volumeInUnits;
protected override void OnStart()
{
i_MA_slow = Indicators.MovingAverage(Bars.ClosePrices, MmeSlow, MovingAverageType.Exponential);
i_MA_fast = Indicators.MovingAverage(Bars.ClosePrices, MmeFast, MovingAverageType.Exponential);
rsi = Indicators.RelativeStrengthIndex(Source, Periods);
macd=Indicators.MacdCrossOver(LongCycle, ShortCycle, Period);
volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
Starttime = Server.Time.Date.AddHours(StartTime);
Stoptime = Server.Time.Date.AddHours(StopTime);
}
protected override void OnBar()
{
if (!MarketTime())
return;
if (Positions.FindAll(Label, SymbolName).Length <= TradeCount)
{
SendTrade();
}
}
private void SendTrade()
{
var MACDLine = macd.MACD.Last(1);
var PrevMACDLine = macd.MACD.Last(2);
var Signal = macd.Signal.Last(1);
var PrevSignal = macd.Signal.Last(2);
if (rsi.Result.LastValue > 25 && rsi.Result.LastValue < 70)
{
if (MACDLine > Signal && PrevMACDLine < PrevSignal && Buy)
{
if (i_MA_fast.Result.LastValue > i_MA_slow.Result.LastValue)
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, volumeInUnits, Label, StopLoss, TakeProfit);
}
}
else if (MACDLine < Signal && PrevMACDLine > PrevSignal && Sell)
{
if (i_MA_fast.Result.LastValue < i_MA_slow.Result.LastValue)
{
ExecuteMarketOrder(TradeType.Sell, SymbolName, volumeInUnits, Label, StopLoss, TakeProfit);
}
}
}
}
private bool MarketTime()
{
var CorrentHour = Server.Time.TimeOfDay.TotalHours;
if (CorrentHour > StartTime && CorrentHour < StopTime)
return true;
else
return false;
}
protected override void OnError(Error result)
{
if (result.Code == ErrorCode.NoMoney)
Stop();
}
}
}
I am also learning. I started a few days ago, but I'm very focused.
If you don't mind, I made some changes, like the organization in the code structure, which is something important, and the option to decide how many operations can be performed in a row.
But, answering your question, I believe that what you did wrong is having mentioned Default==Sell and Default==Buy. In this case, it is only necessary to mention only the variant, which, if true, will follow the code.
Hi Shane and Carneiroads, firstly this is better than normal program that consist of using 3 indicators.
Good work from the founder idea.
If it can still be amend with below idea will serve another purpose.
MACD crossover as open trade.
RSI as close trade.
MME as close trade.
By the way, can please clarify below parameters meaning:
Start Hour / Stop Hour follow Server time = GMT 0 or timezone 0
MA MME= Exponential Moving Average (EMA)
Please correct me if wrong.
Gladly if can hear any feedback from both of you.
Thank you.