TimeFrameSet with Foreign - RSI

Hi,

I have run into an issue with using Foreign and TimeFrameSet. I did read the existing posts as well as the user guide to figure if i did some obvious mistake, but I am unable to work this out.

Here is a snippet of the code (The code is running on a 1min timeframe):

strike = 51000;
strategy_name = "BNF_RSI_OPT_ATM";
symbol = "BANKNIFTY";
PE = "PE.NFO";
CE = "CE.NFO";
expiry_final = "28AUG24";

lastBartime2 = Status("lastbarend");
recordedTimestamp2 = Nz( StaticVarGet( varName2 ) );

if (lastBarTime2 != recordedTimestamp2)
{
StaticVarSet( varName2, lastBartime2 );

StaticVarSet(strategy_name + "pe_strike",strike,1);
pe_price = Foreign(symbol + expiry_final + StrFormat("%.0f",(StaticVarGet(strategy_name + "pe_strike"))) + PE,"C");
StaticVarSet(strategy_name + "pe_price",LastValue(Ref(pe_price,-1)),1);

SetForeign(symbol + expiry_final + StrFormat("%.0f",(StaticVarGet(strategy_name + "pe_strike"))) + PE);
TimeFrameSet(in5Minute);
RSI_pe_5min = RSI(14);
TimeFrameRestore();
RSI_pe = TimeFrameExpand(RSI_pe_5min,in5Minute);


StaticVarSet(strategy_name + "RSI_pe",RSI_pe,1);

StaticVarSet(strategy_name + "ce_strike",strike,1);
ce_price = Foreign(symbol + expiry_final + StrFormat("%.0f",(StaticVarGet(strategy_name + "ce_strike"))) + CE,"C");
StaticVarSet(strategy_name + "ce_price",LastValue(Ref(ce_price,-1)),1);

SetForeign(symbol + expiry_final + StrFormat("%.0f",(StaticVarGet(strategy_name + "ce_strike"))) + CE);
TimeFrameSet(in5Minute);
RSI_ce_5min = RSI(14);
TimeFrameRestore();
RSI_ce = TimeFrameExpand(RSI_ce_5min,in5Minute);

StaticVarSet(strategy_name + "RSI_ce",RSI_ce,1);
}

As soon as I have this portion of the code run, Amibroker just crashes without any error message.
I tried adding _TRACE comments into debugview to figure whats happening:

Code with trace added:

if (lastBarTime2 != recordedTimestamp2)
{
StaticVarSet( varName2, lastBartime2 );

_TRACE("Entering PE no position loop");
StaticVarSet(strategy_name + "pe_strike",strike,1);
_TRACE("PE Strike selected " + StaticVarGet(strategy_name + "pe_strike"));
pe_price = Foreign(symbol + expiry_final + StrFormat("%.0f",(StaticVarGet(strategy_name + "pe_strike"))) + PE,"C");
StaticVarSet(strategy_name + "pe_price",LastValue(Ref(pe_price,-1)),1);
_TRACE("PE Price " + StaticVarGet(strategy_name + "pe_price"));

_TRACE("Entering PE Set Foreign");
_TRACE("Symbol : "+ symbol + expiry_final + StrFormat("%.0f",(StaticVarGet(strategy_name + "pe_strike"))) + PE);
SetForeign(symbol + expiry_final + StrFormat("%.0f",(StaticVarGet(strategy_name + "pe_strike"))) + PE);
_TRACE("Entering Time frame set");
TimeFrameSet(in5Minute);
RSI_pe_5min = RSI(14);
TimeFrameRestore();
_TRACE("Exiting time frame set and restore");
_TRACE("Last value of RSI 14 :" +LastValue(Ref(RSI_pe_5min,-1)));
RSI_pe = TimeFrameExpand(RSI_pe_5min,in5Minute);
_TRACE("Timeframe expand completed for RSI PE");
_TRACE("Last value of RSI 14 :" +LastValue(Ref(RSI_pe,-1)));


StaticVarSet(strategy_name + "RSI_pe",RSI_pe,1);
_TRACE("StaticVarSet completed for RSI PE");
_TRACE("Last value of RSI 14 under staticvar:" +LastValue(Ref(StaticVarGet(strategy_name + "RSI_pe"),-1)));

_TRACE("Completed PE side");

_TRACE("Entering CE no position loop");
StaticVarSet(strategy_name + "ce_strike",strike,1);
_TRACE("CE Strike selected " + StaticVarGet(strategy_name + "ce_strike"));
ce_price = Foreign(symbol + expiry_final + StrFormat("%.0f",(StaticVarGet(strategy_name + "ce_strike"))) + CE,"C");
StaticVarSet(strategy_name + "ce_price",LastValue(Ref(ce_price,-1)),1);
_TRACE("CE Price " + StaticVarGet(strategy_name + "ce_price"));

_TRACE("Entering CE Set Foreign");
_TRACE("Symbol : "+ symbol + expiry_final + StrFormat("%.0f",(StaticVarGet(strategy_name + "ce_strike"))) + CE);
SetForeign(symbol + expiry_final + StrFormat("%.0f",(StaticVarGet(strategy_name + "ce_strike"))) + CE);
_TRACE("Entering Time frame set");
TimeFrameSet(in5Minute);
RSI_ce_5min = RSI(14);
TimeFrameRestore();
_TRACE("Exiting time frame set and restore");
_TRACE("Last value of RSI 14 :" +LastValue(Ref(RSI_ce_5min,-1)));
RSI_ce = TimeFrameExpand(RSI_ce_5min,in5Minute);
_TRACE("Timeframe expand completed for RSI CE");
_TRACE("Last value of RSI 14 :" +LastValue(Ref(RSI_ce,-1)));

StaticVarSet(strategy_name + "RSI_ce",RSI_ce,1);

_TRACE("StaticVarSet completed for RSI CE");
_TRACE("Last value of RSI 14 under staticvar:" +LastValue(Ref(StaticVarGet(strategy_name + "RSI_ce"),-1)));


_TRACE("Completed CE side");
}

