Log in to see Cloud of Tags

Wealth-Lab Wiki

AlmostEquals

RSS

Syntax


public static bool AlmostEquals(this double double1, double double2, double precision)

public static bool AlmostEquals(double double1, double double2, double precision)

Parameter Description

double1First double value
double2Second double value
precisionDesirable precision when comparing

Description

Sometimes there's a need to compare two values of type double to determine whether they are within a close approximation to each other. The following function taken from Stackoverflow will handle it.

Example

Example below compares two double values for approximate equality within one tick value of an instrument.

Example using C# extension methods:


using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;

namespace WealthLab.Strategies { public class AlmostEqualsDemo : WealthScript { protected override void Execute() { double tick = Bars.SymbolInfo.Tick; SetBarColors( Color.Silver, Color.Silver ); for(int bar = 1; bar < Bars.Count; bar++) { bool result = ( Close[bar].AlmostEquals(Low[bar],tick) || Close[bar].AlmostEquals(Low[bar]+tick,tick) || Open[bar].AlmostEquals(Low[bar],tick) || Open[bar].AlmostEquals(Low[bar]+tick,tick) ); if( result ) SetBarColor( bar, Color.Magenta ); } } } }

Legacy syntax example:


using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;
using Community.Components;

namespace WealthLab.Strategies { using c = Community.Components.Calculate; public class AlmostEqualsDemo : WealthScript { protected override void Execute() { double tick = Bars.SymbolInfo.Tick; SetBarColors( Color.Silver, Color.Silver ); for(int bar = 1; bar < Bars.Count; bar++) { bool result = ( c.AlmostEquals(Close[bar],Low[bar],tick) || c.AlmostEquals(Close[bar],Low[bar]+tick,tick) || c.AlmostEquals(Open[bar],Low[bar],tick) || c.AlmostEquals(Open[bar],Low[bar]+tick,tick) ); if( result ) SetBarColor( bar, Color.Magenta ); } } } }

Important Disclaimer: The information provided by Wealth-Lab is strictly for informational purposes and is not to be construed as advice or solicitation to buy or sell any security.  The owner of Wealth-Lab.com assumes no liability resulting from the use of the material contained herein for investment purposes. By using this web site, you agree to the terms of this disclaimer and our Terms of Use.


ScrewTurn Wiki. Some of the icons created by FamFamFam.