Limit order hold with IOG true

Questions about MultiCharts and user contributed studies.
cygneslim
Posts: 6
Joined: 24 Mar 2015

Limit order hold with IOG true

Postby cygneslim » 24 Mar 2015

In Multicharts, I'm having a problem getting a limit order to "stick" for more than one tick after sending, with IOG on/true. This is the same problem I've read about in a couple of other threads where the limit order is sent, cancelled, sent, cancelled, etc. after each tick. I have tried the suggestions but with no success.

I'm using a 1minute bar chart and trying to accomplish the following:
A. Evaluate OPEN tick of 1minute bar for buy or sell condition
i. Look back at the previous tick (closing tick of previous 1min bar) and compare to a variable to determine if buy or sell condition is TRUE
ii. If buy or sell condition = TRUE, then set buy or sell price to the calculatedprice variable
iii. Set "trade" variable to trigger a buy or sell in the trade execution routine.

B. Evaluate value of "trade" variable to determine if a buy or sell should be executed
i.Execute a buy or sell limit at buyprice or sellprice

I'm using the (barstatus(1)=0) code is so that the buyprice or sellprice, and the determination of whether to buy or sell, is set/made only once within the 1min bar and the condition will hold until next 1min bar open tick. This should prevent the constant submitting and cancelling of my limit order until executed. The problem is that even though the trade price is set and the trigger variable is set to buy or sell on the open tick of the 1min bar, the trade trigger variable still resets to zero on the next ticks outside of this "barstatus" routine. I'm not sure why this is happening since this part of the code should only be executed once in each 1min bar.

It seems like there is a simple explanation for this. Can anyone help with this? Example code follows:

Code: Select all

[IntrabarOrderGeneration = True];

Variable:
pricevar (0),
calculatedprice (0),
buyprice (0),
sellprice (0),
trade (0);


if (barstatus(1) = 0) then begin // evaluate opening tick of 1min bar
If close[1] > pricevar[1] then begin //compare closing tick of previous bar to variable
buyprice=calculateprice; //set buyprice variable to calculated price variable
print("O bar # is", maxbarsback+currentbar," ",close[1]," ",buyprice); //for debugging
trade = 1; //set a trade trigger variable for a buy
end else

If close[1] < pricevar[1] then begin //compare closing tick of previous bar to variable
sellprice=calculatedprice; //set sellprice variable to calculated price variable
print("O bar # is", maxbarsback+currentbar," ",close[1]," ",sellprice); //for debugging
trade = -1; //set a trade trigger variable for a sell
end else

If close[1] = calculatedprice[1] then begin //set a trade trigger variable to neutral - no trade
trade=0;
end;
end;

//Following code is to execute a buy,sell, or no trade based on value of "trade" variable.
//The price for the limit order has been calculated in the previous code and the trade trigger
//variable has been set in previous code. My assumption here is that neither variable should
//change until the opening tick of next 1min bar, however when I look at the output window
//the "trade" variable is 1 or -1 on the first tick after 1min bar opening tick, and for all other ticks
//it goes to zero/0. See following code.

if trade=1 then begin //buy next tick if trade variable=1
buy ("B1")next bar at buyprice limit;
print(time,"trade=",trade," ",buyprice); //for debugging
end else

if trade = -1 then begin //sell next tick if trade variable=-1
sellshort ("S1") next bar at sellprice limit;
print(time,"trade=",trade," ",sellprice); //for debugging
end;

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

Re: Limit order hold with IOG true

Postby TJ » 24 Mar 2015

When IntrabarOrderGeneration = True,
the strategy is re-evaluated every tick.

Therefore when "(barstatus(1) = 0)" is no longer true,
the order is no longer valid... and thus cancelled.

cygneslim
Posts: 6
Joined: 24 Mar 2015

Re: Limit order hold with IOG true

Postby cygneslim » 24 Mar 2015

Thanks for the reply, but one thing is still not clear.

If the trade trigger variable ("trade") is set during the opening tick evaluation, then why is this value not held until the next bar opening tick? This variable should not be re-evaluated until the next bar opening tick. Same for the buyprice variable.

And if the the trade trigger and buyprice conditions stay true, the order should not be resubmitted until there is a change. Is this correct?

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

Re: Limit order hold with IOG true

Postby TJ » 24 Mar 2015

Thanks for the reply, but one thing is still not clear.

If the trade trigger variable ("trade") is set during the opening tick evaluation, then why is this value not held until the next bar opening tick? This variable should not be re-evaluated until the next bar opening tick. Same for the buyprice variable.

And if the the trade trigger and buyprice conditions stay true, the order should not be resubmitted until there is a change. Is this correct?
you can condense the logic to this illustration:

