CO
Stop Signal
25 Mar 2017, 13:16
Hi,
Can someone please help in the coding of my cBot. What I need is for the bot to Stop detecting cross-over Signals
after a trade is opened.
Thank you
using System;
using System.Threading;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.API.Requests;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC)]
public class MaBot : Robot
{
[Parameter("Stop Loss", DefaultValue = 10)]
public int StopLoss { get; set; }
[Parameter("Take Profit", DefaultValue = 20)]
public int TakeProfit { get; set; }
[Parameter("MA_Fast_Period", DefaultValue = 1)]
public int _MA_Fast_Period { get; set; }
[Parameter("MA_Slow_Period", DefaultValue = 200)]
public int _MA_Slow_Period { get; set; }
[Parameter("Pips away", DefaultValue = 10)]
public int PipsAway { get; set; }
[Parameter(DefaultValue = 1000, MinValue = 0)]
public int Volume { get; set; }
[Parameter(DefaultValue = 2000, MinValue = 0)]
public int Volume2 { get; set; }
//Global declaration
private const string label = "MA cBot";
private const string label_2 = "MA Pd cBot";
private ExponentialMovingAverage i_Moving_Average;
private ExponentialMovingAverage i_Moving_Average_1;
private ExponentialMovingAverage i_Moving_Average_2;
private ExponentialMovingAverage i_Moving_Average_3;
bool _Compare;
bool _Compare_1;
bool _MA_cross_down;
bool _MA_cross_up;
DateTime LastTradeExecution = new DateTime(0);
protected override void OnStart()
{
Positions.Closed += PositionClosed;
i_Moving_Average = Indicators.ExponentialMovingAverage(MarketSeries.Close, (int)_MA_Fast_Period);
i_Moving_Average_1 = Indicators.ExponentialMovingAverage(MarketSeries.Close, (int)_MA_Slow_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(1) >= i_Moving_Average_1.Result.Last(1));
_Compare_1 = (i_Moving_Average.Result.Last(0) >= i_Moving_Average_2.Result.Last(0));
_MA_cross_up = (!_Compare && _Compare_1);
_MA_cross_down = (_Compare && !_Compare_1);
if (_MA_cross_down && shortPosition == null)
{
ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label, StopLoss, TakeProfit);
}
if (_MA_cross_down && shortPosition == null)
{
double targetPrice = Symbol.Ask + PipsAway * Symbol.PipSize;
PlaceStopOrder(TradeType.Buy, Symbol, Volume2, targetPrice, label_2, StopLoss, TakeProfit);
}
if (_MA_cross_up && longPosition == null)
{
ExecuteMarketOrder(TradeType.Buy, Symbol, Symbol.NormalizeVolume(Volume), label, StopLoss, TakeProfit);
}
if (_MA_cross_up && longPosition == null)
{
double targetPrice = Symbol.Ask + PipsAway * Symbol.PipSize;
PlaceStopOrder(TradeType.Sell, Symbol, Volume2, targetPrice, label_2, StopLoss, TakeProfit);
}
}
private void PositionClosed(PositionClosedEventArgs args)
{
var pos = args.Position;
if (pos.Label == label && pos.NetProfit > 0)
{
foreach (var order in PendingOrders)
{
if (order.Label == label_2 && order.OrderType == PendingOrderType.Stop)
{
CancelPendingOrder(order);
}
}
}
}
}
}
