Page 1 of 1

exit strategy with manual trading

Posted: 12 Apr 2011
by siscop
I am sorry if this would have already been asked but I can’t find anything.
Can I write my own automated exitstrategy with MC7 instead of using the 6 default ones on manual trading?
If yes where can I find the keywords or the docu?

Re: exit strategy with manual trading

Posted: 12 Apr 2011
by Stan Bokov
Yes, you can write your own strategies in the PowerLanguageEditor. All documentation is in the Help File. You will, however, need the regular version of MultiCharts. The free MultiCharts DT does not have the PowerLanguageEditor.

Re: exit strategy with manual trading

Posted: 12 Apr 2011
by siscop
Thats good news Stan...
How do I get my Exitstrategy into that Box to have it right at hand?

Re: exit strategy with manual trading

Posted: 13 Apr 2011
by siscop
it doesnt seem to work with me...
my selfwritten exitstrategy tells me that the marketposition is "0" eventhow I placed an buy order directly on the chart manually.

Re: exit strategy with manual trading

Posted: 13 Apr 2011
by Stan Bokov
You should use the MarketPositionAtBroker_ForTheStrategy keyword. Which exit strategy do you think should be added? I'm curious...

Re: exit strategy with manual trading

Posted: 14 Apr 2011
by siscop
Thank you for the keywords.
I have another problem....
Timeframe M1
Since it is after market I tested it on forex
Code

Code: Select all

//Exit
if c>=o and MarketPosition_at_Broker>0 then
begin
sell next bar at market;
print("sell order should go out");
end;

if c<o and MarketPosition_at_Broker<0 then
buytocover next bar at market;

print("handel: ",MarketPosition_at_Broker," capital: ",InitialCapital," time: ",time," o: ",o," c: ",c);
print("handel2: ",MarketPosition_at_Broker_for_The_Strategy," capital: ",InitialCapital," time: ",time," o: ",o," c: ",c);
Output
handel: 0.00 capital: 100000.00 time: 327.00 o: 1.45 c: 1.45
handel2: 0.00 capital: 100000.00 time: 327.00 o: 1.45 c: 1.45
handel: 0.00 capital: 100000.00 time: 328.00 o: 1.45 c: 1.45
handel2: 0.00 capital: 100000.00 time: 328.00 o: 1.45 c: 1.45
sell order should go out
handel: 120000.00 capital: 100000.00 time: 329.00 o: 1.45 c: 1.45
handel2: 0.00 capital: 100000.00 time: 329.00 o: 1.45 c: 1.45
handel: 120000.00 capital: 100000.00 time: 330.00 o: 1.45 c: 1.45
handel2: 0.00 capital: 100000.00 time: 330.00 o: 1.45 c: 1.45
sell order should go out
handel: 120000.00 capital: 100000.00 time: 331.00 o: 1.45 c: 1.45
handel2: 0.00 capital: 100000.00 time: 331.00 o: 1.45 c: 1.45
Right before the first statement "sell order should go out" (time was "328") I have entered a position and it showed it under "handel" with 120000. Why not under "handel2"?
Positionsize was correct with 120000.
The capital showed here was that from the property entered and not from the broker real capital. I hope MC will change that someday on a new release that I can see the real capital from the broker and not from the entered one in the property statement.

I have 3 questions regarding this:
1. What is the difference from "MarketPosition_at_Broker" and "MarketPosition_at_Broker_for_The_Strategy". I have read the docu but the only diff I could see is that "MarketPosition_at_Broker" is limited to some brokers and IB is one of them.
2. Why didnt it close the position? The exitcondition "c>=o and MarketPosition_at_Broker>0" should have been valid.
3. QM and Chart showes 5 digits after decimel but the output only showes 2 digits after decimel. How can I change that?
Which exit strategy do you think should be added?
I was just thinking about any cutom own written one that the people use as a standard exit.

Re: exit strategy with manual trading

Posted: 14 Apr 2011
by siscop
Another question...
Where can I find the new keyword included in MC7?
MarketPosition_at_Broker is a new one...
Do you have a list of those new keywords so I can try some enhancement build in MC7?
Going trew the docu with all the old ones is way to much work.

