stochastic indicator

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #111282 quote
    Trebor
    Participant
    New

    Hello,

    Can someone help me how to code an indicator that returns the difference in pips between enter at < 20 and exit at >80.

    Thank you.

    #111285 quote
    Vonasi
    Moderator
    Master

    Trebor – Welcome to the forums.

    You may need to give a little more info. What do you mean exactly by pips between >80 and < 20? Do we start counting when the stochastic crosses up or when it crosses down over each of these values? What do we do if it recrosses the 20 – do we start counting again or ignore it?

    #111286 quote
    Trebor
    Participant
    New

    Hi Vonasi,

    Thank you for your answer and thank you for the welcome

    I would like to get it start to count when %K enters <20 and stop count when it becomes >80. I would like to get the difference between entering <20 and to entering >80. Also as you write, I would not like it to start to recount if it reenters <20 before it has entered >80. Hope this is enough information.

    #111290 quote
    Vonasi
    Moderator
    Master

    Try this:

    mystoch = Stochastic[10,3](close)
    
    if not on and mystoch crosses under 20 then 
    myopen = close
    on = 1
    endif
    
    if on and mystoch crosses over 80 then
    myclose = close
    diff = myclose - myopen
    on = 0
    endif
    
    return diff
    Trebor thanked this post
    #111310 quote
    Trebor
    Participant
    New

    Thank you.

    #111317 quote
    GraHal
    Participant
    Master

    Added as Log 182 to here …

    Snippet Link Library

    Trebor thanked this post
    #111322 quote
    Vonasi
    Moderator
    Master

    Just for fun I turned it into something that draws lines on the chart from the open to the close of the simulated trade. The total gain or loss is also displayed on the chart at the close of each trade. Lines and values are red for losers and green for winners.

    I also added a spread value that is subtracted from the results to more accurately simulate what the gain or loss might be. Change it to match whatever instrument you are using the indicator on or set to zero to test with no spread.

    Apply the indicator to the price chart.

    Spread = 1.5
    StochN = 10
    StochK = 3
    
    mystoch = Stochastic[StochN,StochK](customclose)
    
    if not on and mystoch crosses under 20 then
    myopen = close
    openindex = barindex
    on = 1
    endif
    
    if on and mystoch crosses over 80 then
    diff = close - myopen - spread
    r = 0
    g = 128
    if diff <= 0 then
    r = 128
    g = 0
    endif
    drawsegment(openindex, myopen,barindex,close) coloured(r,g,0)
    drawtext("#diff#",barindex,high+average[500](high-low)/2,SansSerif,Bold,12) coloured(r,g,0)
    on = 0
    endif
    
    return
    
    Trebor thanked this post
    #111327 quote
    Vonasi
    Moderator
    Master

    I must be really bored because I developed it a bit further. This version also calculated the returns for going long or short and displays them on the chart. It displays the gain or loss on this trade, the average gain per trade, the win rate for all trades and the total gain or loss for all trades.

    • G = Total gain
    • AvgG = Average Gain per Trade
    • WR = % Win Rate
    • Unlabelled = The gain or loss for this trade.

     

    Long trade results are above the candle in green and short trade results are below in red.

    Spread = 1.5
    StochN = 10
    StochK = 3
    UpperLevel = 80
    LowerLevel = 20
    
    mystoch = Stochastic[StochN,StochK](customclose)
    
    if not on and mystoch crosses under lowerlevel then
    myopen = close
    openindex = barindex
    on = 1
    endif
    
    if on and mystoch crosses over upperlevel then
    diff = close - myopen - spread
    sdiff = myopen - close - spread
    count = count + 1
    if sdiff > 0 then
    r = 128
    g = 0
    swin = swin + 1
    endif
    if diff > 0 then
    r = 0
    g = 128
    lwin = lwin + 1
    endif
    ltotal = ltotal + diff
    stotal = stotal + sdiff
    lwinrate = round(((lwin/count)*100)*10)/10
    swinrate = round(((swin/count)*100)*10)/10
    lavgwin = round((ltotal/count)*10)/10
    savgwin = round((stotal/count)*10)/10
    ltotalr = round(ltotal*10)/10
    stotalr = round(stotal*10)/10
    
    drawsegment(openindex, myopen,barindex,close) coloured(r,g,0)
    
    drawtext("G #ltotalr#",barindex,high+(average[500](high-low)/2)*4,SansSerif,Standard,12) coloured(0,128,0)
    drawtext("AvgG #lavgwin#",barindex,high+(average[500](high-low)/2)*3,SansSerif,Standard,12) coloured(0,128,0)
    drawtext("WR #lwinrate#%",barindex,high+(average[500](high-low)/2)*2,SansSerif,Standard,12) coloured(0,128,0)
    drawtext("#diff#",barindex,high+(average[500](high-low)/2),SansSerif,Standard,12) coloured(0,128,0)
    drawtext("#sdiff#",barindex,low-(average[500](high-low)/2),SansSerif,Standard,12) coloured(128,0,0)
    drawtext("WR #swinrate#%",barindex,low-(average[500](high-low)/2)*2,SansSerif,Standard,12) coloured(128,0,0)
    drawtext("AvgG #savgwin#",barindex,low-(average[500](high-low)/2)*3,SansSerif,Standard,12) coloured(128,0,0)
    drawtext("G #stotalr#",barindex,low-(average[500](high-low)/2)*4,SansSerif,Standard,12) coloured(128,0,0)
    on = 0
    endif
    
    return

     

    I also turned the code into a line chart to show the historical results. You can turn on or off which of the three line types are displayed.

    Really it would be easier just to write a strategy to get all this same information but like I said I was bored!

     

    DisplayTotal = 1 //0=off 1=on
    DisplayWinRate = 1 //0=off 1=on
    DisplayAvgGain = 1 //0=off 1=on
    
    Spread = 1.5
    StochN = 10
    StochK = 3
    UpperLevel = 80
    LowerLevel = 20
    
    mystoch = Stochastic[StochN,StochK](customclose)
    
    if not on and mystoch crosses under lowerlevel then
    myopen = close
    on = 1
    endif
    
    if on and mystoch crosses over upperlevel then
    diff = close - myopen - spread
    sdiff = myopen - close - spread
    count = count + 1
    if sdiff > 0 then
    swin = swin + 1
    endif
    if diff > 0 then
    lwin = lwin + 1
    endif
    
    ltotal = ltotal + diff
    stotal = stotal + sdiff
    lwinrate = round(((lwin/count)*100)*10)/10
    swinrate = round(((swin/count)*100)*10)/10
    lavgwin = round((ltotal/count)*10)/10
    savgwin = round((stotal/count)*10)/10
    on = 0
    endif
    
    once ltotalr = undefined
    once lwinrater = undefined
    once lavgwinr = undefined
    once stotalr = undefined
    once swinrater = undefined
    once savgwinr = undefined
    
    if DisplayTotal then
    ltotalr = ltotal
    stotalr = stotal
    endif
    
    if DisplayWinRate then
    lwinrater = lwinrate
    swinrater = swinrate
    endif
    
    if DisplayAvgGain then
    lavgwinr = lavgwin
    savgwinr = savgwin
    endif
    
    return 0,ltotalr coloured(0,128,0) as "LGain",stotalr coloured(128,0,0) as "SGain",lwinrater coloured(0,128,0) as "LWR%", swinrater coloured(128,0,0) as "SWR%",lavgwinr coloured(0,128,0) as "LAvgG", savgwinr coloured(128,0,0) as "SAvgG"
    Trebor thanked this post
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.

stochastic indicator


ProBuilder support

New Reply
Author
author-avatar
Trebor @trebor Participant
Summary

This topic contains 7 replies,
has 3 voices, and was last updated by Vonasi
6 years, 2 months ago.

Topic Details
Forum: ProBuilder support
Language: English
Started: 10/26/2019
Status: Active
Attachments: 5 files
Logo Logo
Loading...