Long bar Mean reverting code help

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #82452 quote
    Stratus6
    Participant
    Junior

    Hi All,

     

    I would be very grateful for any help with this small sell side code.

    The idea is for shorter time frames which I use for day trading but struggling to automate it.

    Principles as below. Sell side only in this case.

    1. Two longer than average bars become the new ‘range’
    2. The middle of this range is the ‘mean’ for buying back.
    3. Once the range is set every new bar triggers a sell unless its low crosses below the mean
    4. At the ‘mean’ all shorts are bought back and the trade ends

    Questions are one

    1. Is this code ok as I cannot backtest it because I dont know how to (2)
    2. How do I set a MAX position
      DEFPARAM CumulateOrders = True //
      
      Once Upper = 1
      Once Lower = 1
      
      c1 = abs(High[1] - Low[1]) > 1.5*averagetruerange[7]
      
      c2 = abs(High[0] - Low[0]) > 0.8*abs(High[1] - Low[1])
      
      c3 = abs(High[0] - Low[0]) < abs(High[1] - Low[1])
      
      c4 = High[1] > High[0]
      
      
      If c1 and c2 and c3 and c4 then
      Upper = High[1]
      Lower = Lowest[2](low)
      Rangebar=1
      
      Endif
      
      If Rangebar=1 and Low > (Upper+Lower)/2 then
      sellshort 0.1 lots at Upper + averagetruerange[7] limit
      
      Endif
      
      If Low crosses under (Upper+Lower)/2 then
      Rangebar=0
      Exitshort at (Upper+Lower)/2 limit
      
      endif
      
      Set stop $loss 500
      

       

    Many thanks in advance

    #83368 quote
    Stratus6
    Participant
    Junior

    Is there anyone out there? 🙂

    #83374 quote
    Vonasi
    Moderator
    Master

    Your code looks OK.

    To back test just press the ‘ProBacktest my system’ button.

    [attachment file=83375]

    If by MAX position you mean that you want to set a limit to the number of positions opened then you can do this with a condition based on COUNTOFPOSITION or by adding 1 to a variable IF ONMARKET and then resetting that variable IF NOT ONMARKET and then use that variable quantity in your conditions as to whether to put a sellshort order on the market.

    Stratus6 thanked this post
    Screenshot_2-2.png Screenshot_2-2.png
    #83400 quote
    Stratus6
    Participant
    Junior

    Hi Vonasi,

     

    Many thanks for your reply.

    I have tried the back test but the problem I have is the position becomes to large.

    I have 2 questions

    1. Is there a way to code maximum position size ie. Sell every bar until position reaches 10 lots.
    2. Also If I want to set a buy level at say the low[1] before the first lots is sold, how would I code this, as currently each new sell creates a new buy back level!

    Once again this beginner really appreciates your time!

    #83413 quote
    Vonasi
    Moderator
    Master
    If Rangebar=1 and Low > (Upper+Lower)/2 and countofposition < 10 then
    sellshort 0.1 lots at Upper + averagetruerange[7] limit
    Endif
    I’m not sure I fully understand question 2. Maybe this works as it fixes the levels until you have no trades on the market:
    If not onmarket and c1 and c2 and c3 and c4 then
    Upper = High[1]
    Lower = Lowest[2](low)
    Rangebar=1

     

    Stratus6 thanked this post
    #83645 quote
    Stratus6
    Participant
    Junior

    Perfect. Problem solved. If it works I’ll post the full code. Thanks again

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

Long bar Mean reverting code help


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Stratus6 @stratus6 Participant
Summary

This topic contains 5 replies,
has 2 voices, and was last updated by Stratus6
7 years, 4 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 10/10/2018
Status: Active
Attachments: 1 files
Logo Logo
Loading...