Re: exit strategy with manual trading

Posted: 14 Apr 2011
by siscop
on MC7 beta2 the strategy does see the positionsize but it still does not "sell" the position as it should do.
whats wrong with my code (2 post up)?

Re: exit strategy with manual trading

Posted: 15 Apr 2011
by Stan Bokov
Another question...
Where can I find the new keyword included in MC7?
MarketPosition_at_Broker is a new one...
Do you have a list of those new keywords so I can try some enhancement build in MC7?
Going trew the docu with all the old ones is way to much work.
We are working on creating these. I will post an update in the blog once we have them ready.

Re: exit strategy with manual trading

Posted: 16 Apr 2011
by siscop
Thank you Stan.
Looking forward to that list :-)
2. Why didn’t it close the position? The exitcondition "c>=o and MarketPosition_at_Broker>0" should have been valid.
3. QM and Chart shows 5 digits after decimal but the output only shows 2 digits after decimal. How can I change that?
Regarding the "Sell" function
It still does not work. Do I have to open a position via "Sellshort" in order to close the one that does not close with "sell" or do you have another workaround?
It would be nice if you can answer those 2 questions especially the "2."

Re: exit strategy with manual trading

Posted: 18 Apr 2011
by Stan Bokov
1. What is the difference from "MarketPosition_at_Broker" and "MarketPosition_at_Broker_for_The_Strategy". I have read the docu but the only diff I could see is that "MarketPosition_at_Broker" is limited to some brokers and IB is one of them.
---
Market_Position_at_Broker_for_the_Strategy returns the market position at the broker for a given symbol and a particular strategy. Changes in the values returned by the word are based on the events at the broker. The word can only be used in signals and functions.

Market_Position_at_Broker returns the market position at the broker for a given symbol (irrespective of whether the position has resulted from orders submitted through a strategy or manually). Changes in the values returned by the word are based on the events at the broker. The word can only be used in signals and functions.


2. Why didnt it close the position? The exitcondition "c>=o and MarketPosition_at_Broker>0" should have been valid.
---
The condition might not have been valid if you did not have an entry in AT mode. These are exits -
sell next bar at market;
buytocover next bar at market;

If entries did not occur then exits will not either.

3. QM and Chart showes 5 digits after decimel but the output only showes 2 digits after decimel. How can I change that?
---
print(c:10:5);

Re: exit strategy with manual trading

Posted: 18 Apr 2011
by Stan Bokov
Sell closes the long entry.
Sell short closes the long entry (if you have any) and enters a short position.
Buytocover closes the short entry.
Buy closes the short entry (if any) and enters a long position.

Re: exit strategy with manual trading

Posted: 18 Apr 2011
by siscop
Thank you Stan for your Answer.
Sorry I don’t want to be annoying.
My org.question
Can I write my own automated exitstrategy with MC7 instead of using the 6 default ones on manual trading?
Your answer
Yes, you can write your own strategies in the PowerLanguageEditor. All documentation is in the Help File. You will, however, need the regular version of MultiCharts. The free MultiCharts DT does not have the PowerLanguageEditor.
So I was trying to write an exitstrategy for my manual trading entry and not autotradingentry.
In order to look for open position on manuall trading you gave me the keyword “MarketPosition_at_Broker" which worked.
Now the “sell” order works only when entered via autotrading and not manually. So I come back to my org. questions.
Can I write my own automated exitstrategy with MC7 instead of using the 6 default ones on manual trading?

Re: exit strategy with manual trading

Posted: 18 Apr 2011
by Stan Bokov
Entry and exit automation from the Trade Panel are pre-made, but they are highly customizable. You can use one or more at the same time, you can make your own templates, or you can use the Master Strategy which contains multiple inter-related bracket levels. Right now, we don't see a point in developing a language to allow modifying Entry and Exit automation strategies.

You did not specify what it is that you are trying to achieve. If possible, give us an example of what you want in the Trade Panel, and we can try to help you achieve the same logic using the building blocks already included in the program without any scripts.

Re: exit strategy with manual trading

