Topics
Replies
leonardohurtado
22 Jan 2020, 18:41
RE:
lucian said:
Start with this code:
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class EMACross_RSI : Robot { [Parameter("Source")] public DataSeries SourceSeries { get; set; } [Parameter("Label", DefaultValue = "EMA")] public string label { get; set; } [Parameter("Slow Periods", DefaultValue = 30)] public int SlowPeriods { get; set; } [Parameter("Medium Periods", DefaultValue = 12)] public int MediumPeriods { get; set; } [Parameter("Fast Periods", DefaultValue = 5)] public int FastPeriods { get; set; } [Parameter("Stop Loss", DefaultValue = 10)] public int SL { get; set; } [Parameter("Take Profit", DefaultValue = 10)] public double TP { get; set; } [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)] public double Quantity { get; set; } private ExponentialMovingAverage slowMa; private ExponentialMovingAverage mediumMa; private ExponentialMovingAverage fastMa; protected override void OnStart() { fastMa = Indicators.ExponentialMovingAverage(SourceSeries, FastPeriods); mediumMa = Indicators.ExponentialMovingAverage(SourceSeries, MediumPeriods); slowMa = Indicators.ExponentialMovingAverage(SourceSeries, SlowPeriods); } protected override void OnBar() { int index = MarketSeries.OpenTime.Count - 2; if ((fastMa.Result[index] > slowMa.Result[index]) && (mediumMa.Result[index] > slowMa.Result[index]) && (fastMa.Result[index - 1] < slowMa.Result[index - 1]) && (mediumMa.Result[index - 1] < slowMa.Result[index - 1])) { ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, label, SL, TP); } else if ((fastMa.Result[index] < slowMa.Result[index]) && (mediumMa.Result[index] < slowMa.Result[index]) && (fastMa.Result[index - 1] > slowMa.Result[index - 1]) && (mediumMa.Result[index - 1] > slowMa.Result[index - 1])) { ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, label, SL, TP); } } private long VolumeInUnits { get { return Symbol.QuantityToVolume(Quantity); } } } }Also you can download 3xEMA Indicator
Hello Lucian,
Thanks a lot for this coding, I wanted to ask you if there is a way of making it so it only takes 1 position at a time? Meaning, it will close the current position when the opposite signal happens.
I am not a coder, so if you could help me that would be super kind and appreciated.
Warm regards, Leonardo.
@leonardohurtado
leonardohurtado
12 Dec 2019, 23:22
RE:
PanagiotisCharalampous said:
Hi Leonardo,
See below
using System.Linq; using cAlgo.API; using System.IO; using System.Collections; using System.Collections.Generic; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)] public class MACD : Robot { [Parameter("Volume", DefaultValue = 1000)] public int volume { get; set; } [Parameter(DefaultValue = 50, MinValue = 1)] public int StopLoss { get; set; } [Parameter(DefaultValue = 50, MinValue = 1)] public int TakeProfit { get; set; } [Parameter("MACD LongCycle", DefaultValue = 26, MinValue = 1)] public int LongCycle { get; set; } [Parameter("MACD ShortCycle", DefaultValue = 12, MinValue = 1)] public int ShortCycle { get; set; } [Parameter("MACD Period", DefaultValue = 9, MinValue = 1)] public int MACDPeriod { get; set; } private MacdCrossOver _MACD; protected override void OnBar() { var dailySeries = MarketData.GetSeries(TimeFrame.Daily); _MACD = Indicators.MacdCrossOver(dailySeries.Close, LongCycle, ShortCycle, MACDPeriod); if (Positions.Count(x => x.TradeType == TradeType.Buy) == 0 && _MACD.MACD.Last(1) < _MACD.Signal.Last(1) && _MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0) { foreach (var position in Positions.Where(x => x.TradeType == TradeType.Sell)) position.Close(); ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5); } if (Positions.Count(x => x.TradeType == TradeType.Sell) == 0 && _MACD.MACD.Last(1) > _MACD.Signal.Last(1) && _MACD.MACD.Last(0) < _MACD.Signal.Last(0) && _MACD.Signal.Last(0) > 0) { foreach (var position in Positions.Where(x => x.TradeType == TradeType.Buy)) position.Close(); ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5); } } } }
Best Regards,
Panagiotis
I figured it out sir! Thank you again. Very much.
Best regards from Canada,
Leonardo Hurtado
@leonardohurtado
leonardohurtado
12 Dec 2019, 23:10
RE:
PanagiotisCharalampous said:
Hi Leonardo,
See below
using System.Linq; using cAlgo.API; using System.IO; using System.Collections; using System.Collections.Generic; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)] public class MACD : Robot { [Parameter("Volume", DefaultValue = 1000)] public int volume { get; set; } [Parameter(DefaultValue = 50, MinValue = 1)] public int StopLoss { get; set; } [Parameter(DefaultValue = 50, MinValue = 1)] public int TakeProfit { get; set; } [Parameter("MACD LongCycle", DefaultValue = 26, MinValue = 1)] public int LongCycle { get; set; } [Parameter("MACD ShortCycle", DefaultValue = 12, MinValue = 1)] public int ShortCycle { get; set; } [Parameter("MACD Period", DefaultValue = 9, MinValue = 1)] public int MACDPeriod { get; set; } private MacdCrossOver _MACD; protected override void OnBar() { var dailySeries = MarketData.GetSeries(TimeFrame.Daily); _MACD = Indicators.MacdCrossOver(dailySeries.Close, LongCycle, ShortCycle, MACDPeriod); if (Positions.Count(x => x.TradeType == TradeType.Buy) == 0 && _MACD.MACD.Last(1) < _MACD.Signal.Last(1) && _MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0) { foreach (var position in Positions.Where(x => x.TradeType == TradeType.Sell)) position.Close(); ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5); } if (Positions.Count(x => x.TradeType == TradeType.Sell) == 0 && _MACD.MACD.Last(1) > _MACD.Signal.Last(1) && _MACD.MACD.Last(0) < _MACD.Signal.Last(0) && _MACD.Signal.Last(0) > 0) { foreach (var position in Positions.Where(x => x.TradeType == TradeType.Buy)) position.Close(); ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5); } } } }
Best Regards,
Panagiotis
Thank you for the fast reply Mr. Panagiotis. I was wondering how to use this code, should I just add the entirety of it to Lucian's code. Or just replace some parts? I am pretty new with all of this. I truly appreciate your help. Best regards sir!
Leonardo Hurtado
@leonardohurtado
leonardohurtado
11 Dec 2019, 23:00
RE:
PanagiotisCharalampous said:
Hi D D,
Can you please explain what do you mean? I cannot see any problem in the example.
Best Regards,
Panagiotis
Hello Mr. Panagiotis.
I know this is an old threat, but I was wondering if you could help me. I would like for this indicator to only handle 1 trade at a time, which would imply the indicator closing the current trade when the MACD crosses in the opposite direction, and opening the corresponding trade for that crossing.
I would appreciate your help so much, warm regards.
Leonardo Hurtado
@leonardohurtado
leonardohurtado
27 Jan 2020, 22:32
RE: RE: RE:
netmstnet said:
This is exactly what I was looking for, thank you so much!
Have a good one, cheers.
Leonardo.
@leonardohurtado