SWITCH function script usage

Hi, Can someone make sense of what is going on here? My goal is that the 'switch ON' and 'switch OFF' logic is used more than once, so I don't want to have to repeat the logic everywhere. In trying to do that I am finding faulty behaviour as follows:

This extract from the script works, but is not what I want:

TRADING = SWITCH( (DPtot > 0) and (D2P > 0) and (OFFSET(D2P, OFFSET=1) > 0), (DPtot <= 0) );

where DPtot and D2P are both 'plot' variables.

Note: I can delete the ; from the end of this line and the 'script valid' does not fail. On execution, however, the script fails badly, killing a non-relevant plot and causing the switch to fail on permanently.

This extract revision is what I want, but can't be made to work

c1 = (DPtot > 0) and (D2P > 0) and (OFFSET(D2P, OFFSET=1) > 0)

c2 = (DPtot <= 0);

TRADING = SWITCH( c1 , c2 );

NOTE:

  1. If I properly terminate the c1 line with ; the 'script valid' fails and I can't run it.
  2. If I leave the c1 line terminator ; out the script is valid but fails badly: the plot of the TRADING variable disappears.

Final questions

  1. If both switch signals are true, which dominates the result? In logic gates in electronics and other software systems, 'flip-flop' logic can be 'set' or 'reset' - dominant by choice when both signals are on. How does that work in optuma?
  2. Ancillary issue: Property selection fails in the script editor, e.g. if I type in Plot1. I get no options for after the dot. Or any other information. This makes it really hard to know what properties exist, their exact names and what settings they can be (e.g. the names of colours). In the Optuma 101 videos this feature is shown working, but it doesn't work when I use it.

Once again I expect this is just me (I have viewed all the video I can find on the SWITCH and looked at the knowledge base, to no avail) .... but look forward to finding out what's going on....

Thanks again!

colin hales

Hi Colin,

Can you please provide the underlying scripts for DPtot and D2P.

We’ve done some tests here using simple stand in scripts and they appear to be working as expected, so we want to see what happens when using the same criteria you are.

Hi Matthew,

I've put the entire thing in my other thread.

FWIW here is the same script:

// create all the change components DPco = open() - close(OFFSET=1); DPoc = close() - open(); DPhh = high() - high(OFFSET=1); DPll = low() - low(OFFSET=1); DPhl = (high() - low()) * ( ((close() - low()) / (high() - low())) - 0.5); // create the total change DPtot = DPco + DPoc + DPhh + DPll + DPhl; // compute the change compared to previous bar D2P = DPtot - OFFSET(DPtot, OFFSET=1); //c1 = (DPtot > 0) and (D2P > 0) and (OFFSET(D2P, OFFSET=1) > 0) //c2 = (DPtot <= 0) ; //TRADING = SWITCH( c1 , c2 ); TRADING = SWITCH( (DPtot > 0) and (D2P > 0) and (OFFSET(D2P, OFFSET=1) > 0), (DPtot <= 0) ); BUYPRICE = IF( TRADING, (IF(OFFSET(TRADING, OFFSET=1) , BUYPRICE, ( (close() + open() + high() + low())/4.0) ) ), 0); SELLPRICE = IF( TRADING, 0, (IF(OFFSET(TRADING, OFFSET=1), ( (close() + open() + high() + low())/4.0 ), 0) )); //THISPROFIT = IF( TRADING, 0, (IF(OFFSET(TRADING, OFFSET=1), (SELLPRICE - BUYPRICE), 0) )); TOTPROFIT = OFFSET(TOTPROFIT, OFFSET=1) + THISPROFIT; //Plots plot1 = DPtot; plot1.Plotstyle = step; plot1.colour = green; plot1.visible = 1; plot2 = D2P; plot2.Plotstyle = histogram; plot2.colour = red; plot3 = TRADING; plot3.Plotstyle = step; plot3.colour = black; plot3.linewidth = 2; plot3.visible = 1; plot4 = BUYPRICE; plot4.Plotstyle = step; plot4.visible = 1; plot5 = THISPROFIT; plot5.Plotstyle = step; plot5.linewidth = 2; plot5.visible = 1; plot6 = SELLPRICE; plot6.Plotstyle = step; plot6.linewidth = 2; plot6.visible = 1; plot7 = TOTPROFIT; plot7.Plotstyle = step; plot7.linewidth = 2; plot7.visible = 1;

====================

This covers both my pending queries.

I'm really keen to get this sorted. I hope it's simple!

cheers

colin

 

Hi Colin,

Thank you for providing this information.

There appears to be an issue related to the order of the plot lines. The developers are looking into it now, but in the meantime we were able to work around the issue by modifying the script to the following:

 

