NexusFi: Find Your Edge


Home Menu

 





How I could measured latency in MultiChart ?


Discussion in MultiCharts

Updated
    1. trending_up 1,793 views
    2. thumb_up 2 thanks given
    3. group 4 followers
    1. forum 5 posts
    2. attach_file 0 attachments




 
Search this Thread

How I could measured latency in MultiChart ?

  #1 (permalink)
pardo127
madrid spain
 
Posts: 5 since Oct 2016
Thanks Given: 0
Thanks Received: 0

I need to measured the latency of my strategy.

Could help me someone?

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
NexusFi Journal Challenge - April 2024
Feedback and Announcements
New Micros: Ultra 10-Year & Ultra T-Bond -- Live Now
Treasury Notes and Bonds
The space time continuum and the dynamics of a financial …
Emini and Emicro Index
Exit Strategy
NinjaTrader
Are there any eval firms that allow you to sink to your …
Traders Hideout
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
61 thanks
Funded Trader platforms
39 thanks
NexusFi site changelog and issues/problem reporting
26 thanks
Battlestations: Show us your trading desks!
26 thanks
The Program
18 thanks
  #2 (permalink)
 
Fu510n's Avatar
 Fu510n 
Suffield, CT
 
Experience: Advanced
Platform: MC, TS, Python, Rust
Broker: IB, IQFeed, TS, Kraken
Trading: ES, NQ, RTY, YM, CL, RB, 6E
Frequency: Several times daily
Duration: Seconds
Posts: 144 since Oct 2009
Thanks Given: 902
Thanks Received: 143

.Net or EasyLanguage? What exactly are you trying to measure?

-Guy

Follow me on Twitter Reply With Quote
  #3 (permalink)
pardo127
madrid spain
 
Posts: 5 since Oct 2016
Thanks Given: 0
Thanks Received: 0



Fu510n View Post
.Net or EasyLanguage? What exactly are you trying to measure?

-Guy

I want to measure the time that takes my order to IB and the time that takes from the broker to exchange.

I have this Easylanguage code but dont compile in Multichart because It programmed with objects :


{
JJ Log Generator
---------------------------
Based on Code from Mathemagician
Added ideas from AndroidMarvin
Modded by Honza K.
---------------------------

Purpose: Logs order state infos with milisecond difference calculation between events



}

// DefineDLLFunc: "JJLogger.dll", int, "LogMessage", lpstr, lpstr;

Using tsdata.trading;
Using elsystem;

input:
EnableLogging(true),
LogFileName("C:\JJ_LogFromTS.txt");

var:
Intrabarpersist MessagesLogged(0),
Intrabarpersist Order ord(null),
Intrabarpersist tmpString(""),
StopWatch sw( Null ), // to get msec resolution
OrdersProvider op(null),
Intrabarpersist lastElapsed( 0.0 ); // record previous msec value to count diff

Method override void InitializeComponent()
begin
op = New OrdersProvider;
op.Realtime = true;
op.Updated+=opUpdated;
op.Load=true;
sw = New StopWatch; // AndroidMarvin idea - adding stopwatch to count latencies directly
sw.Start();
end;

Method void opUpdated (Objectsender, OrderUpdatedEventArgs args)
begin
if EnableLogging and args.Reason.ToString()<>"initialupdate" then begin
MessagesLogged+=1;
If args.Order<>null then begin
ord = args.Order;
tmpString =
Formatdate("yyyy/MM/dd", ComputerDateTime()) // date
+ "," + FormatTime("HH:mm:ss", ComputerDateTime()) // time
+ "," + numtostr( sw.ElapsedMilliseconds - lastElapsed, 0 ) // milisecond difference from previous event
+ "," + args.State.ToString()
{ + "," + args.Message
+ "," + args.Reason.ToString()
+ "," + ord.AccountID
+ "," + ord.Action.ToString()
+ "," + ord.AvgFilledPrice.ToString()
+ "," + ord.EnteredQuantity.ToString()
+ "," + ord.EnteredTime.ToString()
+ "," + ord.FilledQuantity.ToString()
+ "," + ord.FilledTime.ToString()
+ "," + ord.OrderID.ToString()
+ "," + ord.Originator.ToString()
+ "," + ord.Route.ToString()
+ "," + ord.State.ToString()
+ "," + ord.StateDetail.ToString()
+ "," + ord.StopPrice.ToString()
+ "," + ord.Symbol.ToString()
+ "," + ord.SymbolExtension.ToString()
+ "," + ord.Type.ToString()
} ;

