need help adding reverse code to open-system

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #114375 quote
    ullle73
    Participant
    Senior

    hi, could someone please help me add code to my strategy.

    Right now the system takes a pos in direction when price moved x points from open.

    I want the system to reverse if it goes the other way the same x points.

    Example, price opens at 1500, price goes to 1506, system goes long, when price hits 1494 i want it to close the long and reverse  to short

     

    //-------------------------------------------------------------------------
    // Main code : Open OMX NY opt 5m MED MFE 6p
    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    // Main Code : Straddle Dayopen V2.0
    //-------------------------------------------------------------------------
    // Test On DAX 30 Cash 10 Minute Timeframe 200k bars or from 1/1/2015
    
    // Common Rules
    Defparam Cumulateorders = false
    Defparam Preloadbars = 1000
    
    // On/off
    Extratradecriteria   = 0 // I.e. Long; Only Enters When The Current Bar High Is Lower Then The Lowest Daily High From Today, Yesterday And Day Before. 1
    Usepercentage        = 0 // The Minimum Difference In Percentage [[1] From Dayopen Or In Points [0] From Dayopen
    Mfetrailing          = 1 // Mfe Trailing Stop 1
    Wtrailing            = 1 // Williams 3 Bar Trailing Stop 1
    Breakevenstop        = 1 // Breakevenstop, Move Stoploss When Position Is In Profit. 1
    Excludefirsttwoweeks = 1 // Exclude The First 2 Weeks Of Every Year (Weeknumber 1 And 2)
    
    // Settings
    Positionsize = 1
    SL    = 0.6 // % Stoploss 0.6
    PT    = 1.2 // % Profit Target 1.2
    MFETS = 0.35 // % Mfe Trailing Stop
    BES   = 0.35 // % Break Even Stop 0.35
    BESMP = 0.05 // % Break Even Stop Minimum Profit 0.05
    WTSMP = 0.50 // % Williams Trailing Stop Minimum Profit If Mfe Trailing Stop Is Not Used 0.50
    ETD   = 0    //   Exclude a Trade Day; Sunday = 0
    If Usepercentage Then
    Nopl=((Dayopen*0.15)/100)/pointsize
    Nops=((Dayopen*0.15)/100)/pointsize
    Else
    Nopl=6 //number of points long 6
    Nops=6 //number of points short 6
    Endif
    
    // Day & Time
    Once Entertime = 090000
    Once Lasttime  = 100000
    Once Closetime = 172500 // Greater Then 23.59 Means It Continues Position Overnight
    Once Closetimefr=173000
    
    If Excludefirsttwoweeks=1 Then
    If Year=2015 And Month=1 And (Day>=1 And Day<=18) Then
    Notrading = 1
    Elsif Year=2016 And Month=1 And (Day>=1 And Day<=24) Then
    Notrading = 1
    Elsif Year=2017 And Month=1 And (Day>=1 And Day<=22) Then
    Notrading = 1
    Elsif Year=2018 And Month=1 And (Day>=1 And Day<=21) Then
    Notrading = 1
    Elsif Year=2019 And Month=1 And (Day>=1 And Day<=20) Then
    Notrading = 1
    Else
    Notrading = 0
    Endif
    Endif
    
    Tt1 = Time >= Entertime
    Tt2 = Time <= Lasttime
    Tradetime = Tt1 And Tt2 and Notrading = 0 And Dayofweek <> ETD
    
    // Reset At Start
    If Intradaybarindex = 0 Then
    Longtradecounter = 0
    Shorttradecounter = 0
    Tradecounter = 0
    Mclong = 0
    Mcshort = 0
    Endif
    
    // [pc] Position Criteria
    Pclong  = Countoflongshares < 1 And Longtradecounter < 1 And Tradecounter < 1
    Pcshort = Countofshortshares < 1 And Shorttradecounter < 1 And Tradecounter < 1
    
    // [mc] Main Criteria
    If Time = Entertime Then
    Dayopen=open
    Endif
    
    If High > Dayopen+nopl Then
    Mclong=1
    Else
    Mclong=0
    Endif
    
    If Low < Dayopen-nops Then
    Mcshort=1
    Else
    Mcshort=0
    Endif
    
    // [ec] Extra Criteria
    If Extratradecriteria Then
    Min1 = Min(Dhigh(0),dhigh(1))
    Min2 = Min(Dhigh(1),dhigh(2))
    
    Max1 = Max(Dlow(0),dlow(1))
    Max2 = Max(Dlow(1),dlow(2))
    
    Eclong = High < Min(Min1,min2)
    Ecshort = Low > Max(Max1,max2)
    else
    Eclong=1
    Ecshort=1
    Endif
    
    // Long & Short Entry
    If Tradetime Then
    If Pclong and Mclong And Eclong Then
    Buy Positionsize Contract At Market
    Longtradecounter=longtradecounter + 1
    Tradecounter=tradecounter+1
    Endif
    If Pcshort and Mcshort And Ecshort Then
    Sellshort Positionsize Contract At Market
    Shorttradecounter=shorttradecounter + 1
    Tradecounter=tradecounter+1
    Endif
    Endif
    
    // Break Even Stop
    If Breakevenstop Then
    If Not Onmarket Then
    Newsl=0
    Endif
    If Longonmarket And close-tradeprice(1)>=((Tradeprice/100)*BES)*pipsize Then
    Newsl = Tradeprice(1)+((Tradeprice/100)*BESMP)*pipsize
    Endif
    If Shortonmarket And Tradeprice(1)-close>=((Tradeprice/100)*BES)*pipsize Then
    Newsl = Tradeprice(1)-((Tradeprice/100)*BESMP)*pipsize
    Endif
    If Newsl>0 Then
    Sell At Newsl Stop
    Exitshort At Newsl Stop
    Endif
    Endif
    
    // Exit Mfe Trailing Stop
    If Mfetrailing Then
    Trailingstop = (Tradeprice/100)*MFETS
    If Not Onmarket Then
    Maxprice = 0
    Minprice = Close
    Priceexit = 0
    Endif
    If Longonmarket Then
    Maxprice = Max(Maxprice,close)
    If Maxprice-tradeprice(1)>=trailingstop*pipsize Then
    Priceexit = Maxprice-trailingstop*pipsize
    Endif
    Endif
    If Shortonmarket Then
    Minprice = Min(Minprice,close)
    If Tradeprice(1)-minprice>=trailingstop*pipsize Then
    Priceexit = Minprice+trailingstop*pipsize
    Endif
    Endif
    If Onmarket And Wtrailing=0 And Priceexit>0 Then
    Sell At Market
    Exitshort At Market
    Endif
    Endif
    
    // Exit Williams Trailing Stop
    If Wtrailing Then
    Count=1
    I=0
    J=i+1
    Tot=0
    While Count<4 Do
    Tot=tot+1
    If (Low[j]>=low[i]) And (High[j]<=high[i]) Then
    J=j+1
    Else
    Count=count+1
    I=i+1
    J=i+1
    Endif
    Wend
    
    Basso=lowest[tot](Low)
    Alto=highest[tot](High)
    
    If Close>alto[1] Then
    Ref=basso
    Endif
    If Close<basso[1] Then
    Ref=alto
    Endif
    
    If Onmarket And Mfetrailing=0 And Positionperf>WTSMP Then
    If Low[1]>ref And High<ref Then
    Sell At Market
    Endif
    If High[1]<ref And Low>ref Then
    Exitshort At Market
    Endif
    Endif
    
    If Onmarket And Mfetrailing=1 And Priceexit>0 Then
    If High<ref Then
    Sell At Market
    Endif
    If Low>ref Then
    Exitshort At Market
    Endif
    Endif
    Endif
    
    // Exit At Closetime
    If Onmarket Then
    If Time >= Closetime Then
    Sell At Market
    Exitshort At Market
    Endif
    Endif
    
    // Exit At Closetime Friday
    If Onmarket Then
    If (Currentdayofweek=5 And Time>=closetimefr) Then
    Sell At Market
    Exitshort At Market
    Endif
    Endif
    
    // Build-in Exit
    Set Stop %loss SL
    Set Target %profit PT
    
    //graph 0 Coloured(300,0,0) As "Zeroline"
    //graph (Positionperf*100)coloured(0,0,0,255) As "Positionperformance"
    
    #114376 quote
    Vonasi
    Moderator
    Master

    You’ll find more help if you post in the correct forum. ProBuilder is for indicators. I will move your topic to the ProOrder forum which is for strategies.

    Please try to post in the correct forum with future topics 🙂

    ullle73 thanked this post
    #114937 quote
    ullle73
    Participant
    Senior

    no one?! :((

     

    In my mind it seems like it should be an easy add, unfortunatly i dont know how to.

    #114940 quote
    Vonasi
    Moderator
    Master

    Perhaps there is a lack of enthusiasm to code it because it will achieve little?

    First of all we cannot know what price we bought at because of spread and slippage so within the first five minutes we can only guess that we bought at the same price as the close of the previous candle. After the first five minutes we can actually know our trade price and so set a more accurate reverse price.

    However opening a trade and reversing soon after is never profitable as you pay the spread and slippage and then it reverses back and you reverse again and pay again!

    #114977 quote
    Nicolas
    Keymaster
    Master

    You have to put contrarian pending stop orders at these predefined levels, as soon as you enter the market and replace them as long as you are on market.

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

need help adding reverse code to open-system


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
ullle73 @jonas_rydqvist Participant
Summary

This topic contains 4 replies,
has 3 voices, and was last updated by Nicolas
6 years, 2 months ago.

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