coding 4-day low/high

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #96361 quote
    therookie
    Participant
    Junior

    I am wondering how to put down two things in code:

    1: Go short if today’s close is a new 4-day high

    2. Close the short position if today’s close is a new 4-day low

    Help would be much appreciated!

    #96362 quote
    Harrys
    Participant
    Junior
    DEFPARAM PRELOADBARS = 100
    DEFPARAM Cumulateorders = False
    
    s1 = (close = highest[4](close))
    exit1 = (close = lowest[4](close))
    
    size = 1
    
    If s1 THEN
    sellshort size contracts at market
    ENDIF
    
    If exit1 and shortonmarket then
    exitshort at market
    ENDIF

    Hope this should help

    therookie thanked this post
    #96364 quote
    Vonasi
    Moderator
    Master

    Go short if today’s close is a new 4-day high

    Bit difficult to be 100% certain what you mean by this. Do you mean that today’s close has to be higher than the last three closes (which is what Harrys has coded) or do you mean that today’s close has to be higher than the highest high of the last three candles?

    For the latter your conditions would need to be:

    s1 = close > highest[3](high[1])
    exit1 = close < lowest[3](low[1])
    #96386 quote
    therookie
    Participant
    Junior

    Ok good question, maybe I didn’t specify it well enough.

    I want today’s close to be higher than the last 3 days’ closes so I guess Harrys code is what I want.

    But why is there equal signs and not ‘<‘ and ‘>’ signs?

    #96390 quote
    Nicolas
    Keymaster
    Master

    Try this version: 😉

    s1 = close > highest[3](close[1])
    exit1 = close < lowest[3](close[1])

    s1 = close is higher than the higher close of the last 3 days

    exit1 = close is lower than the lower close of the last 3 days

    #96391 quote
    robertogozzi
    Moderator
    Master

    Harry’s code (lines 4-5) does its job correctly, current close MUST be >= than the previous 3 candles.

    Vonasi’s code does the same job with a slightly different approach.

    #96393 quote
    robertogozzi
    Moderator
    Master

    I can’t edit from my mobile.

    Vonasi’s code does something different.

    Nicolas’code has just a slightly different approach.

    #96405 quote
    Vonasi
    Moderator
    Master

    Harrys code does the job by obtaining the value of the highest close in the last four candles and comparing it to the current close. It assumes that if they are the same value then this close must be the highest. It does not however allow for the fact that one or more of the closes could have identical values in which case your entry criteria is not strictly being met. Nicolas’ version and my version do allow for this possibility. Nicolas’ does it with the last three closes which is what you want whereas my version just does the same thing with the last three highs.

    therookie thanked this post
    #96407 quote
    therookie
    Participant
    Junior

    Thank you guys for the help and you Vonasi for the explanation.

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

coding 4-day low/high


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
therookie @prophet85 Participant
Summary

This topic contains 8 replies,
has 5 voices, and was last updated by therookie
6 years, 10 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 04/13/2019
Status: Active
Attachments: No files
Logo Logo
Loading...