OnChart Stochastic as exit factor LONG
Forums › ProRealTime English forum › ProOrder support › OnChart Stochastic as exit factor LONG
- This topic has 17 replies, 4 voices, and was last updated 5 years ago by soulintact.
-
-
01/14/2019 at 11:05 PM #88623
Here is the full strategy in which I prefer to have the indicator embedded in the code without calling, as for optimization reasons. I still cannot understand why it does not work….
OnChart Stochastic auto trading strategy12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061//-------------------------------------------------------------------------defparam cumulateorders=false// --- settings of ORDERSamount = 100 //quantity of shares/contracts to open for each new order//--------------------------------------------------------------------------------////Indicators//--------------------------------------------------------------------------------////PRC_OnChart Stochastic | indicator//23.05.2018//Nicolas @ www.prorealcode.com//Sharing ProRealTime knowledge// --- settingsKPeriod = 26Slowing = 5DPeriod = 5ATRperiod = 20maPeriod = 20maMethod = 0overBought = 80overSold = 20// --- end of settingsmaPrice = customclosedTR = 0for i = 0 to ATRperiod-1dTR=dTR+max(abs(Dhigh(i)-Dlow(i)),max(abs(Dhigh(i)-Dclose(i+1)),abs(Dlow(i)-Dclose(i+1))))nextavgRange = dTR/ATRperiod//avgRange = AverageTrueRange[ATRperiod]maValue = average[maPeriod,maMethod](maPrice)stochValue = Stochastic[Kperiod,Slowing](maPrice)signalValue = average[Dperiod,maMethod](stochValue)//----Buffer1=maValue //mid levelBuffer2=maValue+(avgRange*(overBought-50)/100) //Overbought levelBuffer3=maValue-(avgRange*(50- overSold)/100) //Oversold levelBuffer4=maValue+(stochValue -50)/100*avgRange //OnChart stochasticBuffer5=maValue+(signalValue-50)/100*avgRange //Signal lineBullONCHARTSTO=Buffer4>Buffer5 and Buffer5<Buffer1BearONCHARTSTO=Buffer5>Buffer4 and Buffer5<Buffer3 and Buffer1>Buffer2//--------------------------------------------------------------------------------////Conditions when to actif not longonmarket and BullONCHARTSTO thenbuy amount shares at marketendifif longonmarket and BearONCHARTSTO and positionperf>0 thensell at marketendif01/14/2019 at 11:45 PM #88626To have a chance to debug code correctly and fast enough, you should (always):
- write clear, readable, easy to understand code
- use correct alignement of code to make it easier to spot erroneous lines
- use meaningful names, BUFFER1,BUFFER2,BUFFER3,BUFFER4,BUFFER5 will lead to slower debugging code and is prone to errors (as you’ll see in a while)
Line 27 is better written like I did, so, in case you want to change Ob/Os tresholds, you’ll have just to change line 26 (not line 27, not anymore).
Line 50, with meaningful names, shows why you entered a trade and could not exit until you had wiped out your capital. It’s because generic, confusing names didn’t allow you to spot that the last part of line 50 is impossible to become true, thus your strategy never met a BEARish condition to exit a trade.
Line 50, the second condition was also wrong; with meaningful names you can easily spot it.
There you go:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061//-------------------------------------------------------------------------defparam cumulateorders=false// --- settings of ORDERSamount = 100 //quantity of shares/contracts to open for each new order//--------------------------------------------------------------------------------////Indicators//--------------------------------------------------------------------------------////PRC_OnChart Stochastic | indicator//23.05.2018//Nicolas @ www.prorealcode.com//Sharing ProRealTime knowledge// --- settingsKPeriod = 26Slowing = 5DPeriod = 5ATRperiod = 20maPeriod = 20maMethod = 0//overBought = 80//overSold = 100 - overBought// --- end of settingsmaPrice = customclosedTR = 0for i = 0 to ATRperiod-1dTR=dTR+max(abs(Dhigh(i)-Dlow(i)),max(abs(Dhigh(i)-Dclose(i+1)),abs(Dlow(i)-Dclose(i+1))))nextavgRange = dTR/ATRperiod//avgRange = AverageTrueRange[ATRperiod]maValue = average[maPeriod,maMethod](maPrice)stochValue = Stochastic[Kperiod,Slowing](maPrice)signalValue = average[Dperiod,maMethod](stochValue)//----MidLevel = maValue //mid level//OBlevel = maValue+(avgRange*(overBought-50)/100) //Overbought level//OSlevel = maValue-(avgRange*(50- overSold)/100) //Oversold levelStoK = maValue+(stochValue -50)/100*avgRange //OnChart stochastic - K lineStoD = maValue+(signalValue-50)/100*avgRange //Signal line - D lineBullONCHARTSTO = StoK > StoD and StoD < MidLevelBearONCHARTSTO = StoD > StoK and StoD > MidLevel// and MidLevel > OBlevel//--------------------------------------------------------------------------------////Conditions when to actif not longonmarket and BullONCHARTSTO thenbuy amount shares at marketendifif longonmarket and BearONCHARTSTO and positionperf>0 thensell at marketendif1 user thanked author for this post.
01/15/2019 at 8:37 AM #88647Thank you very much Roberto. That makes all the sense in the world, and it even solved the problem. I have learned a lot from you. Thank you for being so patient with me!
-
AuthorPosts
Find exclusive trading pro-tools on