System Cross for AmiBroker
This system was found in an English forum in which he wrote occasional formula in AFL, is a comprehensive system. They are examples of many of the functions that return values \u200b\u200bof the TWS, you can optimize Back-Testing and also automate it. I've been trying for about an hour and the code works without problems.
The purchase orders for sale are very simple, if crossing closure upward exponential moving average of N periods "Buy" if it crosses down "sale."
Copy and paste in the Formula Editor window
PointValue = 100;
NumContracts = 1;
MarginDeposit = 100;
PositionSize = NumContracts * MarginDeposit;
Range1=Optimize("range1",2,1,10,1);
Range2=Optimize("range2",2,1,10,1);
Plot(EMA( Close,range1), "Stopline", colorBlue );
Buy = Ref(Cross((Close),(EMA(Close,range1))),-1);
Buystop = Ref(EMA(Close,range1),-1);
BuyPrice = Max(Buystop,Open);
Sell = Ref(Cross(EMA(Close,range2),(Close)),-1);
Sellstop = Ref(EMA(Close,range1),-1);
SellPrice = Min(sellstop,Open);
Short = Sell;
Cover = Buy;
/////////////////// Automation Code //////////////////
PrevDT = StaticVarGet("DateTime");
DT = LastValue(DateTime());
NewBar = DT != PrevDT;
StaticVarSet("DateTime",DT);
if( NewBar )
{
StaticVarSetText("OrderID","");
Plot(1,"",colorYellow,styleArea
AToffTrigger = ParamTrigger("Stop AutoTrading","STOP");
if( ATonTrigger ) StaticVarSet("AutoTrading",1);
else if( AToffTrigger ) StaticVarSet("AutoTrading",0);
AutoTrading = StaticVarGet("AutoTrading");
CancelALL = ParamTrigger("Cancel All Orders","CANCEL ALL");
CloseALL = ParamTrigger("Close All Positions","CLOSE ALL");
MAnualBuy = ParamTrigger("Manual Buy","BUY");
MAnualSell = ParamTrigger("Manual Sell","SELL");
LastBuy = LastValue(Buy) OR MAnualBuy;
LastSell = LastValue(Sell) OR MAnualSell;
ibc = GetTradingInterface("IB");
IBcStatus = ibc.IsConnected();
IBcStatusString = WriteIf(IBCStatus==0,"TWS Not Found",
WriteIf(IBCStatus==1,"Connecting to TWS",
WriteIf(IBCStatus==2,"TWS OK",
WriteIf(IBCStatus==3,"TWS OK (msgs)",""))));
if( CancelAll )
{
ibc.CancelAllPendingOrders();
StaticVarSetText("OrderID","");
}
else if ( CloseAll )
{
ibc.CancelAllPendingOrders();
ibc.CloseAllOpenPositions();
StaticVarSetText("OrderID","");
}
if( IBcStatus AND AutoTrading )
{
OrderID = StaticVarGetText("OrderID");
IBPosSize = ibc.GetPositionSize( Name() );
if(IBPosSize == 0 )
{
if( LastBuy AND OrderID == "" )
{
OID = ibc.PlaceOrder( Name(), "Buy", 1, "MKT", 0, 0,
"DAY", True );
StaticVarSetText("OrderID",OID);
Plot(1,"",colorBrightGreen,styleArea
Plot (1, "", colorRed, style Area
ORderStatus = ibc.GetStatus( LastOrderID , True );
if( ORderStatus != "" ) StaticVarSetText("OrderStatus",ORderStatus);
}
else IBPosSize = 0;
LastOrderID = StaticVarGetText("OrderID");
ORderStatus = StaticVarGetText("OrderStatus");
Title = "\n"+
" Trading Mode: "+WriteIf( AutoTrading,"ON","OFF")+"\n"+
" Last Signal: "+WriteIf(LastBuy,"BUY",WriteIf
(LastSell,"SELL","NoSignal"))+"\n"+
" IB Status: "+IBcStatusString+"\n"+
" Last OrderID: "+LastOrderID+"\n"+
" OrderStatus: "+ORderStatus+"\n"+
" IBPosSize: "+NumToStr(IBPosSize,1.0)+" shares";
< orderid ="="" oid =" ibc.PlaceOrder("> Plot(C,"",1,128);
PricesOnInclude = ParamToggle("Plot Trade Prices","HIDE
} if (ArrowsOnInclude) {
PlotShapes (IIf (Buy, shapeUpArrow, shapeNone) 5.0, L, -20);
PlotShapes (IIf (Sell, shapeDownArrow, shapeNone), IIf (Sell == 3, 6 ,
IIf (Sell == 2, 7, 4)), 0, H, -20);
PlotShapes (IIf (Short, shapeHollowDownArrow, shapeNone), 4.0, H, -3);
PlotShapes (IIf ( Cover, shapeHollowUpArrow, shapeNone), IIf (Cover == 3,
6, IIf (Cover == 2, 7 5)), 0, L, -3);}
Here I go capture all executions that has made the system
If you test the code and you have a problem with the implementation of the signs I've enabled comments so that you can write and see if together we can solve them. Greetings
0 comments:
Post a Comment