I am trying to code a delayed (for example: 5 bars) stoploss signal after a trade confirmation. The idea is to generate a signal for stoploss 5 bars after confirmation of a trade by ibc.getpositioninfo(Name(), "Avg. Cost"). I have used the code below, but it just won't work.
GetPositionInfo returns a single value, not an array, so you're going to need to keep track of when it first returns a value for your symbol, then store that bar, and then check it each following bar until you hit your target of 5 bars having past. Storing the entry bar number in a persistent static variable will also allow it to be stored between Amibroker restarts, if needed.
Below is one possible way that you could get there. Running this once for each bar from entry onwards should give you the signal after 5 bars of the position being open in IB.
BarNum = BarIndex();
CurrentBar = LastValue(BarNum);
TimeStopBars = 5;
TimeStop = False;
Symbol = Name();
OpenPositions = ibc.GetPositionList();
IBPosOpen = StrFind(OpenPositions, Symbol);
if (!IBPosOpen)
StaticVarRemove("BarEntered" + Symbol); // If no position open, remove static var to "zero" it
else
{
EntryBar = Nz(StaticVarGet("BarEntered" + Symbol)); // recall entry bar in static var
if (EntryBar == 0)
StaticVarSet("BarEntered" + Symbol, CurrentBar, True); // if no entry bar is stored, this must be the entry bar
else
{
if (CurrentBar - EntryBar >= TimeStopBars) // check if the target number of bars have passed since entry
TimeStop = True;
}
}
MySignal = TimeStop;
As advised, I have tested the code with paper trade account and added tracing point to see the flow. It seems that the desired signal is not generating owing to the current bar value was kept constant.
Thanks.
BarNum = BarIndex();
CurrentBar = LastValue(BarNum);
_Trace("BarNum: " + BarNum + ", CurrentBar: " + CurrentBar);
TimeStopBars = 5;
TimeStop = False;
Symbol = Name();
OpenPositions = ibc.GetPositionList();
IBPosOpen = StrFind(OpenPositions, Symbol);
_Trace("OpenPositions: " + OpenPositions);
_Trace("IBPosOpen: " + IBPosOpen);
_Trace("Is IBPosOpen Null? " + IsNull(IBPosOpen));
_Trace("Is IBPosOpen Zero? " + WriteVal(IBPosOpen == 0, 1.0));
if (IBPosOpen == 0)
{StaticVarRemove("BarEntered" + Symbol); // If no position open, remove static var to "zero" it
_Trace("IBPosOpen Executed!");}
else
{
{EntryBar = Nz(StaticVarGet("BarEntered" + Symbol)); // recall entry bar in static var
_Trace("EntryBar: " + EntryBar);
if (EntryBar == 0)
{StaticVarSet("BarEntered" + Symbol, CurrentBar, True); // if no entry bar is stored, this must be the entry bar
_Trace("EntryBar = 0, BarEntered: " + BarEntered);
}
else
{
_Trace("EntryBar NOT Equal to 0");
if (CurrentBar - EntryBar >= TimeStopBars) // check if the target number of bars have passed since entry
{
_Trace("CurrentBar: " + CurrentBar + ", EntryBar: " + EntryBar);
_Trace("CurrentBar: " + CurrentBar);
_Trace("EntryBar: " + EntryBar);
TimeStop = True;
_Trace("TimeStop: " + TimeStop);}
_Trace("CurrentBar - EntryBar < TimeStopBars");
}
}
}
MySignal = TimeStop;
_Trace("MySignal: " + MySignal);