Is this the right code for SL and TP?  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
TopGuy
Posts: 16
Joined: 20 Nov 2012
Has thanked: 1 time
Been thanked: 7 times

Is this the right code for SL and TP?

Postby TopGuy » 25 Nov 2012

I did investigate a lot, but I couldn't find a straight forward example of Stop Loss and Take Profit.

Could anybody confirm that this code would do the job?

Code: Select all

protected override void Create()
{
_buyOrder = OrderCreator.MarketNextBar(
new SOrderParameters(Contracts.Default, "Buy", EOrderAction.Buy));
_shortOrder = OrderCreator.MarketNextBar(
new SOrderParameters(Contracts.Default, "Short", EOrderAction.SellShort));
}

protected override void CalcBar()
{
// some code
if (trendDirection)
{
_buyOrder.Send(1);
double currentPosition = Bars.Close[0];
GenerateStopLoss(currentPosition - _sl);
GenerateProfitTarget(currentPosition + _tp);
}
else
{
_shortOrder.Send(1);
double currentPosition = Bars.Close[0];
GenerateStopLoss(currentPosition + _sl);
GenerateProfitTarget(currentPosition - _tp);
}
}

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Is this the right code for SL and TP?

Postby Henry MultiСharts » 26 Nov 2012

There are prebuilt signals called Stop_Loss and Profit_Target. You can use them as examples.

TopGuy
Posts: 16
Joined: 20 Nov 2012
Has thanked: 1 time
Been thanked: 7 times

Re: Is this the right code for SL and TP?

Postby TopGuy » 26 Nov 2012

Henry,

I did read them but I am afraid they have few lines of code that explains nothing. Not even a comment line that explains what they are trying to achieve.

Thank you.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Is this the right code for SL and TP?  [SOLVED]

Postby Henry MultiСharts » 28 Nov 2012

TopGuy, the code from post #1 should work ok.
Please let me know what exact aspects of Stop_Loss and Profit_Target you want to be clarified.

MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Re: Is this the right code for SL and TP?

Postby MidKnight » 28 Nov 2012

I could be wrong.....but from looking at the MC.net docs, generateStopLoss() wants an amount, but it looks like Topguy wants to specify a price. I've actually wanted to specify a price as well. Can you clarify this Henry? If it currently does not allow to specify a price, then it would be highly useful to add that ability for both stop and target orders.

With kind regards,
MK

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Is this the right code for SL and TP?

Postby Henry MultiСharts » 29 Nov 2012

Thank you for your observation MidKnight.
Indeed, with these orders you need to specify not the price but the profit or loss amount.

EdL
Posts: 39
Joined: 18 Feb 2013
Has thanked: 22 times
Been thanked: 11 times

Re: Is this the right code for SL and TP?

Postby EdL » 26 Feb 2013

I'm trying to use the GenerateStopLoss(amt) method on a FOREX symbol.

The example signals Stop_Loss and Profit_Target apply the stop like this:

e.g.
GenerateStopLoss(this.EntryPrice() * _stopLossPercent)

However, my stops are coming up incorrectly - they are way too small.

The FOREX symbol in question has the following properties:

MinMove = 1
PriceScale = 10000
BigPointValue = 100000

It seems that to get the correct stop amount, I need to multiply by the BigPointValue:

e.g.
GenerateStopLoss(this.EntryPrice() * _stopLossPercent * Bars.Info.BigPointValue);

Is this the correct way to apply a stop for this symbol? If not, what is the correct way?
If it IS the correct way, is it the correct way in all situations for all symbols?
Why do the example signals not do it this way?

Many thanks,

Ed

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Is this the right code for SL and TP?

Postby Henry MultiСharts » 27 Feb 2013

Hello Ed,

GenerateStopLoss - Closes out the entire position or the entry if the loss reaches the specified currency value (Amount input). The amount is specified in curency, not in percent.

EdL
Posts: 39
Joined: 18 Feb 2013
Has thanked: 22 times
Been thanked: 11 times

Re: Is this the right code for SL and TP?

Postby EdL » 27 Feb 2013

Hi Henry,

Thanks for your swift response. Unfortunately there are still a couple of things unanswered.
I'm aware that the GenerateStopLoss(amt) takes an amount rather than a percent, as my examples show.

I'm using a percentage of the entry price as my loss amount, in the same way the example signals do. My question is relating to the BigPointValue that I seem to need to multiply my amount by in order for my stop to work.

I'm sorry if I'm missing a basic point here, but maybe to clarify, could you help answer the 4 questions below?
The FOREX symbol in question has the following properties:

MinMove = 1
PriceScale = 10000
BigPointValue = 100000

It seems that to get the correct stop amount, I need to multiply by the BigPointValue:

e.g.
GenerateStopLoss(this.EntryPrice() * _stopLossPercent * Bars.Info.BigPointValue);
Question1: Is this the correct way to apply a stop for this symbol?

Question 2: If not, what is the correct way?

Question 3: If it IS the correct way, is it the correct way in all situations for all symbols?
i.e. should they all be multiplied by the BigPointValue?

Question 4: Why do the example signals not do it this way?

Many thanks,

Ed

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Is this the right code for SL and TP?

Postby Henry MultiСharts » 28 Feb 2013

When you multiply EntryPrice() by _stopLossPercent you get the "amount" value in the chart price. In order to get the "amount" value in real currency you need to have your (EntryPrice() * _stopLossPercent) multiplied by big point value.

1 minimum price increment of the chart = Min.Movement * Price Scale.
1 minimum price increment in real currency = Min.Movement * Price Scale * Big Pont Value.
As the easiest example let's take E-Mini S&P = 1 * 1/4 * 50 = $12.5

1&3) Your approach (GenerateStopLoss(this.EntryPrice() * _stopLossPercent * Bars.Info.BigPointValue)) is correct when you are doing calculations with chart price values in your code, but your final goal is the real money value.
4) Prebuilt signal like Stop_Loss gets real currency value directly through amount input. That is why it does not need additional multiplication.

EdL
Posts: 39
Joined: 18 Feb 2013
Has thanked: 22 times
Been thanked: 11 times

Re: Is this the right code for SL and TP?

Postby EdL » 28 Feb 2013

Hi Henry that's much clearer now. Just what I was after.

Many thanks,

Ed


Return to “MultiCharts .NET”