Topics
Replies
Emilio90
25 Apr 2023, 12:01
RE: info
Spotware said:
This robot is intended to be used as a sample and does not guarantee any particular outcome or profit of any kind. Use it at your own risk.
// ------------------------------------------------------------------------------------------------- // // This robot is intended to be used as a sample and does not guarantee any particular outcome or // profit of any kind. Use it at your own risk // // The "Trailing Stop Loss Sample" Robot places a Buy or Sell Market order according to user input. // When the order is filled it implements trailing stop loss. // // ------------------------------------------------------------------------------------------------- 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 TrailingStopLossSample : Robot { [Parameter("Volume", DefaultValue = 1000)] public int Volume { get; set; } [Parameter("Buy")] public bool Buy { get; set; } [Parameter("Stop Loss", DefaultValue = 5)] public double StopLoss { get; set; } [Parameter("Trigger When Gaining", DefaultValue = 1)] public double TriggerWhenGaining { get; set; } [Parameter("Trailing Stop Loss Distance", DefaultValue = 1)] public double TrailingStopLossDistance { get; set; } private double _highestGain; private bool _isTrailing; protected override void OnStart() { //Execute a market order based on the direction parameter ExecuteMarketOrder(Buy ? TradeType.Buy : TradeType.Sell, Symbol, Volume, "SampleTrailing", StopLoss, null); //Set the position's highest gain in pips _highestGain = Positions[0].Pips; } protected override void OnTick() { var position = Positions.Find("SampleTrailing"); if (position == null) { Stop(); return; } //If the trigger is reached, the robot starts trailing if (!_isTrailing && position.Pips >= TriggerWhenGaining) { _isTrailing = true; } //If the cBot is trailing and the profit in pips is at the highest level, we need to readjust the stop loss if (_isTrailing && _highestGain < position.Pips) { //Based on the position's direction, we calculate the new stop loss price and we modify the position if (position.TradeType == TradeType.Buy) { var newSLprice = Symbol.Ask - (Symbol.PipSize * TrailingStopLossDistance); if (newSLprice > position.StopLoss) { ModifyPosition(position, newSLprice, null); } } else { var newSLprice = Symbol.Bid + (Symbol.PipSize * TrailingStopLossDistance); if (newSLprice < position.StopLoss) { ModifyPosition(position, newSLprice, null); } } //We reset the highest gain _highestGain = position.Pips; } } protected override void OnStop() { // Put your deinitialization logic here } } }
can you add a take profit? is it possible doing more operations?
@Emilio90
Emilio90
06 Dec 2020, 22:43
( Updated at: 07 Dec 2020, 01:45 )
RE: RE: RE: RE:
leonardohurtado said:
netmstnet said:
leonardohurtado said:
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.
Try this:
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; var longPosition = Positions.Find(label, SymbolName, TradeType.Buy); var shortPosition = Positions.Find(label, SymbolName, TradeType.Sell); 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]) && shortPosition == null) { if (longPosition != null) ClosePosition(longPosition); 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]) && longPosition == null) { if (shortPosition != null) ClosePosition(shortPosition); ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, label, SL, TP); } } private long VolumeInUnits { get { return Symbol.QuantityToVolume(Quantity); } } } }
This is exactly what I was looking for, thank you so much!
Have a good one, cheers.
Leonardo.
is it possible add trailing stop and break even?
@Emilio90
Emilio90
17 May 2023, 08:36
RE:
firemyst said:
Thanks to reply me, the problem is that i’m not capable to write a code.
@Emilio90