crossing points
crossing points
13 Oct 2012, 12:01
Hi,
I am developing my first robots from samples and having troubles that i can not explain.
The robot to create close the existing position and create a new position when slow period moving avarage crosses fast period moving avarage and when GrossProfit < anumber. But when I do the backtest and realtime testing it does not create marketorders or close positions at right positions ( crossing points). The GrossProfit condition works right. I have tried to find the error but I could not find it. Could someone please help me?. The codes as below
protected override void OnTick()
{
if (Trade.IsExecuting) return;
int lastIndex = slowMa.Result.Count - 1;
int prevIndex = slowMa.Result.Count - 2;
double currentSlowMa = slowMa.Result[lastIndex];
double currentFastMa = fastMa.Result[lastIndex];
double previousSlowMa = slowMa.Result[prevIndex];
double previousFastMa = fastMa.Result[prevIndex];
bool isLongPositionOpen = position != null && position.TradeType == TradeType.Buy;
bool isShortPositionOpen = position != null && position.TradeType == TradeType.Sell;
if ((previousSlowMa > previousFastMa) && (currentSlowMa <= currentFastMa) && !isLongPositionOpen)
{
ClosePosition();
Buy();
}
if ((previousSlowMa < previousFastMa) && (currentSlowMa >= currentFastMa) && !isShortPositionOpen)
{
ClosePosition();
Sell();
}
if (position != null && position.GrossProfit < -StopLoss)
{
ClosePosition();
}
}
private void ClosePosition()
{
if (position != null)
{
Trade.Close(position);
position = null;
}
}
private void Buy()
{
Trade.CreateBuyMarketOrder(Symbol, Volume);
}
private void Sell()
{
Trade.CreateSellMarketOrder(Symbol, Volume);
}
protected override void OnPositionOpened(Position openedPosition)
{
position = openedPosition;
}
}
}
admin
15 Oct 2012, 16:44
See thread /forum/cbot-support/73
@admin