Posted: 19 Apr 2011
by janus
Market_Position_at_Broker_for_the_Strategy returns the market position at the broker for a given symbol and a particular strategy. Changes in the values returned by the word are based on the events at the broker. The word can only be used in signals and functions.

Market_Position_at_Broker returns the market position at the broker for a given symbol (irrespective of whether the position has resulted from orders submitted through a strategy or manually). Changes in the values returned by the word are based on the events at the broker. The word can only be used in signals and functions.
After an order has been submitted, I typically see Market_Position_at_Broker updated to be equal to Market_Position_at_Broker_for_the_Strategy up to several seconds later (ie, several update ticks apart). Why the delay?

Re: exit strategy with manual trading

Posted: 20 Apr 2011
by admin
2. Why didnt it close the position? The exitcondition "c>=o and MarketPosition_at_Broker>0" should have been valid.
You opened your position manually as a result there is no position activated by the strategy and it does not execute sell exits order. There is a special function we added recently called ChangeMarketPosition which allows you to change market position status virtually i.e. tell to the execution engine that we are actually in position. We'll create instructions how it can be used for automated exist strategies with manual trading and post it to the blog and forum.
3. QM and Chart showes 5 digits after decimel but the output only showes 2 digits after decimel. How can I change that?
A numerical expression can be formatted to specify the minimum number of characters, including the decimal point, and the number of decimal places, to be used for the output:

Expression:C:D

Where: C - minimum number of characters
D - number of decimal places

Print(1.555555:6:3); will print 1.556 in the PowerLanguage Editor Output Log, with one leading space inserted

Re: exit strategy with manual trading

Posted: 20 Apr 2011
by siscop
There is a special function we added recently called ChangeMarketPosition which allows you to change market position status virtually i.e. tell to the execution engine that we are actually in position. We'll create instructions how it can be used for automated exist strategies with manual trading and post it to the blog and forum.
THANK YOU very much…… That is such a GOOD news.
With this keyword ChangeMarketPosition and the keyword MarketPosition_at_Broker I can now write a modul to sync with the broker and write my own exitstrategy whenever I open a position manually…..
That is the answer to my org. question… THANK YOU VERY MUCH *perfect*

Re: exit strategy with manual trading

Posted: 20 Apr 2011
by admin
THANK YOU very much…… That is such a GOOD news.
With this keyword ChangeMarketPosition and the keyword MarketPosition_at_Broker I can now write a modul to sync with the broker and write my own exitstrategy whenever I open a position manually…..
That is the answer to my org. question… THANK YOU VERY MUCH *perfect*
You are very welcome and you are right, this should be enough to sync your manual position and automated exits. Actually ChangeMarketPosition is available in beta 2 although there is no help for it. Here is the signal (modified version of the !From Strategy To Broker MP Synchronizer!) which can be applied on the chart along with your exist strategy and will do perfect sync. The only open issue at this moment is that ChangeMarketPosition requires Average Entry Price from the broker to synchronize manually created position and automated exit signal correctly. This specific script uses Close price instead of position price and such exits like SetStopLoss will use wrong values.

Code: Select all

[intrabarordergeneration = true];

DEFINEDLLFUNC: "kernel32.dll", int, "GetTickCount";

Input : TimeOutMS(1000), LatencyMS(1000);

variables:
textID( text_new_s(d, time_s, c,"CurrentState") ),
intrabarpersist sync_state("MP is synchronized!"),
intrabarpersist diff_state("MP syncronization. Wait "),
intrabarpersist correction_state("MP correction order sent. Wait "),

_rightest(0),
_highest(0),

inner_mp(0),
broker_mp(0),

intrabarpersist mp_diff(false),
intrabarpersist mp_diff_time_start(0),

intrabarpersist mp_corrected(false),
intrabarpersist mp_corrected_time_start(0),

intrabarpersist place_correction_marketorder(false),

intrabarpersist _get_tick_count(0),

correct_contracts(0),
is_buy(false),
is_entry(true);


if getappinfo(airealtimecalc) = 1 and getappinfo(aistrategyauto) = 1 then begin

