Evaluate a string  [SOLVED]

Questions about MultiCharts and user contributed studies.
ChiTownTrader
Posts: 6
Joined: 01 Oct 2014
Has thanked: 2 times

Evaluate a string

Postby ChiTownTrader » 11 Oct 2014

Hi and sincere thanks for any help

I am looking for a command similar to the Matlab/C++ command "EVAL" which evaluates a string.

I am essentially looking to do the below.

string0=" condition1" ;
if s1=1 then string1=" and condition2" else string1=" ";

stringSum=string0+string1;

if EVAL(stringSum) then buy next bar at open;

Thanks so much....moving from language to language is not so "Easy". Even if it is EasyLanguage.

Jeff

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: Evaluate a string

Postby bowlesj3 » 12 Oct 2014

There is no eval command. However I was studying your logic and I think you could just do this

Code: Select all

condition1 = true;
if condition1 = true and S1 = 1 then
then buy next bar at open;
But then again the above could be simplified down to this.

Code: Select all

if S1 = 1 then
then buy next bar at open;

ChiTownTrader
Posts: 6
Joined: 01 Oct 2014
Has thanked: 2 times

Re: Evaluate a string

Postby ChiTownTrader » 12 Oct 2014

Thanks for input. No wonder I could not find "Eval" functionality :(

However, the code you offered will not work for what I am looking to do.
I am essentially looking to have the optimizer "toggle on/off" a condition to buy/sell.
So in my original code. I could have s1 as an input that would be optimized from,
0 to 1 increment 1.
and thereby determining if condition2 was of value or not in the optimization. Such that if the optimization determined that condition 2 was of no value it would toggle s1 to 0.

As there is no "Eval" functionality are there any programers who might know of a way to achieve what I am describing ??? Huge appreciation as I have been hacking away at this for almost a day now and ugh .... Immeasurable thanks in advance !!

--------------------Orig code below------------------------
string0=" condition1" ;
if s1=1 then string1=" and condition2" else string1=" ";

stringSum=string0+string1;

if EVAL(stringSum) then buy next bar at open;

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Evaluate a string

Postby JoshM » 12 Oct 2014

I am essentially looking to have the optimizer "toggle on/off" a condition to buy/sell.
So in my original code. I could have s1 as an input that would be optimized from,
0 to 1 increment 1.
and thereby determining if condition2 was of value or not in the optimization. Such that if the optimization determined that condition 2 was of no value it would toggle s1 to 0.
Something like this?

Code: Select all

Inputs:
s1(0); // Optimizable; in the optimization screen you can set the range,
// like 0 to 1 with steps of 1.

Variables:
s1Variable(0), // This way we can assign a value to s1
ofValue(false);

s1Variable = s1;

// Determine if 'condition2 was of value or not'
ofValue = ....

if (ofValue = false) then
s1Variable = 0; // Toggle the value of s1 to 0.
If this is not what you meant, please try to explain it again in even simpler terms. After reading Matlab's documentation on eval I still don't have the feeling I fully understand what you're saying (but that an also be me). :]

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: Evaluate a string  [SOLVED]

Postby furytrader » 13 Oct 2014

Are you trying to evaluate a conditional statement ... or create a string representation of a conditional statement?

If you're trying to build conditional statements out of two or more sub-conditions, the best way to do that is to create individual conditions which evaluate to a boolean type (i.e., true or false) and then use simple boolean arithmetic to combine them.

For example (and note that I'm not using string notation or quotation marks):

Code: Select all

condition1 = C > Average(C,20); // I'm just including this criteria as an example
condition2 = H = Highest(High,10); // Again, just as an example

If s1 = 1 Then condition3 = condition1 AND condition2 ELSE condition3 = condition1;
If condition3 then buy next bar at open;
Does that do what you're trying to do?

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: Evaluate a string

Postby bowlesj3 » 13 Oct 2014

I was reading a bit on this eval statement.
http://www.atmos.washington.edu/~mitche ... rings.html
I needed some examples rather than the syntax and the person in this article nailed it. eval is not easy to understand. I use to know a unix scripting eval statement very well. Eval has nothing to do with logical and/or gates or any of that stuff. It is more of a parsing tool command (analyzing strings and building a new command). The guy at the link was explaining that its use is more of a file writing tool (preparing strings or converting them). What I am saying is I would forget all about the eval statement.

I think I might understand what you are trying to do from the quote below.
I am essentially looking to have the optimizer "toggle on/off" a condition to buy/sell.
So in my original code. I could have s1 as an input that would be optimized from,
0 to 1 increment 1.
and thereby determining if condition2 was of value or not in the optimization. Such that if the optimization determined that condition 2 was of no value it would toggle s1 to 0.
Here is my best guess as to what you mean and what you want. Syntax may not be perfect. I am creating a website (learning 6 new languages) and sometimes I confuse the syntax across languages now.

Code: Select all

If S1-Input-WhichIsBeingOptimized = 1 then
begin
{since the optimization value is 1 then we want to consider the condition2 test}
if Condition2 = true then
buy next bar at open;
end
else
begin
{since the optimization value is 0 then we avoid then condition2 test}
value1 = 0; {This is a dummy command doing nothing more than making the syntax work}
End;
Regarding this
Such that if the optimization determined that condition 2 was of no value it would toggle s1 to 0.
I think this is what is confusing people. It sounds like you are saying that the S1-Input-WhichIsBeingOptimized value is to be turned back to zero after the buy statement. To do this you would need to have a working field that gets set to the S1-Input-WhichIsBeingOptimized in the currentbar=1 because I do not think you can alter an input (last time I accidentally tried it stopped me). Also, I am not sure it is a good idea to do this since it goes against the whole idea of optimizing? It would confuse the optimizing results to copy the inputs that are suppose to be processed according to the optimizer sequence and change them.
Last edited by bowlesj3 on 13 Oct 2014, edited 4 times in total.

ChiTownTrader
Posts: 6
Joined: 01 Oct 2014
Has thanked: 2 times

Re: Evaluate a string

Postby ChiTownTrader » 13 Oct 2014

Well many thanks to those that replied.

I have yet to test the code from: JoshM / Furytrader / bowlesj3- but wanted to avoid any confusion which was suggested by my description: "I think this is what is confusing people." and also to add how this "idea" might have value. Furthermore, I am not hung up on "eval" I just use it in Matlab and it solved the stated problem fairly elegantly (though EL has not Eval so....).

Nonetheless, I will phrase my question better in context all can relate to.

Say we are starting out looking at a new instrument and we have a bunch (10) of indicators/conditions which are in the buy statement( If condition1 and condition2 and conditinon3 and.... then buy next bar at market.) It would be useful to determine which of the 10 are not adding to the performance of the system in an optimization. This can be done manually/iteratively but very cumbersome.

So, as a --simple-- experiment to find the "bad condition"; I created a system with only 2 conditions of which one is known to have no value (known bad) in improving the system performance.

Lets define this useless condition as:

condition2 = H[10]<L[10] ; {lookback of 10 is irrelevant obviously}

condition1 from FuryTrade is:

condition1 = C > Average(C,20);
condition2 = H[10]<L[10];

My goal is to have the optimizer "find" that condition2 is a "useless condition" in improving the performance of the system by turning an input from 1 to 0. This again is only a test of the system logic by planting a "known bad" condition for the test.

I hope this makes more sense and how the functionality could be useful. I will omit how my original Matlab Eval script solved this problem because Eval seemed to add more confusion. I just thought Java / Matlab being popular and has the EVAL function it would be helpful. I mistyped C++ for Java. C++ has no Eval.

Okay, you kind gents- thanks again for the ideas/code provided, I will look to see if your code accomplishes my goal. I felt bad by not being more clear in my original question and wanted to add clarity into why I asked the question in the first place.

Thanks^2,
Jeff

ChiTownTrader
Posts: 6
Joined: 01 Oct 2014
Has thanked: 2 times

Re: Evaluate a string

Postby ChiTownTrader » 13 Oct 2014

Okay, I think I got it. I used FuryTrader's code as it just read easiest to my weary eyes. Though I believe JoshM and bowlesj3's code would work too.

In an effort to ensure it works I added a little more code to actually make a system which would be optimizable and then looked at the results.


{-----------system where condition 2 does not add value --------------}
inputs:s1(1),fastlength(9),slowlength(18);
vars:var2(0),var3(0);

var2 = Average( close , FastLength ) ;
var3 = Average( close , SlowLength ) ;

Condition1=var2>var3 ;
condition2 = H[10]<L[10]; // the "<" can be flipped to ">" for a test.
{Fury code below verbatim}
If s1 = 1 Then condition3 = condition1 AND condition2 ELSE condition3 = condition1;
If condition3 then buy next bar at open;


If Marketposition=1 and BarsSinceEntry>0 then
begin

Sell("ProfitTAR2") Next Bar all contracts on (entryprice+3) limit;
sell("MMloss") next bar all contracts on (entryprice-3) stop ;

end;

{------------------------------------------------------------------------------------------}

In running the optimization:

s1 from 0 to 1 increment 1
fastlength from 3 to 12
slowlength from 12 to 22

Note the less than sign below (same as above system):
with condition2 = H[10] < L[10]; s1 was --always 0--- in the optimizations with positive net profit.
So, the optimization "found" the bad condition as witnessed by s1 equal to 0. Success !!!

Then I flip the less than to great than as below for as a test.

condition2 = H[10] > L[10]; In this optimization s1 toggles from 0 to 1 in the optimizations with positive net value. See attached .jpg of optimization for clarity.
I think this makes sense for s1 to toggle (0-1) in this case as condition 2 adds no value nor does the condition degrade value. Hence, s1 taking either 0 or 1. Does it make sense that s1 toggles from 0 to 1 in the condition2 = H[10] > L[10]; ???? Or am I rationalizing because my eyes are burning :(

Please forgive the wordiness. I tried to be very clear as my previous text was apparently confusing.

Thanks so much again to: FuryTrader, JoshM and Bowlesj3 for all your help. This is a great board with generous thoughtful people and I hope to contribute to it in some way to "pay it forward. "


Jeff
Attachments
optimization report.JPG
(110.55 KiB) Downloaded 735 times


Return to “MultiCharts”