Topics
Replies
collinganesh
25 Dec 2016, 18:15
Hello,
Can anyone help me to change my cBot EMA indicator from the normal candle stick EMA Close to a EMA that is used on a Renko Chart which uses a EMA RenkoChart close value to plot the EMA line. Sorry for the duplicate post.
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 martingale : 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("MA_Slow_Period", DefaultValue = 200)]
public int _MA_Slow_Period { get; set; }
[Parameter("MA_Fast_Period", DefaultValue = 50)]
public int _MA_Fast_Period { get; set; }
[Parameter(DefaultValue = 1000, MinValue = 0)]
public int Volume { get; set; }
private ExponentialMovingAverage i_Moving_Average;
private ExponentialMovingAverage i_Moving_Average_1;
private ExponentialMovingAverage i_Moving_Average_2;
private ExponentialMovingAverage i_Moving_Average_3;
private const string label = "Martingale";
bool _Compare;
bool _Compare_1;
bool _MA_cross_down;
bool _MA_cross_up;
DateTime LastTradeExecution = new DateTime(0);
protected override void OnStart()
{
Positions.Closed += OnPositionsClosed;
i_Moving_Average = Indicators.ExponentialMovingAverage(MarketSeries.Close, (int)_MA_Slow_Period);
i_Moving_Average_1 = Indicators.ExponentialMovingAverage(MarketSeries.Close, (int)_MA_Fast_Period);
i_Moving_Average_2 = Indicators.ExponentialMovingAverage(MarketSeries.Close, (int)_MA_Slow_Period);
i_Moving_Average_3 = Indicators.ExponentialMovingAverage(MarketSeries.Close, (int)_MA_Fast_Period);
}
protected override void OnTick()
{
if (Trade.IsExecuting)
return;
var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);
_Compare = (i_Moving_Average_3.Result.Last(0) >= i_Moving_Average_2.Result.Last(0));
_Compare_1 = (i_Moving_Average_1.Result.Last(1) >= i_Moving_Average.Result.Last(1));
_MA_cross_down = (!_Compare && _Compare_1);
_MA_cross_up = (_Compare && !_Compare_1);
if (_MA_cross_up && longPosition == null)
{
if (shortPosition != null)
ClosePosition(shortPosition);
ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLoss, TakeProfit);
}
else if (_MA_cross_down && shortPosition == null)
{
if (longPosition != null)
ClosePosition(longPosition);
ExecuteMarketOrder(TradeType.Sell, Symbol, 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);
}
}
}
}
@collinganesh
collinganesh
17 Dec 2016, 23:40
Thanks Lucian. The code works perfectly.
Merry Christmas to you.
@collinganesh
collinganesh
07 Nov 2016, 11:55
RE:
lucian 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 SlootWegte : Robot { [Parameter("Profit $", DefaultValue = 5.0)] public double _profit { get; set; } [Parameter("Loss $", DefaultValue = -3.0)] public double _loss { get; set; } [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)] public int _volume { get; set; } private Random random = new Random(); protected override void OnStart() { ExecuteOrder(_volume, GetRandomTradeType()); Positions.Closed += Positions_Closed; } protected override void OnTick() { foreach (var trade in Positions) { if ((trade.Label.StartsWith("SlootWege")) && ((trade.GrossProfit >= _profit) || (trade.GrossProfit <= _loss))) { ClosePosition(trade); } } } protected override void OnStop() { // Put your deinitialization logic here } private void Positions_Closed(PositionClosedEventArgs args) { if (args.Position.GrossProfit <= _loss) { if (args.Position.TradeType == TradeType.Buy) { ExecuteOrder(_volume, TradeType.Sell); } else { ExecuteOrder(_volume, TradeType.Buy); } } if (args.Position.GrossProfit >= _profit) { if (args.Position.TradeType == TradeType.Buy) { ExecuteOrder(_volume, TradeType.Buy); } else { ExecuteOrder(_volume, TradeType.Sell); } } } private void ExecuteOrder(long volume, TradeType tradeType) { var result = ExecuteMarketOrder(tradeType, Symbol, volume, "SlootWege", null, null); if (result.Error == ErrorCode.NoMoney) Stop(); } private TradeType GetRandomTradeType() { return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell; } } }Hi Lucian,
Is it posible for you to code the bot to open buystop orders when buy order stop loss is triggered, instead of reversing the order and same
for the opposite.
Thanks
@collinganesh
collinganesh
02 Sep 2018, 20:31
RE: RE:
patrick.sifneos@gmail.com said:
@collinganesh