end else begin
tmpString =
args.State.ToString()
+ "," + args.AccountID
+ "," + args.Message
+ "," + args.OrderID
+ "," + args.Reason.ToString()
+ "," + args.Symbol
;
end;

Fileappend(LogFileName, tmpString + newline);
lastElapsed = sw.ElapsedMilliseconds;

end;
end;

// Once (EnableLogging) begin Fileappend(LogFileName, newline + "Logging Active" + newline); end;
plot1(MessagesLogged, "Log Messages");

Reply With Quote
  #4 (permalink)
 
Fu510n's Avatar
 Fu510n 
Suffield, CT
 
Experience: Advanced
Platform: MC, TS, Python, Rust
Broker: IB, IQFeed, TS, Kraken
Trading: ES, NQ, RTY, YM, CL, RB, 6E
Frequency: Several times daily
Duration: Seconds
Posts: 144 since Oct 2009
Thanks Given: 902
Thanks Received: 143

I ripped the code below from my own 10,000+ line EL strategy - no guarantees as to accuracy (though it does compile) or applicability but it will hopefully give you some ideas on benchmarking strategy code that doesn't require TradeStation-only "method" support (which would be a nice MC enhancement at some point).

 
Code
[IntrabarOrderGeneration = True]

vars:
  IntraBarPersist isStrategyAuto( False ),
  IntraBarPersist Tick( 0 ),
  IntraBarPersist _ContractsPerTrade( 1 ),
  IntraBarPersist EntryBar( -1 ),
  IntraBarPersist EntryTick( -1 ),
  IntraBarPersist EntryTime_s( 0.0 ),
  IntraBarPersist PendingTick( -1 ),
  IntraBarPersist PendingTime_s( 0.0 ),
  IntraBarPersist FilledTick( -1 ),
  IntraBarPersist FilledTime_s( 0.0 ),
  TradeTimeOK( True ),  // if not IsHoliday and NewsOK and Time >= TimeStart and Time < _TimeEnd and DayOfWeek( Date ) >= Monday and DayOfWeek( Date ) <= Friday
  ProfitLossOK( True ); // if TodayLoss <= -DailyLossMax


once begin
  Tick = 0;
end;

if BarStatus( 1 ) = -1 then begin // "between ticks" (MTF only?)
  #return;
end else
  Tick = Tick + 1;

{
| If trading live, don't bother processing historical data so orders aren't generated
}
isStrategyAuto = GetAppInfo( aiStrategyAuto ) = 1;
if isStrategyAuto and GetAppInfo( aiRealTimeCalc ) <> 1 then #return;

if EntryBar = -1 and MarketPosition = 0 and ProfitLossOK and TradeTimeOK and BarStatus(1) = 2 then begin

  value1 = 0; // some strategy that generates 1 for longs and -1 for shorts on bar close

  if value1 <> 0 then begin
  
    if value1 = 1 then
      Buy( "LE" {"LONG entry"} ) _ContractsPerTrade Contracts Next Bar at Market
    else
    if value1 = -1 then
      SellShort( "SE" {"SHORT entry"} ) _ContractsPerTrade Contracts Next Bar at Market;

    EntryBar = CurrentBar + MaxBarsBack;
    EntryTick = Tick;
    EntryTime_s = Time_s + MillisecondsFromDateTime( DateTime ) / 1000;
    PendingTick = -1;
    PendingTime_s = 0.0;
    FilledTick = -1;
    FilledTime_s = 0.0;

  end;
end;
  
