in Search

Simple Coding Question

Last post 05-06-2009, 22:27 by wabbit. 10 replies.
Sort Posts: Previous Next
  •  05-06-2009, 4:29 29638

    Simple Coding Question

    Hi

    I am trying to build an indicator as an expert advisor, giving +1 for long positions and -1 for short positions but when I plot even a simple version like below it plots positive 1 for both!

    If(Cross(RSI(14),30),1,0) OR If(Cross(70,RSI(14)),-1,0)

    Is there something I am doing wrong??
  •  05-06-2009, 4:50 29639 in reply to 29638

    Re: Simple Coding Question

    Its ok I figured it out..

    If(Cross(RSI(14),30),1,If(Cross(70,RSI(14)),-1,0))

    I had confused myself but broke it down and then it jumped out at me!
  •  05-06-2009, 5:21 29640 in reply to 29639

    Re: Simple Coding Question

    ksrt,

    Because the signals are mutually exclusive, you can further simplify the expression by exploiting how MS returns 1 or 0 from comparison operations:

    Code:
    ind:=RSI(C,14);
    Cross(ind,30) - Cross(70,ind);


    will return 1 if the first part of the expression is true (which means the second part is false as they are mutually exclusive) and -1 if the first part is false and the second part true.



    wabbit Big Smile [:D]


    "The question of whether a computer can think is no more interesting than the question of whether a submarine can swim."
    Edsgar W. Dijkstra

     

    MS: 6.52 EOD, 7.x EOD, 8.0 PRO, 9.2 PRO w/QC, 10 PRO w/QC & MDK
    For custom MetaStock programming : http://www.wabbit.com.au
    My SkyPE status :
    My SkyPE account : wabbit.com.au

  •  05-06-2009, 5:35 29641 in reply to 29640

    Re: Simple Coding Question

    Thanks Wabbit, makes sense

    I have another one. I am trying to reference another indicator saying if the MAs cross and expert advisor occurred in the last 15 days then 1 (where the expert advisor returns 1 if the event occurred)

    Probably is very basic but I am just trying to play around while I learn the language etc. Here is my attempt but it returns 1 when clearly the expert advisor did not provide a signal in the last 15 days..

    If(Cross(Mov(C,9,E),Mov(C,20,E)) AND
    Ref(Fml("ExpertAdvisor(3)"),15)
    ksrt
  •  05-06-2009, 5:37 29642 in reply to 29641

    Re: Simple Coding Question

    Formula should read:

    If(Cross(Mov(C,9,E),Mov(C,20,E)) AND Ref(Fml("Expert Advisor(3)"),15)<1,1,0)) {...}
  •  05-06-2009, 5:38 29643 in reply to 29642

    Re: Simple Coding Question

    You need the Alert() function.

    wabbit Big Smile [:D]


    "The question of whether a computer can think is no more interesting than the question of whether a submarine can swim."
    Edsgar W. Dijkstra

     

    MS: 6.52 EOD, 7.x EOD, 8.0 PRO, 9.2 PRO w/QC, 10 PRO w/QC & MDK
    For custom MetaStock programming : http://www.wabbit.com.au
    My SkyPE status :
    My SkyPE account : wabbit.com.au

  •  05-06-2009, 5:41 29644 in reply to 29643

    Re: Simple Coding Question

    Great thanks - will give it a go :)
  •  05-06-2009, 8:13 29646 in reply to 29638

    Re: Simple Coding Question - OR Conditions Give Boolean Results, not Signals

    Hello,

    If you are looking for an indicator that gives the signals you want; then, try simply separating the expressions instead.   This gives the buy and sell signals you are looking for.

    If(Cross(RSI(14),30),1,0);
    If(Cross(70,RSI(14)),-1,0);

    The reason that you think you see the results for the buy signal is that MS evaluates the whole OR expression.   Whenever  "(xxxx) OR (xxxx)" is TRUE, the entire expression evaluates to TRUE, which is one.   So with the expression below, you get the Boolean results of 1's and 0's, not the output of the IF() construct for EITHER condition, and the BUY/SELL RSI conditions appear to give a "+1" signal.

    If(Cross(RSI(14),30),1,0) OR  If(Cross(70,RSI(14)),-1,0);
    Joe
  •  05-06-2009, 20:48 29659 in reply to 29646

    Re: Simple Coding Question - OR Conditions Give Boolean Results, not Signals

    jhughey:
    Hello,

    If you are looking for an indicator that gives the signals you want; then, try simply separating the expressions instead.   This gives the buy and sell signals you are looking for.

    If(Cross(RSI(14),30),1,0);
    If(Cross(70,RSI(14)),-1,0);

    The reason that you think you see the results for the buy signal is that MS evaluates the whole OR expression.   Whenever  "(xxxx) OR (xxxx)" is TRUE, the entire expression evaluates to TRUE, which is one.   So with the expression below, you get the Boolean results of 1's and 0's, not the output of the IF() construct for EITHER condition, and the BUY/SELL RSI conditions appear to give a "+1" signal.

    If(Cross(RSI(14),30),1,0) OR  If(Cross(70,RSI(14)),-1,0);


    Here:

    If(Cross(RSI(14),30),1,0);

    can also be written:

    Cross(RSI(14),30);

    by letting MS take care of the return of TRUE (=1) or FALSE (=0) from the expression.


    We can also take a short cut in MS for x*(-1) by writing:

    If(Cross(70,RSI(14)),-1,0);

    as:

    -Cross(70,RSI(14));


    Coders should be acutely aware of what they try to get MS to evaluate and how MS does its evaluation; being particularly cognisant of what constitutes TRUE and FALSE when using representative value.  Core MS sees any positive value as TRUE and any zero or negative number as FALSE.  (NOTE: some add-ins don't so you have to be even more careful using other products).  So in:

    If(Cross(RSI(14),30),1,0) OR  If(Cross(70,RSI(14)),-1,0);

    Let's strip down the components and rewrite the fucntionally exact code:

    x:=Cross(RSI(14),30);
    y:=Cross(70,RSI(14));

    If(x,1,0) OR  If(y,-1,0);

    As 'x' can be evaluated as TRUE (1) or FALSE (0) so too can the first part of the expresson in the last line, so it is functionally exactly the same as just writing 'x'; but things get more interesting in the second part where the 'y' part can only have two values, -1 or 0, both of which evaluate to FALSE in core MS, so the expression 'OR  If(y,-1,0)' will always be false and is therefore redundant.  The code drops out some of the condition for which the trader needs for decision making.



    Hope this helps.

    wabbit Big Smile [:D]


    "The question of whether a computer can think is no more interesting than the question of whether a submarine can swim."
    Edsgar W. Dijkstra

     

    MS: 6.52 EOD, 7.x EOD, 8.0 PRO, 9.2 PRO w/QC, 10 PRO w/QC & MDK
    For custom MetaStock programming : http://www.wabbit.com.au
    My SkyPE status :
    My SkyPE account : wabbit.com.au

  •  05-06-2009, 22:09 29663 in reply to 29659

    MS Booleans return TRUE for nonzero values, regardless of sign.

    Hi Wabbit,

    Thanks for the follow-up. Help me understand something about this expression.

    If(Cross(RSI(14),30),1,0) OR If(Cross(70,RSI(14)),-1,0);

    You said:  "Core MS sees any positive value as TRUE and any zero or negative number as FALSE. "

    I am not sure that is accurate in this situation. Experimentation with this indicator’s behavior implies that the right side’s return of -1 is evaluated as TRUE.

    For example, I tested the above expression side-by-side with the RSI(14) on a stock, and this indicator clearly gives a “1” under both conditions: when it crosses 30% and when it crosses 70%. This implies that the right side has evaluated -1 as TRUE.

    The next step, as a sanity check, I determined the following indicators gave a return values of 1.

    (-1) OR (-1)                returns TRUE
    (0) OR (-1)                 returns TRUE
    (-1) OR (0)                 returns TRUE
    (0) OR (-25)               returns TRUE
    (0) OR (-0.0001)        returns TRUE

    Based on this observation, it would seem that the return of a non-zero entity, positive or negative, would be evaluated by core MS as TRUE?
    Joe
  •  05-06-2009, 22:27 29665 in reply to 29663

    Re: MS Booleans return TRUE for nonzero values, regardless of sign.

    mea culpa.... you're right.

    As I said ,"some add-ins don't so you have to be even more careful using other products." The core MS values are:

    TRUE : any non-zero value
    FALSE : zero


    It always pays to check.


    wabbit Big Smile [:D]


    "The question of whether a computer can think is no more interesting than the question of whether a submarine can swim."
    Edsgar W. Dijkstra

     

    MS: 6.52 EOD, 7.x EOD, 8.0 PRO, 9.2 PRO w/QC, 10 PRO w/QC & MDK
    For custom MetaStock programming : http://www.wabbit.com.au
    My SkyPE status :
    My SkyPE account : wabbit.com.au

View as RSS news feed in XML