Change limit order to market order when cannot fill after 5 seconds  [SOLVED]

Questions about MultiCharts and user contributed studies.
kinwai
Posts: 89
Joined: 03 May 2019
Has thanked: 12 times
Been thanked: 1 time

Change limit order to market order when cannot fill after 5 seconds

Postby kinwai » 28 Jun 2021

I would like to change limit order to market order when target price hit but cannot fill after 5 seconds.

Here is my code, in real trade, it looks the market order doesn’t work even target price meet after 5 seconds, any idea?

Code: Select all

[IntrabarOrderGeneration = true] Var: settle_s(-1), target_price(xxx); if close >= target_price and marketposition > 0 then begin if settle_s = -1 then settle_s = time_s; sell("limit") next bar at target_price limit; end; If eltimetodatetime_s(time_s) - eltimetodatetime_s(settle_s) > eltimetodatetime_s(5) and marketposition > 0 then sell("(Market Close)") next bar at market;

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

Re: Change limit order to market order when cannot fill after 5 seconds

Postby TJ » 28 Jun 2021

Instead of Time_s
try CurrentTime_s

ps. Please check wiki for the CurrentTime_s format.

kinwai
Posts: 89
Joined: 03 May 2019
Has thanked: 12 times
Been thanked: 1 time

Re: Change limit order to market order when cannot fill after 5 seconds

Postby kinwai » 30 Jun 2021

CurrentTime_s doesn’t work also, is it MC bug? Anyone can help?

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

Re: Change limit order to market order when cannot fill after 5 seconds

Postby TJ » 30 Jun 2021

Please note: CurrentTime_s does not work in back test. It is for real time trade only.

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

Re: Change limit order to market order when cannot fill after 5 seconds

Postby TJ » 30 Jun 2021

You do not need the convoluted codes.
All you need is a simple subtraction:

Code: Select all

if currenttime_s - settle_s >5 then . . .
Without seeing the whole code, this is the best I can suggest.

kinwai
Posts: 89
Joined: 03 May 2019
Has thanked: 12 times
Been thanked: 1 time

Re: Change limit order to market order when cannot fill after 5 seconds

Postby kinwai » 30 Jun 2021

I have already tested for real time trade but failed.

If do simple subtraction, it may have logical issue. For example, 135900 - 135859, the subtraction result is 41 second, but actually it is only 1 second.

Code: Select all

if currenttime_s - settle_s >5 then . . .

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

Re: Change limit order to market order when cannot fill after 5 seconds

Postby TJ » 30 Jun 2021

I have already tested for real time trade but failed.

If do simple subtraction, it may have logical issue. For example, 135900 - 135859, the subtraction result is 41 second, but actually it is only 1 second.

Code: Select all

if currenttime_s - settle_s >5 then . . .
Right, I forgotten about the 1/60 part.

Please checkout DateTime.

kinwai
Posts: 89
Joined: 03 May 2019
Has thanked: 12 times
Been thanked: 1 time

Re: Change limit order to market order when cannot fill after 5 seconds  [SOLVED]

Postby kinwai » 01 Jul 2021

I believe I found the root cause, it isn’t related to the usage of CurrentTime_s, Time_s nor DateTime.

Currently, I am using Portfolio trader with 1 min timeframe, it looks the above data type doesn’t support IOG mode even it has IOS option, so the program cannot get the second by tick, instead get the time per minute.

When I put the program in MultiCharts (with Charts display), it can get the current second.


Return to “MultiCharts”