Cbot gives wrong calculation result
Cbot gives wrong calculation result
15 Nov 2021, 14:34
I made a bot that make trades based on the Inside bar pattern- it opens a trade when the previous candles+ wicks is smaller/bigger than the one before it.
It gives me wrong weird math calculation result that contains candles prices like this : Bars.ClosePrices.Last(1) - Bars.OpenPrices.Last(1)
The value c is shown in log which gives this subtractions result.
What could cause this?
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class InsideBarPatternbot : Robot
{
[Parameter(DefaultValue = 0.0)]
public double Parameter { get; set; }
[Parameter("volume", Group = "Volume", DefaultValue = 100000)]
public double volume { get; set; }
static double IB;
static double c;
protected override void OnStart()
{
}
protected override void OnTick()
{
// check if inside bar
if ((Bars.HighPrices.Last(2) > Bars.HighPrices.Last(1) & Bars.LowPrices.Last(2) < Bars.LowPrices.Last(1)))
{
// Print("open price{0}", (Bars.LowPrices.Last(2) - 0.1 * (Bars.HighPrices.Last(2) - Bars.LowPrices.Last(2))));
c = (Bars.ClosePrices.Last(1) - Bars.OpenPrices.Last(1));
Print("calculation {0}", c);
Print("open {0}", Bars.OpenPrices.Last(1));
// IB = (Bars.HighPrices.Last(2) - Bars.LowPrices.Last(2));
if ((Bars.HighPrices.Last(2) - Bars.LowPrices.Last(2)) != IB)
{
// check IB 1s candle range so it closes opens /trades only when new IB
IB = Bars.HighPrices.Last(2) - Bars.LowPrices.Last(2);
foreach (var order in PendingOrders)
{
CancelPendingOrder(order);
}
foreach (var position in Positions)
{
ClosePosition(position);
}
// check if long candle
if (Bars.ClosePrices.Last(2) - Bars.OpenPrices.Last(2) > 0)
{
var openprice = (Bars.HighPrices.Last(2) + 0.1 * (Bars.HighPrices.Last(2) - Bars.LowPrices.Last(2)));
PlaceStopOrder(TradeType.Buy, SymbolName, 100000, openprice, "myStopOrder");
foreach (var order in PendingOrders)
{
var sl1 = 100 * (0.4 * (Bars.HighPrices.Last(2) - Bars.LowPrices.Last(2)));
var tp1 = 100 * (0.8 * (Bars.HighPrices.Last(2) - Bars.LowPrices.Last(2)));
ModifyPendingOrder(order, order.TargetPrice, sl1, tp1, order.ExpirationTime);
}
}
else
{
var openprice1 = (Bars.LowPrices.Last(2) - 0.1 * (Bars.HighPrices.Last(2) - Bars.LowPrices.Last(2)));
PlaceStopOrder(TradeType.Sell, SymbolName, 100000, openprice1, "myStopOrder");
foreach (var order in PendingOrders)
{
Print(" sl {0}", (100 * (0.4 * (Bars.HighPrices.Last(2) - Bars.LowPrices.Last(2)))));
var sl2 = 100 * (0.4 * (Bars.HighPrices.Last(2) - Bars.LowPrices.Last(2)));
var tp2 = 100 * (0.8 * (Bars.HighPrices.Last(2) - Bars.LowPrices.Last(2)));
ModifyPendingOrder(order, order.TargetPrice, sl2, tp2, order.ExpirationTime);
}
}
}
}
}
}
}
Replies
ctid3179522
18 Nov 2021, 11:34
( Updated at: 21 Dec 2023, 09:22 )
RE:
PanagiotisCharalampous said:
Hi there,
Can you please provide us with examples of these "wrong weird math calculation results"?
Best Regards,
Panagiotis
I took 2 screenshots to show them :
@ctid3179522
PanagiotisCharalampous
18 Nov 2021, 11:39
Hi there,
Please explain what do you thing s wrong here. The results seem correct to me.
Best Regards,
Panagiotis
Join us on Telegram and Facebook
@PanagiotisCharalampous
ctid3179522
18 Nov 2021, 21:14
RE:
PanagiotisCharalampous said:
Hi there,
Please explain what do you thing s wrong here. The results seem correct to me.
Best Regards,
Panagiotis
The result of 1.15922 - 1.15949 is - 0,00027 not - 0.000269999999999992
@ctid3179522
PanagiotisCharalampous
18 Nov 2021, 21:26
Hi there,
That's normal. Read here. You need to apply rounding.
Best Regards,
Panagiotis
Join us on Telegram and Facebook
@PanagiotisCharalampous
ctid3179522
21 Nov 2021, 10:12
RE:
PanagiotisCharalampous said:
Hi there,
That's normal. Read here. You need to apply rounding.
Best Regards,
Panagiotis
ok thank you
@ctid3179522
PanagiotisCharalampous
15 Nov 2021, 14:41
Hi there,
Can you please provide us with examples of these "wrong weird math calculation results"?
Best Regards,
Panagiotis
Join us on Telegram and Facebook
@PanagiotisCharalampous