once begin
print( text_setstyle(textID, 1, 0) );
diff_state += text(TimeOutMS*.001, " seconds.");
correction_state += text(LatencyMS*.001, " seconds.");
end;

inner_mp = currentcontracts*marketposition;
broker_mp = MarketPosition_at_Broker;


_rightest = getappinfo(airightdispdatetime);
_highest = getappinfo(aihighestdispvalue);

text_setlocation_s(
textID,
juliantodate(_rightest),
datetime2eltime_s(_rightest),
_highest
);

if broker_mp <> inner_mp then begin


_get_tick_count = GetTickCount ;

// market position differs state
if not mp_diff and not mp_corrected then begin
mp_diff = true;
mp_diff_time_start = _get_tick_count ;
text_setstring(textID, diff_state);
end;

// enter correction state after TimeOut
if mp_diff and not mp_corrected and _get_tick_count - mp_diff_time_start > TimeOutMS then begin
place_correction_marketorder = true ;
mp_corrected = true ;
mp_corrected_time_start = _get_tick_count ;
end;

// correction state
if mp_corrected then begin
text_setstring(textID, correction_state);
if _get_tick_count - mp_corrected_time_start > LatencyMS then begin
mp_corrected_time_start = _get_tick_count ;
mp_diff = false;
mp_corrected = false;
end;
end;

// place correction order
if place_correction_marketorder then begin
place_correction_marketorder = false ;
ChangeMarketPosition(broker_mp - inner_mp, c, "Sync Order");
end;

end
else begin
text_setstring(textID, sync_state);
mp_corrected = false ;
mp_diff = false;
end;

end; // is real time and strategy auto

Re: exit strategy with manual trading

Posted: 20 Apr 2011
by siscop

Code: Select all

if marketposition=0 then
ChangeMarketPosition(MarketPosition_at_Broker,0);

//Exit
if c>=o and marketposition=1 then
begin
sell next bar at market;
print("sell order should go out");
end;

print("handel: ",MarketPosition_at_Broker," capital: ",InitialCapital," marketposition: ",
marketposition, " time: ",time," o: ",o:10:5," c: ",c:10:5);
print("*****************************************************************
***************************************************************");
handel: 0.00 capital: 100000.00 marketposition: 0.00 time: 1533.00 o: 1.45170 c: 1.45140
********************************************************************************************************************************
handel: 200000.00 capital: 100000.00 marketposition: 0.00 time: 1534.00 o: 1.45140 c: 1.45130
********************************************************************************************************************************
handel: 200000.00 capital: 100000.00 marketposition: 1.00 time: 1535.00 o: 1.45130 c: 1.45115
********************************************************************************************************************************
handel: 200000.00 capital: 100000.00 marketposition: 1.00 time: 1536.00 o: 1.45115 c: 1.45105
********************************************************************************************************************************
sell order should go out
handel: 200000.00 capital: 100000.00 marketposition: 1.00 time: 1537.00 o: 1.45105 c: 1.45115
********************************************************************************************************************************
handel: 0.00 capital: 100000.00 marketposition: 0.00 time: 1538.00 o: 1.45115 c: 1.45110
********************************************************************************************************************************
works great...
thank you for the keywords :-)

Re: exit strategy with manual trading

Posted: 20 Apr 2011
by vking
This is really awesome!! Is there anyway to find out the averageprice of the position as well - when entered manually?

Thanks.

Re: exit strategy with manual trading

Posted: 21 Apr 2011
by admin
This is really awesome!! Is there anyway to find out the averageprice of the position as well - when entered manually?

Thanks.
Not at this moment other than enter it manually via input. We'll add a special reserved word which returns AveragePositionPriceFromBroker.

Re: exit strategy with manual trading

Posted: 21 Apr 2011
by vking
Thanks a lot MC Team!!

Re: exit strategy with manual trading

Posted: 21 Apr 2011
by vking
This would actually allow - to close out any positions or enter any positions manually - while the strategy is running - as long - realtime position/AvgPrices are calculated in the strategy.

Can't think of any other reserved words other than position/avgprice that I could think of that would help.