if EntryBar <> -1 and CurrentContracts > 0 then begin

  if PendingTick = -1 then begin
    PendingTick = Tick;
    PendingTime_s = Time_s + MillisecondsFromDateTime( DateTime ) / 1000;
  end;

  if FilledTick = -1 and (MarketPosition_at_Broker <> 0 or not isStrategyAuto) then begin
    FilledTick = Tick;
    FilledTime_s = Time_s + MillisecondsFromDateTime( DateTime ) / 1000;

    MessageLog(
      "EntryTick=[", EntryTick:0:0, "] ",
      "EntryTime=[", EntryTime_s:0:3, "] ",
      "PendingTick=[", PendingTick:0:0, "] ",
      "PendingTime=[", PendingTime_s:0:3, "] ",
      "PendingElapsed=[", (TimeToSeconds( IntPortion( PendingTime_s )) + FracPortion( PendingTime_s )) - (TimeToSeconds( IntPortion( EntryTime_s )) + FracPortion( EntryTime_s )):0:3, "s] ",
      "FilledTime=[", FilledTime_s:0:3, "] ",
      "FilledTick=[", FilledTick:0:0, "] ",
      "FilledElapsed=[", (TimeToSeconds( IntPortion( FilledTime_s )) + FracPortion( FilledTime_s )) - (TimeToSeconds( IntPortion( PendingTime_s )) + FracPortion( PendingTime_s )):0:3, "s] ",                  
      "MarketPosition=[", MarketPosition:0:0, "] ",
      "MarketPosition_at_Broker=[", MarketPosition_at_Broker:0:0, "] ",
      "MarketPosition_at_Broker_for_The_Strategy=[", MarketPosition_at_Broker_for_The_Strategy:0:0, "] ",
      "CurrentContracts=[", CurrentContracts:0:0, "]"
    );
  end;
end;

if EntryBar <> -1 and CurrentContracts > 0 and FilledTick <> -1 then begin
  // manage exit here
  EntryBar = -1; // on trade close..
end else begin
  if EntryBar <> -1 and EntryTick <> Tick then begin
    if CurrentBar + MaxBarsBack > EntryBar then begin
      // delayed entry past desired signal bar (market moving too fast?); either way, bail ASAP rather than take a chance
    end;
  end;
end;

Follow me on Twitter Reply With Quote
Thanked by:
  #5 (permalink)
TornadoES
Madrid, Spain
 
Posts: 7 since Feb 2016
Thanks Given: 7
Thanks Received: 1


Fu510n View Post
I ripped the code below from my own 10,000+ line EL strategy - no guarantees as to accuracy (though it does compile) or applicability but it will hopefully give you some ideas on benchmarking strategy code that doesn't require TradeStation-only "method" support (which would be a nice MC enhancement at some point).

 
Code
[IntrabarOrderGeneration = True]

vars:
  IntraBarPersist isStrategyAuto( False ),
  IntraBarPersist Tick( 0 ),
  IntraBarPersist _ContractsPerTrade( 1 ),
  IntraBarPersist EntryBar( -1 ),
  IntraBarPersist EntryTick( -1 ),
  IntraBarPersist EntryTime_s( 0.0 ),
  IntraBarPersist PendingTick( -1 ),
  IntraBarPersist PendingTime_s( 0.0 ),
  IntraBarPersist FilledTick( -1 ),
  IntraBarPersist FilledTime_s( 0.0 ),
  TradeTimeOK( True ),  // if not IsHoliday and NewsOK and Time >= TimeStart and Time < _TimeEnd and DayOfWeek( Date ) >= Monday and DayOfWeek( Date ) <= Friday
  ProfitLossOK( True ); // if TodayLoss <= -DailyLossMax


once begin
  Tick = 0;
end;

if BarStatus( 1 ) = -1 then begin // "between ticks" (MTF only?)
  #return;
end else
  Tick = Tick + 1;

{
| If trading live, don't bother processing historical data so orders aren't generated
}
isStrategyAuto = GetAppInfo( aiStrategyAuto ) = 1;
if isStrategyAuto and GetAppInfo( aiRealTimeCalc ) <> 1 then #return;

