Why MT4 - ChartSetSymbolPeriod() - slows down the platform? - indicator

I try to use ChartSetSymbolPeriod() for my [ Custom Indicator ], but this indicator slows down my MT4 platform when I try to use it with another [ Expert Advisors ].
Specially while 'Order, Depth of Market' type of [ Expert Advisors ].
//+------------------------------------------------------------------+
//| ChangeSymbol Indicator.mq4 |
//| Copyright 2016, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property indicator_chart_window
string ChangeSP = "Where I go?";
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit() {
//---
ObjectCreate ( 0, ChangeSP, OBJ_BUTTON, 0, 0, 0 );
ObjectSetInteger ( 0, ChangeSP, OBJPROP_XDISTANCE, 15 );
ObjectSetInteger ( 0, ChangeSP, OBJPROP_YDISTANCE, 100 );
ObjectSetInteger ( 0, ChangeSP, OBJPROP_XSIZE, 200 );
ObjectSetInteger ( 0, ChangeSP, OBJPROP_YSIZE, 40 );
ObjectSetString ( 0, ChangeSP, OBJPROP_TEXT, "Go to GBPUSD M15" );
ObjectSetInteger ( 0, ChangeSP, OBJPROP_COLOR, White );
ObjectSetInteger ( 0, ChangeSP, OBJPROP_BGCOLOR, Red );
ObjectSetInteger ( 0, ChangeSP, OBJPROP_BORDER_COLOR, Red );
ObjectSetInteger ( 0, ChangeSP, OBJPROP_BORDER_TYPE, BORDER_FLAT );
ObjectSetInteger ( 0, ChangeSP, OBJPROP_BACK, false );
ObjectSetInteger ( 0, ChangeSP, OBJPROP_HIDDEN, true );
ObjectSetInteger ( 0, ChangeSP, OBJPROP_STATE, false );
ObjectSetInteger ( 0, ChangeSP, OBJPROP_FONTSIZE, 12 );
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start(){
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit(){
return(0);
}
//+------------------------------------------------------------------+
void OnChartEvent( const int id,
const long &lparam,
const double &dparam,
const string &sparam
) {
if ( sparam == ChangeSP ) {
ChangeSPClick( ChangeSP );
ObjectSetInteger( 0, ChangeSP, OBJPROP_STATE, false );
}
}
//+------------------------------------------------------------------+
void ChangeSPClick( bool ChartSetSymbolPeriod ) {
bool ChangeSP_action = ChartSetSymbolPeriod( 0, "GBPUSD", 15 );
}

Performance? First: ALL [ Custom Indicators ] SHARE one SINGLE THREAD !
This New-MQL4.56789 architecture feature imposes even higher need for due care to be taken for non-blocking, performance focused code in [ Custom Indicators ].
An MQL4 documentation next states, that a call to ChartSetSymbolPeriod() is not synchronous, but just adds one more ticket into a TaskQueue.
ChartSetSymbolPeriod()Changes the symbol and period of the specified chart. The function is asynchronous, i.e. it sends the command and does not wait for its execution completion. The command is added to chart message queue and executed only after all previous commands have been processed.
What else might be in the Queue already?MQL4 recognises the following types of <ChartEVENT>-s :
OnChartEvent() is the handler of a group of ChartEvent events:
·CHARTEVENT_KEYDOWN — event of a keystroke, when the chart window is focused;
·CHARTEVENT_MOUSE_MOVE — mouse move events and mouse click events ( if CHART_EVENT_MOUSE_MOVE = true is set for the chart );
·CHARTEVENT_OBJECT_CREATE — event of graphical object creation ( if CHART_EVENT_OBJECT_CREATE = true is set for the chart );
·CHARTEVENT_OBJECT_CHANGE — event of change of an object property via the properties dialog;
·CHARTEVENT_OBJECT_DELETE — event of graphical object deletion ( if CHART_EVENT_OBJECT_DELETE = true is set for the chart );
·CHARTEVENT_OBJECT_CLICK — event of a mouse click in a graphical object belonging to the chart;
·CHARTEVENT_OBJECT_DRAG — event of a graphical object move using the mouse;
·CHARTEVENT_OBJECT_ENDEDIT — event of the finished text editing in the entry box of the LabelEdit graphical object;
·CHARTEVENT_CLICK — event of a mouse click on the chart;
·CHARTEVENT_CHART_CHANGE — event of chart changes; <<<<<<<<<<<<<<<<<<<<
·CHARTEVENT_CUSTOM + n — ID of the user event, where n is in the range from 0 to 65535.
·CHARTEVENT_CUSTOM_LAST — the last acceptable ID of a custom event == ( CHARTEVENT_CUSTOM +65535 ).
A change of Symbol and Period is a major chart undertaking, it makes [ MetaTrader Terminal 4 ] to throw away all current state of the instrument depicted inside the chart, next to move into the Back-of-the-House and to fetch all historically saved records from a [HistoryCentre] ( try F2 to see that facility in action ) and it has to repaint the GUI accordingly.
And guess what,
1) that takes some time
2) that makes a <ChartEVENT> which, again, triggers, the OnChartEvent() handler.
3) move back to the "Square No. 1"
Does it create a mouse-trap-wheel arrangement, to have to run infinitely in a loop?Yes, it does.
Also, one might have already noticed a side-effect
A name in a function call signature masks the name of MQL4 Function
//+------------------------------------------------------------------+
void ChangeSPClick( bool ChartSetSymbolPeriod ) {
bool ChangeSP_action = ChartSetSymbolPeriod( 0, "GBPUSD", 15 );
}

