Sum of Digits of Index Closing Price

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #168293 quote
    WidzoWidzo
    Participant
    Junior

    I have been looking at the mathematical functions but unfortunately I cannot find the mathematical formula for sum of digits e.g. 6939 = 6+9+3+9 = 27

    For example, if I want to code the sum of digits for an index price re FTSE 100 at close, 6939, what will be the formula in code? Kindly help please.

    #168298 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    There’s no built-in function to manipulate single digits.

    You can achieve that by a doing some divisions and multiplications by ten, then rounding the number to subtract the result from the original number to get the desired digit:

    N = 6939          //it can be CLOSE, ... etc or ROUND(CLOSE),... etc to make it an integer
    T = 0
    //right to left
    WHILE N > 0                         //exit the iteration when N = 0
       x = round((N / 10) - 0.5) * 10   //get 693.9, then 693.0, then 6930
       T = T + (N - x)                  //T = itself + 9 (actually 6939 - 6930)
       N = x / 10                       //save the new N (6930)
    WEND                                //repeat for the next digit
    #168299 quote
    WidzoWidzo
    Participant
    Junior

    Roberto, thanks for your prompt reply and this is really helpful.

    robertogozzi thanked this post
    #168300 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    The correct comment in line 7 is:

    //save the new N (693)
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

Sum of Digits of Index Closing Price


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Widzo @widzo Participant
Summary

This topic contains 3 replies,
has 2 voices, and was last updated by robertogozzirobertogozzi
5 years, 5 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 04/28/2021
Status: Active
Attachments: No files
ProRealCode ProRealCode
Loading...