Stop loss created immediately and not after one candles

Questions about MultiCharts and user contributed studies.
Capitanicoraggiosi3
Posts: 11
Joined: 07 Mar 2017
Has thanked: 2 times

Stop loss created immediately and not after one candles

Postby Capitanicoraggiosi3 » 22 Mar 2017

Hi, I'm facing difficulties to let the Stoploss and Take profit to be added immediately when an order is generated.

I've tried with this code:

Code: Select all

[IntrabarOrderGeneration=true]
if marketposition = 0 and EntriesToday(date)=0 then buy 1000 contracts next bar at market;
sell next bar at close-averagetruerange(3) stop;
and when the order is taken the StopLoss is created immediately but if I try also with the same code without the line
" [IntrabarOrderGeneration=true]" I get the same result. Why?

Second question... why in this code the Stoploss is generated only at the opening of the next candle?

Code: Select all

input: ptnshort(18), mystop(200), myprofit(500), FixedRatio(true),deltas (550),profittopassato(-416.88);
Inputs: ControlloOgniSec(30);

Variables:
IntraBarPersist PrevATSStatus(0), atsStatus(0), m0(0), m1(0);

var: mycontracts(1);

if (LastBarOnChart_s = True) then begin

atsStatus = GetAppInfo(aiStrategyAuto);

if (atsStatus <> PrevATSStatus) and (PrevATSStatus = 1) then begin

Alert(Text("STOPPATOOOOO"));
end;

PrevATSStatus = atsStatus;

RecalcLastBarAfter(ControlloOgniSec);
end;


if time = 0915 and ptnbasesa(1) and PtnBaseSA(ptnshort) then begin
sellshort mycontracts contract next bar at market;
end;


if time = 1720 and marketposition <> 0 then begin
buytocover next bar at market;
end;

setstopcontract;
if mystop > 0 then setstoploss(mystop);
If myprofit > 0 then setprofittarget(myprofit);
Is the IntrabarOrderGeneration affected if I run in AA o SA mode?

thanks a lot.

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

Re: Stop loss created immediately and not after one candles

Postby TJ » 22 Mar 2017

marketposition is updated at the end of the bar.

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

Re: Stop loss created immediately and not after one candles

Postby TJ » 23 Mar 2017

Hi, I'm facing difficulties to let the Stoploss and Take profit to be added immediately when an order is generated.
::

For immediate action, you can consider these keywords:

SetBreakEven
SetBreakEven pt
SetDollarTrailing
SetPercentTrailing
SetPercentTrailing pt
SetProfitTarget
SetProfitTarget pt
SetStopContract
SetStopLoss
SetStopLoss pt
SetStopPosition
SetStopShare

escamillo
Posts: 203
Joined: 25 Mar 2011
Has thanked: 23 times
Been thanked: 56 times

Re: Stop loss created immediately and not after one candles

Postby escamillo » 23 Mar 2017

.
Last edited by escamillo on 29 Mar 2017, edited 1 time in total.

Capitanicoraggiosi3
Posts: 11
Joined: 07 Mar 2017
Has thanked: 2 times

Re: Stop loss created immediately and not after one candles

Postby Capitanicoraggiosi3 » 24 Mar 2017

Hi Escamillo, are you telling me that I should put my exit strategy below "SetStopcontract;" will this change the way of working?

I am confused because I have tested this strategy:

Code: Select all

if marketposition = 0 then buy 1000 contracts next bar at market;

if marketposition = 1 then begin
sell ("Exit 1") next bar at close-(averagetruerange(3)*2) stop;
end;
if marketposition = 0 then begin
sell ("Exit 0") next bar at close-(averagetruerange(3)*2) stop;
end;

if marketposition = -1 then begin
sell ("Exit -1") next bar at close-(averagetruerange(3)*2) stop;
end;


setstopcontract;
setstoploss(averagetruerange(2)*3);
setprofittarget(averagetruerange(2)*3);
as you can see in the picture the "Exit 1" has been created immediately right after the order has been created.
1.png
(51.24 KiB) Downloaded 958 times
@TJ, thanks also to you but here it seems that "marketposition" change immediately and not when a new candle has been created.

so my question is... why in the code that I have posted up it work as I would like and in this one doesn't and put the exit strategy after one bar?

Code: Select all


