backtest stocks ath

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #230642 quote
    dovakinus
    Participant
    New

    I want to write a code  for backtesting that buys the highest of the previous bar over a given period (1 week, 1 month, 1 year or more). with a target of 10%. for the stock of my choice. for example, it’s march 2024. i buy the highest reached in april 2024. thanks you

    #230651 quote
    Iván González
    Moderator
    Master

    Hi
    here you have an example (please,consider to use a stop loss or another way to exit than just the profit 10%)

    defparam cumulateorders=false
    n=20 //lookback period
    hhv = highest[n](high)
    
    if not longonmarket and high>hhv[1] then
    buy 1 contract at market
    //set stop %loss 10
    set target %profit 10
    endif
    #230663 quote
    dovakinus
    Participant
    New

    Thank you very much for your help IVAN, but I think I misspoke. Attached is a graphic that better explains what I want.

    SIMENS.jpg SIMENS.jpg
    #230696 quote
    dovakinus
    Participant
    New

    Can you help me please. thanks

    #230701 quote
    Iván González
    Moderator
    Master

    Ok. Should by like this.

    defparam cumulateorders=false
    n=1 //lookback period
    hhv = highest[n](high)
    
    if not longonmarket then
    buy 1 contract at hhv stop
    //set stop %loss 10
    set target %profit 10
    endif
    #230850 quote
    dovakinus
    Participant
    New

    Thank you very much IVAN, but that’s not exactly it. Here are normally the orders that must be taken. I will take example since 2017 ON APPLE. IVAN if you can start from this example to make the code.
    ATH IN 2017: 44. 30
    I TAKE A POSITION IN 2018 IF I REACH 44.3
    THEN 10% TARGET OR 10% STOP LOSS

    SAME IN 2018: 58.36
    I POSITION MYSELF FOR 2019 AT THIS PRICE AT 58.36

    IN 2019 : HIGH REACHED 73.49
    I’M POSITIONED AT THIS PRICE IN 2020

    2020 : HIGH REACHES 138.78
    ENTER THE 2021 MARKET AT THIS PRICE

    AND FOR ALL 10% TARGET AND 10% STOP LOSS FROM THIS PRICE.

    THANK YOU MR

    #230855 quote
    dovakinus
    Participant
    New

    I hope I’ve expressed myself well.

    #230947 quote
    dovakinus
    Participant
    New

    Ivan m’y Freind ?

    #230953 quote
    JC_Bywan
    Moderator
    Master

    First point: about working for any timeframe, just in case, if you’re after an all time high, keep in mind you need to make sure your timeframe is big enough to show all history (yearly or monthly is surely big enough, at the extreme opposite 1mn or (x) seconds might not be for older stocks)

     

    2nd point: keeping in mind first point’s limitation for smaller timeframe, if you have maximum history wanted in your backtest, and considering first bar is barindex=0 then you can try to define your ath with:

    highest[barindex+1](high)

    and check it has been reached in the signal candle with:

    if high = highest[barindex](high) then
    
    buy etc...

     

    3rd point is a moderation point: you asked your question in this topic first, the rule about double post applies so your same question posted later on french forum is considered double post and will be deleted (please have a look at yellow box at bottom of this page the “do not double post” rule). To answer your other question there, anyone can reply if they want and when they want, there isn’t on one hand those who only ask and on the other hand those who only answer, it would only lead to those who ask remaining alone in the end, please feel free to help others too when you see questions you know the answer to. Moderators just have extra tools in their hands to keep the forum tidy within their own availability. If the points above didn’t help enough or didn’t help at all, please feel free to carry on the conversation with anyone, moderator or not, but in this topic, not in a duplicate topic, thanks.

    #230962 quote
    Iván González
    Moderator
    Master

    Hi
    Here you have an example (to be runned in monthly, weekly, daily, …, but not annualy)

    defparam cumulateorders=false
    if year = year[1] then
    n=n+1
    else
    hhv = highest[n](high)
    n=0
    endif
    
    if not longonmarket and high crosses over hhv then
    buy 1 contract at hhv stop
    set stop %loss 10
    set target %profit 10
    endif
    
    graphonprice hhv coloured("red")
    AAPL-Semanal.png AAPL-Semanal.png
    #230964 quote
    Iván González
    Moderator
    Master

    JC, I just saw your comment right after I responded… Thanks

    #230970 quote
    dovakinus
    Participant
    New

    thank you IVAN but i’m probably explaining myself badly but that’s not it.  Like janury 2020 time 6 months. Ath 93.095.  code entry in july 91.28. or it must be  93.095 .

     

    And if you take a period spanning 1 year, nothing appears.

    #230977 quote
    Iván González
    Moderator
    Master

    Yes, in annualy nothing happens because there is not cross over. As I wrote, this system is to be runned in monthly, weekly, daily, timeframe, but not annualy.
    I have coded a cross over the last year high.
    Sorry but I don’t know what you want… maybe another one can help you.

Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.

backtest stocks ath


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
dovakinus @dovakinus Participant
Summary

This topic contains 12 replies,
has 3 voices, and was last updated by Iván González
1 year, 10 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 03/28/2024
Status: Active
Attachments: 2 files
Logo Logo
Loading...