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….
//-------------------------------------------------------------------------
defparam cumulateorders=false
// --- settings of ORDERS
amount = 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
// --- settings
KPeriod = 26
Slowing = 5
DPeriod = 5
ATRperiod = 20
maPeriod = 20
maMethod = 0
overBought = 80
overSold = 20
// --- end of settings
maPrice = customclose
dTR = 0
for i = 0 to ATRperiod-1
dTR=dTR+max(abs(Dhigh(i)-Dlow(i)),max(abs(Dhigh(i)-Dclose(i+1)),abs(Dlow(i)-Dclose(i+1))))
next
avgRange = dTR/ATRperiod
//avgRange = AverageTrueRange[ATRperiod]
maValue = average[maPeriod,maMethod](maPrice)
stochValue = Stochastic[Kperiod,Slowing](maPrice)
signalValue = average[Dperiod,maMethod](stochValue)
//----
Buffer1=maValue //mid level
Buffer2=maValue+(avgRange*(overBought-50)/100) //Overbought level
Buffer3=maValue-(avgRange*(50- overSold)/100) //Oversold level
Buffer4=maValue+(stochValue -50)/100*avgRange //OnChart stochastic
Buffer5=maValue+(signalValue-50)/100*avgRange //Signal line
BullONCHARTSTO=Buffer4>Buffer5 and Buffer5<Buffer1
BearONCHARTSTO=Buffer5>Buffer4 and Buffer5<Buffer3 and Buffer1>Buffer2
//--------------------------------------------------------------------------------//
//Conditions when to act
if not longonmarket and BullONCHARTSTO then
buy amount shares at market
endif
if longonmarket and BearONCHARTSTO and positionperf>0 then
sell at market
endif
To 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:
//-------------------------------------------------------------------------
defparam cumulateorders=false
// --- settings of ORDERS
amount = 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
// --- settings
KPeriod = 26
Slowing = 5
DPeriod = 5
ATRperiod = 20
maPeriod = 20
maMethod = 0
//overBought = 80
//overSold = 100 - overBought
// --- end of settings
maPrice = customclose
dTR = 0
for i = 0 to ATRperiod-1
dTR=dTR+max(abs(Dhigh(i)-Dlow(i)),max(abs(Dhigh(i)-Dclose(i+1)),abs(Dlow(i)-Dclose(i+1))))
next
avgRange = 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 level
StoK = maValue+(stochValue -50)/100*avgRange //OnChart stochastic - K line
StoD = maValue+(signalValue-50)/100*avgRange //Signal line - D line
BullONCHARTSTO = StoK > StoD and StoD < MidLevel
BearONCHARTSTO = StoD > StoK and StoD > MidLevel// and MidLevel > OBlevel
//--------------------------------------------------------------------------------//
//Conditions when to act
if not longonmarket and BullONCHARTSTO then
buy amount shares at market
endif
if longonmarket and BearONCHARTSTO and positionperf>0 then
sell at market
endif
Thank 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!