Help with code for basic market modeling

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #19401 quote
    Derek
    Participant
    Veteran

    Hi!

    I need some programming help since I am still building my skills.

    Due to a bug in Pro Order 10.2 I started to programm indicators instead of backtests because the backtesting engine returned wrong entries and exits on the more sophisticated indicators.

    It all kind of went it’s own way from there and now I use it to model markets on a basic level.

     

    The goal is to evaluate price behavior after a specific pattern occurs (Event A).

    The open of the following bar is then compared to the five following closes.

    Please find this idea in Pseudocode below.

    If eventA[5]=1 and close[0,1,2,3,4] > open[4] then
    
    Gr = gr + 1 // gr is the good result
    
    Else
    
    Br = br +1 //br is the bad result
    
    Endif

     

    One problem is:

    If event A happens on two consecutive days, the code above will check the condition for the first event only once and for second event five times.

    The first event is overwritten by the second. But it should also be evaluated for the next five days. To put it differently, several events of the A-Type can be going on at the same time.

    I resolved this very manually by adding five separate events.

    The following commented code shows the procedure for the first event and how the second event is being initiated.

     

    ////////////////////////////////////////////////////////////////////////////////////////
    if (trigger and event1 = 0) or (trigger and event5 = 5) then     //The trigger is the condition for the event and event1 is not in use.
    event1=1    
    endif
    ////////////////////////////////////////////////////////////////////////////////////////
    if event1[1] = 1 and open1 =0 then
    open1 = open
    flag1=0    //The flag variable is used to count only good results that are in direct relation to an event. Otherwise it will keep counting good results when the price is rising.
    events = events+1   //this is a counter for the number of number of currently ongoing events.
    endif
    
    if event1[1] = 1 and close11= 0 then
    close11 = close
    endif
    if event1[2] = 1 and close11<>0 and open1[1]<>0 then
    close12 = close
    endif
    if event1[3] = 1 and close12<>0 and open1[1]<>0 then
    close13 = close
    endif
    if event1[4] = 1 and close13<>0 and open1[1]<>0 then
    close14 = close
    endif
    if event1[5] = 1 and close14<>0 and open1[1]<>0 then
    close15 = close
    endif
    
    if close11 > open1 and event1=1 and flag1 = 0 then
    gr1= gr1 +1    //event 1 has a good result
    ret1 = close11-open1+ret1    //The difference between the close and the open is the result.
    flag1=1   //This is actually a break in one of the loops. If the next close is also higher it is not counted as "gr".
    gr11=gr11+1  //This is a counter for the first event being a "good result" on the first bar.
    done1=1  // Resets the variables related to event1.
    elsif close12 > open1 and event1=1 and flag1 = 0 then
    gr1= gr1 +1
    ret1 = close12-open1+ret1
    flag1=1
    gr12=gr12+1
    done1=1
    elsif close13 > open1 and event1=1  and flag1 = 0 then
    gr1= gr1 +1
    ret1 = close13-open1+ret1
    flag1=1
    gr13=gr13+1
    done1=1
    elsif close14 > open1 and event1=1 and flag1 = 0 then
    gr1= gr1 +1
    ret1 = close14-open1+ret1
    flag1=1
    gr14=gr14+1
    done1=1
    elsif close15 > open1 and event1=1 and flag1 = 0 then
    gr1= gr1 +1
    ret1 = close15-open1+ret1
    flag1=1
    gr15=gr15+1
    done1=1
    endif
    
    
    //Code for clearing the variables AND accounting for a bad result, since all five closes were below "open1".
    if event1[6]= 1 and event1[5]=1 and event1[4]=1 and event1[3]=1 and event1[2]=1 and event1[1]=1 or done1=1 then
    event1=0
    if flag1=0 then   //in case a good result occurs the flag is set to one.
    br1 = br1+1       //flag is zero after all periods, so it is a bad result.  
    flag1 = 1         
    ret1 = ret1+(close15-open1)
    endif
    open1 = 0
    close11 = 0
    close12 = 0
    close13 = 0
    close14 = 0
    close15 = 0
    done1=0
    events = events-1
    endif
    /////////////////////////////////////////////////////////////////////////////////////////
    //EVENT 2
    ////////////////////////////////////////////////////////////////////////////////////////
    if trigger and event1[1]=1 then
    event2=2
    
    endif

     

    At the end all of the results are summed up:

    gr=gr1+gr2+gr3+gr4+gr5
    br=br1+br2+br3+br4+br5
    ret=ret1+ret2+ret3+ret4+ret5
    binprob=gr/(br+gr)

     

    The return and the binprob variables are the starting points for actual evaluation and analysis. They provide a basic insight into the price movement that follows event A and return a winning-losing probability for this type of event and time span.

    As you can see it is difficult to add more events and more bars following the initial event. Can you show me how to resolve this? Maybe put this procedure into proper loops?

    Ultimately I want to add another event B following event A. This would be something along the lines of:

    If the price crosses above the five days moving average look for a´close higher than tomorrows open within the next five days. But if the price crosses below the 5 days moving average before the good result occurs check if there will be a close below tomorrows open within the next five days following the second cross.

    #19540 quote
    Nicolas
    Keymaster
    Master

    Hi Derek,

    I must admit that read and understand the code you have posted is a bit tricky .. 🙂

    Please don’t be offend, but in order to help you in the most effective way, I believe I should rebuild the whole code in a proper coding way (and it will also be easier and quicker for me).

    Would you mind telling me what is the “eventA” please? I need to start with a good knowledge of the whole plan! Thanks in advance.

    #19545 quote
    Derek
    Participant
    Veteran

    Hi Nicolas,

    thank you for offering your support and I surely am rather thankfull.

    It does not matter what event A is but I have been using John Ehlers Supersmoother from the library.

    The condition is S > S[1].

    Period = 8
    Data = Close
    PI = 3.14159
    f = (1.414*PI) / Period
    a = exp(-f)
    c2 = 2*a*cos(f)
    c3 = -a*a
    c1 = 1 - c2 - c3
    if barindex>Period then
    S = c1*(Data[0]+Data[1])*0.5 + c2*S[1] + c3*S[2]
    endif
    #19654 quote
    Derek
    Participant
    Veteran

    Hello Nicolas,

    let me try to explain in a different way what this is supposed to be.

    I consider it to be more of a universal tool for statistical analysis than an actual indicator or even a handicapped backtest. There is a good result (the condition has been meet after X days) and the corresponding bad result. If I look at todays price chart and ask myself: What is the right side of this market?” the binprob variable can provide an easy insight into the patterns just observed on the chart.

    For example:

    If the price crosses over the Supersmoother what is the probability that the price will (still) be above the indicator in 1,2,3,4,5..X days? If I want to enter a market and the price has been above the indicator for 6 days already does this mean I could also wait for 3-4 days more and hope for a pullback with a 70-80% probability? Thus telling me in which direction I should enter and when a good moment could be comming up?

    The condition to be checked (price above the supersmoother) can easily be adapted to many different needs. In return this indicator can provide a probability distribution for the request.

    Another example are the many forum posts asking for a exit condition after 3 bars. Why 3 bars and not five? The return function can provide an insight into the expected return for every following day.

    I hope this helps you a bit better to understand where this is going.

    In an optimal world those results could be exported into a spreadsheet 🙂 Anyways in my view it is a little swiss army knife and surely think that other users can profit from this, too.

    #23363 quote
    Wing
    Participant
    Veteran

    Hello! A late reply from me here, but an interesting problem. Did you manage to solve it? Was the indicator/system of any use in terms of edge?

    I have created similar systems based on a similar technique (basic form of machine learning). This is tricky to do in PRT, but possible.

    Derek thanked this post
    #23377 quote
    Nicolas
    Keymaster
    Master

    If you want to know if the price goes in the same direction of your indicator, you could use correlation between them with least square regression analysis.

    #23491 quote
    Derek
    Participant
    Veteran

    Hello Wing,

    unfortunately I have not been able to resolve this in PRT and have returned to excel to do it.

    I started this thread to get a better statistical analysis tool and not to test an actual system.

    #23492 quote
    Derek
    Participant
    Veteran

    @Nicolas: I cannot wrap my head around your suggestion. Would you mind to elaborate a little more?

    #23515 quote
    Nicolas
    Keymaster
    Master

    @Derek

    Sorry, I just read again the whole thread, it will not be helpful for your statistical analysis tool. I’m jumping from topic to topic, so sometimes I should refresh my ideas 🙂

    For example: If the price crosses over the Supersmoother what is the probability that the price will (still) be above the indicator in 1,2,3,4,5..X days? If I want to enter a market and the price has been above the indicator for 6 days already does this mean I could also wait for 3-4 days more and hope for a pullback with a 70-80% probability? Thus telling me in which direction I should enter and when a good moment could be comming up?

    If I’m referring to this example, all we need to do is to count how much bars were above since the last cross of Close above the Supersmoother curve? No matter if a candle Close was above or below the previous one, etc. only a count above/below Supersmoother?

    #23559 quote
    Derek
    Participant
    Veteran

    @Nicolas:That’s more like it.

    The price crossing the Supersmoother is an event or a condition. The question is: What is the probability that the event will still be true in X bars?

    So, maybe, the code has to start from the bar when prices crosses UNDER the Supersmoother and then look back how many bars the price was above the Supersmoother. The direction of the price does not matter.

    Every time the price crosses ABOVE the Supersmoother the number of events observed increases ( event = event +1).

    Every time the price was above the Supersmoother for longer than X bars it was a good event ( good event +1).

    Good event / event  = Probability

    #23572 quote
    Nicolas
    Keymaster
    Master

    Something like this?

    Period = 8
    Data = Close
    PI = 3.14159
    f = (1.414*PI) / Period
    a = exp(-f)
    c2 = 2*a*cos(f)
    c3 = -a*a
    c1 = 1 - c2 - c3
    if barindex>Period then
    S = c1*(Data[0]+Data[1])*0.5 + c2*S[1] + c3*S[2]
    endif
    
    if close crosses over S then 
     event=event+1
     overbar = barindex
    endif
    
    if close crosses under S then 
     goodevent = goodevent+(barindex-overbar)
    endif 
    
    stats = goodevent/event
    
    return stats 
    Derek and Wing thanked this post
    #23658 quote
    Derek
    Participant
    Veteran

    That is quite something Nicolas. Thank you.

    Please find my version of the stats variable below.

    Period = 8
    Data = Close
    PI = 3.14159
    f = (1.414*PI) / Period
    a = exp(-f)
    c2 = 2*a*cos(f)
    c3 = -a*a
    c1 = 1 - c2 - c3
    if barindex>Period then
    S = c1*(Data[0]+Data[1])*0.5 + c2*S[1] + c3*S[2]
    endif
    
    once duration = 0
    
    if close crosses over S then
    event=event+1
    overbar = barindex
    endif
    
    if close crosses under S then
    underbar = barindex
    duration = underbar-overbar
    calc = 1
    else
    calc = 0
    //goodevent = goodevent+(barindex-overbar)
    endif
    
    if calc = 1 and duration > 4 then
    goodevent = goodevent + 1
    endif
    
    stats = goodevent/event
    
    return stats
    

     

    In the attached screenshot you can see that a lower high of the stats variable can be a reasonable indicator for a downswing and a higher low an indicator for the corresponding upswing. A long flat top of the indicator could be the end of a major trend.

    Wing and Nicolas thanked this post
    #23682 quote
    Nicolas
    Keymaster
    Master

    Purely statistical speaking, you can now add a moving average with a period of your choice with standard deviation from it :1 and 2

    Period = 8
    Data = Close
    PI = 3.14159
    f = (1.414*PI) / Period
    a = exp(-f)
    c2 = 2*a*cos(f)
    c3 = -a*a
    c1 = 1 - c2 - c3
    if barindex>Period then
    S = c1*(Data[0]+Data[1])*0.5 + c2*S[1] + c3*S[2]
    endif
    
    once duration = 0
    
    if close crosses over S then
    event=event+1
    overbar = barindex
    endif
    
    if close crosses under S then
    underbar = barindex
    duration = underbar-overbar
    calc = 1
    else
    calc = 0
    //goodevent = goodevent+(barindex-overbar)
    endif
    
    if calc = 1 and duration > 4 then
    goodevent = goodevent + 1
    endif
    
    stats = goodevent/event
    
    meanP = 500
    if barindex>500 then 
    mean = average[meanP](stats)
    std1up = mean+std[meanP](stats)
    std1dn = mean-std[meanP](stats)
    std2up = mean+std[meanP](stats)*2
    std2dn = mean-std[meanP](stats)*2
    endif
    
    
    return stats, mean, std1up,std1dn,std2up,std2dn
Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.

Help with code for basic market modeling


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Derek @derek Participant
Summary

This topic contains 12 replies,
has 3 voices, and was last updated by Nicolas
9 years ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 12/30/2016
Status: Active
Attachments: No files
Logo Logo
Loading...