Stop orders filled way to late
Stop orders filled way to late
06 May 2015, 09:20
Hi
I have created a cbot and in the results of backtesting, i get hundreds of little take profits of a negative 1-2 pip loss because the orders are being filled about 100 pips from the price so the orders get filled at a price that is already past the take profit resulting in the trade instantly closing at a 1 pip loss.
Any idea why this is happening?
Thanks
My code is below:
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 ChannelSnifferv3 : Robot { [Parameter(DefaultValue = 10000)] public double Volume { get; set; } [Parameter(DefaultValue = 30)] public int TakeProfit { get; set; } [Parameter(DefaultValue = 0)] public int StopLoss { get; set; } [Parameter(DefaultValue = 0.001)] public double ChannelWidth { get; set; } [Parameter(DefaultValue = 10)] public int ChannelTime { get; set; } [Parameter("Start Hour", DefaultValue = 1.0)] public double StartTime { get; set; } [Parameter("Stop Hour", DefaultValue = 6.0)] public double StopTime { get; set; } [Parameter("Entry Distance", DefaultValue = 20)] public int EntryDistance { get; set; } private DateTime _startTime; private DateTime _stopTime; private int BarsInChan; private double ChanTop; private double ChanBottom; protected override void OnStart() { _startTime = Server.Time.Date.AddHours(StartTime); _stopTime = Server.Time.Date.AddHours(StopTime); Positions.Opened += OnPositionsOpened; BarsInChan = 0; ChanTop = 0; ChanBottom = 0; } protected override void OnBar() { if (Trade.IsExecuting) return; var currentHours = Server.Time.TimeOfDay.TotalHours; bool tradeTime = StartTime < StopTime ? currentHours > StartTime && currentHours < StopTime : currentHours < StopTime || currentHours > StartTime; // if (!tradeTime) // return; if (Positions.Count != 0) return; if (PendingOrders.Count != 0) return; double tempOpen = MarketSeries.Open.LastValue; double tempClose = MarketSeries.Close.LastValue; if (tempOpen > tempClose) { ChanTop = tempOpen; ChanBottom = tempClose; } else { ChanTop = tempClose; ChanBottom = tempOpen; } if (ChanTop - ChanBottom <= ChannelWidth) { BarsInChan++; if (BarsInChan == 1) { ChanTop = MarketSeries.Median.LastValue + (ChannelWidth / 2); ChanBottom = MarketSeries.Median.LastValue - (ChannelWidth / 2); } } if (BarsInChan != 0) { for (int i = ChannelTime; i > 1; i--) { tempOpen = MarketSeries.Open.Last(i); tempClose = MarketSeries.Close.Last(i); double tempT = 0; double tempB = 0; if (tempOpen > tempClose) { tempT = tempOpen; tempB = tempClose; } else { tempT = tempClose; tempB = tempOpen; } if (tempT - tempB <= ChannelWidth) { if (tempT <= ChanTop && tempB >= ChanBottom) BarsInChan++; else { BarsInChan = 0; break; } } } } if (BarsInChan == ChannelTime) { PlaceStopOrder(TradeType.Buy, Symbol, (long)Volume, ChanTop + EntryDistance * Symbol.PipSize, "My Trade", StopLoss, TakeProfit); PlaceStopOrder(TradeType.Sell, Symbol, (long)Volume, ChanBottom - EntryDistance * Symbol.PipSize, "My Trade2", StopLoss, TakeProfit); BarsInChan = 0; } return; } protected override void OnStop() { // Put your deinitialization logic here } private void OnPositionsOpened(PositionOpenedEventArgs args) { if (Trade.IsExecuting) return; foreach (var order in PendingOrders) { CancelPendingOrder(order); } } } }
Replies
Spotware
18 Jun 2015, 12:52
Dear Trader,
We do not provide coding assistance services. We more than glad to assist you with specific questions about cAlgo.API. You also can contact one of our Partners or post a job in Development Jobs section for further coding assistance.
@Spotware
onemind
06 May 2015, 09:36
Ok, i widened the takeprofit to 100 and only one order was closed at -1 pip on a really long bar.
I take it this is just extreme slippage?
@onemind