Compiling error on combining Long/Short strategy code scripts  [SOLVED]

Questions about MultiCharts and user contributed studies.
Hemmo
Posts: 9
Joined: 28 Jun 2018
Location: Newcastle, NSW, Australia
Has thanked: 7 times
Been thanked: 1 time

Compiling error on combining Long/Short strategy code scripts

Postby Hemmo » 13 Jul 2018

I currently have a separate script for a long strategy and script for the related short strategy. I wish to combine them into the one Long/Short MultiCharts signal strategy script.

This is the combined strategy code:

Code: Select all

{
Strategy Details:
Symbol: U1
Market 2: U2
Market 3: U3
Start Date: 20161002
Stop Date: 20170930
Out of Sample: 30 %
Fitness Function: PNL
Profit Target On: Yes
Profit Multiple: 2
Stop Loss On: Yes
Stop Loss Multiple: 2
Highest High On: No
Highest High Lookback: 0
Lowest Low On: No
Lowest Low Lookback: 0
Max Time: 1000000
Profitable Closes: 1000000
}

// ------------------------------------------
// declarations
//
variables: long_on(1), ATR(0), PT_ON(1), SL_ON(1), PT(0), SL(0), HH(0), LL(0);
variables: long_on(0), ATR(0), PT_ON(1), SL_ON(1), PT(0), SL(0), HH(0), LL(0);
variables: tt (0), max_time(1000000), profitable_closes(1000000), prof_x(0);
atr = avgTrueRange(8);
tt = totaltrades;


// -----------------------------------------
// entry
//
Condition1 = open[0] > average(close,8) and open[0] > average(close,200) and high[0] < average(close,3) and low[0] > average(close,200);
If condition1 and (Marketposition = 0 or (max_time - barssinceentry = 0) or (profitable_closes - prof_x = 1 and close >= close[1]) or (high >= PT and PT_ON = 1) or (high >= highest(h,0) and hh = 1) or (low <= SL and SL_on = 1) or (Low <= lowest(low,0) and LL = 1)) then begin
PT = close + atr * 2.00;
SL = close - atr * 2.00;
prof_x = 0;
end;
if condition1 = true and long_on = 1 then buy ("Entry") 100000 contracts this bar close;

Condition2 = open[0] > average(close,8) and open[0] > average(close,200) and low[0] > average(close,3) and low[0] < average(close,200);
If condition2 and (Marketposition = 0 or (max_time - barssinceentry = 0) or (profitable_closes - prof_x = 1 and close <= close[1]) or (low <= PT and PT_ON = 1) or (high >= highest(h,0) and hh = 1) or (high >= SL and SL_on = 1) or (Low <= lowest(low,0) and LL = 1)) then begin
PT = close - atr * 2.00;
SL = close + atr * 2.00;
prof_x = 0;
end;
if condition2 and long_on <> 1 then sellshort ("EntryS") 100000 contracts this bar close;


// ------------------------------------------
// exits
//
if barssinceentry > 0 and long_on = 1 and close >= entryprice then prof_x += 1;
if prof_x >= profitable_closes then sell ("ProfX") all contracts this bar close;
if barssinceentry > max_time-1 then sell ("TimeX") all contracts this bar close;
if HH = 1 and long_on = 1 then sell ("HHx") all contracts next bar at highest(h,0) limit;
if LL = 1 and long_on = 1 then sell ("LLx") all contracts next bar at lowest(l,0) stop;
if PT_ON = 1 and long_on = 1 then sell ("PTx") all contracts next bar at PT limit;
if SL_ON = 1 and long_on = 1 then sell ("SLx") all contracts next bar at SL stop;
// if marketposition = 1 and time >= 1555 then sell all contracts this bar close;

if barssinceentry > 0 and long_on <> 1 and close <= entryprice then prof_x += 1;
if prof_x >= profitable_closes then buyToCover("ProfXs") all contracts this bar close;
if barssinceentry > max_time-1 then buyToCover("TimeXs") all contracts this bar close;
if HH = 1 and long_on <> 1 then buyToCover ("HHxs") all contracts next bar at highest(h,0) stop;
if LL = 1 and long_on <> 1 then buyToCover ("LLxs") all contract next bar at lowest(l,0) limit;
if PT_ON = 1 and long_on <> 1 then buyToCover ("PTxs") all contracts next bar at PT limit;
if SL_ON = 1 and long_on <> 1 then buyToCover ("SLxs") all contracts next bar at SL stop;
// if marketposition = -1 and time >= 1555 then buytocover all contracts this bar close;
When I attempt to Compile the combined strategy code in MultiCharts, the following error message is generated:
this word has already been defined
line 26, column 11

which is in reference to the bold type section of:
variables: long_on(1), ATR(0), PT_ON(1), SL_ON(1), PT(0), SL(0), HH(0), LL(0);
variables: long_on(0), ATR(0), PT_ON(1), SL_ON(1), PT(0), SL(0), HH(0), LL(0);
variables: tt (0), max_time(1000000), profitable_closes(1000000), prof_x(0);

If I remove the entire line of:

Code: Select all

variables: long_on(0), ATR(0), PT_ON(1), SL_ON(1), PT(0), SL(0), HH(0), LL(0);
I am then able to Compile the strategy, however, the back testing function does not generate any short trades. (Note, I have separately Compiled and back tested just the short strategy code in isolation, and yes, the code works for the short trades).

Can anyone provide the correct wording/approach to fix the problem?

A solution to my issue would be warmly received. Thank you.

sptrader
Posts: 742
Joined: 09 Apr 2010
Location: Texas
Has thanked: 483 times
Been thanked: 274 times
Contact:

Re: Compiling error on combining Long/Short strategy code scripts  [SOLVED]

Postby sptrader » 13 Jul 2018

I think the problem is that you have the variable "long_on" defined twice, each with different default values. ( 1 and 0 ).
You should give it a slightly different name as a variable and in the rest of the code.
I always use unique variables on each side of a signal. (long and short entries are treated separately and should be coded separately).
I'm not a professional programmer but that's my guess.
Good trading.


Return to “MultiCharts”