Which version you are planning to introduce these reserved words? Any other broker specific words you are going to introduce as well?

Thanks

Re: exit strategy with manual trading

Posted: 22 Apr 2011
by janus
I haven't used the new manual order trading feature yet. After reading this thread it appears to bypass the automated order management system, which would explain why the built-in Strategy Performance and Strategy Position keywords don't work. Is this the case? If so, I think I will continue to use my own manual order system I developed some time ago as it utilizes the standard order commands. I've also included the ability to draw lines on the charts for entries and exits, horizontal and sloping (the latter is for trailing stops although I could use it for entries too). I will still try the new feature but I've added so many useful buttons (over 20) on my window it will be hard for me to let go of it; but I will try :-)

Re: exit strategy with manual trading

Posted: 22 Apr 2011
by siscop
@janus
There are so many reasons why the strategy can disconnect and loose all the marketposition on a restart. I could name you 4 right of the stand - all happened in the last 2 weeks.
I am sure that all people reading this think about a familiar situation and developed their kind of a workaround how to act after their strategy disconnected on a open position. I am sure you had more then enough experience yourself.
That is one of the major reason I need a strategy that syncs with the open position after a restart.

Re: exit strategy with manual trading

Posted: 22 Apr 2011
by janus
@janus
There are so many reasons why the strategy can disconnect and loose all the marketposition on a restart.
Yes, I'm well aware of that. That's why I've developed workarounds so that I can restart and resume trading from where I've left off without any loss of information. I still may drop all this and move to the new version, but then again I may not. I'm still thinking of going to the next step and write my own order management system using the available IB API's, and just use MC for charting purposes. It depends if I get bored of trading and feel like doing some more coding :-)

Re: exit strategy with manual trading

Posted: 16 May 2011
by escamillo

Code: Select all

[intrabarordergeneration = true];

DEFINEDLLFUNC: "kernel32.dll", int, "GetTickCount";

Input : TimeOutMS(1000), LatencyMS(1000);

variables:
textID( text_new_s(d, time_s, c,"CurrentState") ),
intrabarpersist sync_state("MP is synchronized!"),
intrabarpersist diff_state("MP syncronization. Wait "),
intrabarpersist correction_state("MP correction order sent. Wait "),

_rightest(0),
_highest(0),

inner_mp(0),
broker_mp(0),

intrabarpersist mp_diff(false),
intrabarpersist mp_diff_time_start(0),

intrabarpersist mp_corrected(false),
intrabarpersist mp_corrected_time_start(0),

intrabarpersist place_correction_marketorder(false),

intrabarpersist _get_tick_count(0),

correct_contracts(0),
is_buy(false),
is_entry(true);


if getappinfo(airealtimecalc) = 1 and getappinfo(aistrategyauto) = 1 then begin

once begin
print( text_setstyle(textID, 1, 0) );
diff_state += text(TimeOutMS*.001, " seconds.");
correction_state += text(LatencyMS*.001, " seconds.");
end;

inner_mp = currentcontracts*marketposition;
broker_mp = MarketPosition_at_Broker;


_rightest = getappinfo(airightdispdatetime);
_highest = getappinfo(aihighestdispvalue);

text_setlocation_s(
textID,
juliantodate(_rightest),
datetime2eltime_s(_rightest),
_highest
);

if broker_mp <> inner_mp then begin


_get_tick_count = GetTickCount ;

// market position differs state
if not mp_diff and not mp_corrected then begin
mp_diff = true;
mp_diff_time_start = _get_tick_count ;
text_setstring(textID, diff_state);
end;

// enter correction state after TimeOut
if mp_diff and not mp_corrected and _get_tick_count - mp_diff_time_start > TimeOutMS then begin
place_correction_marketorder = true ;
mp_corrected = true ;
mp_corrected_time_start = _get_tick_count ;
end;

// correction state
if mp_corrected then begin
text_setstring(textID, correction_state);
if _get_tick_count - mp_corrected_time_start > LatencyMS then begin
mp_corrected_time_start = _get_tick_count ;
mp_diff = false;
mp_corrected = false;
end;
end;

