How can I reverse a position in backtest ?  [SOLVED]

Questions about MultiCharts and user contributed studies.
maxmax68
Posts: 163
Joined: 20 Nov 2012
Has thanked: 55 times
Been thanked: 48 times

How can I reverse a position in backtest ?

Postby maxmax68 » 25 Dec 2013

Hi,
I restarted using MC after a long time, and I need your help to refresh my memory.
For testing I was trying to code a simple reverse system, and I don't remember if
is it possible to reverse directly a short position in a long position and vice versa,
without having to close short position with buytocover and open long with buy ?
Thanks for your help.
Regards
Massimo

Code: Select all

[IntrabarOrderGeneration = false]
inputs:
NumBars( 3 ), Qty(1);

variables:
MP( 0 );

once cleardebug;

MP = MarketPosition() ;

if time_s > 172300 AND MP = 1 then
begin
Sell Qty Shares Next Bar AT Market ;
end
else if time_s > 172300 AND MP = -1 then
begin
buytocover Qty Shares Next Bar AT Market;
end
else if MP = 0 AND HIGH > Highest(HIGH,NumBars)[1] then
begin
Buy Qty Shares Next Bar AT Market;
end
else if MP = 0 AND LOW < Lowest(LOW,NumBars)[1] then
begin
sellshort Qty Shares Next Bar AT Market ;
end
else if MP = -1 AND HIGH > Highest(HIGH,NumBars)[1] then
begin
buytocover Qty Shares Next Bar AT Market;
buy Qty Shares Next Bar AT Market;
end
else if MP = 1 AND LOW < Lowest(LOW,NumBars)[1] then
begin
sellshort Qty Shares Next Bar AT Market;
sell Qty Shares Next Bar AT Market ;
end;

print (MP);

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: How can I reverse a position in backtest ?  [SOLVED]

Postby TJ » 25 Dec 2013

if you have a long position,
and you trigger a SELLSHORT order,
MultiCharts will automatically reverse the position for you.

Example 1
position= 0 contract
code:

Code: Select all

SELLSHORT ("S") 1 contract next bar at market;
will result in selling 1 contracts.


Example 2
position= long 1 contract
code:

Code: Select all

SELLSHORT ("S") 1 contract next bar at market;
will result in selling 2 contracts; 1 for flattening the long, and 1 for going short.


Example 3
position= long 1 contract
code:

Code: Select all

SELLSHORT ("S") 2 contract next bar at market;
will result in selling 3 contracts; 1 for flattening the long, and 2 for going short.


Return to “MultiCharts”