Coin Toss Code Issue
Forums › ProRealTime English forum › ProOrder support › Coin Toss Code Issue
- This topic has 5 replies, 3 voices, and was last updated 7 years ago by random45689.
-
-
05/03/2017 at 2:10 PM #34425
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273//================= PARAMETERS ================//*********************************************DEFPARAM PRELOADBARS = 2000DEFPARAM CUMULATEORDERS = FALSE//============== CONSTANTS ====================//*********************************************ONCE MINPOSITIONSIZE=1 //MINIMUM ORDER SIZE AS PER IG OR WHATEVER YOU CHOOSE - POSITION SIZE WILL ALSO INCREASE/DECREASE BY THIS FIGUREONCE MYNUMBER=CURRENTSECOND //INITIAL SEED FIGURE FOR RANDOM NUMBER GENERATORONCE 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 OCCURREDONCE HIGHESTHIGH = 0 //USED IN ATR CALCULATIONONCE LOWESTLOW = LOW[1]*10000 //USED IN ATR CALCULATIONONCE 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 TIMEFRAMEONCE FIRSTTRADE=0 //USED TO ALLOW MANUAL TRADE FOR FIRST TRADE//========== SPREAD VARIATIONS ================//*********************************************IF Time >= 070000 AND Time < 080000 THENSPREAD=2ELSIF Time >= 080000 AND Time < 163000 THENSPREAD=1ELSIF Time >= 163000 AND Time < 210000 THENSPREAD=2ELSESPREAD=5ENDIF//======== '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)/10RANDOM=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 THENAVERAGEATR=INITIALATR //ALLOWS FOR IMMEDIATE ORDER ENTRY ON STRATEGY STARTENDIFIF 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 THENAVERAGEATR=((INITIALATR*999)+TR0)/1000IF HIGHESTHIGH>PREVIOUSBLOCKHIGH AND LOWESTLOW<PREVIOUSBLOCKLOW THENTR0 = HIGHESTHIGH-LOWESTLOWELSIF PREVIOUSBLOCKCLOSE>HIGHESTHIGH OR PREVIOUSBLOCKCLOSE<LOWESTLOW THENB1 = ABS(HIGHESTHIGH-PREVIOUSBLOCKCLOSE)B2 = ABS(LOWESTLOW-PREVIOUSBLOCKCLOSE)TR0 = MAX(B1,B2)ELSEC1 = HIGHESTHIGH-LOWESTLOWC2 = ABS(HIGHESTHIGH-PREVIOUSBLOCKCLOSE)C3 = ABS(LOWESTLOW-PREVIOUSBLOCKCLOSE)C4 = MAX(C1,C2)TR0 = MAX(C3,C4)ENDIFENDIFIF COUNT = 2 THENAVERAGEATR = ((AVERAGEATR*999)+TR0)/1000IF HIGHESTHIGH>PREVIOUSBLOCKHIGH AND LOWESTLOW<PREVIOUSBLOCKLOW THENTR0 = HIGHESTHIGH-LOWESTLOWELSIF PREVIOUSBLOCKCLOSE>HIGHESTHIGH OR PREVIOUSBLOCKCLOSE<LOWESTLOW THENB1 = ABS(HIGHESTHIGH-PREVIOUSBLOCKCLOSE)B2 = ABS(LOWESTLOW-PREVIOUSBLOCKCLOSE)TR0 = MAX(B1,B2)ELSEC1 = HIGHESTHIGH-LOWESTLOWC2 = ABS(HIGHESTHIGH-PREVIOUSBLOCKCLOSE)C3 = ABS(LOWESTLOW-PREVIOUSBLOCKCLOSE)C4 = MAX(C1,C2)TR0 = MAX(C3,C4)ENDIFENDIFHIGHESTHIGH = HIGHLOWESTLOW = LOWPREVIOUSBLOCKHIGH = HIGHESTHIGH[1]PREVIOUSBLOCKLOW = LOWESTLOW[1]PREVIOUSBLOCKCLOSE = CLOSE[1]IF COUNT < 2 THENCOUNT=COUNT+1ENDIFELSEHIGHESTHIGH = MAX(HIGHESTHIGH,HIGH)LOWESTLOW = MIN(LOWESTLOW,LOW)IF COUNT>=1 THENIF HIGHESTHIGH>PREVIOUSBLOCKHIGH AND LOWESTLOW<PREVIOUSBLOCKLOW THENA1 = HIGHESTHIGH-LOWESTLOWTR0 = MAX(TR0,A1)ELSIF PREVIOUSBLOCKCLOSE>HIGHESTHIGH OR PREVIOUSBLOCKCLOSE<LOWESTLOW THENB1 = ABS(HIGHESTHIGH-PREVIOUSBLOCKCLOSE)B2 = ABS(LOWESTLOW-PREVIOUSBLOCKCLOSE)B3 = MAX(B1,B2)TR0 = MAX(TR0,B3)ELSEC1 = HIGHESTHIGH-LOWESTLOWC2 = ABS(HIGHESTHIGH-PREVIOUSBLOCKCLOSE)C3 = ABS(LOWESTLOW-PREVIOUSBLOCKCLOSE)C4 = MAX(C1,C2)C5 = MAX(C3,C4)TR0 = MAX(TR0,C5)ENDIFENDIFENDIF//ONGOING ATR CALCULATION END//*******************************************//================TRADING====================//*******************************************//ORDER ENTRY BEGINIF NOT ONMARKET THENIF STRATEGYPROFIT<-(2*MINPOSITIONSIZE*AVERAGEATR*POSITIONMULTIPLIER) THEN //'FAILSAFE' OVERALL STOPLOSSQUITENDIF//TRADING BOUNDARIES CALCULATION BEGINIF POSCOUNT=0 THENPOSITIONSIZE=MINPOSITIONSIZEPOSBOUNDARY=(POSITIONSIZE*AVERAGEATR*POSITIONMULTIPLIER)PREVIOUSPOSBOUNDARY=-POSBOUNDARYTARGETPOSBOUNDARY=POSBOUNDARY+((POSITIONSIZE+MINPOSITIONSIZE)*AVERAGEATR*POSITIONMULTIPLIER)ENDIFIF STRATEGYPROFIT > TARGETPOSBOUNDARY THENPREVIOUSPOSBOUNDARY=POSBOUNDARYPOSBOUNDARY=POSBOUNDARY+((POSITIONSIZE+MINPOSITIONSIZE)*AVERAGEATR*POSITIONMULTIPLIER)POSITIONSIZE=POSITIONSIZE+MINPOSITIONSIZETARGETPOSBOUNDARY=POSBOUNDARY+((POSITIONSIZE+MINPOSITIONSIZE)*AVERAGEATR*POSITIONMULTIPLIER)POSCOUNT=1ENDIFIF STRATEGYPROFIT < PREVIOUSPOSBOUNDARY THENTARGETPOSBOUNDARY=POSBOUNDARYIF TARGETPOSBOUNDARY<=(MINPOSITIONSIZE*AVERAGEATR*POSITIONMULTIPLIER)+(2*MINPOSITIONSIZE*AVERAGEATR*POSITIONMULTIPLIER) THENTARGETPOSBOUNDARY=(MINPOSITIONSIZE*AVERAGEATR*POSITIONMULTIPLIER)+(2*MINPOSITIONSIZE*AVERAGEATR*POSITIONMULTIPLIER)ENDIFPOSBOUNDARY=PREVIOUSPOSBOUNDARYIF POSBOUNDARY<=(MINPOSITIONSIZE*AVERAGEATR*POSITIONMULTIPLIER) THENPOSBOUNDARY=(MINPOSITIONSIZE*AVERAGEATR*POSITIONMULTIPLIER)ENDIFPREVIOUSPOSBOUNDARY=POSBOUNDARY-((POSITIONSIZE-MINPOSITIONSIZE)*AVERAGEATR*POSITIONMULTIPLIER)IF PREVIOUSPOSBOUNDARY<=0 OR PREVIOUSPOSBOUNDARY=POSBOUNDARY THENPREVIOUSPOSBOUNDARY=-(MINPOSITIONSIZE*AVERAGEATR*POSITIONMULTIPLIER)ENDIFPOSITIONSIZE=MAX(MINPOSITIONSIZE, POSITIONSIZE-MINPOSITIONSIZE)ENDIF//TRADING BOUNDARIES CALCULATION END//POSITION SIZE 'STOPLOSS' BEGINIF N=1 THENIF POSITIONSIZE<=HIGHESTPOSITIONSIZE-(2*MINPOSITIONSIZE) THENQUITENDIFENDIFIF POSITIONSIZE>5*MINPOSITIONSIZE AND POSITIONSIZE<=10*MINPOSITIONSIZE THENHIGHESTPOSITIONSIZE = MAX(HIGHESTPOSITIONSIZE, POSITIONSIZE)N=1ENDIFIF N=2 THENIF POSITIONSIZE<=HIGHESTPOSITIONSIZE-MINPOSITIONSIZE THENQUITENDIFENDIFIF POSITIONSIZE>10*MINPOSITIONSIZE THENHIGHESTPOSITIONSIZE = MAX(HIGHESTPOSITIONSIZE, POSITIONSIZE)N=2ENDIF//POSITION SIZE 'STOPLOSS' END//ORDER ENTRY BEGINIF FIRSTTRADE=0 THENSELLSHORT POSITIONSIZE CONTRACT AT MARKETCONTINUATION=1LADDERCOUNT=1ELSIF FIRSTTRADE=1 THENIF RANDOM=1 THENBUY POSITIONSIZE CONTRACTS AT MARKETCONTINUATION=1LADDERCOUNT=1ENDIFIF RANDOM=0 THENSELLSHORT POSITIONSIZE CONTRACT AT MARKETCONTINUATION =1LADDERCOUNT=1ENDIFENDIF//ORDER ENTRY ENDENDIF//ORDER MANAGEMENT BEGINIF LONGONMARKET THENSTOPLOSS=AVERAGEATR+SPREADSTOPPRICE = TRADEPRICE(1)-STOPLOSSIF CLOSE>TRADEPRICE(1) THENPRICEDIFFERENCE = CLOSE-TRADEPRICE(1)STOPLADDER = ROUND(PRICEDIFFERENCE/STOPLOSS)IF STOPLADDER<3 THENSTOPLADDER=0ENDIFCONTINUATION = SUMMATION[LADDERCOUNT](STOPLADDER>=STOPLADDER[1])=LADDERCOUNTLADDERCOUNT=LADDERCOUNT+1ENDIFENDIFIF LONGONMARKET AND CLOSE<=STOPPRICE OR CONTINUATION=0 THENSELL AT MARKETSTOPLADDER=0LADDERCOUNT=1FIRSTTRADE=1ENDIFIF SHORTONMARKET THENSTOPLOSS=AVERAGEATR+SPREADSTOPPRICE = TRADEPRICE(1)+STOPLOSSIF CLOSE<TRADEPRICE(1) THENPRICEDIFFERENCE = TRADEPRICE(1)-CLOSESTOPLADDER = ROUND(PRICEDIFFERENCE/STOPLOSS)IF STOPLADDER<3 THENSTOPLADDER=0ENDIFCONTINUATION = SUMMATION[LADDERCOUNT](STOPLADDER>=STOPLADDER[1])=LADDERCOUNTLADDERCOUNT=LADDERCOUNT+1ENDIFENDIFIF SHORTONMARKET AND CLOSE>=STOPPRICE OR CONTINUATION=0 THENEXITSHORT AT MARKETSTOPLADDER=0LADDERCOUNT=1FIRSTTRADE=1ENDIF//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 PREVIOUSBLOCKCLOSE05/03/2017 at 2:16 PM #3442905/03/2017 at 4:00 PM #34455Thanks (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
123MYNUMBER=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?)
05/03/2017 at 7:06 PM #34475It was just a rough idea while browsing different topics and because of
1<span class="token keyword">HIGH</span><span class="token punctuation">[</span>MYNUMBER<span class="token operator">*</span><span class="token number">10</span><span class="token punctuation">]</span>.. 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 🙂
05/04/2017 at 12:35 PM #3451005/04/2017 at 1:32 PM #34511Hi 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
-
AuthorPosts
Find exclusive trading pro-tools on