Script - flatting the position in backtest  [SOLVED]

Questions about MultiCharts and user contributed studies.
HHHenry
Posts: 7
Joined: 09 May 2013
Has thanked: 2 times

Script - flatting the position in backtest

Postby HHHenry » 21 May 2013

Hey guys,

I'm new to MC and hv just encountered some problem in scripting,
please help.

let say if my buy signal triggered and currently holding 1 long contract,
if i want to exit:
1. when high of current bar crossing var1, or
2. when low hits stoploss,

Code: Select all

if marketposition>0 then begin
if high>var1 then sell ("sell") next bar at var1 stop;
setstoploss(10*10);
end;
how should I modify above code?
please advise.

Thanks

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

Re: Script - flatting the position in backtest

Postby TJ » 22 May 2013

Hey guys,

I'm new to MC and hv just encountered some problem in scripting,
please help.

let say if my buy signal triggered and currently holding 1 long contract,
if i want to exit:
1. when high of current bar crossing var1, or
2. when low hits stoploss,

Code: Select all

if marketposition>0 then begin
if high>var1 then sell ("sell") next bar at var1 stop;
setstoploss(10*10);
end;
how should I modify above code?
please advise.

Thanks
Please detail the "problem" you are encountering.


see post #5
viewtopic.php?f=16&t=10811

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Script - flatting the position in backtest

Postby Andrew MultiCharts » 22 May 2013

let say if my buy signal triggered and currently holding 1 long contract,
if i want to exit:
1. when high of current bar crossing var1, or
If you want to exit instantly when the described condition is met, you should be using IOG + 1 tick BM to achive this goal witht he following code:

Code: Select all

[IntrabarOrderGeneration=true]
Vars: var1(0);
if marketposition > 0 then begin
If var1 crosses above high then sell ("sell") next bar market;
end;
If you want to do this with a stop order, then should differentiate 2 things:
1. Place stop order, when particular conditions are met;
2. Place stop order with a particular price.
In your example you place stop order when your conditions are met, but it doesn't guarantee the order will be executed right after that.
2. when low hits stoploss,
The condition is not clear. If you mean "when stop loss level is hit", then your initial SetStopLoss variant is ok.

HHHenry
Posts: 7
Joined: 09 May 2013
Has thanked: 2 times

Re: Script - flatting the position in backtest

Postby HHHenry » 22 May 2013

hi TJ,
Thanks for the link!

hi Andrew,
Just wonder, if i turn on IOG and use code "sell next bar at market",
then the sell order will be executed at next tick in live trading?
and the sell order will be executed at next open in back-test?

secondly,
If i want to set a stop loss order(placing a market order to flat all existing position) once the price went down X pts below my entry point, how should I write the script?

many thx

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Script - flatting the position in backtest

Postby Andrew MultiCharts » 23 May 2013

Just wonder, if i turn on IOG and use code "sell next bar at market",
then the sell order will be executed at next tick in live trading?...
Yes, correct.
...and the sell order will be executed at next open in back-test?
On nearest Open/High/Low/Close, if Bar Magnifier is not used(see the Calculation on historical data. IOG enabled. Bar Magnifier disabled), depends on when it was generated.
If you use Bar Magnifier set to, let's say 1 tick, the market order will be executed on next tick (see Calculation on historical data. IOG enabled. Bar Magnifier enabled).
secondly,
If i want to set a stop loss order(placing a market order to flat all existing position) once the price went down X pts below my entry point, how should I write the script?
Auto trading/forwardtesting/backtesting?
Use EntryPrice and the combination of Point * MinMove (this way you get 1 tick step) keywords to calculate the level and once this level is met, place your market order.

HHHenry
Posts: 7
Joined: 09 May 2013
Has thanked: 2 times

Re: Script - flatting the position in backtest

Postby HHHenry » 27 May 2013

Thanks Andrew,

Could you please take a look of my script and point out any wrong scripting?
Here I just post long side only:

i will be using 1min chart to do live trading,
so i expect to buy 1 contract per minute (at the price of var1) as long as low<=var1.

Code: Select all

if low <= var1 then buy next bar at var1 limit;
if
then i want to close out ALL the existing position at market when high hits var2

Code: Select all

if marketposition>0 and high cross above var2 then sell all contract next bar at var2 market;
or the long position will exit at entryprice-50

Code: Select all

if marketposition>0 then sell next bar at entryprice-50 stop;
btw could you please tell me how to clarify each entryprice as I wish to exit 50 pts below each entryprice for the corresponding contract.


many thanks!

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Script - flatting the position in backtest

Postby Andrew MultiCharts » 28 May 2013

Could you please take a look of my script and point out any wrong scripting?
Here I just post long side only:

i will be using 1min chart to do live trading,
so i expect to buy 1 contract per minute (at the price of var1) as long as low<=var1.

Code: Select all

if low <= var1 then buy next bar at var1 limit;
if
then i want to close out ALL the existing position at market when high hits var2

Code: Select all

if marketposition>0 and high cross above var2 then sell all contract next bar at var2 market;
or the long position will exit at entryprice-50

Code: Select all

if marketposition>0 then sell next bar at entryprice-50 stop;
Dear HHHenry, the code will be working the way you write it. Unfortunately we don't provide custom scripting and debugging for free. The script above may work slightly different from your description. You will see it, if you check when your conditions are met and trace chart prices and compare to orders prices.
btw could you please tell me how to clarify each entryprice as I wish to exit 50 pts below each entryprice for the corresponding contract.
The PosTradeEntryPrice should be helpful.

HHHenry
Posts: 7
Joined: 09 May 2013
Has thanked: 2 times

Re: Script - flatting the position in backtest

Postby HHHenry » 28 May 2013

Understood Andrew, thanks for the comment.
Will make use of PosTradeEntryPrice as recommended.

One last question:
what commend should I use to generate log of recording the movement/calculation of the signal?

Thanks

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Script - flatting the position in backtest  [SOLVED]

Postby Andrew MultiCharts » 29 May 2013

One last question:
what commend should I use to generate log of recording the movement/calculation of the signal?
Print or FileAppend. This article contains a good example.


Return to “MultiCharts”