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.