Delayed StopLoss Signal generated by execution of an order

Hi Team,

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.

Pls help.

Thanks.

GetPositionStatus = ibc.GetPositionInfo(Name(), "Avg. Cost");
InMktStatus[BarCount-1] = IsNull( GetPositionStatus[BarCount-1] ); 
InMktAct[BarCount-1] = IIF(InMktStatus[BarCount-1] == 0 && InMktStatus[BarCount-2] == 1, TRUE, FALSE); 
If( InMktAct[BarCount-1] == 1 )
{
	for(t=0; t <5; t++)
	{ Signal = TRUE; }
}
Else
{ Signal = FALSE; }

Hi Roy

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;
1 Like

MacAllan,

Thanks for your assistance.

As I have very limited programming experience, I have just copy and paste the code into the main program and run it with the paper trade account.

But the signal didnt call out in 5 min and during this period, the current bar value was kept constant. Pls let me know if I have missed something....

Thanks again for your assistance.

Sincerely,
Roy

There are many variables at play which could be causing you to get unexpected results.

I would suggest you start by following some of the advice here to try and uncover what is happening.

MacAllan,

Thanks for your advice.
I am taking a close review at it now.

Sincerely,
Roy

MacAllan,

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);

Testing%20Code