Related

Wait for 30 seconds before plotting buy or sell signals in amibroker

Hi I am new to coding and i am trying to automate using the given code. I found an afl for intraday trading which sometime repaint signals. I want the system to wait for 30 seconds if its a 5min candle, 90 seconds for 15min candle so on.
I tried using
SecsSinceUpdateNum = DateTimeDiff(dateNowNum, lastSymbolDateNum);
timenum();
function to get the time of signal generated and compare whether the signal exists after 30sec if true plot buy or sell signals.
please help me with the code. thank you in advance
Odd=13;
CoefOdd=round(Odd/2);
Even=12;
Coefeven=Even/2;
Coefeven2=Coefeven+1;
CongestionPercent=2.8;
TriangularOdd=MA(MA(C,CoefOdd),CoefOdd);
TriangularEven=MA(MA(C,Coefeven),Coefeven2);
finalMov_avg=IIf(Odd > even,triangularOdd,TriangularEven);
Color=colorBrightGreen;
tickercolor=colorWhite;
Plot(finalMov_avg,"",IIf(C < finalmov_avg,colorRed,Color),styleLine|styleThick);
Plot(C,"",tickercolor,styleCandle);
Title=Name()+"..."+"( "+WriteIf(Odd > even,WriteVal(Odd,1),WriteVal(even,1))+" ) Period "+EncodeColor(Color)+"Triangular"+WriteIf(Odd > even,"ODD","EVEN")+" Moving Average"+"..."+EncodeColor(colorBlack)+ WriteIf(C < finalMov_avg,"Close is "+EncodeColor(colorRed)+"Below"+EncodeColor(colorBlack)+" Moving Average by ","Close is"+EncodeColor(colorBrightGreen)+" Above"+EncodeColor(colorBlack)+" Moving Average by ")+"("+WriteVal(((C/finalMov_avg)-1)*100,1.1)+"% )"+"\n"+WriteIf(finalmov_avg-Ref(finalmov_avg,-1)>0," Slope Of Average is UP : ","Slope Of Average is DOWN :")+WriteIf((((C/finalMov_avg)-1)*100 <= CongestionPercent AND ((C/finalMov_avg)-1)*100 >= -CongestionPercent),EncodeColor(colorYellow)+" with Price Congestion / Divergence to Average ","")+"\n"+WriteIf(Ref(C,-1) < Ref(finalmov_avg,-1) AND C > finalmov_avg,EncodeColor(colorGreen)+"Possible Change in Trend From Down to Up"+"\n"+" OR Short Term Correction of Previous Trend",WriteIf(Ref(C,-1) > Ref(finalmov_avg,-1) AND C < finalmov_avg,EncodeColor(colorRed)+"Possible Change in Trend From Up to Down "+"\n"+" OR Short Term Correction to Previous Trend",""))+"\n"+WriteIf(C > finalmov_avg,EncodeColor(colorGreen)+"Close has been above Moving Average ( "+WriteVal(BarsSince(C < finalmov_avg),1)+" ) Bars",EncodeColor(colorRed)+"Close has been Below Moving Average ( "+WriteVal(BarsSince(C > finalmov_avg),1)+" ) Bars")+"\n"+EncodeColor(colorBlack)+"The average # of Bars Above ( "+WriteVal(round(Cum(BarsSince(C < finalmov_avg)/Cum(1))),1)+" )"+"\n"+"The average # of Bars Below ( "+WriteVal(round(Cum(BarsSince(C > finalmov_avg)/Cum(1))),1)+" )";
_SECTION_BEGIN("AFL Example");
SetBarsRequired(10000,10000); /* this ensures that the charts include all bars AND NOT just those on screen */
SetFormulaName("Sample System"); /*name it for backtest report identification */
SetTradeDelays( 1, 1, 1, 1 ); /* delay entry/exit by one bar */
SetOption( "initialequity", 100000 ); /* starting capital */
PositionSize = -10; /* trade size will be 10% of available equty */
SetOption( "MaxOpenPositions", 6 ); /* I don't want to comit more than 60% of Equity at any one time */
SetOption( "PriceBoundChecking", 1 ); /* trade only within the chart bar's price range */
SetOption( "CommissionMode", 2 ); /* set commissions AND costs as $ per trade */
SetOption( "CommissionAmount", 32.95 ); /* commissions AND cost */
SetOption( "UsePrevBarEquityForPosSizing", 1 ); /*set the use of last bars equity for trade size*/
PositionScore = 100/C; /*Set the order for which stock trades when get mulitple signals in one bar in backtesting */
LongPer = Param("Long Period", 50, 30, 100, 5 ); /* select periods with parameter window */
ShortPer = Param("Short Period", 5, 3, 10, 1 );
LongMA = EMA( C, LongPer );
ShortMA = EMA( C, ShortPer );
LastHigh = HHV( H, LongPer );
Buy1 = Cross( ShortMA, LongMA ) AND H > Ref( LastHigh, -1 );
Sell1 = Cross( LongMA, ShortMA );
Buy = Ref(buy1,-1);
sell = ref(sell1,-1);
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
Filter = Buy OR Sell;
AddTextColumn( FullName(), "Company Name" );
AddColumn( Buy, "Buy", 1 );
AddColumn( Sell, "Sell", 1 );
AddColumn( C, "Close", 1.3 );
AddColumn( H, "High", 1.3 );
AddColumn( LastHigh, "HHV", 1.3 );
AddColumn( LongMA, "Long MA", 1,3 );
AddColumn( ShortMA, "Short MA", 1,3 );
GraphXSpace = 10;
//Plot( C, " Close Price", colorGrey50, styleBar );
Plot( LongMA, " EMA(C,"+WriteVal(LongPer,1)+")", colorBrown, styleLine|styleNoRescale );
Plot( ShortMA, " EMA(C,"+WriteVal(ShortPer,1)+")", colorBlue, styleLine|styleNoRescale );
Plot( Ref(Lasthigh,-1), " HHV(H,"+WriteVal(LongPer,1)+")", colorRed, styleNoLine|styleDots|styleNoRescale );
//PlotShapes( shapeUpArrow*Buy, colorGreen, 0, L, -10 );
//PlotShapes( shapeDownArrow*Sell, colorRed, 0, H, -10 );
Title = " {{NAME}} {{DATE}} {{INTERVAL}} "+_DEFAULT_NAME()+" Chart values : {{VALUES}} ";
_SECTION_END();
no=Param( "Swing", 5, 1, 55 );
res=HHV(H,no);
sup=LLV(L,no);
tsl=IIf(ValueWhen(IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0))!=0,IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0)),1)==1,sup,res);
Buy = Cross(C,res) ;
Sell = Cross(sup,C) ;
Buy = ref(Buy,-1);
Sell = Ref(Sell,-1);
_SECTION_END();
a=C;
g=(EMA(Close,3) * (2 / 4 - 1)-EMA(Close,5) * (2 / 6 - 1)) / (2 /4- 2 /6);
e=Ref(tsl,-1);
Buy = Cross(C,tsl) ;
Sell = Cross(tsl,C) ;
SellPrice=ValueWhen(Sell,e,1);
BuyPrice=ValueWhen(Buy,e,1);
Long=Flip(Buy,Sell);
Shrt=Flip(Sell,Buy );
Filter=Buy OR Sell;
Buy = Cross(C,tsl) ;
Sell = Cross(tsl,C) ;
shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes( shape, IIf( Buy, colorGreen, colorRed ),0, IIf( Buy, Low, High ) );
a1=Ref(tsl,-1);
dist = 0.8*ATR(1); //0.8
dist1 = 1.8*ATR(1); //1.2
for( i = 0; i < BarCount; i++ )
{
if( Buy[i] )
{
PlotText( "Buy:" + L[ i ] + "\nTgt: " + (a1[i]*1.005) + "\nSL: " + (tsl[i]*0.9975), i, L[ i ]-dist[i], colorLime);
}
if( Sell[i] )
{
PlotText( "Sell:" + H[ i ] + "\nT: " + (a1[i]*0.995) + "\nSL: " + (tsl[i]*1.0025), i, H[ i ]+dist1[i], colorred);
}
}

