Coin Toss Code Issue

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #34425 quote
    random45689
    Member
    New

    Hi All

    I know this is a big ask as it is a lot of code to wade through but hopefully someone might be able to spot the issue/provide some feedback as to what is wrong with my code below

    Basically I have all but given up on using any indicators/techniques for order entry and have decided instead to rely on the good old Coin Toss for order entry -> heads = buy tails = sell – then the majority of the strategy is how the trade is managed

    Sounds daft I know but strategies both in demo and live have been performing better than average

    The problem is that occasionally (after a week/couple weeks) and only some not all of the strategies will fail with the DEFPARAM/load more bars error and I cannot work out what is causing the problem – I’ve highlighted the part of code I ‘think’ is causing the problem but it is not using any indicators – it is only the random number generator that uses indicators and the ‘largest’ is the RSI[14] so 2000 bars is more than enough to load

    Difficult to go into all the details of how strategy is working but if there is any interest/feedback just ask away and more than happy to try and explain 🙂

    Cheers!

    Max

    //================= PARAMETERS ================
    //*********************************************
    DEFPARAM PRELOADBARS = 2000
    DEFPARAM CUMULATEORDERS = FALSE
    
    //============== CONSTANTS ====================
    //*********************************************
    ONCE MINPOSITIONSIZE=1 //MINIMUM ORDER SIZE AS PER IG OR WHATEVER YOU CHOOSE - POSITION SIZE WILL ALSO INCREASE/DECREASE BY THIS FIGURE
    ONCE MYNUMBER=CURRENTSECOND //INITIAL SEED FIGURE FOR RANDOM NUMBER GENERATOR
    ONCE POSITIONMULTIPLIER=4 //BASICALLY 'HOW MANY WINS BEFORE POSITION SIZE INCREASES'
    ONCE COUNT = 0 //USED TO HOLD OFF ATR CALCULATION UNTIL FIRST CHANGE OF HOUR HAS OCCURRED
    ONCE HIGHESTHIGH = 0 //USED IN ATR CALCULATION
    ONCE LOWESTLOW = LOW[1]*10000 //USED IN ATR CALCULATION
    ONCE INITIALATR=21.706 //THIS IS TAKEN MANUALLY FROM A HIGHER TIMEFRAME CHART USING BASIC INDICATOR - ATR=AVERAGETRUERANGE[8](CLOSE) AVERAGEATR=AVERAGE[1000] - MOSTLY USE 1 OR 2 HOUR TIMEFRAME
    ONCE FIRSTTRADE=0 //USED TO ALLOW MANUAL TRADE FOR FIRST TRADE
    
    //========== SPREAD VARIATIONS ================
    //*********************************************
    IF Time >= 070000 AND Time < 080000 THEN
    SPREAD=2
    ELSIF  Time >= 080000 AND Time < 163000 THEN
    SPREAD=1
    ELSIF Time >= 163000 AND Time < 210000 THEN
    SPREAD=2
    ELSE
    SPREAD=5
    ENDIF
    
    //======== 'RANDOM' NUMBER GENERATOR ==========
    //*********************************************
    MYNUMBER=round(((((RSI[14]*RSI[8])*(HIGH[MYNUMBER]+MEDIANPRICE))*((INTRADAYBARINDEX/(TIME+1)))+DAYS+(DAYOFWEEK+1)*MONTH)-ROUND((((RSI[14]*RSI[8])*(HIGH[MYNUMBER]+MEDIANPRICE))*((INTRADAYBARINDEX/(TIME+1)))+DAYS+(DAYOFWEEK+1)*MONTH)/10-0.49)*10)-0.4)/10
    
    RANDOM=ROUND(MYNUMBER[(MYNUMBER*10)])
    //SLIGHTLY EDITED VERSION OF CODE FROM HERE - https://www.prorealcode.com/topic/random-value/ - THANK YOU @WING!
    
    //================ STRATEGY ===================
    //*********************************************
    IF COUNT = 0 THEN
    AVERAGEATR=INITIALATR //ALLOWS FOR IMMEDIATE ORDER ENTRY ON STRATEGY START
    ENDIF
    
    
    IF OPENHOUR<>OPENHOUR[1] THEN //1HRAVGATR - USED IF TAKING AVERAGEATR FROM 1 HOUR CHART
    
    //IF (OPENHOUR[1] = 23 AND OPENHOUR = 0) OR (OPENHOUR[1]=1 AND OPENHOUR=2) OR (OPENHOUR[1]=3 AND OPENHOUR=4) OR (OPENHOUR[1]=5 AND OPENHOUR=6) OR (OPENHOUR[1]=7 AND OPENHOUR=8) OR (OPENHOUR[1]=9 AND OPENHOUR=10) OR (OPENHOUR[1]=11 AND OPENHOUR=12) OR (OPENHOUR[1]=13 AND OPENHOUR=14) OR (OPENHOUR[1]=15 AND OPENHOUR=16) OR (OPENHOUR[1]=17 AND OPENHOUR=18)OR (OPENHOUR[1]=19 AND OPENHOUR=20) OR (OPENHOUR[1]=21 AND OPENHOUR=22) THEN //2HRAVGATR - USED IF TAKING AVERAGEATR FROM 2 HOUR CHART
    
    //IF (OPENHOUR[1] = 23 AND OPENHOUR = 0) OR (OPENHOUR[1]=2 AND OPENHOUR=3) OR (OPENHOUR[1]=5 AND OPENHOUR=6) OR (OPENHOUR[1]=8 AND OPENHOUR=9) OR (OPENHOUR[1]=11 AND OPENHOUR=12) OR (OPENHOUR[1]=14 AND OPENHOUR=15) OR (OPENHOUR[1]=17 AND OPENHOUR=18) OR (OPENHOUR[1]=20 AND OPENHOUR=21) THEN //3HRAVGATR - USED IF TAKING AVERAGEATR FROM 3 HOUR CHART
    
    //IF (OPENHOUR[1] = 23 AND OPENHOUR = 0) OR (OPENHOUR[1]=3 AND OPENHOUR=4) OR (OPENHOUR[1]=7 AND OPENHOUR=8) OR (OPENHOUR[1]=11 AND OPENHOUR=12) OR (OPENHOUR[1]=15 AND OPENHOUR=16) OR (OPENHOUR[1]=19 AND OPENHOUR=20) THEN //4HRAVGATR - USED IF TAKING AVERAGEATR FROM 4 HOUR CHART
    
    //ONGOING ATR CALCULATION BEGIN (THIS IS WHERE I THINK THE PROBLEM IS...)
    IF COUNT = 1 THEN
    AVERAGEATR=((INITIALATR*999)+TR0)/1000
    IF HIGHESTHIGH>PREVIOUSBLOCKHIGH AND LOWESTLOW<PREVIOUSBLOCKLOW THEN
    TR0 = HIGHESTHIGH-LOWESTLOW
    ELSIF PREVIOUSBLOCKCLOSE>HIGHESTHIGH OR PREVIOUSBLOCKCLOSE<LOWESTLOW THEN
    B1 = ABS(HIGHESTHIGH-PREVIOUSBLOCKCLOSE)
    B2 = ABS(LOWESTLOW-PREVIOUSBLOCKCLOSE)
    TR0 = MAX(B1,B2)
    ELSE
    C1 = HIGHESTHIGH-LOWESTLOW
    C2 = ABS(HIGHESTHIGH-PREVIOUSBLOCKCLOSE)
    C3 = ABS(LOWESTLOW-PREVIOUSBLOCKCLOSE)
    C4 = MAX(C1,C2)
    TR0 = MAX(C3,C4)
    ENDIF
    ENDIF
    
    IF COUNT = 2 THEN
    AVERAGEATR = ((AVERAGEATR*999)+TR0)/1000
    IF HIGHESTHIGH>PREVIOUSBLOCKHIGH AND LOWESTLOW<PREVIOUSBLOCKLOW THEN
    TR0 = HIGHESTHIGH-LOWESTLOW
    ELSIF PREVIOUSBLOCKCLOSE>HIGHESTHIGH OR PREVIOUSBLOCKCLOSE<LOWESTLOW THEN
    B1 = ABS(HIGHESTHIGH-PREVIOUSBLOCKCLOSE)
    B2 = ABS(LOWESTLOW-PREVIOUSBLOCKCLOSE)
    TR0 = MAX(B1,B2)
    ELSE
    C1 = HIGHESTHIGH-LOWESTLOW
    C2 = ABS(HIGHESTHIGH-PREVIOUSBLOCKCLOSE)
    C3 = ABS(LOWESTLOW-PREVIOUSBLOCKCLOSE)
    C4 = MAX(C1,C2)
    TR0 = MAX(C3,C4)
    ENDIF
    ENDIF
    
    HIGHESTHIGH = HIGH
    LOWESTLOW = LOW
    
    PREVIOUSBLOCKHIGH = HIGHESTHIGH[1]
    PREVIOUSBLOCKLOW = LOWESTLOW[1]
    PREVIOUSBLOCKCLOSE = CLOSE[1]
    
    IF COUNT < 2 THEN
    COUNT=COUNT+1
    ENDIF
    
    ELSE
    HIGHESTHIGH = MAX(HIGHESTHIGH,HIGH)
    LOWESTLOW = MIN(LOWESTLOW,LOW)
    
    IF COUNT>=1 THEN
    IF HIGHESTHIGH>PREVIOUSBLOCKHIGH AND LOWESTLOW<PREVIOUSBLOCKLOW THEN
    A1 = HIGHESTHIGH-LOWESTLOW
    TR0 = MAX(TR0,A1)
    ELSIF PREVIOUSBLOCKCLOSE>HIGHESTHIGH OR PREVIOUSBLOCKCLOSE<LOWESTLOW THEN
    B1 = ABS(HIGHESTHIGH-PREVIOUSBLOCKCLOSE)
    B2 = ABS(LOWESTLOW-PREVIOUSBLOCKCLOSE)
    B3 = MAX(B1,B2)
    TR0 = MAX(TR0,B3)
    ELSE
    C1 = HIGHESTHIGH-LOWESTLOW
    C2 = ABS(HIGHESTHIGH-PREVIOUSBLOCKCLOSE)
    C3 = ABS(LOWESTLOW-PREVIOUSBLOCKCLOSE)
    C4 = MAX(C1,C2)
    C5 = MAX(C3,C4)
    TR0 = MAX(TR0,C5)
    ENDIF
    ENDIF
    ENDIF
    //ONGOING ATR CALCULATION END
    
    //*******************************************
    //================TRADING====================
    //*******************************************
    //ORDER ENTRY BEGIN
    IF NOT ONMARKET THEN
    
    IF STRATEGYPROFIT<-(2*MINPOSITIONSIZE*AVERAGEATR*POSITIONMULTIPLIER) THEN //'FAILSAFE' OVERALL STOPLOSS
    QUIT
    ENDIF
    
    //TRADING BOUNDARIES CALCULATION BEGIN
    IF POSCOUNT=0 THEN
    POSITIONSIZE=MINPOSITIONSIZE
    POSBOUNDARY=(POSITIONSIZE*AVERAGEATR*POSITIONMULTIPLIER)
    PREVIOUSPOSBOUNDARY=-POSBOUNDARY
    TARGETPOSBOUNDARY=POSBOUNDARY+((POSITIONSIZE+MINPOSITIONSIZE)*AVERAGEATR*POSITIONMULTIPLIER)
    ENDIF
    
    IF STRATEGYPROFIT > TARGETPOSBOUNDARY THEN
    PREVIOUSPOSBOUNDARY=POSBOUNDARY
    POSBOUNDARY=POSBOUNDARY+((POSITIONSIZE+MINPOSITIONSIZE)*AVERAGEATR*POSITIONMULTIPLIER)
    POSITIONSIZE=POSITIONSIZE+MINPOSITIONSIZE
    TARGETPOSBOUNDARY=POSBOUNDARY+((POSITIONSIZE+MINPOSITIONSIZE)*AVERAGEATR*POSITIONMULTIPLIER)
    POSCOUNT=1
    ENDIF
    
    IF STRATEGYPROFIT < PREVIOUSPOSBOUNDARY THEN
    TARGETPOSBOUNDARY=POSBOUNDARY
    IF TARGETPOSBOUNDARY<=(MINPOSITIONSIZE*AVERAGEATR*POSITIONMULTIPLIER)+(2*MINPOSITIONSIZE*AVERAGEATR*POSITIONMULTIPLIER) THEN
    TARGETPOSBOUNDARY=(MINPOSITIONSIZE*AVERAGEATR*POSITIONMULTIPLIER)+(2*MINPOSITIONSIZE*AVERAGEATR*POSITIONMULTIPLIER)
    ENDIF
    POSBOUNDARY=PREVIOUSPOSBOUNDARY
    IF POSBOUNDARY<=(MINPOSITIONSIZE*AVERAGEATR*POSITIONMULTIPLIER) THEN
    POSBOUNDARY=(MINPOSITIONSIZE*AVERAGEATR*POSITIONMULTIPLIER)
    ENDIF
    PREVIOUSPOSBOUNDARY=POSBOUNDARY-((POSITIONSIZE-MINPOSITIONSIZE)*AVERAGEATR*POSITIONMULTIPLIER)
    IF PREVIOUSPOSBOUNDARY<=0 OR PREVIOUSPOSBOUNDARY=POSBOUNDARY THEN
    PREVIOUSPOSBOUNDARY=-(MINPOSITIONSIZE*AVERAGEATR*POSITIONMULTIPLIER)
    ENDIF
    POSITIONSIZE=MAX(MINPOSITIONSIZE, POSITIONSIZE-MINPOSITIONSIZE)
    ENDIF
    //TRADING BOUNDARIES CALCULATION END
    
    //POSITION SIZE 'STOPLOSS' BEGIN
    IF N=1 THEN
    IF POSITIONSIZE<=HIGHESTPOSITIONSIZE-(2*MINPOSITIONSIZE) THEN
    QUIT
    ENDIF
    ENDIF
    
    IF POSITIONSIZE>5*MINPOSITIONSIZE AND POSITIONSIZE<=10*MINPOSITIONSIZE THEN
    HIGHESTPOSITIONSIZE = MAX(HIGHESTPOSITIONSIZE, POSITIONSIZE)
    N=1
    ENDIF
    
    IF N=2 THEN
    IF POSITIONSIZE<=HIGHESTPOSITIONSIZE-MINPOSITIONSIZE THEN
    QUIT
    ENDIF
    ENDIF
    
    IF POSITIONSIZE>10*MINPOSITIONSIZE THEN
    HIGHESTPOSITIONSIZE = MAX(HIGHESTPOSITIONSIZE, POSITIONSIZE)
    N=2
    ENDIF
    //POSITION SIZE 'STOPLOSS' END
    
    //ORDER ENTRY BEGIN
    IF FIRSTTRADE=0 THEN
    SELLSHORT POSITIONSIZE CONTRACT AT MARKET
    CONTINUATION=1
    LADDERCOUNT=1
    ELSIF FIRSTTRADE=1 THEN
    IF RANDOM=1 THEN
    BUY POSITIONSIZE CONTRACTS AT MARKET
    CONTINUATION=1
    LADDERCOUNT=1
    ENDIF
    IF RANDOM=0 THEN
    SELLSHORT POSITIONSIZE CONTRACT AT MARKET
    CONTINUATION =1
    LADDERCOUNT=1
    ENDIF
    ENDIF
    //ORDER ENTRY END
    ENDIF
    
    //ORDER MANAGEMENT BEGIN
    IF LONGONMARKET THEN
    STOPLOSS=AVERAGEATR+SPREAD
    STOPPRICE = TRADEPRICE(1)-STOPLOSS
    IF CLOSE>TRADEPRICE(1) THEN
    PRICEDIFFERENCE = CLOSE-TRADEPRICE(1)
    STOPLADDER = ROUND(PRICEDIFFERENCE/STOPLOSS)
    IF STOPLADDER<3 THEN
    STOPLADDER=0
    ENDIF
    CONTINUATION = SUMMATION[LADDERCOUNT](STOPLADDER>=STOPLADDER[1])=LADDERCOUNT
    LADDERCOUNT=LADDERCOUNT+1
    ENDIF
    ENDIF
    
    IF LONGONMARKET AND CLOSE<=STOPPRICE OR CONTINUATION=0 THEN
    SELL AT MARKET
    STOPLADDER=0
    LADDERCOUNT=1
    FIRSTTRADE=1
    ENDIF
    
    IF SHORTONMARKET THEN
    STOPLOSS=AVERAGEATR+SPREAD
    STOPPRICE = TRADEPRICE(1)+STOPLOSS
    IF CLOSE<TRADEPRICE(1) THEN
    PRICEDIFFERENCE = TRADEPRICE(1)-CLOSE
    STOPLADDER = ROUND(PRICEDIFFERENCE/STOPLOSS)
    IF STOPLADDER<3 THEN
    STOPLADDER=0
    ENDIF
    CONTINUATION = SUMMATION[LADDERCOUNT](STOPLADDER>=STOPLADDER[1])=LADDERCOUNT
    LADDERCOUNT=LADDERCOUNT+1
    ENDIF
    ENDIF
    
    IF SHORTONMARKET AND CLOSE>=STOPPRICE OR CONTINUATION=0 THEN
    EXITSHORT AT MARKET
    STOPLADDER=0
    LADDERCOUNT=1
    FIRSTTRADE=1
    ENDIF
    //ORDER MANAGEMENT END
    
    //**********************************************************************************************
    //GRAPH POSBOUNDARY coloured(255,0,255)
    //GRAPH PREVIOUSPOSBOUNDARY coloured(255,0,255)
    //GRAPH TARGETPOSBOUNDARY coloured(0,0,255)
    //GRAPH POSITIONSIZE COLOURED(0,0,0,25)
    //GRAPH STRATEGYPROFIT coloured(0,128,0)
    //GRAPH AVERAGEATR
    //GRAPH POSCOUNT
    //GRAPH STOPLOSS
    //GRAPH RANDOM
    //GRAPH STOPLADDER
    //GRAPH ATR
    //GRAPH STOPPRICE
    //GRAPH HIGH
    //GRAPH LOW
    //GRAPH HIGHESTHIGH
    //GRAPH LOWESTLOW
    //GRAPH PREVIOUSBLOCKCLOSE
    //GRAPH PREVIOUSBLOCKLOW
    //GRAPH PREVIOUSBLOCKHIGH
    //GRAPH PREVIOUSBLOCKCLOSE
    #34429 quote
    Nicolas
    Keymaster
    Master

    MYNUMBER should be less than 2000?

    #34455 quote
    random45689
    Member
    New

    Thanks (as ever!) for a quick reply Nicolas

     

    I have no idea how you saw that and I don’t 100% follow as MYNUMBER could only ever be between 0 and 59 initially and then between 0 and 1 from then on but it got me thinking/realised some errors and have edited the code as follows

    MYNUMBER=ABS(ROUND(((((RSI[14]*RSI[8])*(HIGH[MYNUMBER*10]+MEDIANPRICE))*((INTRADAYBARINDEX/(TIME+1)))+DAYS+(DAYOFWEEK+1)*MONTH)-ROUND((((RSI[14]*RSI[8])*(HIGH[MYNUMBER*10]+MEDIANPRICE))*((INTRADAYBARINDEX/(TIME+1)))+DAYS+(DAYOFWEEK+1)*MONTH)/10-0.49)*10)-0.4)/10)
    
    RANDOM=ROUND(MYNUMBER[(MYNUMBER*10)])

    I’ve deleted the ONCE MYNUMBER=CURRENTSECOND and added the *10 for MYNUMBER (no idea how this was working if for example MYNUMBER = 0.3!)  but I also noticed I was getting the occasional -0 for MYNUMBER so have added the ABS

    Maybe that was it… hmmm will test and get back if still happening – thank you again! 🙂

     

     

    (On another note Nicolas I see the code above is all left justified which implies/I have had this happen before when there are issues with missing/incorrect ENDIF statements – but I notice it happening more and more when it is code that is fine/I have run and which works and I cannot see any issue in the syntax of the code – when it happens I just CTRL-A -> CTRL-X -> CTRL-V and all the code is properly aligned but it makes me nervous that there is something wrong… wondered if you might know why it is doing that?)

    #34475 quote
    Nicolas
    Keymaster
    Master

    It was just a rough idea while browsing different topics and because of

    HIGH[MYNUMBER*10]

    .. I didn’t test the code, but did you GRAPH MYNUMBER already?

    About indentation of the codes … pfff what a mess, I already spent too much time trying to make it works, believe me I would be glad to have it sort out since the time I’m trying to get it through! The indentation is lost when you copy/paste from the platform IDE, you can try on notepad for example, that’s why my syntax highlighter would need to parse all the codes and rebuild it, but it is really painful to code 🙂

    #34510 quote
    Wing
    Participant
    Veteran

    On an unrelated note it is good to see that someone found a use for my code snippet. I can also see that it can cause problems if you change parts of it.

    #34511 quote
    random45689
    Member
    New

    Hi Wing yes thank you very much 🙂

    The only changes I made to your code originally was to add +1 to TIME as I had issues in testing months ago and thought perhaps it might be to do with when TIME = 000000 – dividing by 0 would be infinity/cause problems no?

    The other thing I changed was to divide the whole calculation by 10 so as to get results between 0 and 1 which I could then ROUND to give a definite 0 or 1 for the coin toss

    Nicolas – cool thanks I see how you spotted it now but am still bit confused as the highest MYNUMBER could be initially would be 59 (it’s run on 3 second timeframe) so 59*10=590 still nowhere near 2000 then after that it would be highest of 1*10=10 – anyway I have edited and will get some demo strategies up and running and see how we go

    Re: the code indentation issue I was meaning in the platform IDE itself when I open up a strategy – sometimes it’s all left aligned which makes me uncomfortable all is not quite right with the code – but totally understand the headache you must be having trying to get it formatting properly into the forums here – bigger fish to fry am sure/don’t worry about it 😉

    I’ve been running the strategy both in demo and live for a few months now and on the whole was doing quite well BUT fell foul of the age old problem of ‘getting carried away’ and having setup too many systems running in parallel (live) the first round French election Sunday ‘jump’ was not kind to me/I was foolish as it was obvious in hindsight too many strategies had ended up short on the Friday going into the weekend and yeh this/any strategy I think can not deal well with those kinds of gaps! 🙁 *ouch*

    Am attaching a screenshot to show how the strategy plays out/see if anyone might potentially take interest in wanting to know more/help out on the trade management side of things

    Cheers

    Max

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

Coin Toss Code Issue


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

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

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