Backtesting: Buy order Problem ie BarIndex

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #141242 quote
    Courtney Smith
    Participant
    Junior

    So I am still learning Pro Real Code syntax. I have a successful trading system i been using for years but finally want to get into automated trading and i want to do some back testing first.

    So the system consist of 3 different conditions

    1. Todays close –  The 10 day Moving Average of the close.
    2. Todays 10 day Moving Average – The 10 day Moving Average from 10 days ago
    3. Todays close – the close from 40 days ago.

    if all three conditions are met (count = 3) then i want to buy 1 contract and go long. if all 3 conditions are not met (countb= -3) then  i want to sell 1 contract. once we get long (or short) we tighten or loosen the stop depending on what the condition count (or count b) is at +3 +2 +1 -1 -2 -3.

    I use daily (time frame) and regardless of the units (i use 200 units). Starting the backtest i need to skip the first 40 days of the data as all three of the conditions have not had time to be tested yet. I would assume i can use the “bar index” to determine when i can finally activate buy or selling but the system just doesn’t seem to work as I thought. If you use AMD you can see in my screen shot that the first buy signal is at 50 days  it it should be may 18th on the 40th day. So my question is how do I use bar index or some other code to ensure that the buying and selling happens after the first 40 days?

    !!!!!!!!!!!Add to Variables!!!!!! x[1], Period[40], ATR[14]
    
    
    count=0
    countb=0
    
    MA = Average[period](Dclose(period))
    cond1= dclose(0)-average[period](close)
    cond2= average[period](close)-MA
    cond3= dclose(0)-dclose(x)
    if cond1>0 then
    count=count+1
    endif
    if cond2>0 then
    count=count+1
    endif
    if cond3>0 then
    count=count+1
    endif
    if cond1<0 then
    countb=countb-1
    endif
    if cond2<0 then
    countb=countb-1
    endif
    if cond3<0 then
    countb=countb-1
    endif
    
    
    
    // Buying
    if barindex >40 then
    if not LongOnMarket and count =3 then
    buy 1 contract at market
    endif
    endif
    

     

    Any help would be greatly appreciated.

    #141252 quote
    GraHal
    Participant
    Master

    Todays close – the close from 40 days ago.

    If TF = Daily then below code will satisfy condition.

    PreLoad Bars are a default of 2000 so why wait 40 days using bar index?

     

    Close - Close[40]
    #141256 quote
    Courtney Smith
    Participant
    Junior

    Sorry for my inexperience, but I am not sure what the “TF” means?

    I attached another screen shot. the first arrow shows a 3+ condition. that should have been my first purchase not 10 days later. I just dont know why my code is not making the first purchase on the correct day. Clearly the indicator at the bottom says its a +3 day just not sure why the backtest is not picking it up correctly.

    #141273 quote
    GraHal
    Participant
    Master

    TF means Timeframe which is the time required for one bar to complete and then a new bar starts.

    #141274 quote
    Nicolas
    Keymaster
    Master

    Because your code is waiting for barindex to be more than 40 bars, while it is not needed because history is preloaded and your MA and daily Close are already known at first bar when you launch the strategy. Try with this modified code:

    // !!!!!!!!!!!Add to Variables!!!!!! x[1], Period[40], ATR[14]
    
    count=0
    countb=0
    
    MA = Average[period](Dclose(period))
    cond1= dclose(0)-average[period](close)
    cond2= average[period](close)-MA
    cond3= dclose(0)-dclose(x)
    if cond1>0 then
    count=count+1
    endif
    if cond2>0 then
    count=count+1
    endif
    if cond3>0 then
    count=count+1
    endif
    if cond1<0 then
    countb=countb-1
    endif
    if cond2<0 then
    countb=countb-1
    endif
    if cond3<0 then
    countb=countb-1
    endif
    
    // Buying
    if not LongOnMarket and count =3 then
     buy 1 contract at market
    endif
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.

Backtesting: Buy order Problem ie BarIndex


ProOrder support

New Reply
Author
Summary

This topic contains 4 replies,
has 3 voices, and was last updated by Nicolas
5 years, 5 months ago.

Topic Details
Forum: ProOrder support
Language: English
Started: 08/09/2020
Status: Active
Attachments: 3 files
Logo Logo
Loading...