// place correction order
if place_correction_marketorder then begin
place_correction_marketorder = false ;
ChangeMarketPosition(broker_mp - inner_mp, c, "Sync Order");
end;

end
else begin
text_setstring(textID, sync_state);
mp_corrected = false ;
mp_diff = false;
end;

end; // is real time and strategy auto
Could you please clarify some things about this code?
1). I presume that it will cause to be recognized within a chart an active order if a manual order has been placed on the "Trade Bar". Correct? [So this effectively is the same as TS <#LE Assume> ?].
2). If so, then this would be used with/in a self-programmed Exit Strategy, in order that that Exit Strategy can be used with a manual (Trade Bar) entry. True? If so, will it cause the manual order to be recognized by the code after one tick, or after how long?
3). If this is to be used in conjunction with a coded Exit Strategy, then where should this code be placed? In the same PLE document/script that contains the Exit Strategy, preceding the Exit Strategy code? Or can/should it be placed in its own PLE script Strategy document, separate from the coded Exit Strategy?
4). Can the term MarketPosition be used with this code? Such as, if present code is written as: if MarketPostion <> 0 then begin

Thanks.

Re: exit strategy with manual trading

Posted: 17 May 2011
by Stan Bokov
This signal is meant to synchronize the strategy position with the position at the broker. If you 'interfere', i.e. send an order from the chart or from the Trade Bar, the strategy will act accordingly given the new market position.

However, this script was not written for situations like this, but for strategies for a large number of orders, which use OCO-groups and take into account individual differences of the brokers (FXOrder2Go, if I remember correctly). It has IOG turned on, so it recalculates on every new tick.

The signal is applied separately from your other signals.

Re: exit strategy with manual trading

Posted: 18 May 2011
by escamillo
However, this script was not written for situations like this, but for strategies for a large number of orders, which use OCO-groups and take into account individual differences of the brokers (FXOrder2Go, if I remember correctly). It has IOG turned on, so it recalculates on every new tick.

The signal is applied separately from your other signals.
OK. Thank you. Since this is not meant to be used with individual orders placed via Trade Bar, is there a method to have charts automatically recognize Trade Bar orders so that underlying chart Exit Strategy code will recognize the order and become activated?

Re: exit strategy with manual trading

Posted: 19 May 2011
by Stan Bokov
Two new keywords were created for MC 7 Beta 4 - AvgEntryPrice_at_Broker and AvgEntryPrice_at_Broker_for_The_Strategy. These two keywords, combined with the signal posted above, will effectively solve any issues about synchronizing a strategy with the market position at broker.

A signal called "!From Broker To Strategy MP Synchronizer!" will be included in the standard signal package in beta 4. It will allow you to synchronize your strategies with whatever is happening at the broker. Essentially, if you want to enter positions manually, but you want to write your own strategy to handle the exits - this signal will sync your strategy, so it knows about all the orders that you made by hand.

Re: exit strategy with manual trading

Posted: 19 May 2011
by sptrader
Stan: any estimate of when Beta 4 will be released ?

Re: exit strategy with manual trading

Posted: 19 May 2011
by Stan Bokov
If we are planning the final release of MC7 in June... I would say Beta 4 will likely come before June, or in its beginning.

Re: exit strategy with manual trading

Posted: 19 May 2011
by vking
Thanks a lot Stan. Have been eagerly waiting for this feature for a long time. This would make this product really powerful - to use "auto" + "manual" trading combined on same chart - which would interact with each other - synchronized positions.

Thanks

Two new keywords were created for MC 7 Beta 4 - AvgEntryPrice_at_Broker and AvgEntryPrice_at_Broker_for_The_Strategy. These two keywords, combined with the signal posted above, will effectively solve any issues about synchronizing a strategy with the market position at broker.

A signal called "!From Broker To Strategy MP Synchronizer!" will be included in the standard signal package in beta 4. It will allow you to synchronize your strategies with whatever is happening at the broker. Essentially, if you want to enter positions manually, but you want to write your own strategy to handle the exits - this signal will sync your strategy, so it knows about all the orders that you made by hand.