0

I want to write a code in pine script that enables me to calculate the number of pips between the high and low of only the First candle of the day. I also want to use this value in the form of a variable to set stop losses and targets.

I got this on the web but don't know how it works. Can we modify this to meet the above criteria?

getHighLowNumPips() => (high - low) / syminfo.mintick

plot(getHighLowNumPips())

I am a newbie in pine script and I am not able to write the code. Any help will be greatly appreciated :)

Thanks,

e2e4
  • 3,428
  • 6
  • 18
  • 30
Aryagm
  • 530
  • 1
  • 7
  • 18

1 Answers1

2

In the example below the script will calculate the number of pips according to your getHighLowNumPips function on the first candle of the day and ignore other candles.

getHighLowNumPips() => (high - low) / syminfo.mintick 

bool isNewDay = dayofmonth != dayofmonth[1]

var float highLowPips = na
if isNewDay
    highLowPips := getHighLowNumPips()

plot(highLowPips)

Note that built-in dayofmonth use the exchange's timezone.

e2e4
  • 3,428
  • 6
  • 18
  • 30
  • Thanks for the answer. But there is one small issue, the high's and low's are not refreshing when the day changes. So the high and low of the previous day is taken into consideration. Can we modify it so that it refreshes every day. Thanks – Aryagm Oct 09 '20 at 14:09
  • Another small question, what is the variable in this piece of code. If I want to exit when my loss equals the number of pips between low and high of first candle what will be the loss (variable)? And what is the function of syminfo.mintick ? Thanks, – Aryagm Oct 09 '20 at 14:23
  • Ah, sorry, missed the part in your question about daily evaluation. Updated the answer. – e2e4 Oct 09 '20 at 14:23
  • Don't use comments to ask another questions, create a new question publication for each one of them instead. Regards, – e2e4 Oct 09 '20 at 14:26
  • If the Answer is useful you can upvote the answer and mark it as accepted, so other volunteers won't spend time analyzing it and writing multiple answers as you no longer need it. See more info about how stackoverflow works here - https://stackoverflow.com/help/accepted-answer – e2e4 Oct 09 '20 at 14:32
  • Sorry to extend the comments... but could you also please answer https://stackoverflow.com/q/64294843/14420572?sem=2. It's really important. Thanks, – Aryagm Oct 10 '20 at 17:00