reuse Trailing Stop (TS) while holding a position

Questions about MultiCharts and user contributed studies.
kernel
Posts: 91
Joined: 19 Feb 2013
Has thanked: 21 times
Been thanked: 4 times

reuse Trailing Stop (TS) while holding a position

Postby kernel » 04 Aug 2016

Hi,

I'd like to reuse trailing stop (TS) mechanism in my code as demonstrated in the following pseudo-code. The code shows only the short part. TS.S refers to Trailing Stop (price) for Short. However, the trailing stop works just fine only for the first time it gets hit from below. As market keeps falling, new trailing stop price is calculated, however, even after this newly calculated trailing stop gets hit again and again from below, it never triggers the "buytocover" line. However, "KKKK" does get printed each time when price crosses above the trailing stop price. it's just that "buytocover" line doesn't work.

Just to test the code, I replace the "buytocover" line with "sellshort 1 contract next bar at market;", nothing happens even when the TS line gets hit for the first time, however, I do get "KKKK" printed after this line.

Any inputs? Thanks!

Code: Select all

[IntrabarOrderGeneration = TRUE]



if marketposition=-1 then begin

//calculate Trailing Stop price line
..............

TS.S= XXXXX;

..............



if C>TS.S and TS.S.triggered=false then begin
buytocover 1 contracts next bar at market;
TS.S.triggered=true;
print("KKKK");
end;


//after TS gets hit

if TS.S.triggered then begin

//do some processing right after TS gets hit

TS.S.triggered=false;

end;

end;


//when flat, clear all variables
if marketposition=0 then begin

TS.S=999999;
TS.S.triggered=false;

.......
end;


-kernel
Last edited by kernel on 06 Aug 2016, edited 1 time in total.

User avatar
bensat
Posts: 331
Joined: 04 Oct 2014
Has thanked: 46 times
Been thanked: 104 times

Re: reuse Trailing Stop (TS)

Postby bensat » 05 Aug 2016

First I would suggest to avoid & get rid of unnecessary IF-THEN statements ....

Code: Select all



//after TS gets hit

if TS.S.triggered then begin

//do some processing right after TS gets hit

TS.S.triggered=false;

end;
By the way, why you need to set a boolean data type to force an exit of 'marketposition=(-1)', if 'marketposition=(0)' won't allow a 'buytocover' or 'sell' ????

Regards

Ben

kernel
Posts: 91
Joined: 19 Feb 2013
Has thanked: 21 times
Been thanked: 4 times

Re: reuse Trailing Stop (TS)

Postby kernel » 05 Aug 2016

Ben,

Like I said in my first post, I'd like to be able to reuse this trailing stop mechanism, the boolean variable serves as a toggle and monitors if the trailing stop has been hit.

Since the "KKKK" gets printed, the "buytocover" line has been executed for sure. But for some reason, no contract is sold, my short position size remain intact.

why you need to set a boolean data type to force an exit of 'marketposition=(-1)', if 'marketposition=(0)' won't allow a 'buytocover' or 'sell' ????

Regards

Ben

User avatar
bensat
Posts: 331
Joined: 04 Oct 2014
Has thanked: 46 times
Been thanked: 104 times

Re: reuse Trailing Stop (TS)

Postby bensat » 05 Aug 2016

Code: Select all

if marketposition=-1 then begin
assumes you have a <SHORT> - position

Code: Select all

TS.S= XXXXX;
defines the price for the <TRAILINGSTOP SHORT>

Code: Select all

if C>TS.S then buytocover 1 contracts next bar at market;
commands to cover <SHORT> if close is greater TRAILINGSTOP SHORT >> so trailingsstop must have been hit and there is no true/false necessary .. otherwise system logic would not command to cover and 'marketposition' stays as (-1)

Code: Select all

//after TS gets hit

if TS.S.triggered then begin

//do some processing right after TS gets hit
TS.S.triggered=false;

end;
Sets your var already from 'triggered' to 'not triggered' before you define any action for a new <SHORT>. So the 'algo' thinks the trailing stop was not triggered so therefore I can't get short again. This is 100% unnecessary and wrong.

Code: Select all

//when flat, clear all variables
if marketposition=0 then begin

TS.S=999999;
TS.S.triggered=false;

.......
end;
You again set the var to non-triggered. From the logic the trailing stop must have been triggered otherwise you would not have status for marketposition = 0. This is 100% unnecessary.


..... just a simple trailing stop example from my side for <Shorts> only. No true/false needed. Further I would like to recommend to separate entries/exits in different signals.

Code: Select all

inputs: p1( 12 ), p2( 26 ), p3( 9 ), diff_tsl( 3 ), diff_tss( 3 ) ;
vars: var0( 0 ), var1( 0 ), var2( 0 ) ;
vars: trss( 0 ), trsl( 0 );

// MACD
var0 = MACD( close, p1, p2 ) ;
var1 = XAverage( var0, p3 ) ;
var2 = var0 - var1 ;

condition1 = CurrentBar > 2 and var2 crosses over 0 ;
condition2 = CurrentBar > 2 and var2 crosses under 0 ;

if marketposition = (0) then
begin
// Rest TrailingStops Long/Short
trss = 99999999;
trsl = 0;

// Long Entry
//if condition1 then buy ( "MacdLE" ) next bar at market ;

// Short Entry
if condition2 then sell short ( "MacdSE" ) next bar at market ;
end;

// TrailingStop Short
if marketposition = (-1) then
begin
trss = low + diff_tss; //
if trss > trss[1] then trss = trss[1];

if close > trss then buytocover next bar at market;
end;

// TrailingStop Long
if marketposition = (1) then
begin
trsl = high - diff_tsl;
if trsl < trsl[1] then trsl = trsl[1];

if close < trsl then sell next bar at market;
end;

But I may just misunderstood you and I'm on the wrong way here. If so than sorry in advance.


Regards

Ben

kernel
Posts: 91
Joined: 19 Feb 2013
Has thanked: 21 times
Been thanked: 4 times

Re: reuse Trailing Stop (TS) while holding a position

Postby kernel » 06 Aug 2016

Ben,

Sorry I failed to make it clear that this trailing stop mechanism is intended to be reused while holding a position. When market falls to a certain amount, the TS.S is set. If it triggers, only PARTIAL position exits. After that, TS.S is set to 999999 (reset). If certain market condition meets, this TS.S will be set to a new value. If this newly set TS.S triggers again, another portion of my position will be liquidated...and so on.

-Kernel

User avatar
bensat
Posts: 331
Joined: 04 Oct 2014
Has thanked: 46 times
Been thanked: 104 times

Re: reuse Trailing Stop (TS) while holding a position

Postby bensat » 06 Aug 2016

I thought so you will come up with multi-exits. But I do not see any difference as you need to filter your further actions regarding current contracts are held only. But you have to realize it by yourself, at least you have to want to.

Further, as suggested previously, I would take all the different entries & exits in separated signals as 'modules'. Much easier to work with and makes the implementation of your ideas, modifications etc much much easier and less prone for mistakes.

My Regards

Ben


Return to “MultiCharts”