Converting coding into IOG coding

Questions about MultiCharts and user contributed studies.
allenlowe
Posts: 37
Joined: 29 Jun 2011
Has thanked: 2 times
Been thanked: 3 times

Converting coding into IOG coding

Postby allenlowe » 12 May 2012

Dear All,

My original message was posted in the user contributed section but no one answer. Therefore, I post it again here to see anyone can give me some tips on how should I program my code.

I got problem in converting my coding for IOG use with backtesting work. Can someone hints me how to do it?

My original coding is using minute bar with a series of stop selling and buying price calculated at the end of each bar for next bar use. The EntryPrice of different transaction is calculated base on the historical price including last bar's close. It works perfect for my simple strategy when backtesting. Below is part of my coding without IOG.

Code: Select all

if condition3 and condition7 then begin
Sell short ("Open Short") NumContract contracts next bar EntryPrice1 stop;
end;

if condition3 then begin
Sell ("Close Long") from entry ("Open Long") next bar EntryPrice2 stop;
Sell ("Close Long A") from entry ("Open Long A") next bar EntryPrice2 stop;
end;

if condition4 and condition7 then begin
Buy ("Open Long") NumContract contracts next bar EntryPrice4 stop;
end;

if condition4 then begin
Buytocover("Close Short") from entry ("Open Short") next bar EntryPrice5 stop;
end;

if condition5 and condition7 then begin
Buy ("Open Long A") NumContract contracts next bar EntryPrice6 stop;
end;

There is no problem when I use IB demo account to test it. However, when I test in real, problems come. First thing is the slip which can be solved by using limit order. Second thing become a problem, IB inform me that I got too high cancellation rate. This is because my EntryPrice is set far above and below the current trading price, my strategy is trying to capture market abnormal so only 1 or 2 trading will occur every day but I submitted a new order with a new EntryPrice every minute. Therefore, I need to change my coding, below is my new coding.

Code: Select all

if condition3 and condition7 and Last <= EntryPrice1 then begin
Sell short ("Open Short") NumContract contracts next bar EntryPrice1 limit;
end;

if condition3 and Last <= EntryPrice2 then begin
Sell ("Close Long") from entry ("Open Long") next bar EntryPrice2 limit;
Sell ("Close Long A") from entry ("Open Long A") next bar EntryPrice2 limit;
end;

if condition4 and condition7 and Last >= EntryPrice4 then begin
Buy ("Open Long") NumContract contracts next bar EntryPrice4 limit;
end;

if condition4 and Last >= EntryPrice5 then begin
Buytocover("Close Short") from entry ("Open Short") next bar EntryPrice5 limit;
end;

if condition5 and condition7 and Last >= EntryPrice6 then begin
Buy ("Open Long A") NumContract contracts next bar EntryPrice6 limit;
end;
I use the code with IOG turn on and limit-to-market set to 5 second. In theory, it is fine except if the order generated at the last second in a minute may be cancelled without execute. This part JoshM already hints me how to re-code my script to handle this.

However, my conditions also depends on historically execution of the trade, I need to have the backtesting work properly in order to get the future execution correct. In my coding, because the keyword "Last" cannot use in backtesting. I cannot make the backtesting work correctly. Does any can hint me how can I do that? I cannot use bar magnifier because my instrument is custom instrument. I believe it should be very easy just I am not good in programming and MC only.

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

Re: Converting coding into IOG coding

Postby TJ » 13 May 2012

I would start with a flow chart...

then supplement it with diagrams of critical points.


The flow chart should cover ALL the permutation of "What-ifs" and "If-Then-Else".


There is no easy way to code a strategy. A good set of flow charts and diagrams will carry you far -- You either spend the time now, or spend the time debugging later.

allenlowe
Posts: 37
Joined: 29 Jun 2011
Has thanked: 2 times
Been thanked: 3 times

Re: Converting coding into IOG coding

Postby allenlowe » 13 May 2012

Thanks a lot. TJ

But I didn't get it, could you explain a little bit more?

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

Re: Converting coding into IOG coding

Postby TJ » 13 May 2012

Thanks a lot. TJ

But I didn't get it, could you explain a little bit more?
What don't you get?

allenlowe
Posts: 37
Joined: 29 Jun 2011
Has thanked: 2 times
Been thanked: 3 times

Re: Converting coding into IOG coding

Postby allenlowe » 13 May 2012

My coding is no problem in terms of trading with real time data. The problem is that it cannot backtesting because the keyword "last" is not supported in backtesting with IOG. I just want to know any work around.

When I do the coding, I don't use a flow chart like you. However, I create an excel worksheet first to testing the logic with data before actual coding, I don't think my coding logic got problem. I think may be I am not good in EL so that don't know how to work around with the keyword "last".

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

Re: Converting coding into IOG coding

Postby TJ » 13 May 2012

My coding is no problem in terms of trading with real time data. The problem is that it cannot backtesting because the keyword "last" is not supported in backtesting with IOG. I just want to know any work around.

When I do the coding, I don't use a flow chart like you. However, I create an excel worksheet first to testing the logic with data before actual coding, I don't think my coding logic got problem. I think may be I am not good in EL so that don't know how to work around with the keyword "last".
What is your intention in using "Last"?

Have you checked the wiki for definition and usage example?

allenlowe
Posts: 37
Joined: 29 Jun 2011
Has thanked: 2 times
Been thanked: 3 times

Re: Converting coding into IOG coding

Postby allenlowe » 13 May 2012

My intention is to replace the stop order generate every minute, the code works in both backtesting and real. The only problem is it generate a stop order every minute without execute and IB doesn't allow.

Therefore, if the last price >= mypresetprice with IOG on, buy at mypresetprice. The code works in real time but not backtesting. I would like to know is there a work around I can make it able to backtest so that I can optimize it.

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

Re: Converting coding into IOG coding

Postby TJ » 13 May 2012

My intention is to replace the stop order generate every minute, the code works in both backtesting and real. The only problem is it generate a stop order every minute without execute and IB doesn't allow.

Therefore, if the last price >= mypresetprice with IOG on, buy at mypresetprice. The code works in real time but not backtesting. I would like to know is there a work around I can make it able to backtest so that I can optimize it.
but why do you want to use the keyword LAST?

Have you checked the wiki for definition and usage example?

allenlowe
Posts: 37
Joined: 29 Jun 2011
Has thanked: 2 times
Been thanked: 3 times

Re: Converting coding into IOG coding

Postby allenlowe » 13 May 2012

If I don't use LAST, how can I refer to the last trade tick? I did look at the wiki definition of "LAST", same as the help file. What is the different?

Dear All,

My question is any other code word I can use to replace the word "LAST" and can work in backtesting. Any other have idea?

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

Re: Converting coding into IOG coding

Postby TJ » 13 May 2012

If I don't use LAST, how can I refer to the last trade tick? I did look at the wiki definition of "LAST", same as the help file. What is the different?

Dear All,

My question is any other code word I can use to replace the word "LAST" and can work in backtesting. Any other have idea?
LAST is for quote field use.

You should use CLOSE instead.

Before a bar is finished, in real time, CLOSE is the latest available price of the bar.

allenlowe
Posts: 37
Joined: 29 Jun 2011
Has thanked: 2 times
Been thanked: 3 times

Re: Converting coding into IOG coding

Postby allenlowe » 14 May 2012

Thanks a lot. I never notice the different. I will try to recode it and backtesting again.

Thanks


Return to “MultiCharts”