Orders are not executed
Orders are not executed
18 Feb 2019, 20:46
Hi guys,
Appreciate any help on this. I have changed the concept of the bot to trade when Pirce breaks above sma with specified BrokenPips value, but it does not actually execute the trade when the crossover happens.
if (_sma.Result.HasCrossedAbove(MarketSeries.Close, -1) && Symbol.Ask > sma + BrokenPips * Symbol.PipSize && longPosition == null && shortPosition == null)
{
ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, Label, StopLoss, TakeProfit);
}
Full code here
using cAlgo.API; using cAlgo.API.Indicators; namespace cAlgo.Robots { [Robot(AccessRights = AccessRights.None)] public class Sma252 : Robot { private SimpleMovingAverage _sma; private Position _position; private bool _isShortPositionOpen; private bool _isLongPositionOpen; [Parameter(DefaultValue = 252)] public int Period { get; set; } [Parameter(DefaultValue = 10000)] public int Volume { get; set; } [Parameter()] public DataSeries Source { get; set; } [Parameter("Broken Pips", DefaultValue = 30, MinValue = 0, Step = 10)] public double BrokenPips { get; set; } [Parameter("Take Profit", DefaultValue = 40, MinValue = 0, Step = 10)] public double TakeProfit { get; set; } [Parameter("Stop Loss", DefaultValue = 40, MinValue = 0, Step = 10)] public double StopLoss { get; set; } private string Label = "SMAbot"; protected override void OnStart() { _sma = Indicators.SimpleMovingAverage(Source, Period); } protected override void OnBar() { var longPosition = Positions.Find(Label, Symbol, TradeType.Buy); var shortPosition = Positions.Find(Label, Symbol, TradeType.Sell); if (Trade.IsExecuting) return; var lastIndex = MarketSeries.Close.Count - 1; _isLongPositionOpen = _position != null && _position.TradeType == TradeType.Buy; _isShortPositionOpen = _position != null && _position.TradeType == TradeType.Sell; double close = MarketSeries.Close[lastIndex - 1]; double lastClose = MarketSeries.Close[lastIndex - 2]; double sma = _sma.Result[lastIndex - 1]; double lastSma = _sma.Result[lastIndex - 2]; if (_sma.Result.HasCrossedAbove(MarketSeries.Close, -1) && Symbol.Ask > sma + BrokenPips * Symbol.PipSize && longPosition == null && shortPosition == null) { ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, Label, StopLoss, TakeProfit); } else if (_sma.Result.HasCrossedBelow(MarketSeries.Close, -1) && Symbol.Bid < sma - BrokenPips * Symbol.PipSize && longPosition == null && shortPosition == null) { ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, Label, StopLoss, TakeProfit); } } /// <summary> /// _position holds the latest opened position /// </summary> /// <param name="openedPosition"></param> protected override void OnPositionOpened(Position openedPosition) { _position = openedPosition; } /// <summary> /// Create Buy Order /// </summary> private void Buy() { Trade.CreateBuyMarketOrder(Symbol, Volume); } /// <summary> /// Create Sell Order /// </summary> private void Sell() { Trade.CreateSellMarketOrder(Symbol, Volume); } /// <summary> /// Close Position /// </summary> /// <param name="pos"></param> private void ClosePosition(Position pos) { if (pos == null) return; Trade.Close(pos); } /// <summary> /// Print Error /// </summary> /// <param name="error"></param> protected override void OnError(Error error) { Print(error.Code.ToString()); } } }
Replies
Skypips
19 Feb 2019, 10:23
RE:
Hi Panagiotis,
Tank you for the reply. I am new to coding, trying to see how this function works as well, I have also tried value 0 but i think it is not related to what I am trying to do. The idea is:
Conditions for trade Buy:
1. If price crossed above SMA
2. Then, if price breaks X amount of pips after it crossed SMA
3. Place buy order
Panagiotis Charalampous said:
Hi sky pips,
Why do you use -1 in HasCrossedAbove(MarketSeries.Close, -1) ?
Best Regards,
Panagiotis
@Skypips
PanagiotisCharalampous
19 Feb 2019, 10:29
Hi sky pips,
This paramaters indicates how many bars will be considered for the cross checking. So -1 is invarid and 0 will only check the current bar. If you set it to 1 it will check the previous bar as well and it is more apporriate when this function is used in an OnBar() method.
Best Regards,
Panagiotis
@PanagiotisCharalampous
PanagiotisCharalampous
19 Feb 2019, 11:05
Hi sky pips,
The code seems to be a bit contradicting. E.g you check if the SMA has crossed below the close price and then you check if the price is below the SMA. I guess this will always be false.
Best Regards,
Panagiotis
@PanagiotisCharalampous
PanagiotisCharalampous
19 Feb 2019, 09:49
Hi sky pips,
Why do you use -1 in HasCrossedAbove(MarketSeries.Close, -1) ?
Best Regards,
Panagiotis
@PanagiotisCharalampous