StochasticDelta strategy
Author: tradeoholic
Creation Date: 4/5/2021 11:47 AM
profile picture

tradeoholic

#1
Hello,

I'm trying to get involved into some more complex programming, and I would have an EOD strategy
which I can't manage to code. I have written the script, but it doesn't do anything. I ran the script on
the 30 ^DJI stocks. Please help me to write it correctly.

Rules:

To set the conditions we first have to:
-count the number of stocks in the watchlist for which is true that their stochastic value is below 50
and their stochastic value is trending upwards (this number is 'sto_below50'), and we build the
percentage value of 'sto_below50' related to the number of all stocks in the watchlist (that is
'uptrpctg'). Than we:
-count the number of stocks in the watchlist for which is true that their stochastic value is above 50
and their stochastic value is trending downwards (this number is 'sto_above50'), and we build the
percentage value of 'sto_above50' related to the number of all stocks in the watchlist (that is
'downtrpctg').
-we build the 'stoDelta' indicator such a way that we subtract 'uptrpctg' - 'downtrpctg'.

Every bar when 'stoDelta' and ^DJI close price moves in the opposite direction (one day movement
from 'bar-1' to 'bar' at close price), we enter position for ^DJI on bar at close price opposite its one
day movement, and exit on bar+1 at close.

CODE:
Please log in to see this code.
profile picture

Cone

#2
That's a pretty good attempt, but there's certainly a better way to go about it. (There's always a better way!)

One reason it might not do anything is because your main loop is starting on bar 9990. If you're loading less than 40 years of daily bars, that's not going to execute anything. Also, making pctg as a List and then referring to it as if it were a DataSeries with a bar number is a mistake.

But let me ask, why would you backtest by "trading" ^DJI instead of an index proxy that you can actually trade, like DIA?

profile picture

tradeoholic

#3
At first I used ^DJI because besides the built-in data set I haven't downloaded other symbol's data feed yet, and ^DJI and DIA is moving similarly so I thought it's eligible to see if the code works. But you are right, it's not tradeable, so I will download other data, and will run the script on DIA.

I think I should create a custom series and should put the value of the difference between uptrpctg - downtrpctg into this series for each bar running through the symbols in the watchlist. But I don't know how to do it ? Any suggestion ?

Anyway I ran the code I wrote from 04.10.1982, it's running but at the end the positions list empty.
profile picture

Cone

#4
Here's a good pattern to follow when you need to create an index that you can't get out of Index-Lab ;)

CODE:
Please log in to see this code.
profile picture

tradeoholic

#5
thank you Cone for the detailed code, I'm beginning to understand what are you doing, but unfortunately
something is wrong with it because

1 - it doesn't show any trades in the trades list and equity curve, although I downloaded data for 'DIA'.
2 - the expression 'StoDeltaIndex' is underlined red (in the row creating the 'Dataseries sdi'
in 'protected override void Execute()') and writes the following:
"The call is ambiguous between the following methods or properties:'stoDelta_analysis.StoDeltaIndex(Bars,IList,
int,int)' and 'stoDelta_analysis.StoDeltaIndex(Bars,Ilist, int, int)'"
3 - the expression 'StoDeltaIndex' is underlined red also (in the row 'public DataSeries StoDeltaIndex')
and writes the following:"Type 'stoDelta_analysis' already defines a member called 'StoDeltaIndex' with the same
parameter types"
4 - the expression 'PlotSymbolTrades' is underlined red (by 'plot the traded symbol and its trades')
and it doesn't plot the symbol for 'DIA' with the trades.
profile picture

Cone

#6
You usually don't need to worry about the red underlines - but you can fix that by changing the class to another unique name in the same Wealth-Lab session, like this:

CODE:
Please log in to see this code.


Do that, hit Compile. Is the compile successful?
If not, comment out the statement:
CODE:
Please log in to see this code.


Compile successful?
If not, what does it say?
profile picture

tradeoholic

#7
I changed the class name to: public class stoDelta_analysis123 : WealthScript
Then Compiled succesfully, but 'PlotSymbolTrades' is still underlined red.
Then I commented out //this.PlotSymbolTrades(dia, cp); and ran the script again
Only the 'StoDeltaIndex' is plot, but trades list and equity curve are empty
It is writing green that:
'Runtime error:Basic price for Position entry cannot be zero
at the following place:WealthLab.Strategies.stoDelta_analysis123.Execute()
at the following place:WealthLab.WealthScript.ShortAtClose(Int32 bar)
at the following place:WealthLab.WealthScript.ShortAtClose(Int32 bar, String signalName)
at the following place:WealthLab.WealthScript.b(Double A_0)
profile picture

Cone

#8
QUOTE:
Then Compiled succesfully, but 'PlotSymbolTrades' is still underlined red.
This is okay, you can ignore the red lines if the script compiles successfully.


QUOTE:
'Runtime error:Basic price for Position entry cannot be zero
Okay, that's because you're running the script from a date that starts before the symbol you're trying to trade (DIA). I changed the for loop to start at the FirstActualBar of the external series, so just re-copy it, compile and run.
profile picture

tradeoholic

#9
That's it. It works fine, thanks a lot.
profile picture

Cone

#10
By the way, you should check that the calculation is correct, because I did not!
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).