setprofittarget/StopLoss - for different contracts

Questions about MultiCharts and user contributed studies.
vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

setprofittarget/StopLoss - for different contracts

Postby vking » 31 May 2011

Hello - I am trying to issue low level strategy commands to bypass EL by issuing "setprofittarget" and Stoploss commands and to understand them better with an example. Would you please review and help filling up the Psuedo code. ( Please note that I have searched the example usage for these key words but all of them related to a single contract or for full position .. In this case - it is partial position exit using setprofittarget - and I couldn't figure out how to do it for partial contracts).

Let's say - I have 4 contracts long and would like:

- Sell 2 contracts for 1 tick profit
- Sell 2 contracts for 2 ticks profit
- Stop loss 8 ticks for all 4 contracts

Can someone help with pseudo code for achieving it with the help of "setprofittarget" ( wanted to avoid "sell limit" or "sell stop" commands if possible).

Thanks
----

Code: Select all

Vars: FirstPass(True);

if FirstPass and Marketposition=0 then begin
        buy 4 contracts next bar market;
end;

if MarketPosition<>0 then begin
      FirstPass=False;
      <... Stoploss 8 ticks for all contracts .. using ....any of the other keywords
      SetStopPosition.. or SetStopContract.. or ..SetStopLoss...>

      <....Sell 2 contracts at 1 tick profit - using setprofittarget code....>

      <...Sell 2 contracts at 2 ticks profilt - using setprofittarget code...>
end;


User avatar
TJ
Posts: 6550
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 965 times
Been thanked: 1892 times

Re: setprofittarget/StopLoss - for different contracts

Postby TJ » 31 May 2011

vking wrote:Hello - I am trying to issue low level strategy commands to bypass EL by issuing "setprofittarget" and Stoploss commands and to understand them better with an example. Would you please review and help filling up the Psuedo code. ( Please note that I have searched the example usage for these key words but all of them related to a single contract or for full position .. In this case - it is partial position exit using setprofittarget - and I couldn't figure out how to do it for partial contracts).

Let's say - I have 4 contracts long and would like:

- Sell 2 contracts for 1 tick profit
- Sell 2 contracts for 2 ticks profit
- Stop loss 8 ticks for all 4 contracts

Can someone help with pseudo code for achieving it with the help of "setprofittarget" ( wanted to avoid "sell limit" or "sell stop" commands if possible).

Thanks
----

Code: Select all

Vars: FirstPass(True);

if FirstPass and Marketposition=0 then begin
        buy 4 contracts next bar market;
end;

if MarketPosition<>0 then begin
      FirstPass=False;
      <... Stoploss 8 ticks for all contracts .. using ....any of the other keywords
      SetStopPosition.. or SetStopContract.. or ..SetStopLoss...>

      <....Sell 2 contracts at 1 tick profit - using setprofittarget code....>

      <...Sell 2 contracts at 2 ticks profilt - using setprofittarget code...>
end;



I would suggest you to start with a flow chart.

User avatar
TJ
Posts: 6550
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 965 times
Been thanked: 1892 times

Re: setprofittarget/StopLoss - for different contracts

Postby TJ » 01 Jun 2011

vking wrote:Hello - I am trying to issue low level strategy commands to bypass EL by issuing "setprofittarget" and Stoploss commands and to understand them better with an example. Would you please review and help filling up the Psuedo code.

Let's say - I have 4 contracts long and would like:

- Sell 2 contracts for 1 tick profit
- Sell 2 contracts for 2 ticks profit
- Stop loss 8 ticks for all 4 contracts

Can someone help with pseudo code for achieving it with the help of "setprofittarget" ( wanted to avoid "sell limit" or "sell stop" commands if possible).

Thanks
----

Code: Select all

Vars: FirstPass(True);

if FirstPass and Marketposition=0 then begin
        buy 4 contracts next bar market;
end;

if MarketPosition<>0 then begin
      FirstPass=False;
      <... Stoploss 8 ticks for all contracts .. using ....any of the other keywords
      SetStopPosition.. or SetStopContract.. or ..SetStopLoss...>

      <....Sell 2 contracts at 1 tick profit - using setprofittarget code....>

      <...Sell 2 contracts at 2 ticks profilt - using setprofittarget code...>
end;



have you read the manual?

"setprofittarget" and "setStoploss" commands are not your regular EL commands.
They are part of the Strategy Engine.
These are Global commands... ie. one command for the entire strategy that covers all the scenarios... they are meant to give a global exit instruction.

If you want to scale out, you have to use regular keywords (ie sell and buytocover) to write step-by-step instructions.

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: setprofittarget/StopLoss - for different contracts

Postby vking » 01 Jun 2011

Thanks TJ.

My original request - "I am trying to issue low level strategy commands to bypass EL" - with an understanding that these are low level commands which are part of strategy engine and precisely the reason wanted to see - if I can use them to scale out as described. The reason to pick this route is due to the limitation of MC EL Code ( limit orders) - unable to issue multiple "order" commands at the "same tick".

I wasn't sure - whether there is some "smart" way to use these commands to scale-out somehow. If the straight answer is "No" - that's fine - I would accept the limitation and continue exploring the "order handling" portion outside MC - as issuing multiple orders at the same tick is a requirement for me.

Thanks.

User avatar
TJ
Posts: 6550
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 965 times
Been thanked: 1892 times

Re: setprofittarget/StopLoss - for different contracts

Postby TJ » 01 Jun 2011

vking wrote:Thanks TJ.

My original request - "I am trying to issue low level strategy commands to bypass EL" - with an understanding that these are low level commands which are part of strategy engine and precisely the reason wanted to see - if I can use them to scale out as described. The reason to pick this route is due to the limitation of MC EL Code ( limit orders) - unable to issue multiple "order" commands at the "same tick".

I wasn't sure - whether there is some "smart" way to use these commands to scale-out somehow. If the straight answer is "No" - that's fine - I would accept the limitation and continue exploring the "order handling" portion outside MC - as issuing multiple orders at the same tick is a requirement for me.

Thanks.


I am not sure if I understand you fully; words can only describe so much, especially when we might not be on the same page.

They best way to start is to draw a flow chart.
Anything else can be a crosstalk.

User avatar
TJ
Posts: 6550
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 965 times
Been thanked: 1892 times

Re: setprofittarget/StopLoss - for different contracts

Postby TJ » 01 Jun 2011

[quote="vking"]...

Code: Select all

Vars: FirstPass(True);

if FirstPass and Marketposition=0 then begin
        buy 4 contracts next bar market;
end;

if MarketPosition<>0 then begin
      FirstPass=False;
      <... Stoploss 8 ticks for all contracts .. using ....any of the other keywords
      SetStopPosition.. or SetStopContract.. or ..SetStopLoss...>

      <....Sell 2 contracts at 1 tick profit - using setprofittarget code....>

      <...Sell 2 contracts at 2 ticks profilt - using setprofittarget code...>
end;




see post #3
viewtopic.php?f=16&t=6929

User avatar
piranhaxp
Posts: 236
Joined: 18 Oct 2005
Has thanked: 4 times
Been thanked: 29 times

Re: setprofittarget/StopLoss - for different contracts

Postby piranhaxp » 01 Jun 2011

The reason to pick this route is due to the limitation of MC EL Code ( limit orders) - unable to issue multiple "order" commands at the "same tick"


Why that ? If you separate your strategy into 6 strategies you can do it ....

Entry- Strategy (IntraBar Order Generation "OFF")
Exit Long 1 - Strategy (IntraBar Order Generation "ON")
Exit Long 2 - Strategy (IntraBar Order Generation "ON")
Exit Short 1 - Strategy (IntraBar Order Generation "ON")
Exit Short 2 - Strategy (IntraBar Order Generation "ON")
Exit StopLoss - Strategy (IntraBar Order Generation "OFF " with SetStopLoss Command or "ON" with stop rules based on price)

Further you could work with "currentcontracts"-command to define trading conditions based on your held contracts in position. Be a little more flexible or open minded ;o).

Regards.

Mike
These users thanked the author piranhaxp for the post:
Dave Masalov

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: setprofittarget/StopLoss - for different contracts

Postby vking » 01 Jun 2011

TJ/piranhaxp - thanks for the suggestions. Let me explain further on the issue/limitations/convenience:
------------
piranhaxp's comment - Why that ? If you separate your strategy into 6 strategies you can do it ...
------------
I definitely don't want to separate the exit logic into multiple strategies. Interested in using one single chart and one single strategy if possible

TJ - To answer your question:

DOM Entry based:
- You would have an option to define immediate exits at different levels "as soon as you get a fill" - It doesn't wait for the first target to fill before placing the orders for second target ( first target contracts and second target contracts enters the order queue "as soon as" the entry is filled.

EL Based Strategy(you can't issue target1 & target2 orders simultaneously(as soon as your entry order gets filled).
- Entry order is filled
- depending on the contracts in hand - you would have to place the first target order
- depending on the contracts in hand(after the first target is filled - contracts in hand changes) you would have to place the order for second target.
- Until the first target is filled - I don't have the ability to enter the queue through current EL method( with manual I can do it through DOM ).

If there is way to mimic - whatever we can do it in DOM ( as soon as the entry order is filled ) - multiple target orders can be placed as needed - which can be better controlled through EL.

Hope I have explained it better this time.

Thanks
Last edited by vking on 01 Jun 2011, edited 1 time in total.

User avatar
TJ
Posts: 6550
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 965 times
Been thanked: 1892 times

Re: setprofittarget/StopLoss - for different contracts

Postby TJ » 01 Jun 2011

vking wrote:TJ/piranhaxp - thanks for the suggestions. Let me explain further on the issue/limitations/convenience:
...

TJ - To answer your question:

- Let's take a look at the DOM entry - You would have an option to define immediate exits at different levels "as soon as you get a fill" - It doesn't wait for the first target to fill before placing the orders for second target ( first target contracts and second target contracts enters the order queue "as soon as" the entry is filled.

- In strategy (EL based) - you can't issue target1 & target2 orders simultaneously(as soon as your entry order gets filled).
- Entry order is filled
- depending on the contracts in hand - you would have to place the first target order
- depending on the contracts in hand(after the first target is filled - contracts in hand changes you would have to place the order for second target.
- Until the first target is filled - I don't have the ability to enter the queue through current EL method( with manual I can do it through DOM ).

If there is way to mimic - whatever we can do it in DOM ( as soon as the entry order is filled ) - multiple target orders can be placed as needed - which can be better controlled through EL.

Hope I have explained it better this time.

Thanks


What chart resolution you are using?

Have you look at IntrabarOrderGenerations (IOG)?

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: setprofittarget/StopLoss - for different contracts

Postby vking » 01 Jun 2011

Whether IOG on/off - as far as I know - it doesn't make a difference as - I still won't be able to place the second target orders until the first target contracts are filled:

Code 1:

Code: Select all


[IntraBarOrderGeneration=true];

Vars: intrabarpersist AEP(0);
Vars: FirstPass(True);

if FirstPass and MarketPosition=0 then begin
     Buy 4 contracts next bar market;
     FirstPass=False;
end;

if MarketPosition <> 0 then begin
   AEP=AverageEntryPrice;
   if CurrentContracts=4 then begin
     Sell 2 contracts next bar at AEP+0.50 limit;
   end else begin
      // This code doesn't get executed until the first  target is filled.       
      Sell CurrentContracts contracts next bar at AEP+1.00 limit;
  end;
end;



Code 2:

Code: Select all


[IntraBarOrderGeneration=true];

Vars: intrabarpersist AEP(0);
Vars: FirstPass(True);

if FirstPass and MarketPosition=0 then begin
     Buy 4 contracts next bar market;
     FirstPass=False;
end;

// In this case also - it doesn't place both the limit orders
if MarketPosition <> 0 then begin
   AEP=AverageEntryPrice;
   if CurrentContracts=4 then begin
     Sell 2 contracts next bar at AEP+0.50 limit;
     Sell 2 contracts next bar at AEP+1.00 limit;
  end;
end;


User avatar
TJ
Posts: 6550
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 965 times
Been thanked: 1892 times

Re: setprofittarget/StopLoss - for different contracts

Postby TJ » 01 Jun 2011

vking wrote:Whether IOG on/off - as far as I know - it doesn't make a difference as - I still won't be able to place the second target orders until the first target contracts are filled:
...



1. You have to start with a flow chart. Otherwise all you have is a blob of thoughts.

2. From what I can see (I might be wrong), you are trying to trade intra-bar, using EOB methodology.

Sorry I can't be of more help. I will let others chime in from now.
These users thanked the author TJ for the post:
vking

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: setprofittarget/StopLoss - for different contracts

Postby vking » 01 Jun 2011

thanks TJ.

Based on my understanding - Even on 1 tick chart - I can't place the order for second target until my first target contracts are filled as simply EL can't place 2 limit orders for the same tick ( and I can't mimic DOM like immediate mulitple target limit orders on fill).

Wondering if there is any smart way to mimic - what DOM entries can do through EL.

Thanks

escamillo
Posts: 197
Joined: 25 Mar 2011
Has thanked: 22 times
Been thanked: 55 times

Re: setprofittarget/StopLoss - for different contracts

Postby escamillo » 01 Jun 2011

vking wrote:Whether IOG on/off - as far as I know - it doesn't make a difference as - I still won't be able to place the second target orders until the first target contracts are filled:

Incorrect. With [IntraBarOrderGeneration = TRUE] multiple exit orders are/can be generated as soon as Entries are filled.

However, if you have a Stop order closer to current price than your targeted exits, then the Stop order will be active and NOT the targeted exit orders.

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: setprofittarget/StopLoss - for different contracts

Postby vking » 01 Jun 2011

Thanks - escamillo.

Based on my earlier experiments - it is NOT the case.

I am going to check further and see - how it works:

- 1 tick chart
- no Stops
- Market entry order and multiple target limits

Would update this thread shortly.

Thanks.

escamillo
Posts: 197
Joined: 25 Mar 2011
Has thanked: 22 times
Been thanked: 55 times

Re: setprofittarget/StopLoss - for different contracts

Postby escamillo » 01 Jun 2011

Try adding a condition such as the following (which is something like saying if current price is greater than zero), which will create a true condition in addition to MP = 1. Works for me, live orders.

If UseLimitOrders then
begin
If High[0] >= X1 - 1000 then
Sell ("LX1.L") Size_Exit Contracts Next Bar X1 LIMIT ;
If High[0] >= X2 - 1000 then
Sell ("LX2.L") Size_Exit Contracts Next Bar X2 LIMIT ;
These users thanked the author escamillo for the post (total 2):
Stan Bokovvking

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: setprofittarget/StopLoss - for different contracts

Postby vking » 01 Jun 2011

escamillo - Thanks for the tip. Appreciate it.

I am going to try out and keep you posted with the result.

Once again appreciate it!!

User avatar
piranhaxp
Posts: 236
Joined: 18 Oct 2005
Has thanked: 4 times
Been thanked: 29 times

Re: setprofittarget/StopLoss - for different contracts

Postby piranhaxp » 01 Jun 2011

I definitely don't want to separate the exit logic into multiple strategies. Interested in using one single chart and one single strategy if possible


I don't know where the problem is ? You can put x strategies in one chart. In your case on intrabar-order-generation you can't execute entries on bar close and exits intrabar in one strategy on any easy way. Impossible.

That's why you have to separate them. Therefore i would suggest following solution.

XYZ (LE) - Strategy

Code: Select all

[IntraBarOrderGeneration=false];

Vars: AEP(0);
Vars: FirstPass(True);

if FirstPass and MarketPosition=0 then begin
     Buy 4 contracts next bar market;
     FirstPass=False;
end;


XYZ (LX1) - Strategy

Code: Select all

[IntraBarOrderGeneration=true];

Vars: AEP(0);
Vars: LX1(0);

if MarketPosition = 1 then
     begin
           AEP = AverageEntryPrice;
           LX1 = AEP + 0.500000;
                  if CurrentContracts = 4 then
                        begin
                              Sell 2 contracts next bar at lx1 limit;
                        end;
      end;


XYZ (LX2) - Strategy

Code: Select all

[IntraBarOrderGeneration=true];

Vars: AEP(0);
Vars: LX2(0);
Vars: SIZE(0);

if MarketPosition = 1 then
     begin
           AEP = AverageEntryPrice;
           LX2 = AEP + 1.00000;
                  if CurrentContracts < 4 then SIZE = Currentcontracts else SIZE = 2;
                  {may it happens you get positive slippage so you want to sell more at LX2}
                  if Currentcontracts <= 4 then
                        begin
                              Sell SIZE contracts next bar at LX2 limit;
                        end;
      end;


vice versa for short-positions ..... Put all 3 strategies or with shorts 5 in 1 chart. Then change the exit strategies to "enable intra-bar-order-generation" in the property section of format strategies of each strategy. And that's it.

If I got you totally wrong just forget it.

Mike
These users thanked the author piranhaxp for the post (total 2):
Stan Bokovvking

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: setprofittarget/StopLoss - for different contracts

Postby vking » 01 Jun 2011

Mike - Thanks for your suggestion and Your solution might work - if the position size is a fixed size - but - based on my understanding and "limited knowledge" - can't be used - with dynamic scale-in/scale-outs in a complex strategy that I am trying to build.

However the solution escamillo Suggested - if it works - I can't be more happier !!

Updates to follow..

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: setprofittarget/StopLoss - for different contracts

Postby vking » 01 Jun 2011

escamillo - Thanks for the tip.

For "Single Strategy" - this seem to work fine. Do you by any chance know - whether similar code - "exit code" would work fine for entries from multiple strategies/manual entries etc ( with newly introduced keywords

MarketPosition_at_Broker
MarketPosition_at_Broker_for_The_Strategy
AvgEntryPrice_at_Broker
AvgEntryPrice_at_Broker_for_The_Strategy

I would assume - I have to use the keyword "Total". Going to experiment further on this but curious to know - if you have experimented in this space and found any issues or came across typical usage.

thanks

Code: Select all

[IntraBarOrderGeneration=true];

Vars: FirstPass(True);

Vars: intrabarpersist AEP(0);
Vars: intrabarpersist MktPosition(0);

if FirstPass and MarketPosition=0 and LastBarOnChart_s then begin
     Buy 4 contracts next bar market;
     FirstPass=False;
end;

MktPosition=MarketPosition;
AEP=avgentryprice;

if MarketPosition=1 and MarketPosition_at_Broker = 4 and Random(2)>0 then begin
     Sell 2 contracts next bar at AEP+0.50 limit;
end;

if MarketPosition=1 and MarketPosition_at_Broker >=2 and Random(3)>0 then begin
     Sell 2 contracts next bar at AEP+1.00 limit;
end;

setstoploss(500);

escamillo
Posts: 197
Joined: 25 Mar 2011
Has thanked: 22 times
Been thanked: 55 times

Re: setprofittarget/StopLoss - for different contracts

Postby escamillo » 01 Jun 2011

vking wrote:escamillo - Thanks for the tip.

For "Single Strategy" - this seem to work fine. Do you by any chance know - whether similar code - "exit code" would work fine for entries from multiple strategies/manual entries etc ( with newly introduced keywords

MarketPosition_at_Broker
MarketPosition_at_Broker_for_The_Strategy
AvgEntryPrice_at_Broker
AvgEntryPrice_at_Broker_for_The_Strategy

I would assume - I have to use the keyword "Total". Going to experiment further on this but curious to know - if you have experimented in this space and found any issues or came across typical usage.

thanks

I have not used those yet so am not familiar with what happens when they are used and how an order is/orders are identified. Depending on what you are putting in to your Entry code, you might be able to do what you want using Global Variables to pass information from your Entry strats to your Exit strat.

dblend
Posts: 16
Joined: 11 Jul 2011
Been thanked: 1 time

Re: setprofittarget/StopLoss - for different contracts

Postby dblend » 05 Mar 2012

Following on this thread, i have an issue that when i place a setprofittarget. if i get a partial fill, the remianing quantity gets cancelled at the end of the bar. A new order for the setprofittarget is NOT replaced, i.e. i have closed part of my order and then i have to manually close the rest. I too cannot use replace to market partial filled as this will not work correctly on my exchange. Does anyone have any ideas as to how i can get the setprofittarget to always be placed aslong as i have an open position even if it is partially filled? I am using a low chart resolution as it works better for my strategy.


Return to “MultiCharts”