Add take profit on SMA Cbot
Created at 05 Aug 2019, 05:44
BR
Add take profit on SMA Cbot
05 Aug 2019, 05:44
Hi guys,
Appreciate if anyone can help with adding Take Profit with this code
Parameter TakeProfit is there but it is not placed in the action of the code.
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class TrendRobot : Robot { [Parameter("MA Type")] public MovingAverageType MAType { get; set; } [Parameter()] public DataSeries SourceSeries { get; set; } [Parameter("Slow Periods", DefaultValue = 10)] public int SlowPeriods { get; set; } [Parameter("Fast Periods", DefaultValue = 5)] public int FastPeriods { get; set; } [Parameter(DefaultValue = 10000, MinValue = 0)] public int Volume { get; set; } [Parameter("Take Profit", DefaultValue = 40, MinValue = 0, Step = 10)] public double TakeProfit { get; set; } private MovingAverage slowMa; private MovingAverage fastMa; private const string label = "Sample Trend Robot"; protected override void OnStart() { fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType); slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType); } protected override void OnTick() { var longPosition = Positions.Find(label, Symbol, TradeType.Buy); var shortPosition = Positions.Find(label, Symbol, 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 && longPosition == null) { if (shortPosition != null) ClosePosition(shortPosition); ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label); } else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && shortPosition == null) { if (longPosition != null) ClosePosition(longPosition); ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label); } } } }
Symposium
05 Aug 2019, 07:41
I have coded a version with a Trailing Stop, Let me know if you want it.? For the code above.. I have added the SL as well... just set it to zero if it's not being used....
Hope this is of help... Cheers
@Symposium