//Candela Inversiva Trailing ATR - Works using 60-minute
Inputs: ControlloOgniSec(5);
inputs:
MyStartTime(0), myendtime(2400),
MyProfit(0.06),Mystoplossn (0.0075),gap(0),
Length( 20 ), NumDevs( 2), mycontracts(20000);

variables:
UpperBand( 0 ),LowerBand( 0 ), Middleband(0),
mystop(0), candelalong(false), candelashort(false),PrevATSStatus(0), atsStatus(0);
input: par_atr(9); //Parametro ATR per stop
var: newstoplong(0),valstoplong(0),m0(0),m1(0);
var: newstopshort(0), valstopshort (0);

if (LastBarOnChart_s = True) then begin

atsStatus = GetAppInfo(aiStrategyAuto);

if (atsStatus <> PrevATSStatus) and (PrevATSStatus = 1) then begin

Alert(Text("STOPPATOOOOO"));
end;

PrevATSStatus = atsStatus;

RecalcLastBarAfter(ControlloOgniSec);
end;




UpperBand = BollingerBand( c, Length, +NumDevs ) ;
LowerBand = BollingerBand( c, Length, -NumDevs ) ;
Middleband = (Upperband + Lowerband) / 2;

candelalong = close[1]<lowerband and close[1]<open[1] and close>open and low>low[1] and close>lowerband;

candelashort = close[1]>upperband and close[1]>open[1] and close<open and high<high[1] and close<upperband;

if time >= MyStartTime and time <=myendtime and candelalong=true then begin
buy mycontracts contracts next bar at high stop;
mystop = low[1]-gap;
valstoplong = close - (AvgTrueRange(14)*par_atr);

End;
if time >= MyStartTime and time <=myendtime and candelashort=true then begin
sellshort mycontracts contracts next bar at low stop;
valstopshort = close + (AvgTrueRange(14)*par_atr);
mystop = high[1]+gap;
End;


if marketposition = 1 then begin
newstoplong = close - (AvgTrueRange(12)*par_atr);
if newstoplong > valstoplong then valstoplong = newstoplong;
sell ("Exit L") next bar at maxlist(valstoplong,mystop) stop;
end;


if marketposition = -1 then begin
newstopshort = close + (AvgTrueRange(12)*par_atr);
if newstopshort < valstopshort then valstopshort = newstopshort;
buytocover ("Exit S") next bar at minlist(valstopshort, mystop) stop;
end;

setstopcontract;
if mystoplossn > 0 then setstoploss(mystoplossn);
If MyProfit > 0 then setprofittarget(MyProfit);



//RecalcLastBarAfter(ControlloOgniSec);

if marketposition = 0 then m0=0;
if marketposition = 1 then m0=1;
if marketposition = -1 then m0=-1;
m1=m0[1];

if m1=0 and m0=1 then alert(Text("Apertura posizione Long di",mycontracts, " contratti al prezzo di ", avgentryprice));
if m1=1 and m0=0 then alert(Text("Chiusa posizione Long di",mycontracts, " contratti con P/L di ", positionprofit(1)));
if m1=0 and m0=-1 then alert(Text("Apertura posizione Short di",mycontracts, " contratti al prezzo di ", avgentryprice));
if m1=-1 and m0=0 then alert(Text("Chiusa posizione Short di",mycontracts, " contratti con P/L di ", positionprofit(1)));

2.png
(26.05 KiB) Downloaded 958 times

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Stop loss created immediately and not after one candles

Postby JoshM » 26 Mar 2017

so my question is... why in the code that I have posted up it work as I would like and in this one doesn't and put the exit strategy after one bar?
In your second example you don't seem to use intra-bar order generation, while in your first example (at the top of this thread) you did. Now this doesn't necessarily mean that this is caused by intra-bar order generation because that setting can also be enabled by hand, and so we can't always see it when just looking at the code.

However, if you use intra-bar order generation, then the MultiCharts strategy calculates with every incoming real-time tick. With intra-bar order generation turned off (the default behaviour), the strategy calculates once per bar (at bar close).

If one of your signals uses intra-bar order generation, then it can submit a stop-loss order immediately after opening a position. Without intra-bar order generation, the signal will have to wait till the bar closes before it can calculate (and generate a stop-loss order during that calculation).

See intra-bar order generation for more. Here's information on setting intra-bar order generation with code, while this wiki page discusses configuring that option by hand.


Return to “MultiCharts”