// create all the change components
DPco = open() - close(OFFSET=1);
DPoc = close() - open();
DPhh = high() - high(OFFSET=1);
DPll = low() - low(OFFSET=1);
DPhl = (high() - low()) * ( ((close() - low()) / (high() - low())) - 0.5);
// create the total change 
DPtot = DPco + DPoc + DPhh + DPll + DPhl;
// compute the change compared to previous bar 
D2P = DPtot - OFFSET(DPtot, OFFSET=1);
plot1 = DPtot;
plot2 = D2P;
c1 = (DPtot > 0) and (D2P > 0) and (OFFSET(D2P, OFFSET=1) > 0);
c2 = (DPtot <= 0) ; TRADING = SWITCH( c1 , c2 ); //TRADING = SWITCH( (DPtot > 0) and (D2P > 0) and (OFFSET(D2P, OFFSET=1) > 0), (DPtot <= 0) );
BUYPRICE = IF( TRADING, (IF(OFFSET(TRADING, OFFSET=1) , BUYPRICE, ( (close() + open() + high() + low())/4.0) ) ), 0);
SELLPRICE = IF( TRADING, 0, (IF(OFFSET(TRADING, OFFSET=1), ( (close() + open() + high() + low())/4.0 ), 0) ));
THISPROFIT = IF( TRADING, 0, (IF(OFFSET(TRADING, OFFSET=1), (SELLPRICE - BUYPRICE), 0) ));
TOTPROFIT = OFFSET(TOTPROFIT, OFFSET=1) + THISPROFIT;
plot3 = TRADING;
plot4 = BUYPRICE;
plot6 = SELLPRICE;
plot5 = THISPROFIT;
plot7 = TOTPROFIT;
plot1.Plotstyle = Step;
plot1.colour = green;
plot2.Plotstyle = Histogram;
plot2.colour = red;
plot3.Plotstyle = step;
plot3.colour = black;
plot3.linewidth = 2;
plot4.Plotstyle = step;
plot5.Plotstyle = step;
plot5.linewidth = 2;
plot6.Plotstyle = step;
plot6.linewidth = 2;
plot7.Plotstyle = step;
plot7.linewidth = 2;

You can see the position of Plot1 and Plot2 were moved higher into the script, this allowed it to be saved. We also removed some of the dot notations like .visible which do not exist (we’re also investigating the plot notation functions).

With regards to your earlier query yesterday, the following table can be used as a reference as to how the Switch function works in each scenario:

SwitchTable

In this example P1 is the turn on condition and P2 is the turn off condition.

Once an update is available to resolve the ordering issue with the plot lines in the script i will post an update on this thread.

 

 

 

Hi Matthew,

Thanks. A great deal of understanding arose and I have completely revised my script. I see that the SWITCH function acts as a toggle when both inputs are on. You might want to update the knowledge base.

I have 1 final nasty bug. I'll put the full code at the end. The troublesome bit is this:

===============================

 

BUYPRICE = IF( Ton, THISPRICE, BUYPRICE);

plot4 = BUYPRICE;

SELLPRICE = IF( Toff, THISPRICE,0);

plot5 = SELLPRICE;

THISPROFIT = IF( ((TRADING <= 0) and Toff), (SELLPRICE - BUYPRICE), 0);

//THISPROFIT = IF( ((TRADING <= 0) and Toff), SELLPRICE -(1.0 * BUYPRICE) , 0);

plot6 = THISPROFIT;

//plot6 = 0.5 * BUYPRICE; // this plot works, BUYPRICE is as expected

etc....

====================

BUYPRICE is

  1. not surviving the trip between computation, plotting and its subsequent use in TOTPROFIT
  2. somehow being re-typed from a float to a boolean?

If I install the bold lines to plot BUYPRICE I get it plotted at 0.5 where you'd expect.

If I re-type BUYPRICE by coercing to a float by multiplying by 1.0 it still doesn't work.

No matter how I do it, if I include BUYPRICE it in TOTPROFIT IF(), it is as if it's always zero. The SELLPRICE - BUYPRICE fails.

The full code....

==========================

