Laurens,
You made many good improvements! Once you work everything out you may want to open it in its own window (although in a CDF I am not sure what that would do)
Your problem is that you are not correctly handling all of your states when you change stock names.
Your code is
InputField[Dynamic[p1, {Automatic, listfun1}], String, FieldSize -> 4]
This will run listfun1 and change the stock name (although your syntax is a bit dangerous and confusing here -- I would use listfun1[#]& and make listfun1 and listfun2 functions of one variable -- although it does work the way it is)
The problem is that this code updates the value of the list1 variable, however, it does nothing to all the other variables that depend on list1 (listt, start, end, y). I recommend that you make your listfun1 and listfun2 functions update all of the variables that you need and everything will update properly when you tab out of the field. Also, you may want to add Selectable->False to your textcells so that you can't edit them and can tab over them (they will select as a whole but you can't click on them or go inside). Here is a quick hack that "fixes" list1fun but not list2fun -- I did not verify that all settings are updating properly but this should give you the general idea -- when you change something you must change everything that depends on it in a consistent way. I tried to make minimal changes although you may want to consider some reorganization that will simplify the code. For example, testing for strings in y[[1]] is not the cleanest code -- maybe consider returning just 1,2, or 3 and create some other variable with your strings and plots -- but you have to think this through to see if it causes other issues.
DynamicModule[{a, b, c, d, p1 = "GE", p2 = "MSFT", list1, list2,
listfun1, listfun2, listt, start, end, relprice, x,
y = {" price of stock 1 ", {}}, xpart, ypart, imagesize},
listfun1[a_] :=
Module[{}, list1 = FinancialData[a, "OHLCV", {2016, 1, 1}];
listt = list1[[All, 1]];
y[[2]] =
Switch[y[[1]], " price of stock 1 ", list1[[All, 2, 4]],
" price of stock 2", list2[[All, 2, 4]],
"quotient stock prices 1 and 2",
list1[[All, 2, 4]]/list2[[All, 2, 4]]];
start = Round[Length[listt]* 0.25];
end = Round[Length[listt]* 0.75]];
listfun1[p1];
listfun2[c_, d_] := list2 = FinancialData[c, "OHLCV", {2016, 1, 1}];
listfun2[p2, p2];
Panel[Column[{(*Choice of the 2 stocks*)
Row[{TextCell[" Stock 1: ", Selectable -> False],
InputField[Dynamic[p1, {Automatic, listfun1[#] &}], String,
FieldSize -> 4],
TextCell[" Compare with stock 2: ",
Selectable -> False],
InputField[Dynamic[p2, {Automatic, listfun2}], String,
FieldSize -> 4]}],(*Choice of the time interval*)
IntervalSlider[{Dynamic[start], Dynamic[end]}, {1, Length[listt],
1}, ImageSize -> {500, 20}, Method -> "Stop",
MinIntervalSize -> 1], Dynamic[start];
Dynamic[end];
Row[{Dynamic[DateString[DateObject[listt[[start]]]]],
TextCell[
" "\
, Selectable -> False],
Dynamic[DateString[
DateObject[
listt[[end]]]]]}],(*Choice of quantities to be plotted*)
Row[{TextCell[" ", Selectable -> False],
Dynamic[RadioButton[
Dynamic[y], {" price of stock 1 ", list1[[All, 2, 4]]}]],
TextCell[" price stock 1 ", Selectable -> False],
Dynamic[RadioButton[
Dynamic[y], {" price of stock 2", list2[[All, 2, 4]]}]],
TextCell[" price stock 2 ", Selectable -> False],
Dynamic[RadioButton[
Dynamic[y], {"quotient stock prices 1 and 2",
list1[[All, 2, 4]]/list2[[All, 2, 4]]}]],
TextCell[" quotient stock prices 1 and 2",
Selectable -> False]}], xpart = Dynamic[Range[start, end]];
ypart = Dynamic[y[[2]][[start ;; end]]];
Dynamic[
ListLinePlot[
Partition[Riffle[Range[start, end], y[[2]][[start ;; end]]], 2],
ImageSize -> 700]]}]]]
I hope this helps.
Regards,
Neil