TWS auto trading is not for newbies, was: Trailing Stop Loss

Hey guys,
Ive tried and searched everything but couldnt find a solution.
Im not sure, its an issue coupled to the Demo issue or its just bad coding :smiley: im really new to AFL and IBC.
Here is my Code:

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	plot price	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	calculate trading signals and plot them	//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Buy = Cross(ATR(10),Ref(ATR(10),-1));
Sell = 0;
Short = 0;
Cover = 0;

PlotShapes(IIf(Buy, shapeUpArrow, shapeNone), colorGreen, 0, L);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone), colorRed, 0, H);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	stop loss		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
StopLevel = 1 - Param("trailing stop %", 0.05, 0.01, 10, 0.01)/100;
trailARRAY = Null;
trailstop = 0;
for( i = 1; i < BarCount; i++ ) 
{ 
	if( trailstop == 0 AND Buy[ i ] ) 
	{ 
		trailstop = High[ i ] * stoplevel;
	} 
	if( trailstop > 0 AND Low[ i ] < trailstop ) 
	{ 
		Sell[ i ] = 1;
		SellPrice[ i ] = trailstop;
		trailstop = 0; 
	} 
	if( trailstop > 0 ) 
	{ 
		trailstop = Max( High[ i ] * stoplevel, trailstop );
		trailARRAY[ i ] = trailstop;
	}
}
Plot( trailARRAY,"trailing stop level", colorRed );

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	IB Controller Commands	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ibc = GetTradingInterface("IB");

// for long
if( LastValue( Buy ) )
{
	// check if we are connected OK
	if( ibc.IsConnected() )
	{			
		// check if we do not have already open position on this stock
		if( ibc.GetPositionSize( Name() ) == 0 )
		{
			// brachet order
			parentID = ibc.PlaceOrder(Name(), "BUY", 100, "MKT", 0, 0, "GTC", True );
			ibc.PlaceOrder(Name(), "SELL", 100, "LMT", 0, 0, "GTC", True, 100, "", parentID );
			ibc.PlaceOrder(Name(), "SELL", 100, "STP", 0, 0, "GTC", True, 100, "", parentID );
		}
	// remove double orders in IBC (Demo Account)
	ibc.Sleep( 4000 );
	}	
}

He is just not recognize the STP.
if im doing

ibc.PlaceOrder(Name(), "SELL", 100, "STP", trailARRAY, trailARRAY, "GTC", True, 100, "", parentID );

really weird things happens. Its transmiting the bracket order double without the stop loss.

Another Question is: Someone said, that the double order problem is solved by using "statvargettext" or something like that... Can someone can explain this?
Really appriciate some answers :grinning_face_with_smiling_eyes:

Without at least 3 months of learning of AFL, you should not even try auto-trading because this is equivalent of giving machine gun to a child. Just don't. You would blow up your account. Copy-paste artists should not auto-trade. Learn the basics of programming in AFL first. Read a lot.

3 Likes

Hey Tomasz,
Thanks for replying and yes, you are so right :smiley:
Im in the process of learning right now and never would start Auto Trading without beeing much bether at AFL.
It is, more or less, copy pasted but only for the purpose of showing my issue.
My own strategy will not be like this^^

Thanks again :slight_smile: i will try to solve on my own to improve my skills a bit more.

Friendly Regards
Diren Bulut