// create all the change components DPco = open() - close(OFFSET=1); DPoc = close() - open(); DPhh = high() - high(OFFSET=1); DPll = low() - low(OFFSET=1); DPhl = (high() - low()) * ( ((close() - low()) / (high() - low())) - 0.5); // create the total change DPtot = DPco + DPoc + DPhh + DPll + DPhl; // compute the change compared to previous bar D2P = DPtot - OFFSET(DPtot, OFFSET=1); // Special plot placement to avoid optuma bug 1 May 2018 plot1 = DPtot; plot2 = D2P; c1 = (DPtot > 0) and (D2P > 0) and (OFFSET(D2P, OFFSET=1) > 0); c2 = (DPtot <= 0) ; TRADING = SWITCH( c1 , c2 ); // Positive edge on TRADING plot3 = TRADING; Ton = (TRADING > 0) and (OFFSET(TRADING, OFFSET=1) <= 0); // Positive edge on TRADING Toff = (TRADING <= 0) and (OFFSET(TRADING, OFFSET=1) > 0); // Negative edge on TRADING THISPRICE = (close() + open() + high() + low())/4.0; BUYPRICE = IF( Ton, THISPRICE, BUYPRICE); plot4 = BUYPRICE; SELLPRICE = IF( Toff, THISPRICE,0); plot5 = SELLPRICE; THISPROFIT = IF( ((TRADING <= 0) and Toff), (SELLPRICE - BUYPRICE), 0); //THISPROFIT = IF( ((TRADING <= 0) and Toff), SELLPRICE , 0); plot6 = THISPROFIT; //plot6 = 1.0 * BUYPRICE; //TOTPROFIT = IF( TRADING, OFFSET(TOTPROFIT, OFFSET=1), (IF(OFFSET(TRADING, OFFSET=1), (OFFSET(TOTPROFIT, OFFSET=1) + THISPROFIT), OFFSET(TOTPROFIT, OFFSET=1)) )); //TOTPROFIT = OFFSET(TOTPROFIT, OFFSET=1) + THISPROFIT; //plot7 = TOTPROFIT; plot1.Plotstyle = Step; plot1.colour = green; plot2.Plotstyle = Histogram; plot2.colour = red; plot3.Plotstyle = step; plot3.colour = black; plot3.linewidth = 2; plot4.Plotstyle = step; plot4.linestyle = dash; plot5.Plotstyle = step; plot5.linewidth = 2; plot6.Plotstyle = step; plot6.Colour = lightgreen; plot6.linewidth = 2; //plot7.Plotstyle = step; //plot7.linewidth = 2;

===============================

Also: In the absense of its automation, is there anyplace I can get the DOT functions and settings for PLOT.etc = somevalue?

I'm exploring workarounds for my TOTPROFIT computation, but feel you ought to know there's something severely amiss in the script compiler/interpreter.

Or I've messed up somehow!

cheers

colin

 

Hi Matthew,

I've fiddled about with the script to locate the 'plot' scripts in the right place. The script is now working.

So I can leave off for now, except for 2 questions

  1. I'd still like to know where the plotn.this = that, this and that options are listed.
  2. I'd like to bring up a tile showing values of my plot7, plot8 and plot9 or any other variables in my script. If you can clarify whether this is possible? I've tried playing with all the available options like 'chart element' .... to no avail.

Nearly there!

regards

colin

 

 

 

Hi Colin,

There is an issue with the plot options where only the first plot is showing the full menu:

Ex3

This has been fixed in the build i am currently testing, and we hope to have it released to beta tomorrow.

With showing the values in a Watchlist of a tool like Chart Element the script will need to be adjusted. Plots are used for Show View / Show Plot they aren’t compatible with WL / Chart Element. Instead you’d modify the script to remove the plot1, plot2 references, and end the script the the specific value you are wanting to show, for example:

// create all the change components
DPco = open() - close(OFFSET=1);
DPoc = close() - open();
DPhh = high() - high(OFFSET=1);
DPll = low() - low(OFFSET=1);
DPhl = (high() - low()) * ( ((close() - low()) / (high() - low())) - 0.5);
// create the total change 
DPtot = DPco + DPoc + DPhh + DPll + DPhl;
// compute the change compared to previous bar 
D2P = DPtot - OFFSET(DPtot, OFFSET=1);
c1 = (DPtot > 0) and (D2P > 0) and (OFFSET(D2P, OFFSET=1) > 0);
c2 = (DPtot <= 0) ; TRADING = SWITCH( c1 , c2 ); //TRADING = SWITCH( (DPtot > 0) and (D2P > 0) and (OFFSET(D2P, OFFSET=1) > 0), (DPtot <= 0) );
BUYPRICE = IF( TRADING, (IF(OFFSET(TRADING, OFFSET=1) , BUYPRICE, ( (close() + open() + high() + low())/4.0) ) ), 0);
SELLPRICE = IF( TRADING, 0, (IF(OFFSET(TRADING, OFFSET=1), ( (close() + open() + high() + low())/4.0 ), 0) ));
THISPROFIT = IF( TRADING, 0, (IF(OFFSET(TRADING, OFFSET=1), (SELLPRICE - BUYPRICE), 0) ));
TOTPROFIT = OFFSET(TOTPROFIT, OFFSET=1) + THISPROFIT;
TOTPROFIT

This script used with something like Chart Element will display the current value of the TOTPROFIT (plot7) part of the script.

 

 

HI Matthew,

Thanks. I can muddle along as is. I can now start proper experiments.

Perhaps I should become a beta tester? It sounds like I'll be hammering things hard for a while yet. It may be apt.

Thanks for your help.

regards

Colin