Trace screenshot:

One thing weird I noticed was that towards the end of the trace output - the value assigned to RSI_ce and the value by just printing it after assigning it to StaticVarSet are both different.

Need some help figuring whats going wrong here please.

If any other information is required, please let me know.

Thank you,
Siddhant Kankani

If i remove the TimeFrame functions, it works fine.
If i remove the Foreign and work on the base symbol, then also there is no issue.

Something that I am doing wrong with the timeframe functions and RSI with foreign but unable to figure out.

THanks
Siddhant

You don't even say what version and what bitness and what data source you are using.

Please follow this advice: How to ask a good question

My apologies for the oversight.

Amibroker version : 6.40.4
The realtime plugin is Globaldatafeeds, but I am facing this issue even without RT data.
Tried running the same on a local DB with bar replay and still had the issue.

Thanks
Siddhant

When you are being asked to provide version and BITNESS, you are supposed to answer with BOTH version and BITNESS

It is absolutely CRUCIAL to know if it is 32-bit or 64-bit.

Sorry again,
Its a 64-bit version.

Some additional info,

Looks like there is some error with the StaticVarSet.
As soon as I comment that out, the program doesnt crash anymore.

@Tomasz Can you please guide as to what I may be doing wrong?

Here is a simplified version of the code with which I am able to reproduce the error:

StaticVarSet(strategy_name + "ce_strike",strike,1);
ce_price = Foreign("NIFTY29AUG2425000CE.NFO","C");
StaticVarSet(strategy_name + "ce_price",LastValue(Ref(ce_price,-1)),1);
RSI_ce = RSIa(ce_price,14);

StaticVarSet(strategy_name + "RSI_ce",RSI_ce,1);

As soon as I comment the StaticVarSet(strategy_name + "RSI_ce",RSI_ce,1) - it works fine.
Otherwise, crashes as soon as i click on APPLY.

Thanks
Siddhant

Remove persistent flag from StaticVarSet and retry

Thanks @Tomasz

I changed the StaticVarSet to hold only the last completed bar value instead of the entire array and it worked fine.
StaticVarSet(strategy_name + "RSI_ce",Lastvalue(Ref(RSI_ce,-1)),1)

This works fine as a workaround. But I'm still confused as to why I cant have the entire array stored in the StaticVarSet.

Regards
Siddhant Kankani

I told you remove "Persist" flag. Did you try it?

When you are storing just last value you are using way less memory.

Chances are you are storing way too much data in static variables and you are running out of memory. When application runs out of available memory it might crash because in such unusual situation it can't display any message box as message boxes REQUIRE MEMORY TOO.

Also, any application that allows 3rd party plugins is at the mercy of 3rd party code. 3rd party plugins are often plagued with errors (GlobalDataFeeds is unfortunately no exception) and cause instability, increased memory consumption, memory leaks, and all kind of problems. That is one of the reasons why modern web browsers DON'T ALLOW native binary plugins as history taught them hard lesson that plugins made browsers unstable.

Unfortunately, one can trust 3rd party vendors to deliver bug-free code. The only way to address that is to either don't allow 3rd party plugins or isolate 3rd party plugins completely from the application and run them as completely separate process. This has extra performance cost, but might be the only way to go forward, given quality issues with 3rd party plugins.

The persist flag is important in my case since I am using the data in another code.
However, I understand what may have caused the application to crash - which makes sense since storing only the last value allows it to run perfectly fine.

Thank you for the guidance and the time.

Regards
Siddhant

You should use StaticVarInfo to find out how much memory you are using.