Code: Select all

if (barstatus(1) = 0) then BUY
else don't BUY;

cygneslim
Posts: 6
Joined: 24 Mar 2015

Re: Limit order hold with IOG true

Postby cygneslim » 24 Mar 2015

Okay, clear.

Let me ask a very direct question...if no buy/sell conditions have changed, is it possible to hold the limit order without cancel and resubmit each tick with IOG true?

I'd like to know if I'm trying to do more than the software allows.

Thanks

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

Re: Limit order hold with IOG true

Postby escamillo » 24 Mar 2015

this works for me:

Code: Select all

If LastBarOnChart and EntriesToday(Date) = 0 and Buy_Condition
then
Buy Size_Entry Contracts Next Bar Your_Price LIMIT ;

cygneslim
Posts: 6
Joined: 24 Mar 2015

Re: Limit order hold with IOG true

Postby cygneslim » 24 Mar 2015

Thanks for the reply escamillo.

I'm not sure if you're example is the same as mine. I have not used lastbaronchart, so I'm not familiar with its properties. I will test its use this evening.

What I want to do is change the limit order on the opening tick of each 1min bar, and then I want it to hold until the order is either executed or until the next 1min begins at which point a new order would be submitted.

If I can find a way to keep the order entry conditions TRUE with IOG TRUE(re-evaluating each tick), until the opening tick of the next bar, I think this will solve my problem.

It's still not clear to me if this is within the capabilities of Multicharts?

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Limit order hold with IOG true

Postby tony » 24 Mar 2015

I just posted a long post and then had a page reload error and I lost my post. So I'll keep this one short. TJ was right, when conditions change so that the limit order is no longer valid, MC will cancel the order. The only ways around this (I believe) are (1) use market orders on entry or (2) write a line of code that holds a value and then in your conditional statements that trigger the limit have something like if varA=true then buy at limit;

As long as varA=true then the limit will not cancel.

cygneslim
Posts: 6
Joined: 24 Mar 2015

Re: Limit order hold with IOG true

Postby cygneslim » 24 Mar 2015

Thanks for your help Tony.

I actually found what I was missing on another forum. It was suggested to use intrabarpersist for the variables. I am now able to get the variable values to hold for all ticks except the opening tick of a bar, where the variable values should be reset. Below is basically what I did:

Code: Select all

[Intrabarordergeneration=TRUE];

variables:
intrabarpersist tradetrigger (0);
intrabarpersist buyprice (0);

tradetrigger=tradetrigger;
buyprice=buyprice;

if *condition to buy is TRUE* then tradetrigger=1 and buyprice=*price*;

if tradetrigger=1 then buy next bar at buyprice limit;

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Limit order hold with IOG true

Postby tony » 24 Mar 2015

Holding values is one thing, but don't be confused with essentially holding a conditional statement(s). Here's an example of what I mean.

If Close > var1 then begin; buy 1 contract at var1 Limit;

So assume var1 = 2005.25 and close is 2005.50, then the conditional statement is met, the buy limit at 2005.25 is sent from MC to your broker, etc. The limit sits there waiting to fill. Now next tick, close is 2005.25, and the statement close > var1 is no longer true, so MC cancels that Limit buy order at your broker.

So what you need to do is when that conditional statement "close > var1" is true, write your script in such a way that it is held, regardless of what happens on the next tick. That's different than intrabarpersist which is holding a variable value.

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

Re: Limit order hold with IOG true

Postby escamillo » 25 Mar 2015

--you can calculate your Limit price level behind BarStatus(1) = 2, so that the Limit price will update at the end of each bar.
--using the code that I posted above that will actually place a live Limit order, your Limit order price will be active and update to the currently calculated Limit price at the end of each bar.

You need to use LastBarOnChart (or perhaps some other limiter that I am less familiar with) or else previous-bar instances of your order condition being true will trigger an order.

cygneslim
Posts: 6
Joined: 24 Mar 2015

Re: Limit order hold with IOG true

Postby cygneslim » 25 Mar 2015

Thank you all very much for the suggestions. As of now, my code is executing as expected and I no longer have the issue with limit orders being cancelled and resubmitted each tick. They are holding now.

As for holding the value of a variable vs. holding the state of a condition, I need to investigate this further.

Thanks again.

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Limit order hold with IOG true

Postby tony » 25 Mar 2015

Glad to hear. If I can help in regards to holding a "conditional statement" which is kind of misleading how I wrote that term, please feel free to PM and I'll share what I can. It's a neat trick to know and easy to do. You'll find it helpful at some point with writing scripts.


Return to “MultiCharts”