CBot giving diference result with same indicator logic (not woking for no apperent reason)
CBot giving diference result with same indicator logic (not woking for no apperent reason)
01 May 2022, 11:44
I have create a cBot that places stop order when indicator's LastValue is 1. Now the code is very simple
Now there are two versions of indicator code with same logic (i believe) but one is working and other one is not
code -1(working)
code -2 (not working )
Both indicator producing same results but cBot is working with only one.
=> Backtesting results of Cbot with working code AND excuse me i added arrow in code afterwards to better explain my indicator code
Replies
voltharus
02 May 2022, 19:35
( Updated at: 21 Dec 2023, 09:22 )
RE:
amusleh said:
Hi,
Please post the code, and let us know what exactly is not working.
Thanks for reply
well above problem has been solved with some tweaks but i have another problem see, cBot is not setting specified stoploss at certain time i.e 2:30 ,except this time stoploss is correct for every position.
CODE FOR cBOT
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class Sample_cBot : Robot
{
public DOWNTRADEI DOWN;
public UPTRADEI UP;
protected override void OnStart()
{
DOWN = Indicators.GetIndicator<DOWNTRADEI>(0.0);
UP = Indicators.GetIndicator<UPTRADEI>(0.0);
}
protected override void OnTick()
{
// Put your core logic here
}
protected override void OnBar()
{
if (DOWN.Result.Last(1) == 1)
{
var st = Math.Abs((DOWN.stoploss.LastValue - Bars.OpenPrices.Last(1)) / Symbol.PipSize);
st = st + 2;
PlaceStopOrder(TradeType.Sell, Symbol, 1000, Bars.OpenPrices.Last(1) - Symbol.PipSize * 0.5, "DOWNTRADE", st, st * 2, Server.Time.AddMinutes(59));
}
if (UP.Result.Last(1) == 2)
{
var st2 = Math.Abs((UP.stoploss.LastValue - Bars.OpenPrices.Last(1)) / Symbol.PipSize);
st2 = st2 + 2;
PlaceStopOrder(TradeType.Buy, Symbol, 1000, Bars.OpenPrices.Last(1) + Symbol.PipSize * 0.5, "UPTRADE", st2, st2 * 2, Server.Time.AddMinutes(59));
}
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
}
}
CODE FOR INDICATOR -
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class UPTRADEI : Indicator
{
[Parameter(DefaultValue = 0.0)]
public double Parameter { get; set; }
[Output("Main")]
public IndicatorDataSeries Result { get; set; }
[Output("ST")]
public IndicatorDataSeries stoploss { get; set; }
private Bars DailyCandle;
protected override void Initialize()
{
// Initialize and create nested indicators
DailyCandle = MarketData.GetBars(TimeFrame.Daily);
}
public override void Calculate(int index)
{
// Set up for Downtrades
var upBar = Bars.ClosePrices[index] > Bars.OpenPrices[index];
var downBar = Bars.ClosePrices[index - 1] < Bars.OpenPrices[index - 1];
// Set up for Uptrades
var downBar1 = Bars.ClosePrices[index] < Bars.OpenPrices[index];
var upBar1 = Bars.ClosePrices[index - 1] > Bars.OpenPrices[index - 1];
int k = 200;
var indexD = DailyCandle.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
// CODE FOR TAKING DOWN TRADES
#region CODE FOR TAKING UP TRADES
if (DailyCandle.HighPrices[indexD - 3] < DailyCandle.HighPrices[indexD - 2] && DailyCandle.HighPrices[indexD - 2] < DailyCandle.HighPrices[indexD - 1])
{
if (downBar1 && upBar1)
{
for (int i = 1; i <= k; i++)
{
// condition set
var Last1 = Bars.ClosePrices[index - i - 1] < Bars.OpenPrices[index - i - 1];
var Last2 = Bars.ClosePrices[index - i - 2] < Bars.OpenPrices[index - i - 2];
var con1FT = Bars.ClosePrices[index - 1] > Bars.HighPrices[index - i - 1];
if (Bars.ClosePrices[index - i] < Bars.OpenPrices[index - i])
{
break;
}
if (Bars.ClosePrices[index - i] > Bars.OpenPrices[index - i])
{
if (Last1 && Last2 && con1FT || Bars.OpenPrices[index] > Bars.HighPrices[index - i] && Last1 && Last2)
{
Result[index] = 2;
stoploss[index] = Bars.LowPrices.Minimum(i + 3);
if (Result[index] == 2)
{
Chart.DrawIcon(index.ToString(), ChartIconType.UpArrow, index, Bars.LowPrices[index] - 10 * Symbol.PipSize, "green");
}
break;
}
}
else
Result[index] = 0;
}
}
else
Result[index] = 0;
}
else
{
Result[index] = 0;
stoploss[index] = 0;
}
#endregion
}
}
}
some picture examples
in second picture stoploss is not set at specified price you can see this in position information in picture
@voltharus
voltharus
03 May 2022, 18:04
RE:
amusleh said:
Hi,
Most probably the value you use for stop loss is too small, try to Print the stop loss value before placing order and see how many Pips were the stop loss.
No, stop loss is not small and it is at the wick of the candle I specified with arrow and if you look at the picture you can see it is not a small stop loss and as I said it is happening at 2:30.
All I want to say is I am unable to understand why this is happening. Stop loss is 8 pips to be exact. In second picture I am calculating stop loss from closed of 2:30 candle and the wick of candle I specified with arrow, so the stop loss become the difference between these two candle.
@voltharus
voltharus
04 May 2022, 10:46
RE:
amusleh said:
Hi,
I was not able to replicate the issue, I back tested the cBot from 17/10/2021 to now, and all placed orders and filled positions had stop loss.
Please post the DOWNTRADEI indicator code, and on which broker you tested the cBot? tick data or m1 bars?
I have tested with both tick data and m1 bars and result is still same and to replicate the issue please try this from 17/02/2021, on the 18 feb you will see stop loss is negative which should not happen and my broker is IC Markets.
CODE FOR DOWNTRADEI -
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class UPTRADEI : Indicator
{
[Parameter(DefaultValue = 0.0)]
public double Parameter { get; set; }
[Output("Main")]
public IndicatorDataSeries Result { get; set; }
[Output("ST")]
public IndicatorDataSeries stoploss { get; set; }
private Bars DailyCandle;
protected override void Initialize()
{
// Initialize and create nested indicators
DailyCandle = MarketData.GetBars(TimeFrame.Daily);
}
public override void Calculate(int index)
{
// Set up for Downtrades
var upBar = Bars.ClosePrices[index] > Bars.OpenPrices[index];
var downBar = Bars.ClosePrices[index - 1] < Bars.OpenPrices[index - 1];
// Set up for Uptrades
var downBar1 = Bars.ClosePrices[index] < Bars.OpenPrices[index];
var upBar1 = Bars.ClosePrices[index - 1] > Bars.OpenPrices[index - 1];
int k = 200;
var indexD = DailyCandle.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
// CODE FOR TAKING DOWN TRADES
#region CODE FOR TAKING UP TRADES
if (DailyCandle.HighPrices[indexD - 3] < DailyCandle.HighPrices[indexD - 2] && DailyCandle.HighPrices[indexD - 2] < DailyCandle.HighPrices[indexD - 1])
{
if (downBar1 && upBar1)
{
for (int i = 1; i <= k; i++)
{
// condition set
var Last1 = Bars.ClosePrices[index - i - 1] < Bars.OpenPrices[index - i - 1];
var Last2 = Bars.ClosePrices[index - i - 2] < Bars.OpenPrices[index - i - 2];
var con1FT = Bars.ClosePrices[index - 1] > Bars.HighPrices[index - i - 1];
if (Bars.ClosePrices[index - i] < Bars.OpenPrices[index - i])
{
break;
}
if (Bars.ClosePrices[index - i] > Bars.OpenPrices[index - i])
{
if (Last1 && Last2 && con1FT || Bars.OpenPrices[index] > Bars.HighPrices[index - i] && Last1 && Last2)
{
if (Bars.HighPrices[index - i - 1] < Bars.HighPrices[index - i - 2] && Bars.LowPrices[index - i - 1] < Bars.LowPrices[index - i - 2])
{
if (Bars.HighPrices[index - i - 1] < Bars.HighPrices[index - i] && Bars.LowPrices[index - i - 1] < Bars.LowPrices[index - i])
{
Result[index] = 2;
stoploss[index] = Bars.LowPrices.Minimum(i + 3);
}
}
if (Result[index] == 2)
{
Chart.DrawIcon(index.ToString(), ChartIconType.UpArrow, index, Bars.LowPrices[index] - 10 * Symbol.PipSize, "green");
}
break;
}
}
else
Result[index] = 0;
}
}
else
Result[index] = 0;
}
else
{
Result[index] = 0;
stoploss[index] = 0;
}
#endregion
}
}
}
@voltharus
amusleh
05 May 2022, 10:34
Hi,
I tested again on IC Markets m1 bars from 17/02/2021 - now, all positions had stop loss, try this:
using System;
using cAlgo.API;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class Sample_cBot : Robot
{
public DOWNTRADEI DOWN;
public UPTRADEI UP;
protected override void OnStart()
{
DOWN = Indicators.GetIndicator<DOWNTRADEI>(0.0);
UP = Indicators.GetIndicator<UPTRADEI>(0.0);
PendingOrders.Filled += PendingOrders_Filled;
}
private void PendingOrders_Filled(PendingOrderFilledEventArgs obj)
{
if (obj.Position.StopLoss.HasValue == false)
{
Print("Position without stop loss");
Stop();
}
else
{
Print("Position stop loss: {0}", obj.Position.StopLoss);
}
}
protected override void OnBar()
{
if (DOWN.Result.Last(1) == 1)
{
var st = Math.Abs((DOWN.stoploss.LastValue - Bars.OpenPrices.Last(1)) / Symbol.PipSize);
st = st + 2;
PlaceStopOrder(TradeType.Sell, Symbol, 1000, Bars.OpenPrices.Last(1) - Symbol.PipSize * 0.5, "DOWNTRADE", st, st * 2, Server.Time.AddMinutes(59));
}
if (UP.Result.Last(1) == 2)
{
var st2 = Math.Abs((UP.stoploss.LastValue - Bars.OpenPrices.Last(1)) / Symbol.PipSize);
st2 = st2 + 2;
PlaceStopOrder(TradeType.Buy, Symbol, 1000, Bars.OpenPrices.Last(1) + Symbol.PipSize * 0.5, "UPTRADE", st2, st2 * 2, Server.Time.AddMinutes(59));
}
}
}
}
If an order filled and it's position had no stop loss the bot will print "Position without stop loss" and stops.
@amusleh
amusleh
02 May 2022, 08:44
Hi,
Please post the code, and let us know what exactly is not working.
@amusleh