Need help for simple script coding

Questions about MultiCharts and user contributed studies.
treksis
Posts: 13
Joined: 18 Jul 2014
Has thanked: 4 times

Need help for simple script coding

Postby treksis » 21 Jul 2014

Hi, I'm struggling to find a way to close my position when :
"Slow Stochastic crosses 80 and close < 10 day moving average. So far It is good.
BUT, in case stochastic comes below the OverBought zone(80), and close < 10 day moving average, my command won't execute because of " Slow Stochastic crosses 80 and " criteria.

So all in short, I want to find a way to close my position when slow stochastic first CROSSED 80 and ever since then,(even if stochastic comes below 80) if close < 10 day moving average, then close my position.

Thanks.

Code: Select all

inputs: Length( 14 ), OverSold( 20 );
variables: var0( 0 ), var1( 0 ), var2( 0 ), var3( 0 ) ;

Value1 = Stochastic( H, L, C, Length, 3, 3, 1, var0, var1, var2, var3 ) ;

condition1 = var2 crosses over Oversold and var2 <25 and XAverage(Close,50) > XAverage(Close,200) ;
if condition1 then
Buy ( "StochLE" ) this bar on close;


[b]////////////////////// ////////////////problem zone /////////////////////
condition2 = var2 crosses above 80;
if condition2 and close < Average(Close,10)
then
sell ( "StochLE_Profit_OUT" ) this bar on close ;


condition4 = var2 crosses over 30;
if condition4 and close < Average(Close,10)
then sell ( "StochLE_Profit_OUT1" ) this bar on close ;

///////////////////////////////////////////////////////////////////////[/b]

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

Re: Need help for simple script coding

Postby furytrader » 22 Jul 2014

Without re-writing your code, here's one way you could do it:

Create a variable called 80cross(0).

When you buy the market, set the value of this variable to 0.

For each bar that you're still in your position, you test whether the stochastic cross condition occurs. If it does, you set the value of 80cross to 1.

Next, for each bar that you're still in a position, you then test for when the close < Average(Close,10) AND 80cross = 1.

If these two conditions tie out to TRUE, you exit.

If you have problem coding this, let me know.

treksis
Posts: 13
Joined: 18 Jul 2014
Has thanked: 4 times

Re: Need help for simple script coding

Postby treksis » 22 Jul 2014

Thanks for replying
I finally completed my code by adding tons of if and else statement

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Need help for simple script coding

Postby tony » 22 Jul 2014

Thanks for replying
I finally completed my code by adding tons of if and else statement
Adding "tons" of statements sounds dangerous. Follow the MC style of programming, less is more. If your plan is to run live auto-trading, it's best to keep things as simple as possible. Less chance of an unforeseen problem resulting when real money is on the line.

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

Re: Need help for simple script coding

Postby furytrader » 22 Jul 2014

Tony is right - if you want some help on how to do that, just post it here (you can keep out the proprietary stuff).


Return to “MultiCharts”