Help with adding a trailing stop
Created at 16 May 2015, 06:49
SH
Help with adding a trailing stop
16 May 2015, 06:49
How would I add a trailing stop to this algorithm?
Thanks
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 first_hand_written_code : Robot { //parameters [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("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)] public double Quantity { get; set; } private MovingAverage slowMa; private MovingAverage fastMa; private const string label = "Sample Trend Cbot"; //loads up needed indicators protected override void OnStart() { fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType); slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType); } protected override void OnTick() { //declaring variables during ticks 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); // when buy signal is called if (previousSlowMa > previousFastMa && currentSlowMa < currentFastMa && longPosition == null) { //if theres a short position in playy if (shortPosition != null) ClosePosition(shortPosition); ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, label); } //when short signal is called else if (previousSlowMa < previousFastMa && currentSlowMa > currentFastMa && shortPosition == null) { // if there are long positions in play if (longPosition != null) ClosePosition(longPosition); ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, label); } } private long VolumeInUnits { get { return Symbol.QuantityToVolume(Quantity); } } } }
Replies
Shawn_Mapilot
19 May 2015, 00:24
RE:
tradermatrix said:
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 first_hand_written_code : Robot { //parameters [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("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01)] public double lots { get; set; } [Parameter("TakeProfitPips", DefaultValue = 200)] public int tp { get; set; } [Parameter("StopLossPips", DefaultValue = 30)] public int sl { get; set; } [Parameter("trigger ", DefaultValue = 0)] public int Trigger { get; set; } [Parameter("Trailing", DefaultValue = 0)] public int Trailing { get; set; } private MovingAverage slowMa; private MovingAverage fastMa; private const string label = "Trend Trailing"; protected override void OnStart() { fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType); slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType); } protected override void OnTick() { TRAILING(); int volume = (int)(lots * 100000); //declaring variables during ticks 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); // when buy signal is called if (previousSlowMa > previousFastMa && currentSlowMa < currentFastMa && longPosition == null) { //if theres a short position in playy if (shortPosition != null) ClosePosition(shortPosition); ExecuteMarketOrder(TradeType.Buy, Symbol, volume, label, sl, tp); } //when short signal is called else if (previousSlowMa < previousFastMa && currentSlowMa > currentFastMa && shortPosition == null) { // if there are long positions in play if (longPosition != null) ClosePosition(longPosition); ExecuteMarketOrder(TradeType.Sell, Symbol, volume, label, sl, tp); } } private void TRAILING() { if (Trailing > 0 && Trigger > 0) { Position[] positions = Positions.FindAll(label, Symbol); foreach (Position position in positions) { if (position.TradeType == TradeType.Sell) { double distance = position.EntryPrice - Symbol.Ask; if (distance >= Trigger * Symbol.PipSize) { double newStopLossPrice = Symbol.Ask + Trailing * Symbol.PipSize; if (position.StopLoss == null || newStopLossPrice < position.StopLoss) { ModifyPosition(position, newStopLossPrice, position.TakeProfit); } } } else { double distance = Symbol.Bid - position.EntryPrice; if (distance >= Trigger * Symbol.PipSize) { double newStopLossPrice = Symbol.Bid - Trailing * Symbol.PipSize; if (position.StopLoss == null || newStopLossPrice > position.StopLoss) { ModifyPosition(position, newStopLossPrice, position.TakeProfit); } } } } } } } }good trades
Thank you :) !
@Shawn_Mapilot
tradermatrix
17 May 2015, 19:15
good trades
@tradermatrix