Draw Signal Arrow and Code Correction
Forums › ProRealTime English forum › ProBuilder support › Draw Signal Arrow and Code Correction
- This topic has 13 replies, 5 voices, and was last updated 9 months ago by WhyAskOZ.
-
-
02/07/2024 at 5:39 AM #227500
Hi, Everyone.. !
I am learning to prepare code to create buy and sell signal based on simple moving average. This is just trial so using it to learn rather trade, however it seems I am missing something in the code so I would appreciate any help. Before Posting I watched this video ( https://www.youtube.com/watch?v=NL8GJoYVT00&t=85s ) to learn and followed it but it seems I am using v12 and what was taught in the video was older version.
See below my code and image for the output I get. In the code I have defined variable for Short (i.e 5) and Long (i.e. 15).
123456789101112//define parametersMAShort = Average[Short](close)MALong = Average[Long](close)If MAShort CROSSES OVER MALong THENSignal = lowELSESignal = HighENDIFRETURN SignalInstead of arrow i used dott as explained the youtube video however I would appreciate if you some guide on how to draw arrow. I wanted to show up arrow in green colour every time MA5 crosses over MA15. However as you can see in the image the code keep printing the blue dots not just when MA5 cross over MA15 but it continue to have dot all the way then turn it into red colour.
Now, I wanted to show red arrow when MA5 crosses below MA15 with red colour (i used dot again) but it isn’t working.
basically i can see red dot when MA5 is above MA15 and blue when MA5 is below MA15, which isn’t what i asked in the signal a well.
If there is another YouTube channel where i can have a look this type of examples but with never version of PRT then please suggest.
Thanks in advance for the help.
Regards,
02/07/2024 at 9:12 AM #227511Hi,
To display arrows you need to use graphic commands DRAWARROWUP / DOWN, that’s independant from variables (here “signal” formatted as dots) returned by the final line “return”. Here is amended code suggestion for arrows at cross over (assuming they are wanted only at cross over):
123456789101112//define parametersMAShort = Average[Short](close)MALong = Average[Long](close)If MAShort CROSSES OVER MALong THENDRAWARROWUP(barindex, low-2*pipsize) coloured(0,255,0)//Signal = lowELSif MAShort CROSSES UNDER MALong THENDRAWARROWDOWN(barindex, high+2*pipsize) coloured(255,0,0)//Signal = HighENDIFRETURN// or if you want to see your MA's: RETURN MAShort,MALong1 user thanked author for this post.
02/07/2024 at 11:16 AM #227522Thank you so much. It worked as I expected.
Now, when I compared your code with mine I can see you have not used “Signal” in your code and still there was signal came out as an arrow which i guess something to learn for me.
I slightly modified the code as below and I keep getting downward arrow multiple times instead of once. I know program is doing what I wrote so it’s not the issue with the program but instead it is my limitation on what other logic i can use so that only time downward arrow appears if there was upward arrow before ! (see attached image on what I get when i put below code in RPT).
12345678910111213//define parametersMAShort = Average[Short](close)MALong = Average[Long](close)If MAShort CROSSES OVER MALong AND Close > MAShort THENDRAWARROWUP(barindex, low-2*pipsize) coloured(0,255,0)//Signal = lowELSIF Close CROSSES UNDER MALong THENDRAWARROWDOWN(barindex, high+2*pipsize) COLOURED(255,0,0)//Signal = highENDIFRETURN// or if you want to see your MA's: RETURN MAShort,MALong02/07/2024 at 5:26 PM #227596Yes, two ways to draw something in probuilder, one way is to say which variables in your code you want to see drawn for each candle in the final “return” line of the indicator, the other way is to use some “graphical” instructions which you can find in current site in this section:
https://www.prorealcode.com/documentation/category/graphical/
If you want to only see the latest 2 up and down crossover, and real time replacement of them without relaunching the indicator, you can adapt slightly the code made in this post:
https://www.prorealcode.com/topic/draw-segments-around-signal-bar/#post-227251
in this way:
DEFPARAM DRAWONLASTBARONLY = TRUE
//define parameters
//short=5
//long=15MAShort = Average[Short](close)
MALong = Average[Long](close)If MAShort CROSSES OVER MALong THEN
memob1=barindex
memolow=low
ELSif MAShort CROSSES UNDER MALong THEN
memob2=barindex
memohigh=high
ENDIFDRAWARROWUP(memob1, memolow-2*pipsize) coloured(0,255,0)
DRAWARROWDOWN(memob2, memohigh+2*pipsize) coloured(255,0,0)RETURN
1 user thanked author for this post.
02/07/2024 at 9:56 PM #227613Thank you. The issue still remains, as you can see in attached image, signal number 6 and 9 shouldn’t have appeared because what I was looking for is that the only time red signal appears if there was green signal earlier. all other signal in the image are correctly appearing as expected. The also reduced number of red signals but it didn’t eliminate the error we need to remove.
we need a logic that trigger the drawdown arrow only if there was up arrow before ! hope image will help to understand what i meant. As i don’t need sell signal at 6 or 9 as there was no buy signal earlier to this two bar..!
02/07/2024 at 10:13 PM #227615try with this:
Alternative arrows conditions1234567891011121314151617181920// define parametersShort = 15Long = 35distance = 10// end of parametersMAShort = Average[Short](close)MALong = Average[Long](close)If MAShort CROSSES OVER MALong and LngArrowOK = 0 THENDRAWARROWUP(barindex, low-distance*pipsize) coloured(0,255,0)//Signal = lowLngArrowOK = 1SrtArrowOK = 0ELSif MAShort CROSSES UNDER MALong and SrtArrowOK = 0 THENDRAWARROWDOWN(barindex, high+distance*pipsize) coloured(255,0,0)//Signal = HighLngArrowOK = 0SrtArrowOK = 1ENDIFRETURN MAShort as "Short MA", MALong as "Long MA"1 user thanked author for this post.
02/07/2024 at 10:51 PM #227618Thank you so much. Your amendment worked. I notice in your code you are using “LngArrowOK” and “SrtArrowOK” , but are they name you used or something already prebuilt function in the PRT?
Also, you used a very smart logic where you are using this two in a sequence such that arrow only appears if up or down arrow was previously appeared and hence removing issue i faced earlier. Very smart and intelligent logic!
However, the variable distance is creating issue as having it set to 10 means Arrow are not on the chart but they fly up or down and I need to zoom in too much to see them on some of the stock while it appears correctly on others. I reduced value from 10 to 0.1 and it worked on majority but still not correctly set on few stocks. so is there a way to have them appear on the candle up or down side ?
02/08/2024 at 1:17 AM #227620Thank you so much. Your amendment worked. I notice in your code you are using “LngArrowOK” and “SrtArrowOK” , but are they name you used or something already prebuilt function in the PRT?
No, they are just variable names. You can change their names to whatever you want. Note that most of the black texts are variables and the blue and green texts are programming language commands; and the quantities are in magenta.
Also, you used a very smart logic where you are using this two in a sequence such that arrow only appears if up or down arrow was previously appeared and hence removing issue i faced earlier. Very smart and intelligent logic!
Congratulations, you have got the logic perfectly.
However, the variable distance is creating issue as having it set to 10 means Arrow are not on the chart but they fly up or down and I need to zoom in too much to see them on some of the stock while it appears correctly on others. I reduced value from 10 to 0.1 and it worked on majority but still not correctly set on few stocks.
I decided to create a variable called distance in the settings section so that you can change the distance of the arrow depending on the time period or the financial instrument to which you apply the indicator.
It is also more agile for you to replace the graphic command of the arrows with the graphic command that paints vertical lines, assigning a different color to each type of entry. Look this new code:1234567891011121314151617181920212223// define parametersShort = 15Long = 35distance = 10opacity = 50 // Opacity of vertical lines (0: transparent; 255: opaque)// end of parametersMAShort = Average[Short](close)MALong = Average[Long](close)If MAShort CROSSES OVER MALong and LngArrowOK = 0 THEN//DRAWARROWUP(barindex, low-distance*pipsize) coloured(0,255,0)//Signal = lowDRAWVLINE(barindex) coloured(0,200,0,opacity) style(line,5) //Signal = lowLngArrowOK = 1SrtArrowOK = 0ELSif MAShort CROSSES UNDER MALong and SrtArrowOK = 0 THEN//DRAWARROWDOWN(barindex, high+distance*pipsize) coloured(255,0,0)//Signal = HighDRAWVLINE(barindex) coloured(255,0,0,opacity) style(line,5) //Signal = lowLngArrowOK = 0SrtArrowOK = 1ENDIFRETURN MAShort as "Short MA", MALong as "Long MA"so is there a way to have them appear on the candle up or down side ?
I don’t know if I’m understanding what you’re saying correctly because “distance” variable is used to decide how far each type of arrow is located (some up and others down, taking the candle close as a starting point).
1 user thanked author for this post.
02/08/2024 at 2:23 AM #227621I understand that the distance variable will allow manually adjusting location but if you want to go through various stocks saved in your list then it will be difficult to use it. As per the image below, you can see that for large and even for mid cap prie stock the arrow literally not visible as they all appear at the bottom of the page. only for the small cap arrow appears just above or below the candle and some location inside the candle.
So what I was looking for is to have upward arrow to be placed at the bottom of the candle and downward arrow at the top of the candle as default as per attached second image you will see them in pink circles.
I checked code provided above by JC_Bwyan and used his line ” low–2*pipsize ” but removed numerical number so it is now “low*pipsize” and it gives arrow on top & bottom of the candle for small caps but not for large caps or mid caps.
12345678910111213141516171819// define parametersdistance = 0.15// end of parametersMAShort = Average[Short](close)MALong = Average[Long](close)If MAShort CROSSES OVER MALong and LngArrowOK = 0 THENDRAWARROWUP(barindex, low*pipsize) coloured(0,255,0)//Signal = lowLngArrowOK = 1SrtArrowOK = 0ELSif MAShort CROSSES UNDER MALong and SrtArrowOK = 0 THENDRAWARROWDOWN(barindex, high*pipsize) coloured(255,0,0)//Signal = HighLngArrowOK = 0SrtArrowOK = 1ENDIFRETURN MAShort as "Short MA", MALong as "Long MA"02/08/2024 at 10:56 AM #227656Hi!
I use ATR and it works quite good, at least for me 🙂
Example drawing arrows123456789101112131415161718192021// define parametersShort=15Long=35distance = 0.15ATR = averagetruerange[10](close)// end of parametersMAShort = Average[Short](close)MALong = Average[Long](close)If MAShort CROSSES OVER MALong and LngArrowOK = 0 THENDRAWARROWUP(barindex, low-0.1*atr) coloured(0,255,0)//Signal = lowLngArrowOK = 1SrtArrowOK = 0ELSif MAShort CROSSES UNDER MALong and SrtArrowOK = 0 THENDRAWARROWDOWN(barindex, high+0.1*atr) coloured(255,0,0)//Signal = HighLngArrowOK = 0SrtArrowOK = 1ENDIFRETURN MAShort as "Short MA", MALong as "Long MA"02/08/2024 at 1:32 PM #227662Hi there !!
What Iván has explained is a quite practical solution for your case (I would replace the value that he has written “o.1” with the text “distance”, which is the distance variable that we have previously mentioned, to be able to control the adjustment of the distance according to your need).
Also try my last code, the one that uses vertical lines.1 user thanked author for this post.
02/11/2024 at 10:43 PM #22794502/14/2024 at 6:53 AM #228124I used -0.1 & +0.1 as per your code and it works on large cap but doesn’t work on penny stocks.
could you check on your system if you can see arrow correctly for the stock code ” TTI ” it’s Australian stock Traffic technology? and let me know if it works in your system?
02/14/2024 at 6:55 AM #228125 -
AuthorPosts