Unable to add this custom metric to Amibroker backtest report

I would like to add an extra column to indicate volatility in the backtest report.
Here is my code. The extra column volatility_recent appears but no value appears in the column. However, if I were to use the commented line trade.AddCustomMetric( "proceeds", trade.Shares*trade.ExitPrice );, some numerical value appears in the column.
What is wrong with the code?
if ( Status( "action" ) == actionPortfolio )
{
bo = GetBacktesterObject();
// run default backtest procedure without generating the trade list
bo.Backtest( True );
volatility_recent = ATR(30);
// iterate through closed trades
for ( trade = bo.GetFirstTrade( ); trade; trade = bo.GetNextTrade( ) )
{
trade.AddCustomMetric( "volatility_recent", volatility_recent );
//trade.AddCustomMetric( "proceeds", trade.Shares*trade.ExitPrice );
}
// iterate through open positions
for ( trade = bo.GetFirstOpenPos( ); trade; trade = bo.GetNextOpenPos( ) )
{
trade.AddCustomMetric( "volatility_recent", volatility_recent );
//trade.AddCustomMetric( "proceeds", trade.Shares*trade.ExitPrice );
}
// generate trade list
bo.ListTrades( );
}
I'm finding it really interesting that you copy text and code solution by others line by line without giving reference.
Your second post here at stackoverflow is line by line copy of responses by Tomasz and me to you at forum.amibroker.com
https://forum.amibroker.com/t/unable-to-add-this-custom-metric-to-backtest-report/7153
Custom metric needs to be scalar (number), not array. ATR(30) is an array. So, use LastValue to get last value of array or Lookup to get the value at specified bar. Pass ATR array of symbol from 1st phase to 2nd phase of backtest via static variables. Then in custom metric line use lookup to extract array element at certain date time (trade.EntryDateTime or trade.ExitDateTime).
StaticVarSet( "CBT_ATR_" + Name(), ATR(30) );
if ( Status( "action" ) == actionPortfolio )
{
bo = GetBacktesterObject();
// run default backtest procedure without generating the trade list
bo.Backtest( True );
// iterate through closed trades
for ( trade = bo.GetFirstTrade( ); trade; trade = bo.GetNextTrade( ) )
{
trade.AddCustomMetric( "volatility_recent", Lookup( StaticVarGet( "CBT_ATR_" + trade.Symbol ), trade.ExitDateTime ) );
//trade.AddCustomMetric( "proceeds", trade.Shares*trade.EntryPrice );
}
// iterate through open positions
for ( trade = bo.GetFirstOpenPos( ); trade; trade = bo.GetNextOpenPos( ) )
{
trade.AddCustomMetric( "volatility_recent", Lookup( StaticVarGet( "CBT_ATR_" + trade.Symbol ), Trade.ExitDateTime ) );
//trade.AddCustomMetric( "proceeds", trade.Shares*trade.EntryPrice );
}
// generate trade list
bo.ListTrades( );
}
EDIT: The credit goes to fxshrat who posted the answer at https://forum.amibroker.com/t/unable-to-add-this-custom-metric-to-backtest-report/7153/2
His answer was posted here and it was rude to post without references. Apologies to fxshrat and Tomasz.

