forex price tracking i.e spread slippage

Questions about MultiCharts and user contributed studies.
User avatar
t-rader
Posts: 139
Joined: 02 Feb 2011
Location: Australia
Has thanked: 11 times
Been thanked: 27 times

forex price tracking i.e spread slippage

Postby t-rader » 24 Jun 2014

Hi All,

I'm trading forex and trying to add some price tracking such as spread and slippage, you know so I can keep a close eye on my broker ;)

The basis of my code is as follows:

Code: Select all

[IntraBarOrderGeneration=true];

Var:

Intrabarpersist EntryBid(0),
Intrabarpersist EntryAsk(0);

if marketposition = 0 and condition1 = true then being
buy next bar
if EntryBid = 0 then EntryBid = close;
if EntryAsk = 0 then EntryAsk = close data[2]
end;

if marketposition = 1 then begin
if EntryBid <> 0 then begin
Alert(Text(EntryBid, " / ", EntryAsk);
EntryBid = 0;
EntryAsk = 0;
end;
end;
So basically I want to know what the close of data 1 and data 2 is when my entry order is initially triggered.

Now what's happening is I apply this code to a chart and using print I can see the code is working. Then when I turn on autotrade the first trade that happens returns the EntryBid and EntryAsk of the very first trade that happened on the chart in back test mode! Why!? But then the next trade that happens reports the correct EntryBid and EntryAsk.

I must be doing something stupidly wrong here :S

User avatar
t-rader
Posts: 139
Joined: 02 Feb 2011
Location: Australia
Has thanked: 11 times
Been thanked: 27 times

Re: forex price tracking i.e spread slippage

Postby t-rader » 07 Jul 2014

Bump, anyone have any thoughts on why my code is not working :(

I just want to save value of data 1 and data 2 into a variable when the initial order is sent and not update that value even if it takes more than 1 tick to be filled. Once the the position is filled I want to email alert the values and then reset them.

Sounds so simply but my code is not working, I must be doing something wrong here :S

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: forex price tracking i.e spread slippage

Postby Henry MultiСharts » 08 Jul 2014

Hello t-rader,

When you turn on the auto trading in SA mode it recalculates the study on the "max bars back" bars ignoring the order generation commands and preserving the variable values. In order to make your code function properly you need to wrap your code with the the following condition:

Code: Select all

if getappinfo(aiStrategyAuto)=1 and LastBarOnChart then begin
//your study logic here
end;
That is also possible to use InsideAsk and InsideBid to get the current realtime ask and bid prices from the chart's status line (instead of referencing two data series).

User avatar
t-rader
Posts: 139
Joined: 02 Feb 2011
Location: Australia
Has thanked: 11 times
Been thanked: 27 times

Re: forex price tracking i.e spread slippage

Postby t-rader » 05 Aug 2014

Hello t-rader,

When you turn on the auto trading in SA mode it recalculates the study on the "max bars back" bars ignoring the order generation commands and preserving the variable values. In order to make your code function properly you need to wrap your code with the the following condition:

Code: Select all

if getappinfo(aiStrategyAuto)=1 and LastBarOnChart then begin
//your study logic here
end;
That is also possible to use InsideAsk and InsideBid to get the current realtime ask and bid prices from the chart's status line (instead of referencing two data series).
Hi Henry,

I finally got around to trying this and it still doesn't work. I tried the following:

Code: Select all

[IntraBarOrderGeneration=true];

Var:

Intrabarpersist EntryBid(0),
Intrabarpersist EntryAsk(0);

if marketposition = 0 and condition1 = true then being
buy next bar
if getappinfo(aiStrategyAuto)=1 and LastBarOnChart then begin
if EntryBid = 0 then EntryBid = close;
if EntryAsk = 0 then EntryAsk = close data[2]
end;
end;

if marketposition = 1 then begin
if getappinfo(aiStrategyAuto)=1 and LastBarOnChart then begin
if EntryBid <> 0 then begin
Alert(Text(EntryBid, " / ", EntryAsk);
EntryBid = 0;
EntryAsk = 0;
end;
end;
end;
What's happening now is that when I turn auto trading on it executes a trade on the last tick of the bar but its reporting the EntryBid and EntryAsk variables as the open price of that bar, not the closing price when the trade was triggered.

Can you please explain more why the I even have to wrap the code in the getappinfo(aiStrategyAuto) and use LastBarOnChart? Shouldn't the variables just update on each tick as I'm using IOG and Intrabarpersist?

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: forex price tracking i.e spread slippage

Postby Henry MultiСharts » 06 Aug 2014

Please try this code:

Code: Select all

once cleardebug;
[IntraBarOrderGeneration=true];

Var:

Intrabarpersist EntryBid(0),
Intrabarpersist EntryAsk(0);

if getappinfo(aiStrategyAuto)=1 and LastBarOnChart_s then begin
if marketposition = 0 {and condition1 = true} then begin
buy next bar at market;

EntryBid = close;
EntryAsk = close data2;
end;

if marketposition = 1 and EntryBid <> 0 then begin
Alert(Text(EntryBid, " / ", EntryAsk));
EntryBid = 0;
EntryAsk = 0;
end;
print ("B= ", EntryBid:0:5, " A= ", EntryAsk:0:5);
end;
getappinfo(aiStrategyAuto)=1 and LastBarOnChart_s are required for performing the calculations only on the most recent bar of the data series and if the auto trading is turned on. Make sure "Realtime history matching is disabled in the signal properties".

In order to run the code on a single data series you can change the variables to the following code:

Code: Select all

EntryBid = insidebid;
EntryAsk = insideask;

User avatar
t-rader
Posts: 139
Joined: 02 Feb 2011
Location: Australia
Has thanked: 11 times
Been thanked: 27 times

Re: forex price tracking i.e spread slippage

Postby t-rader » 06 Aug 2014

Please try this code:

Code: Select all

once cleardebug;
[IntraBarOrderGeneration=true];

Var:

Intrabarpersist EntryBid(0),
Intrabarpersist EntryAsk(0);

if getappinfo(aiStrategyAuto)=1 and LastBarOnChart_s then begin
if marketposition = 0 {and condition1 = true} then begin
buy next bar at market;

EntryBid = close;
EntryAsk = close data2;
end;

if marketposition = 1 and EntryBid <> 0 then begin
Alert(Text(EntryBid, " / ", EntryAsk));
EntryBid = 0;
EntryAsk = 0;
end;
print ("B= ", EntryBid:0:5, " A= ", EntryAsk:0:5);
end;
getappinfo(aiStrategyAuto)=1 and LastBarOnChart_s are required for performing the calculations only on the most recent bar of the data series and if the auto trading is turned on. Make sure "Realtime history matching is disabled in the signal properties".

In order to run the code on a single data series you can change the variables to the following code:

Code: Select all

EntryBid = insidebid;
EntryAsk = insideask;
Hi Henry,

I appreciate your help but doing this will stop the code from working in back testing.

I'm simply using a variable to store the price of data 1 and data 2 so I do not understand why I would have to tell this code to only run when in auto trading mode and when its the last bar on chart. It seems 2 data series makes the code function differently, is this the reason you are suggesting what you're suggesting, because I'm referencing 2 data series?

I also tried switching to use insidebid and insideask built-in keywords as you suggested BUT these don't function correctly either.

I triggered an entry order on the last tick of the bar and print the insidebid. Now I would expect inside bid to be the price of the last tick of the bar, or maybe even the first tick of the next bar? but the price reported is neither!? So I have no idea where insidebid is getting this tick/price from??? Although it does seem close to the current price...

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: forex price tracking i.e spread slippage

Postby Henry MultiСharts » 07 Aug 2014

t-rader, from your initial posts I've assumed that you need the code to function in real-time auto trading only. Do you need to have a complete order log for both backtesting and auto trading? Do you want the log to be not reset after you start the auto trading? Which auto trading mode do you use - SA or AA? Do you use Bar Magnifier?

User avatar
t-rader
Posts: 139
Joined: 02 Feb 2011
Location: Australia
Has thanked: 11 times
Been thanked: 27 times

Re: forex price tracking i.e spread slippage

Postby t-rader » 09 Aug 2014

t-rader, from your initial posts I've assumed that you need the code to function in real-time auto trading only. Do you need to have a complete order log for both backtesting and auto trading? Do you want the log to be not reset after you start the auto trading? Which auto trading mode do you use - SA or AA? Do you use Bar Magnifier?
I need my overall code to function in both back test and real-time live trading, but do not necessarily require a complete order log for backtesting just live auto-trading.

I trade in SA.

For back testing I use Bar Magnifier, Extended Backtest mode and IOG.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: forex price tracking i.e spread slippage

Postby Henry MultiСharts » 11 Aug 2014

t-rader, the following code works ok on our end in SA mode, both for real-time trading and back testing using Bar Magnifier, Extended Backtest mode and IOG using MultiCharts64 Version 8.8 Release (Build 9589).
If that does not work for you - please come to our live chat Monday-Friday 6:30 am - 3 pm EST to demonstrate this behavior remotely.

Code: Select all

once cleardebug;
[IntraBarOrderGeneration=true];
Var:
intrabarpersist EntryBid(0),
intrabarpersist EntryAsk(0);

if marketposition = 0 {and condition1 = true} then begin
buy next bar at market;
EntryBid = close data2;
EntryAsk = close;
end;

if marketposition = 1 and EntryBid <> 0 then begin
print (" BN= ", Symbol_CurrentBar, " B= ", EntryBid:0:5, " A= ", EntryAsk:0:5);
Alert(Text(EntryBid, " / ", EntryAsk));
EntryBid = 0;
EntryAsk = 0;
end;


Return to “MultiCharts”