SH
Adding a martingale to my robot
30 Apr 2015, 05:46
How would I be able to allow the martingale approach to work with this robot? Thanks :)
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 SampleTrendRobot : Robot
{
[Parameter("MA Type")]
public MovingAverageType MAType { get; set; }
[Parameter("Source")]
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("RSIPeriods", DefaultValue = 14)]
public int RSIPeriods { get; set; }
private RelativeStrengthIndex rsi;
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);
rsi = Indicators.RelativeStrengthIndex(SourceSeries, RSIPeriods);
}
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 && rsi.Result.LastValue < 30 && longPosition == null)
{
if (shortPosition != null)
ClosePosition(shortPosition);
ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
}
else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && rsi.Result.LastValue > 70 && shortPosition == null)
{
if (longPosition != null)
ClosePosition(longPosition);
ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
}
}
}
}
Replies
deklin
03 May 2015, 15:31
Here is an example that adds a martingale approach to your bot, without changing the trading strategy:
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 SampleTrendRobot : Robot
{
[Parameter("MA Type")]
public MovingAverageType MAType { get; set; }
[Parameter("Source")]
public DataSeries SourceSeries { get; set; }
[Parameter("Slow Periods", DefaultValue = 8)]
public int SlowPeriods { get; set; }
[Parameter("Fast Periods", DefaultValue = 3)]
public int FastPeriods { get; set; }
[Parameter(DefaultValue = 1000, MinValue = 0)]
public int Volume { get; set; }
[Parameter("RSIPeriods", DefaultValue = 8)]
public int RSIPeriods { get; set; }
private RelativeStrengthIndex rsi;
private MovingAverage slowMa;
private MovingAverage fastMa;
private const string label = "Trend Martangle Robot";
private double TopBalance = 0;
protected override void OnStart()
{
TopBalance = Account.Balance;
fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
rsi = Indicators.RelativeStrengthIndex(SourceSeries, RSIPeriods);
}
protected override void OnTick()
{
if(Account.Balance>TopBalance) TopBalance=Account.Balance;
var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);
var position = Positions.Find(label, Symbol);
var currentSlowMa = slowMa.Result.Last(0);
var currentFastMa = fastMa.Result.Last(0);
var previousSlowMa = slowMa.Result.Last(1);
var previousFastMa = fastMa.Result.Last(1);
double v = Volume;
if (position != null && Account.Balance < TopBalance) v = position.Volume * 2;
if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa && rsi.Result.LastValue < 30 && longPosition == null)
{
if (shortPosition != null) {
ClosePosition(shortPosition);
}
ExecuteMarketOrder(TradeType.Buy, Symbol, Symbol.NormalizeVolume(v), label);
}
else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && rsi.Result.LastValue > 70 && shortPosition == null)
{
if (longPosition != null) {
ClosePosition(longPosition);
}
ExecuteMarketOrder(TradeType.Sell, Symbol, Symbol.NormalizeVolume(v), label);
}
}
}
}
Here is an example of a martingale approach to your bot with built-in stop loss and take profit levels:
// Test on USDCAD & EURUSD & AUDUSD
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 SampleTrendRobot : Robot
{
[Parameter("MA Type")]
public MovingAverageType MAType { get; set; }
[Parameter("Source")]
public DataSeries SourceSeries { get; set; }
[Parameter("Stop Loss", DefaultValue = 50)]
public int StopLoss { get; set; }
[Parameter("Take Profit", DefaultValue = 60)]
public int TakeProfit { get; set; }
[Parameter("Slow Periods", DefaultValue = 8)]
public int SlowPeriods { get; set; }
[Parameter("Fast Periods", DefaultValue = 3)]
public int FastPeriods { get; set; }
[Parameter(DefaultValue = 1000, MinValue = 0)]
public int Volume { get; set; }
[Parameter("RSIPeriods", DefaultValue = 8)]
public int RSIPeriods { get; set; }
private RelativeStrengthIndex rsi;
private MovingAverage slowMa;
private MovingAverage fastMa;
private const string label = "Trend Martangle Robot";
protected override void OnStart()
{
Positions.Closed += OnPositionsClosed;
fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
rsi = Indicators.RelativeStrengthIndex(SourceSeries, RSIPeriods);
}
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 && rsi.Result.LastValue < 30 && longPosition == null)
{
if (shortPosition != null)
ClosePosition(shortPosition);
ExecuteMarketOrder(TradeType.Buy, Symbol, Symbol.NormalizeVolume(Volume), label, StopLoss, TakeProfit);
}
else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && rsi.Result.LastValue > 70 && shortPosition == null)
{
if (longPosition != null)
ClosePosition(longPosition);
ExecuteMarketOrder(TradeType.Sell, Symbol, Symbol.NormalizeVolume(Volume), label, StopLoss, TakeProfit);
}
}
private void OnPositionsClosed(PositionClosedEventArgs args)
{
var position = args.Position;
if (position.GrossProfit < 0)
{
TradeType tt = TradeType.Sell;
if(position.TradeType==TradeType.Sell) tt = TradeType.Buy;
ExecuteMarketOrder(tt, Symbol, Symbol.NormalizeVolume(position.Volume * 2), "Martingale", StopLoss, TakeProfit);
}
}
}
}
I tested both of these with the USDCAD pair. I would not actually use either of these in a live-trading environment.
@deklin

.ics
01 May 2015, 19:29
Hi,
Check out the function "private void OnPositionsClosed(PositionClosedEventArgs args)" in "Sample Martingale cBot".
It contains all the code you need.
Greetings
@.ics