Prevent Amibroker from entering the same day as its exit in the same symbol

I am attempting to create a share/stock portfolio based system that will enter at the open and possibly exit on the same day at close if the conditions are met. I have this basicaly working. The thing i cant get going is that I would like my stock system to only ever have 1 open postion in a company at any time.
It seems that if there is both an exit and an entry on the same day, amibroker backtesting is allowing the same company to be purchased on the open, if that same company has a sell order on that same day. Here is an example of this:
Notice at point 1 - we would be entering at the open on the 17th
At point 2, we get a sell signal that day, so we should exit at Close on the 24th.
However at point 3 - we have an entry for the same company on the same day.
To be clear - I would like to allow multiple entries on the same day - this is working. The only thing i would like to figure out is to prevent the backtester from entering the SAME company on the SAME day it exits, as due to the system rules, we would have one day of having 2 positions in the 1 company.
Here is the sample code to replicate this:
SetOption("AllowSameBarExit", True );
SetOption("SettlementDelay", 1 );
Buy = C > MA(C,10);
Sell = C < MA(C,10) OR C > O;
// trade on todays open
SetTradeDelays( 0, 0, 0, 0 );
BuyPrice = Open;
SellPrice = Close;
SetPositionSize( 20, spsPercentOfEquity );
I have read and re-read the page on portfolio timing: here but I still cant figure out how to prevent the entries for the same company on the same day as an exit.
Any help would be greatly appreciated!
UPDATE
It appears that using the OR C > O in the SELL condition is effecting this. If I remove the OR C > O part, I get the correct behaviour. It is entering on the NEXT day. Now Im wondering how to use that exit without reverting back to same bar same company entry and exit...
Thanks to Tomasz from Amibroker for posting the below solution:
SetOption("AllowSameBarExit", True );
BuyPrice = Open;
SellPrice = Close;
Buy = Ref( Close > MA( Close, 10 ), -1 );
Sell = Close > Open OR Close < MA( Close,10);
// removing buys you don't want
intrade = False;
for( i = 0; i < BarCount; i++ )
{
if( NOT intrade )
{
if( Buy[ i ] )
{
intrade = True;
// same bar sell
if( Sell[ i ] ) intrade = False;
}
}
else
{
if( Sell[ i ] )
{
intrade = False;
Buy[ i ] = False; // remove buy if exited this bar
}
}
}
You can find : a detailed discussion here

