Need Trailing Stop
Need Trailing Stop
28 Nov 2016, 21:09
Can somebody add trailing stops for this robot pls?
// -------------------------------------------------------------------------------------------------
//
// This code is a cAlgo API sample.
//
// This cBot 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 "Sample Trend cBot" will buy when fast period moving average crosses the slow period moving average and sell when
// the fast period moving average crosses the slow period moving average. The orders are closed when an opposite signal
// is generated. There can only by one Buy or Sell order at any time.
//
// -------------------------------------------------------------------------------------------------
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 SampleTrend : Robot
{
[Parameter("MA Type")]
public MovingAverageType MAType { get; set; }
[Parameter()]
public DataSeries SourceSeries { get; set; }
[Parameter("Stop Loss (pips)", DefaultValue = 20, MinValue = 1)]
public int StopLossInPips { get; set; }
[Parameter("Take Profit (pips)", DefaultValue = 60, MinValue = 1)]
public int TakeProfitInPips { get; set; }
[Parameter("Slow Periods", DefaultValue = 65)]
public int SlowPeriods { get; set; }
[Parameter("Fast Periods", DefaultValue = 15)]
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";
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, VolumeInUnits, label, StopLossInPips, TakeProfitInPips);
}
else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && shortPosition == null)
{
if (longPosition != null)
ClosePosition(longPosition);
ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, label, StopLossInPips, TakeProfitInPips);
}
}
private long VolumeInUnits
{
get { return Symbol.QuantityToVolume(Quantity); }
}
}
}
tradermatrix
28 Nov 2016, 21:51
hi
Start if trigger > 0
@tradermatrix