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