SMA/EMA crossover strategy not working as I think it should

Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • #161380 quote
    Essraw
    Participant
    Junior

    Hey! I have written so much more advanced things than this so it is pretty embarrassing to write this, but part of my big code didn’t act like I wanted it and when I wrote out a sample and  it still didn’t work, it is just a normal MA crossover. The buy and sell don’t appear where they are supposed to.

    Please tell me what I have done wrong.

    I will attach the code and also a picture of the result.

    baseline=exponentialaverage(close)[100]
    sma=average(close)[20]
    
    if sma crosses over baseline then
    buy 1 shares at market
    endif
    if sma crosses under baseline then
    sellshort 1 shares at market
    endif
    #161381 quote
    GraHal
    Participant
    Master

    The buy and sell don’t appear where they are supposed to.

    Code is good so it must be your expectations above that are misleading you?

    Attach a screenshot and explain more what you expecting?

    #161382 quote
    Essraw
    Participant
    Junior

    Right I’m sorry, I tried, I’ll try again.

    Capturetest.png Capturetest.png
    #161384 quote
    GraHal
    Participant
    Master

    Try GRAPH = sma crosses over baseline in your Algo code … does it show a 1 / true at correct positions?

    #161385 quote
    Vonasi
    Moderator
    Master

    Essraw – I have given your topic a more meaningful title. Please try to use more descriptive titles on any future topics. 

    #161386 quote
    Vonasi
    Moderator
    Master

    Have you double checked whether your SMA and EMA indicators are both using close and not something else such as median price?

    #161387 quote
    Essraw
    Participant
    Junior

    Thank you Vonasi, GraHal I don’t quite get what you mean.

    #161388 quote
    Vonasi
    Moderator
    Master

    GraHal is suggesting adding the line:

    GRAPH sma crosses over baseline

    ….to the bottom of your strategy. I imagine though that it will just show the same points of crossover as your strategy is opening and closing trades.

    #161389 quote
    Essraw
    Participant
    Junior

    Oh I see, yeah I tested it now, it shows the same!

    #161390 quote
    Essraw
    Participant
    Junior

    Yes they are all using close

    #161391 quote
    Vonasi
    Moderator
    Master

    Have you checked your SMA and EMA settings? Perhaps you have them averaging something different or a horizontal shift added so your indicators are showing something different to your strategy code?

    GraHal thanked this post
    Screenshot_1-2.png Screenshot_1-2.png
    #161393 quote
    Vonasi
    Moderator
    Master

    I have just done a quick test and can confirm that something is wrong. It appears to be trading when either close or high or low crosses the EMA. I will test further but there does seem to be some sort of bug.

    Essraw thanked this post
    Screenshot_2-1.png Screenshot_2-1.png
    #161395 quote
    Essraw
    Participant
    Junior

    no, nothing like that

    capturetest2.png capturetest2.png
    #161397 quote
    Essraw
    Participant
    Junior

    Yes it seems very weird! I have done a similar code a few weeks back and there was no problem!

    #161398 quote
    Vonasi
    Moderator
    Master

    It is a typo in your first two lines. Change it to this:

    baseline=exponentialaverage[100](close)
    sma=average[20](close)
    Essraw and GraHal thanked this post
Viewing 15 posts - 1 through 15 (of 17 total)
  • You must be logged in to reply to this topic.

SMA/EMA crossover strategy not working as I think it should


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Essraw @essraw Participant
Summary

This topic contains 16 replies,
has 3 voices, and was last updated by Vonasi
5 years ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 02/14/2021
Status: Active
Attachments: 4 files
Logo Logo
Loading...