Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
687 views
in Technique[技术] by (71.8m points)

pine script - Pinescript : Round of nearest 0.05 in decimal places

I wish to round off any values to the nearest tick in decimal points. For example if the calculated TP or SL is 101.68 it should be rounded off to 101.70 my ticker's decimals moves in 0.05 increments.

In Metatrader4 I acheived this as

price=NormalizeDouble(price-NormalizeDouble(MathMod(price,0.05),2),2);

How to achieve this in Pinescript?

question from:https://stackoverflow.com/questions/65651036/pinescript-round-of-nearest-0-05-in-decimal-places

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
//@version=4
study("Round to tick", overlay=true)

f_round_up_to_tick(x, mintick)=>
    mult = 1 / mintick
    value = ceil(x*mult)/mult
    
f_round_down_to_tick(x, mintick)=>
    mult = 1 / mintick
    value = floor(x*mult)/mult

var float   stopLoss = 101.68

roundedStopLos = f_round_up_to_tick(stopLoss, syminfo.mintick)

if barstate.islast
    label.new(bar_index, high, tostring(roundedStopLos))

Source: Rounding to the nearest tick in Pine Script


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...