Login Page - Create Account

Support Board


Date/Time: Sun, 28 Apr 2024 08:22:16 +0000



[Programming Help] - is there any ACSIL function that rounds

View Count: 2263

[2018-08-11 19:18:01]
User61576 - Posts: 418
floats and double to to be by tick size?
trying to send a limit order which will not be lower than the stop price and both prices are double.
lvl1OrderPrice = sc.BaseDataIn[SC_LOW][cur - 1] + (lvl1Price.GetInt() * sc.TickSize);
I am trying to get a result which is good both for CL (65.54) and for ZB (112.034375)
pls advise
thanks
Date Time Of Last Edit: 2018-08-11 19:18:15
[2018-08-12 15:33:46]
bradh - Posts: 854
When I want to round to say the nearest .000001, I multiply by 1,000,000, then use round() to take it to the nearest integer, then divide by 1,000,000.

Like this:

lvl1OrderPrice = round(sc.BaseData[SC_LAST][sc.Index] * 100000) / 100000;

Brad
[2018-08-12 18:14:07]
WarriorTrader - Posts: 245
The fastest way to round something is to add .5 and only take the integer part of the answer. I will have to look for some code examples but the ides is simple. To round 3.7 just add 3.7 + .5 which gives 4.2 and throw out the .2
to round 3.7 --> 3.7 + .5 = 4.2 = 4

That technique can be used for decimal places also.



--WT
Date Time Of Last Edit: 2018-08-13 05:49:09
[2018-08-12 19:04:35]
@sstfrederik - Posts: 403
sc.RoundToTickSize() / sc.RoundToIncrement()
[2018-08-13 20:09:30]
User61576 - Posts: 418
using the following
lvl1OrderPrice = sc.RoundToTickSize((double)(sc.BaseDataIn[SC_LOW][cur - 1] + (lvl1Price.GetInt() * sc.TickSize)), 0.01);
gives me 69.070000000000007

and without (double) it's not compiling so it's working as a double...
also
lvl1OrderPrice = round(lvl1OrderPrice * 100) / 100;

I tried to work with long double and still got the same results. looks like the floating point keeps giving me the full double result.

I need to get 69.07
[2018-08-14 15:03:32]
WarriorTrader - Posts: 245
The fastest way to round something is to add .5 and only take the integer part of the answer. I will have to look for some code examples but the ides is simple. To round 3.7 just add 3.7 + .5 which gives 4.2 and throw out the .2
to round 3.7 --> 3.7 + .5 = 4.2 = 4

That technique can be used for decimal places also.



Add .005 to the number then truncate the result at the second decimal place. A C++ function probably exists to accomplish what you want, but if you can't find one the method I descried will work.



--WT
[2018-08-14 18:39:20]
@sstfrederik - Posts: 403
double lvl1OrderPrice = sc.RoundToTickSize((sc.BaseDataIn[SC_LOW][sc.Index] + ((double)lvl1Price.GetInt() * sc.TickSize)), 0.01);

Date Time Of Last Edit: 2018-08-14 18:39:42
[2018-08-17 14:55:12]
User61576 - Posts: 418
well, found out that i didn't defined my problem correctly and i was trying to compare 2 price
i thought if i will round both i could compare tham but found that the floating points keeps all the digits...
bottom line the solution i found is to create a function to compare 2 arguments and work upon the result of the functions:

int compare(float f1, float f2)
{
  float epsilon = 0.00001;
  long float diff = fabs(f1 - f2);
  if (diff < epsilon) return 0;
  else if (f1 > f2) return 1;
  else return - 1;
}

I hope this will be useful for someone else as well
[2018-08-17 15:17:51]
@sstfrederik - Posts: 403
sc.FormattedEvaluate()
[2018-08-17 15:50:34]
User61576 - Posts: 418
thank. i have much more to learn.
btw do you happen to know why whil i am in debug mode SC replay keeps going in the background although i can debug line by line and when i stop debug suddenly all "cash" of ticks is seen on sc screen?
[2018-08-17 15:55:26]
@sstfrederik - Posts: 403
I dont know what you mean.
[2018-08-18 19:51:25]
User61576 - Posts: 418
i am using VS2017 and while I am in the debug mode, SC is not working properly and not displaying orders on screen etc.
is this is a known issue?
[2018-08-18 19:58:45]
@sstfrederik - Posts: 403
I dont use that. Cant help here.
[2018-08-18 20:04:26]
User61576 - Posts: 418
what do you do use?
[2018-08-18 20:15:11]
@sstfrederik - Posts: 403
Any text editor will do. Only use VS when I want to compile locally.

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account