comma separation for numbers in Framer - framerjs

I have a dynamic text layer with a number
dollarAmountValue = 9000
dollarAmount = new Layer
dollarAmount.html = "$" + dollarAmountValue
I also have a + and - button that increments dollarAmountValue by 100
dollarPlus.onClick ->
dollarAmountValue = dollarAmountValue + 100
dollarMinus.onClick ->
dollarAmountValue = dollarAmountValue - 100
So the dollar amount displays as $9000, all good. What I'm trying to do is display it as $9,000 and have the comma separation for 1000's as the +/- increment buttons are pressed. Any help would be greatly appreciated. Thanks!

Here's something short:
convertToDollar = (value) ->
value.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');
You can then use it like this:
convertToDollar(9000)
# Result: 9,000.00
convertToDollar(9100)
# Result: 9,100.00
Framer.js uses Coffeescript, which compiles into Javascript, so it may be helpful to look at some Javascript answers. The above code is based on:
https://stackoverflow.com/a/14428340/747339

Related

Can't calculate the right Volume RSI in MQL4 with a functioning Pine-Script Example

I want to "translate" a Pine-Script to MQL4 but I get the wrong output in MQL4 compared to the Pine-Script in Trading-view.
I wrote the Indicator in Pine-Script since it seems fairly easy to do so.
After I got the result that I was looking for I shortened the Pine-Script.
Here the working Pine-Script:
// Pinescript - whole Code to recreate the Indicator
study( "Volume RSI", shorttitle = "VoRSI" )
periode = input( 3, title = "Periode", minval = 1 )
VoRSI = rsi( volume, periode )
plot( VoRSI, color = #000000, linewidth = 2 )
Now I want to translate that code to MQL4 but I keep getting different outputs.
Here is the MQL4 code I wrote so far:
// MQL4 Code
input int InpRSIPeriod = 3; // RSI Period
double sumn = 0.0;
double sump = 0.0;
double VoRSI = 0.0;
int i = 0;
void OnTick() {
for ( i; i < InpRSIPeriod; i++ ) {
// Check if the Volume is buy or sell
double close = iClose( Symbol(), 0, i );
double old_close = iClose( Symbol(), 0, i + 1 );
if ( close - old_close < 0 )
{
// If the Volume is positive, add it up to the positive sum "sump"
sump = sump + iVolume( Symbol(), 0, i + 1 );
}
else
{
// If the Volume is negative, add it up to the negative sum "sumn"
sumn = sumn + iVolume( Symbol(), 0, i + 1 );
}
}
// Get the MA of the sump and sumn for the Input Period
double Volume_p = sump / InpRSIPeriod;
double Volume_n = sumn / InpRSIPeriod;
// Calculate the RSI for the Volume
VoRSI = 100 - 100 / ( 1 + Volume_p / Volume_n );
// Print Volume RSI for comparison with Tradingview
Print( VoRSI );
// Reset the Variables for the next "OnTick" Event
i = 0;
sumn = 0;
sump = 0;
}
I already checked if the Period, Symbol and timeframe are the same and also have a Screenshoot of the different outputs.
I already tried to follow the function-explanations in the pine-script for the rsi, max, rma and sma function but I cant get any results that seem to be halfway running.
I expect to translate the Pine-Script into MQL4.
I do not want to draw the whole Volume RSI as a Indicator in the Chart.
I just want to calculate the value of the Volume RSI of the last whole periode (when new candel opens) to check if it reaches higher than 80.
After that I want to check when it comes back below 80 again and use that as a threshold wether a trade should be opened or not.
I want a simple function that gets the Period as an input and takes the current pair and Timeframe to return the desired value between 0 and 100.
Up to now my translation persists to provide the wrong output value.
What am I missing in the Calculation? Can someone tell me what is the right way to calculate my Tradingview-Indicator with MQL4?
Q : Can someone tell me what is the right way to calculate my Tradingview-Indicator with MQL4?
Your main miss of the target is in putting the code into a wrong type of MQL4-code. MetaTrader Terminal can place an indicator via a Custom Indicator-type of MQL4-code.
There you have to declare so called IndicatorBuffer(s), that contain pre-computed values of the said indicator and these buffers are separately mapped onto indicator-lines ( depending on the type of the GUI-presentation style - lines, area-between-lines, etc ).
In case you insist on having a Custom-Indicator-less indicator, which is pretty legal and needed in some use-cases, than you need to implement you own "mechanisation" of drawing lines into a separate sub-window of the GUI in the Expert-Advisor-code, where you will manage all the settings and plotting "manually" as you wish, segment by segment ( we use this for many reasons during prototyping, so as to avoid all the Custom-Indicator dependencies and calling-interface gritty-nitties during the complex trading exosystem integration - so pretty well sure about doability and performance benefits & costs of going this way ).
The decision is yours, MQL4 can do it either way.
Q : What am I missing in the Calculation?
BONUS PART : A hidden gem for improving The Performance ...
In either way of going via Custom-Indicator-type-of-MQL4-code or an Expert-Advisor-type-of-MQL4-code a decision it is possible to avoid a per-QUOTE-arrival re-calculation of the whole "depth" of the RSI. There is a frozen-part and a one, hot-end of the indicator line and performance-wise it is more than wise to keep static records of "old" and frozen data and just update the "live"-hot-end of the indicator-line. That saves a lot of the response-latency your GUI consumes from any real-time response-loop...

Unclear progress bar logic

My progress bar:
https://imgur.com/M6S3xkA
The problem:
How you can see, this is not a regular progress bar, the logic is unclear for me.
Let's say - when you have ~30 points (from 1000), you should have 50% in progress bar,
but how to count this magic number?
In normal case with 30 points (from 1000) you should have around <5%.
I will happy for any clues!
You should calculate the progress based on PARTS, which you split. In your case, you split range to 5 parts.
Step 1: Find which part the current point are in. (if point is 30, it is over 2 parts - (0..3) and (3..20))
Step 2: Find which percentage of the point in current part (if point is 30, current part is (20..50) -> percentage = (30 - 20) / (50 - 20) = 0.333
Step 3: Combine them and divide the parts number.
Sample code:
PARTS = [(0..3), (3..20), (20..50), (50..200), (200..1000)]
PARTS_NUMBER = PARTS.size # 5
def calculate_percentage point
part_index = PARTS.find_index {|x| x.include?(point)}
part = PARTS[part_index]
remain = (point - part.first) * 1.0 / (part.last - part.first)
(part_index + remain) / PARTS_NUMBER
end
calculate_percentage(30) # => 0.4666666666666667
Hope it helps :D

Checking if point is in front

I'm building a small application that takes the user's device's lat/long position as well as the heading (true north) and tells you what sort of points of interests are in front of the user. I am getting all my points of interests from google maps api.
I thought the easiest way to do this would be a dot product calculation between my forward vector and the AB vector however I have some false positives.
Here is my code:
func isFront(_ p1 : Point, _ p2 : Point, _ p1Heading : Double) -> Bool {
let forward = Point(cos(p1Heading), sin(p1Heading))
let AB = Point(p2.x - p1.x, p2.y - p1.y)
let lenAB = (AB.x * AB.x + AB.y * AB.y).squareRoot()
let normalAB = Point(AB.x / lenAB, AB.y / lenAB)
let dot = (normalAB.x * forward.x + normalAB.y * forward.y)
return (dot > 0)
}
So something that is coming back as true but I think shouldnt is:
My location(lat/lng): 42.359291, -71.059638 heading: 173.89306640625
Point of interest (lat/lng): 42.359980, -71.060303
Is this a good approach in figuring out if things are in front or should I look into doing something different?
Yes dot product is a good approach for this but I do not see any dot product in your equations/code I see only messed up gibberish there... If I get it right:
p1 is device position
forward is forward direction
p2 is tested point
then it should be:
let forward = Point(cos(p1Heading), sin(p1Heading))
let dot = ((p2.x-p1.x)*forward.x)+((p2.y-p1.y)*forward.y)
return (dot>0.0)
As you can see no lengths are required. Also no normal (even if your normal is not a normal but some gibberish). I think you should read some book/tutorial on vector math basic and check how things are computed here the 2D stuff you mess up:
dot(a,b) = a.x*b.x + a.y*b.y
normal(a) = Point(a.y,-a.x)

How to generate a Random Floating point Number in range, Swift

I'm fairly new to Swift, only having used Python and Pascal before. I was wondering if anyone could help with generating a floating point number in range. I know that cannot be done straight up. So this is what I've created. However, it doesn't seem to work.
func location() {
// let DivisionConstant = UInt32(1000)
let randomIntHeight = arc4random_uniform(1000000) + 12340000
let randomIntWidth = arc4random_uniform(1000000) + 7500000
XRandomFloat = Float(randomIntHeight / UInt32(10000))
YRandomFloat = Float(randomIntWidth / UInt32(10000))
randomXFloat = CGFloat(XRandomFloat)
randomYFloat = CGFloat(YRandomFloat)
self.Item.center = CGPointMake(randomXFloat, randomYFloat)
}
By the looks of it, when I run it, it is not dividing by the value of the DivisionConstant, so I commented this and replaced it with a raw value. However, self.Item still appears off screen. Any advice would be greatly appreciated.
This division probably isn't what you intended:
XRandomFloat = Float(randomIntHeight / UInt32(10000))
This performs integer division (discarding any remainder) and then converts the result to Float. What you probably meant was:
XRandomFloat = Float(randomIntHeight) / Float(10000)
This is a floating point number with a granularity of approximately 1/10000.
Your initial code:
let randomIntHeight = arc4random_uniform(1000000) + 12340000
generates a random number between 12340000 and (12340000+1000000-1). Given your final scaling, that means a range of 1234 and 1333. This seems odd for your final goals. I assume you really meant just arc4random_uniform(12340000), but I may misunderstand your goal.
Given your comments, I think you've over-complicated this. The following should give you a random point on the screen, assuming you want an integral (i.e. non-fractional) point, which is almost always what you'd want:
let bounds = UIScreen.mainScreen().bounds
let x = arc4random_uniform(UInt32(bounds.width))
let y = arc4random_uniform(UInt32(bounds.height))
let randomPoint = CGPoint(x: CGFloat(x), y: CGFloat(y))
Your problem is that you're adding the the maximum value to your random value, so of course it's always going to be offscreen.
I'm not sure what numbers you're hoping to generate, but what you're getting are results like:
1317.0, 764.0
1237.0, 795.0
1320.0, 814.0
1275.0, 794.0
1314.0, 758.0
1300.0, 758.0
1260.0, 809.0
1279.0, 768.0
1315.0, 838.0
1284.0, 763.0
1273.0, 828.0
1263.0, 770.0
1252.0, 776.0
1255.0, 848.0
1277.0, 847.0
1236.0, 847.0
1320.0, 772.0
1268.0, 759.0
You're then using this as the center of a UI element. Unless it's very large, it's likely to be off-screen.

Need help in using turtle module

So I'm writing a program using the turtle module. I have 2 questions:
I have 4 turtles. How can I make their names show up on the screen while drawing?
After I finish drawing, how can I exit one screen and open another within the same program?
I have 4 turtles. How can I make their names show up on the screen
while drawing?
Sure, here's a crude example:
from random import choice
from turtle import Turtle, Screen
NAMES = ['Donnie', 'Raph', 'Mickey', 'Leo']
SPEEDS = ["slowest", "slow", "normal", "fast", "fastest"]
FONT = ("Arial", 12, "normal")
MAGNIFICATION = 5
STAMP_SIZE = 20
MAXIMUM_SPEED = 10
LINE_OFFSET = 50
screen = Screen()
WINDOW_WIDTH = screen.window_width()
START, FINISH = LINE_OFFSET - WINDOW_WIDTH//2, WINDOW_WIDTH//2 - (LINE_OFFSET + MAXIMUM_SPEED)
turtles = {name: Turtle(shape='turtle') for name in NAMES}
for offset, turtle in enumerate(turtles.values(), start=-len(NAMES)//2):
turtle.turtlesize(MAGNIFICATION)
turtle.color("black", "white")
turtle.penup()
turtle.goto(START, offset * MAGNIFICATION * STAMP_SIZE)
turtle.speed(choice(SPEEDS))
turtle.write("", font=FONT) # dummy write for 1st undo
winner = False
while not winner:
for name, turtle in turtles.items():
turtle.undo() # unwrite name
turtle.forward(turtle.speed() + 1)
turtle.write(name, font=FONT) # rewrite name
if turtle.xcor() >= FINISH:
winner = True
break
screen.exitonclick()
To do better with respect to text centering and eliminating flicker, we would need one or more extra invisible turtles to just handle the text. However, when the turtles turn, the text won't turn with them, it'll always be horizontal.

Resources