Corona SDK, Timer pause and resume does not work?

My timer pause and resume does not work? it has a logical error which said WARNING:timer.resume(timerID)ignored b/c was not paused. Any idea? Should i add add or remove listener to remove to stop the transition of the timer?
local timerEnabled = true -- Stops or allows our countdown timer
local touchEnabled = false
local delay_timer
local timer_trans -- transition of my timerbar
local timer_rect --display object
local timer_bar -- display object
function pauseGame( )
if (timerEnabled==false) then
timer.pause(timer_bar)
answer_rect:removeEventListener( "touch", buttonTouched )
elseif (timerEnabled==true) then
timer.resume(timer_bar)
answer_rect:addEventListener( "touch", buttonTouched )
end
end
local pauseButtonPress = function( event )
pauseGame()
end
timer_rect = display.newRect(uiGroup, _W*0.5, question_rect.y+question_rect.height, _W, 50 )
timer_rect.anchorY = 0
timer_rect:setFillColor(0,0.3,0.7)
if timerEnabled == true then
timer_bar = display.newRect(uiGroup, 0, timer_rect.y+timer_rect.height-20, _W, 35)
timer_bar.anchorX = 0
timer_bar:setFillColor(0.8,1,1)
end
pauseButton= widget.newButton{
defaultFile = "images1/Buttons/Pause.png",
overFile= "images1/Buttons/Resume.png",
onRelease = pauseButtonPress,
}
pauseButton.x = 100
pauseButton.y = 90
pauseButton.xScale = .6
pauseButton.yScale = .6
timer.pause() and timer.resume() should receive a timerID as param, not the object.
Example
local function listener( event )
print( "listener called" )
end
timer1 = timer.performWithDelay( 2000, listener ) -- wait 2 seconds
-- sometime later...
local result = timer.pause( timer1 )
print( "Time remaining is " .. result )
More information about timer.pause() and timer.resume() you may find in documentation.

change text of clock display using gstreamer 0.10

os : linux
gstreamer version : 0.10(this is requirement , so I cant migrate to 1.0)
Issue: change default text with grey to something more visible like black for clock display
Summary:
I am using gstreamer c code to stream video from webcam and at top of video it display time but unfortunately its in grey so its difficult to see it . Can someone please let me know how to make to black or to some other colour.
below is my snippet of code.
source = gst_element_factory_make( "v4l2src", "source" );
g_object_set( G_OBJECT ( source ), "device", "/dev/video0", NULL );
clockDisplay = gst_element_factory_make( "clockoverlay", "clock-time" );
capsFilter = gst_element_factory_make( "capsfilter", "camera_caps" );
conv = gst_element_factory_make( "ffmpegcolorspace", "Colorconverter" );
videoRate = gst_element_factory_make( "videorate", "videorate-element");
capsFilterRate = gst_element_factory_make( "capsfilter", "video-rate");
videoEnc = gst_element_factory_make( "ffenc_mpeg4", "videoenc" );
udpSink = gst_element_factory_make( "udpsink", "udpsink" );
g_object_set( G_OBJECT( udpSink ),
"host", hostAdd.c_str(),
"port", PORT_NUM_REMOTE_FOR_STREAMING,
NULL
);
// cap filter #1
caps = gst_caps_from_string( "video/x-raw-yuv,format=(fourcc)YUY2,width=320,height=240,framerate=10/1" );
g_object_set ( capsFilter, "caps", caps, NULL );
gst_caps_unref( caps );
You can list the properties of clockoverlay with:
gst-inspect-1.0 clockoverlay
There should be properties related to the color of the text. In 1.0 it is named 'color'. Then just use g_object_set to set the color as an int in ARGB format. There is also an 'outline-color' to make it even more visible.

Resources