if EntryBar = -1 and MarketPosition = 0 and ProfitLossOK and TradeTimeOK and BarStatus(1) = 2 then begin

  value1 = 0; // some strategy that generates 1 for longs and -1 for shorts on bar close

  if value1 <> 0 then begin
  
    if value1 = 1 then
      Buy( "LE" {"LONG entry"} ) _ContractsPerTrade Contracts Next Bar at Market
    else
    if value1 = -1 then
      SellShort( "SE" {"SHORT entry"} ) _ContractsPerTrade Contracts Next Bar at Market;

    EntryBar = CurrentBar + MaxBarsBack;
    EntryTick = Tick;
    EntryTime_s = Time_s + MillisecondsFromDateTime( DateTime ) / 1000;
    PendingTick = -1;
    PendingTime_s = 0.0;
    FilledTick = -1;
    FilledTime_s = 0.0;

  end;
end;
  
if EntryBar <> -1 and CurrentContracts > 0 then begin

  if PendingTick = -1 then begin
    PendingTick = Tick;
    PendingTime_s = Time_s + MillisecondsFromDateTime( DateTime ) / 1000;
  end;

  if FilledTick = -1 and (MarketPosition_at_Broker <> 0 or not isStrategyAuto) then begin
    FilledTick = Tick;
    FilledTime_s = Time_s + MillisecondsFromDateTime( DateTime ) / 1000;

    MessageLog(
      "EntryTick=[", EntryTick:0:0, "] ",
      "EntryTime=[", EntryTime_s:0:3, "] ",
      "PendingTick=[", PendingTick:0:0, "] ",
      "PendingTime=[", PendingTime_s:0:3, "] ",
      "PendingElapsed=[", (TimeToSeconds( IntPortion( PendingTime_s )) + FracPortion( PendingTime_s )) - (TimeToSeconds( IntPortion( EntryTime_s )) + FracPortion( EntryTime_s )):0:3, "s] ",
      "FilledTime=[", FilledTime_s:0:3, "] ",
      "FilledTick=[", FilledTick:0:0, "] ",
      "FilledElapsed=[", (TimeToSeconds( IntPortion( FilledTime_s )) + FracPortion( FilledTime_s )) - (TimeToSeconds( IntPortion( PendingTime_s )) + FracPortion( PendingTime_s )):0:3, "s] ",                  
      "MarketPosition=[", MarketPosition:0:0, "] ",
      "MarketPosition_at_Broker=[", MarketPosition_at_Broker:0:0, "] ",
      "MarketPosition_at_Broker_for_The_Strategy=[", MarketPosition_at_Broker_for_The_Strategy:0:0, "] ",
      "CurrentContracts=[", CurrentContracts:0:0, "]"
    );
  end;
end;

if EntryBar <> -1 and CurrentContracts > 0 and FilledTick <> -1 then begin
  // manage exit here
  EntryBar = -1; // on trade close..
end else begin
  if EntryBar <> -1 and EntryTick <> Tick then begin
    if CurrentBar + MaxBarsBack > EntryBar then begin
      // delayed entry past desired signal bar (market moving too fast?); either way, bail ASAP rather than take a chance
    end;
  end;
end;

Thanks for the post FU510n.

I am trying to understand the code I am stuck with the expression "EntryTick:0:0". What does this instruction do?

Thanks in advance.

Reply With Quote
  #6 (permalink)
 
Fu510n's Avatar
 Fu510n 
Suffield, CT
 
Experience: Advanced
Platform: MC, TS, Python, Rust
Broker: IB, IQFeed, TS, Kraken
Trading: ES, NQ, RTY, YM, CL, RB, 6E
Frequency: Several times daily
Duration: Seconds
Posts: 144 since Oct 2009
Thanks Given: 902
Thanks Received: 143

Lifting from Print or even on BMT in various places MultiCharts [AUTOLINK]Easylanguage[/AUTOLINK] Price Decimal Points ... it's just output formatting (that in the case of "EntryTick:0:0" just prints it as a basic integer without ANY decimal places).

Quoting 
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.
The default output format for a numerical expression is two decimal places and a minimum of seven characters.

If the number of decimal places in the numerical expression is more than the specified number, the value will be will be rounded off to the specified number of decimal places.

If the number of characters in the output is less than the specified minimum, leading spaces will be added to bring the output to the specified minimum value.


Follow me on Twitter Reply With Quote
Thanked by:




Last Updated on November 1, 2016


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts