Wanted: small code snippet for reinvestment

Viewing 15 posts - 1 through 15 (of 69 total)
  • Author
    Posts
  • #56939 quote
    JohnScher
    Participant
    Veteran

    Hello.

    Who can help out with a surely simple code?

    The following strategy is to be extended by a re-investment strategy in the sense, so that
    the size of the position changes based on the initial capital.

    capital = 10000

     

    for Trade 1

    Position = capital/capital

     

    for all following trades

    if capital (new) <= 10000 then

    position = 1

    if capital (new) >10000 then
    position = capital (new) / 10000

    How can integrate this in the maincode?

    Attached the maincode

    // MainCode : DailyOpenlong
    // Dax 1 Euro
    // TimeFrame4H
    // created by JohnScher
    
    
    //..............................................................
    // maincode
    //..............................................................
    
    
    defparam cumulateorders = false
    
    position = 1
    
    TradingDayLong =  dayofweek = 1 or dayofweek = 2 or dayofweek = 3 or  dayofweek = 4 or dayofweek = 5
    TradingtimeLong =   time = 090000 //or  time = 130000 or time = 170000
    
    c1 = TR (close) > 25
    c2 = TEMA [4] (close)  >  ExponentialAverage [4] (close)
    
    IF  TradingDayLong and TradingTimeLong Then
    If  c1 and c2 THEN
    buy position CONTRACT AT MARKET
    Endif
    ENDIF
    
    
    
    // set stop loss
    IF time = 090000 or time = 130000 or time = 170000 or time = 210000 then
    If Repulse[3](close)< -0.3 Then
    sell at market
    Endif
    ENDIF
    
    // set target profit
    set target %profit 3
    
    
    //..............................................................
    // end maincode
    DailyOpenLong-1.png DailyOpenLong-1.png DailyOpenLong-2.png DailyOpenLong-2.png DailyOpenLong-3.png DailyOpenLong-3.png
    #56947 quote
    Vonasi
    Moderator
    Master
    Capital = 10000
    Equity = Capital + StrategyProfit
    Position = Max(1,Equity/Capital)
    #56948 quote
    JohnScher
    Participant
    Veteran

    ty i try

    #56949 quote
    JohnScher
    Participant
    Veteran

    result with Re-Invest  …

    DailyOpenLong-4.png DailyOpenLong-4.png
    #56953 quote
    JohnScher
    Participant
    Veteran

    the same code without Re-Invest but with SaisonalPatternMultiplier from Pathfinder-Systems

     

    // MainCode : DailyOpenlong
    // Dax 1 Euro
    // TimeFrame4H
    // created by JohnScher
    // with SaisonalPatternMultiplier from Pathfinder-Systems
    
    
    //..............................................................
    // maincode
    //..............................................................
    
    
    defparam cumulateorders = false
    
    // begin Pathfinder Multiplier
    
    ONCE January1 = 3 //0 risk(3)
    ONCE January2 = 0 //3 ok
    ONCE February1 = 3 //3 ok
    ONCE February2 = 3 //0 risk(3)
    ONCE March1 = 3 //0 risk(3)
    ONCE March2 = 2 //3 ok
    ONCE April1 = 3 //3 ok
    ONCE April2 = 3 //3 ok
    ONCE May1 = 1 //0 risk(1)
    ONCE May2 = 1 //0 risk(1)
    ONCE June1 = 1 //1 ok 2
    ONCE June2 = 2 //3 ok
    ONCE July1 = 3 //1 chance
    ONCE July2 = 2 //3 ok
    ONCE August1 = 2 //1 chance 1
    ONCE August2 = 3 //3 ok
    ONCE September1 = 3 //0 risk(3)
    ONCE September2 = 0 //0 ok
    ONCE October1 = 3 //0 risk(3)
    ONCE October2 = 2 //3 ok
    ONCE November1 = 1 //1 ok
    ONCE November2 = 3 //3 ok
    ONCE December1 = 3 // 1 chance
    ONCE December2 = 2 //3 ok
    
    
    // set saisonal multiplier
    currentDayOfTheMonth = Day
    midOfMonth = 15
    IF CurrentMonth = 1 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = January1
    ELSE
    saisonalPatternMultiplier = January2
    ENDIF
    ELSIF CurrentMonth = 2 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = February1
    ELSE
    saisonalPatternMultiplier = February2
    ENDIF
    ELSIF CurrentMonth = 3 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = March1
    ELSE
    saisonalPatternMultiplier = March2
    ENDIF
    ELSIF CurrentMonth = 4 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = April1
    ELSE
    saisonalPatternMultiplier = April2
    ENDIF
    ELSIF CurrentMonth = 5 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = May1
    ELSE
    saisonalPatternMultiplier = May2
    ENDIF
    ELSIF CurrentMonth = 6 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = June1
    ELSE
    saisonalPatternMultiplier = June2
    ENDIF
    ELSIF CurrentMonth = 7 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = July1
    ELSE
    saisonalPatternMultiplier = July2
    ENDIF
    ELSIF CurrentMonth = 8 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = August1
    ELSE
    saisonalPatternMultiplier = August2
    ENDIF
    ELSIF CurrentMonth = 9 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = September1
    ELSE
    saisonalPatternMultiplier = September2
    ENDIF
    ELSIF CurrentMonth = 10 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = October1
    ELSE
    saisonalPatternMultiplier = October2
    ENDIF
    ELSIF CurrentMonth = 11 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = November1
    ELSE
    saisonalPatternMultiplier = November2
    ENDIF
    ELSIF CurrentMonth = 12 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = December1
    ELSE
    saisonalPatternMultiplier = December2
    ENDIF
    Endif
    // end Pathfinder Multiplier
    
    
    // start maincode
    position = 1
    
    TradingDayLong =  dayofweek = 1 or dayofweek = 2 or dayofweek = 3 or  dayofweek = 4 or dayofweek = 5
    TradingtimeLong =   time = 090000 //or  time = 130000 or time = 170000
    
    c1 = TR (close) > 25
    c2 = TEMA [4] (close)  >  ExponentialAverage [4] (close)
    
    IF  TradingDayLong and TradingTimeLong Then
    If  c1 and c2 THEN
    buy position*saisonalpatternmultiplier CONTRACT AT MARKET
    Endif
    ENDIF
    
    
    
    // set stop loss
    IF time = 090000 or time = 130000 or time = 170000 or time = 210000 then
    If Repulse[3](close)< -0.3 Then
    sell at market
    Endif
    ENDIF
    
    // set target profit
    set target %profit 3
    
    
    //..............................................................
    // end maincode
    // end

    kind regards an thanks to vonasi

    DailyOpenLong-5.png DailyOpenLong-5.png
    #56955 quote
    JohnScher
    Participant
    Veteran

    at 01.01.17 the programm will run in demo and live for 1 year, after that 1 will post results

    i will add

     

    set stop %loss 5%

     

    for insurance against all damage, e. g. an exploding nuclear power plant or something the likes

    #56956 quote
    Vonasi
    Moderator
    Master

    No problem. your strategy kicks straight into profit and never dips into a loss so the re-investment snippet never gets the opportunity to reduce the position size to 1 in reality.

    Interesting strategy – thanks for sharing it. I find myself writing a lot of long only strategies on the DAX 4 hour at the moment and they all seem to be easy to make profitable and all back test and walk forward test OK which leaves me with a slight sense of ‘it all seems too good to be true’. I hope I am wrong.

    #56957 quote
    Vonasi
    Moderator
    Master

    I notice that your test results are on the 4 hour chart but you have entry/exit criteria set on times that cannot be achieved in a 4 hour chart.

    IF time = 090000 or time = 130000 or time = 170000 or time = 210000 then

    Did you use different times than the ones posted on here?

    #56961 quote
    Vonasi
    Moderator
    Master

    Just for interest I sometimes use this for position sizing.

    Capital = 10000
    Equity = Capital + StrategyProfit
    Position = Max(1, Equity * (1/Capital))
    Position = Round(Position*100)
    Position = Position/100

    It starts with a position size of one and increases position size as equity increases and reduces it as equity decreases. It protects your capital better at the expense of overall profit. The last two lines are to ensure that decimal sizes work live on IG. The only downside to this if you start with a small capital as it sticks with the same % of equity so a starting stake of 1 with capital of 5000 is 2% per position as opposed to the 1% with starting capital of 10000.

    #56963 quote
    Vonasi
    Moderator
    Master

    Out of interest I just did a quick comparison of true returns comparing the strategy with seasonal adjustment to the one without.  I set position size to 1 and then in the seasonally adjusted strategy I totalled up the total amount staked and then divided it by the number of bets and then divided the amount won by this to be able to compare apples with apples. Interestingly the seasonally adjusted strategy added 15.7% to profits. For me the only downside of this is the increase in initial position size from 1 to an average of 2.75 meaning that the losses can be pretty big ones – on the bright side the winners are too but you need a good bank size and a strong heart to run it live starting with such big stake sizes.

    #56964 quote
    JohnScher
    Participant
    Veteran

    Hello again.

    I wasn’t really so concerned with the strategy presented here. Rather, coding the re-investment caused me problems, somehow I didn’t quite get through it. Now, however, I have one of you here. With it I will now be able to examine my other strategies to see whether it is worthwhile to use the Re-Invest.
    The Dax DailyOpenLong strategy presented here is thus a benefit for the Re-Invest coder and, of course, for the PRT community.
    Thanks to all of you!

    Kind regards
    JohnScher

    Translated with http://www.DeepL.com/Translator

    #56971 quote
    Vonasi
    Moderator
    Master

    Hello again JohnScher. Your code has interested me and I have been trying a few things out. You might want to change your entry criteria to include a third one.

    c1 = TR (close) > 25
    c2 = TEMA[4](close)  >  ExponentialAverage[4](close)
    c3 = Repulse[3](close) > 0.3
    
    IF TradingDayLong and TradingTimeLong Then
    If  c1 and c2 and c3 THEN
    buy position CONTRACT AT MARKET
    Endif
    ENDIF

    This stops entries that can quickly be closed out if Repulse is too close. Improves win rate and profitability. Hope this helps.

    #56975 quote
    Vonasi
    Moderator
    Master

    Another possibility if running the seasonal adjustment on a small starting bank is to only allow the seasonal adjustment to be added if funds allow.

    IF (Your Conditions) THEN
    Equity = Capital + StrategyProfit
    Position = Max(1, Equity * (1/Capital))
    Position = Position * saisonalpatternmultiplier
    Position = Round(Position*100)
    Position = Position/100
    IF Position > Equity * 0.0001 THEN
    Position = 1
    ENDIF
    Buy Position Contracts AT Market
    ENDIF
    #57145 quote
    JohnScher
    Participant
    Veteran

    @vonasi

     

    I would never have made it without you.

    see it https://www.prorealcode.com/library/

    my first publication here in the library

    Thanks so lot !!

    #57152 quote
    Vonasi
    Moderator
    Master

    No problem JohnScher. You did all the hardwork and testing!

    I’m running this slightly adjusted version with a third entry condition (as suggested above) that gives some differential to the Repulse exit conditions so that an entry is not made if Repulse is too close to the exit criteria. It gave me a better win rate and higher profit over your version posted above.

    //-------------------------------------------------------------------------
    // Main code : Daily Open Long with Season
    //-------------------------------------------------------------------------
    // MainCode : DailyOpenlong
    // Dax 1 Euro
    // TimeFrame4H
    // created by JohnScher
    // with SaisonalPatternMultiplier from Pathfinder-Systems
     
    DEFPARAM cumulateorders = false
     
    // begin Pathfinder Multiplier
     
    ONCE January1 = 3 //0 risk(3)
    ONCE January2 = 0 //3 ok
    ONCE February1 = 3 //3 ok
    ONCE February2 = 3 //0 risk(3)
    ONCE March1 = 3 //0 risk(3)
    ONCE March2 = 2 //3 ok
    ONCE April1 = 3 //3 ok
    ONCE April2 = 3 //3 ok
    ONCE May1 = 1 //0 risk(1)
    ONCE May2 = 1 //0 risk(1)
    ONCE June1 = 1 //1 ok 2
    ONCE June2 = 2 //3 ok
    ONCE July1 = 3 //1 chance
    ONCE July2 = 2 //3 ok
    ONCE August1 = 2 //1 chance 1
    ONCE August2 = 3 //3 ok
    ONCE September1 = 3 //0 risk(3)
    ONCE September2 = 0 //0 ok
    ONCE October1 = 3 //0 risk(3)
    ONCE October2 = 2 //3 ok
    ONCE November1 = 1 //1 ok
    ONCE November2 = 3 //3 ok
    ONCE December1 = 3 // 1 chance
    ONCE December2 = 2 //3 ok
     
     
    // set saisonal multiplier
    currentDayOfTheMonth = Day
    midOfMonth = 15
    IF CurrentMonth = 1 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = January1
    ELSE
    saisonalPatternMultiplier = January2
    ENDIF
    ELSIF CurrentMonth = 2 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = February1
    ELSE
    saisonalPatternMultiplier = February2
    ENDIF
    ELSIF CurrentMonth = 3 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = March1
    ELSE
    saisonalPatternMultiplier = March2
    ENDIF
    ELSIF CurrentMonth = 4 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = April1
    ELSE
    saisonalPatternMultiplier = April2
    ENDIF
    ELSIF CurrentMonth = 5 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = May1
    ELSE
    saisonalPatternMultiplier = May2
    ENDIF
    ELSIF CurrentMonth = 6 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = June1
    ELSE
    saisonalPatternMultiplier = June2
    ENDIF
    ELSIF CurrentMonth = 7 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = July1
    ELSE
    saisonalPatternMultiplier = July2
    ENDIF
    ELSIF CurrentMonth = 8 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = August1
    ELSE
    saisonalPatternMultiplier = August2
    ENDIF
    ELSIF CurrentMonth = 9 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = September1
    ELSE
    saisonalPatternMultiplier = September2
    ENDIF
    ELSIF CurrentMonth = 10 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = October1
    ELSE
    saisonalPatternMultiplier = October2
    ENDIF
    ELSIF CurrentMonth = 11 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = November1
    ELSE
    saisonalPatternMultiplier = November2
    ENDIF
    ELSIF CurrentMonth = 12 THEN
    IF currentDayOfTheMonth <= midOfMonth THEN
    saisonalPatternMultiplier = December1
    ELSE
    saisonalPatternMultiplier = December2
    ENDIF
    Endif
    // end Pathfinder Multiplier
     
    // start maincode
    Capital = 10000
    Equity = Capital + StrategyProfit
    Position = Max(1, Equity * (1/Capital))
    Position = Round(Position*100)
    Position = Position/100
    //Position = 1
     
    TradingDayLong =  dayofweek = 1 or dayofweek = 2 or dayofweek = 3 or  dayofweek = 4 or dayofweek = 5
    TradingtimeLong =   time = 080000 //or  time = 130000 or time = 170000
     
    c1 = TR (close) > 25
    c2 = TEMA [4] (close)  >  ExponentialAverage [4] (close)
    c3 = Repulse[3](close) > 0.3
    
    IF TradingDayLong and TradingTimeLong Then
    If  c1 and c2 and c3 THEN
    buy position*saisonalpatternmultiplier CONTRACT AT MARKET
    ENDIF
    ENDIF
     
    // set stop loss
    IF time = 080000 or time = 120000 or time = 160000 or time = 200000 then
    If Repulse[3](close) < -0.3 Then
    sell at market
    Endif
    ENDIF
     
    // set target profit
    set target %profit 3
    set stop %loss 5
    
Viewing 15 posts - 1 through 15 (of 69 total)
  • You must be logged in to reply to this topic.

Wanted: small code snippet for reinvestment


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
JohnScher @johnscher Participant
Summary

This topic contains 68 replies,
has 7 voices, and was last updated by Nicolas
8 years, 1 month ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 12/31/2017
Status: Active
Attachments: 23 files
Logo Logo
Loading...