Simple questions about a trend following strategy

Forums ProRealTime English forum ProOrder support Simple questions about a trend following strategy

Viewing 15 posts - 1 through 15 (of 15 total)
  • #35571

    Hi everybody,

    I recently get familiar with Pro Real Time, spent some time reading its manual and some topics in this forum.

    I still have some basic questions and be very thankful if you can help me to code my strategy.
    Here are my 4 questions:(sorry if they are too basic)

    1) When we use CrossOver function in PRT, whether it considers shadows as well or it only works based on close price?

    2) May I adjust the Crossover code like this:
    “if High crossover MM20 “
    OR
    “If close crossover MM20 “

    3) How is the best to code an increasing moving average?

    Please dont laugh at me but this is the only thing I imaging: Uptrend = Average(20)>Average{2}(20)>Average{3}(20)>Average{4}(20)>….>Average{5}(20)

    4) finally I wrote below code and test it in Pro Back Test, but it doesn’t bring any result after test. It seems the code is not working. Or it doesn’t find any position to trade. What’s your opinion?

    Edited by moderator to make PRT code format appear, please use the <> button in your new messages toolbar to insert PRT code

    Many thanks for your help
    Hugh
    #35587

    Hi,

    1. “crosses over” uses live close (shadows being just the trace of past temporary close before a candle is complete), might change during ongoing candle, but in the end uses final close value when candle is complete
    2. you can do “if any_variable crosses over any_other_variable then…”
    3. syntax for a simple moving average applied on close would be average[20](close), uptrend on 2 candles would be average[20](close)>average[20](close)[1], if you want continuous up over 3 candles then you need to use the “and” keyword:

      For more than 3 candles add another AND, etc…

    4. Line 1 : average(60) to be replaced by average[60](close)
      Line 7 : average(20) to be replaced by average[20](close)

     

    As a general tip, when typing your code in the code creation window, you should see in the bar menu a lightbulb icon, if you start typing a keyword (like “averag” for example) and click on the lightbulb icon, it should provide you with clickable example of matching keywords with associated syntax. That’s always useful when in doubt, even for experienced prt coders.

    2 users thanked author for this post.
    #35825

    Thank you so much @Noobywan ,

    Thanks for  putting me in the right direction, now I am working on my strategy and it goes forward very well.

    Just one more question: I wanna take long positions whenever price comes to touch the MA 60, or it’s very close to it like maximum 20 pip higher than MA60.

    I coded this, but I had a problem. PRT only considers close, open, high and low of a bar when it closed. However I want to get the position as soon as price came close to MA60 . I don’t want to wait until candle become close.

    I know there is a check box called “use tick by tick chart” , however even this wasn’t helpful.

    <>

    MA60= Average[60]

    If not long on market and price > MA60 and price < MA60+20

    then buy 1 contract

    endif

    <>

    this is my code , but PRT is not understanding what is Price !!

    Can you help please?

     

    Thanks

    Hugh

    #35837

    You need to use “close” instead of “price”, because your live price is the latest close of the ongoing candle while it’s not complete yet.

    Also, on a stocks index like the ftse the cas the dax etc… it’s ok to compare close and moving averages in “points” and adding points to them because the pointsize=1, but in forex you have to be careful if using pips they are in the same “dimension” than your variables (imagine your forex pair close is worth 1,20561 for example, and your MA60 in the 1,20450 area, you are not going to add 20 to this because it would become 21,20450… not what you wanted) so it depends on your pipsize for the forex pair you consider (see keyword pipsize and actual pipsize in your graph window).

    MA60 should be average[60](close)

    One last info, using the “<>” button to insert code is not achieved by typing <> but by clicking on it (see attachment)

    2 users thanked author for this post.
    #35842

    Code can be embed into BBcode like in the attached picture.

     

    1 user thanked author for this post.
    #35847

    Oh that was a great point I missed, so in Forex I should say if close < MA[60]+0.0020 , right?

    I ran the below code last night on GBPJPY from Jan 2015 until now, it only trades 4 times 🙁

    I supposed it only compares the close of the candles which you said it’s not the case, so what else can lead to this problem? Why it only took 4 positions over 2 years?!

    Thanks again for your time and valuable insight 🙂

    #36032

    Hi @Noobywan

    Sorry for bothering you again, not sure if you have seen my above message. Why do you think my code only took 4 positions over 2 years in GBPJPY?

    Thanks a lot for your help and advise 🙂

    #36036

    +20 should be converted to the current instrument:

     

    1 user thanked author for this post.
    #36043

    Hi,

    I rarely comment other people strategies and backtest, only for bug searching, and assuming posting rules are respected (ie providing screen capture of backtest for at least allowing other members either to spot the potential error sources from your attachments, or for being able to check they can reproduce same results and start the bug search from a sound same set of data).

    However, here, in order to help I’ll make an exception : without screen capture and without knowing which timeframe you ran your backtest, a pretty good guess for the “only 4 trades” might be that the strategy equity curve quickly reaches zero capital (and therefore doesn’t take any new trade having lost all initial capital), and if that’s the case when you look at your backtest equity curve, then I’d say that’s because the strategy has no stop, and too easy buy/sell strategies based on moving averages often could turn out to be losing money fairly fast. Even after the pipsize modification discussed is applied in the code in line 2.

    This is not a criticism, like I said I don’t comment strategies, it is not my place, it’s just a guess as to what happened from a partial set of information.

    Algorithmic trading is long and demanding journey. Could be discouraging at first, but Nicolas website allow all of us to feel less lonely and learn from each other along the way. Good luck.

    2 users thanked author for this post.
    #36133

    Hi @Noobywan ,

    I understand your point about not commenting on other people strategies, however I believe my problem currently is not with the strategy itself.

    The problem is about how to use PRT for automation a strategy. And just for making everything simple, I am trying to code a very basic thing:

    The strategy for example is buying when price is close to MA60 and then close the position when price cross under MA 20. (It doesn’t short sell to be as simple as possible)

    I am testing my coding skills with PRT with above simple example.

    The result is very odd, computer only trades 5 times over 2 years with this strategy. You simply know there are much more than 5 times during 2 years which price gonna be close to MA 60 however I don’t know why computer doesn’t take those positions.

    I made 3 screen shots, 1 from the code itself, 1 from the chart which I high lighted 2 spots which code should have taken position but it didn’t. Finally the 3rd screen shot is showing my maximum draw down was only $400 and there was enough money in account to take more positions.

    I really appreciate your time and advice, so I will get understand how this PRT is working. Then I can start to develop my own strategy.


    @nicolas
    thanks for your little tip in above comment which was extremely helpful 🙂

    Looking forward to hearing from you guys,

    many thanks again 🙂

     

     

    #36137

    My only guess is computer only considers closing price of each day, so it doesn’t count live prices which being traded during the day…

    it only took a position or close it when closing price of the day triggers the condition…

    So if that’s the case, then my next question would be how shall I ask computer to consider live prices which people trading during the day…

    #36143

    Ok, another guess, if not yet too familiar with PRT and you expect it to buy “real time”, however it’s not what the software does, it checks your conditions are true at the end of the candle and sends the order at the beginning of the next. So if you’re in daily timeframe, it as you guessed because end of candle is end of day. But it doesn’t have to be end of day, because you can choose lower than daily timeframe (for example H1) and then same principle of “conditions true or not at end of candle”, in case of H1 at end of each hour… So you might see your price getting close enough during a candle, but if it gets away at candle close or on the other side of it and your code doesn’t take that into account, then it won’t trigger the order.

    Also, when order is triggered, it happens at beginning of next candle so when checking behaviour of a strategy at first you might think it happens one candle too late but no, it’s doing ok because of order at beginning of next candle (this should change in the very near future with introduction of multitimeframe capability for proorder, opening the door for sending an order from smaller timeframe while strategy timeframe candle is not finished yet… but we don’t have this yet so I stick to what current PRT version 10.3 does).

    One great keyword to know for debugging strategies, or checking behaviour is what we expect and possibly modifying the code accordingly, is GRAPH (no more than 5 graph lines for 5 variables). Try this:

    When running the strategy you’ll have an extra window with your strategy displaying the “graph-ed” values. Should show you when the software considers at candle close if your (MA60, MA60+20*pipsize) window is met at close (=5) or almost met but not at close (=1).

    One last useful info just in case, you can check the value of pipsize at the top left of the window, just after “gbpjpy”, there’s a little “i” in a square you can bring your mouse above it and “i”nformation appears in a window, including pipsize value which for gbpjpy is 0.01

    1 user thanked author for this post.
    #36145

    I only did a 2 min scan of above posts (as you are getting better help than I can give) but I did notice you said … strategy for example is buying when price is close to MA60 … you are not by any chance confused about price Close (price at end of a bar) with close (a small percentage away) are you?

    Many apologies if above is not the case, but sometimes the daftest things can give us problems and we can’t see the wood for the trees? 🙂

    GraHal

    #36214

    Hi @Noobywan

    thanks for your explanation. The only thing I didn’t get is a few days back you said:

    “You need to use “close” instead of “price”, because your live price is the latest close of the ongoing candle while it’s not complete yet.”

    So I didn’t get whether PRT is using closing price of each candle, or the live price?

    My understanding is unfortunately it’s using only closing price of the candle, and exerciser the buying order at the beginning of next candle which is too late. However I can’t believe a sophisticated software like PRT is not able to use LIVE price…

    Is there any option I can use LIVE price instead of waiting for closing price?  Simply I want to do the trade as soon as Live price comes near to MA60 in Daily or comes higher than Pivot line etc etc.

    Many thanks again for your time and all the help and advice 🙂

    #36218

    Ok, I’ll try to rephrase it better: “price” is not a keyword in PRT coding language, in order to obtain “live price” (real live, not a replay of the history of the live), the correct keyword is “close”. So because “close” means the “last price only” for a complete candle, what I was trying to highlight is that “close” is also the live price for a not yet complete on-going candle. I was just trying with this to explain how “close” is used for 2 things, which are in fact just one thing from a certain point of view (if you consider that for a not yet complete candle, its true final close is not known yet, then in a way its latest price while uncomplete is a temporary close).

    That distinction between complete candle and not complete yet candle being made, then order execution is a separate subject. Yes order is sent at completion of a candle to be meant to be executed at opening of next candle (assuming perfect conditions for internet connection and fast calculations of code don’t make the order arrive too late).

    It is actually more important than one would think to be able to wait for candle completion in a particular timeframe to execute an order without having false signals. So we don’t want PRT to “change” that. However it is understandable that one could also want to trade from a smaller timeframe while using data of a bigger timeframe. That’s where multitiframe capability will soon be available and useful, both for when not able to simulate bigger timeframe data, and for when it is possible but requires some extra coding one doesn’t want to do.

    In your particular case, say you’re happy to trade a 1 minute timeframe rather than a daily timeframe for your price getting close to MA60 daily, and you don’t want to wait until multitimeframe becomes available in the PRT ProOrder module, you can still run your previously daily code into a minute timeframe by modifying your code accordingly. You would need to simulate where you daily MA60 is, by using keyword Dclose (making daily close value available for lower timeframes):

    https://www.prorealcode.com/documentation/dclose/

    MA60daily = (close + Dclose(1) + …etc… + Dclose(59) ) / 60

    You might want to code it in a “for to next” loop rather than typing the entire line of code with the 60 data.

    https://www.prorealcode.com/documentation/for/

    This should return your daily MA60 even if you are in a one minute timeframe, in which average[60](close) would only be the MA60 for the last 60 minutes of course.

    I hope I was clearer like this, because I’ll be away for a few days so it’s unlikely I post again for at least the first part of the week, if not the entire week possibly… (this will give me a taste of how Nicolas feels when he comes back after a few days and becomes several forum pages late…)

Viewing 15 posts - 1 through 15 (of 15 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login