lunes, 5 de septiembre de 2022

PLATAFORMA SIMPLE DE TRADING

Hoy traigo una aplicación en Amibroker que es a la vez sencilla y práctica. Es una plataforma que sirve para operar acciones y que aprovecha las nuevas funciones gráficas para situar varios botones sobre el chart de precios. También permite ver una aplicación práctica de las variables estáticas. Y también muestra la programación del IBController.



En el siguiente vídeo se puede ver el funcionamiento: https://youtu.be/psjU21LYGL4


CÓDIGO AMIBROKER
------------------------------

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// PLATAFORMA SIMPLE DE TRADING 
// OSCAR G. CAGIGAS
// 5 SEPTIEMBRE 2022
// ESTE CÓDIGO SE PROPORCIONA "TAL CUAL" Y NO SE GARANTIZA QUE ESTÉ LIBRE DE ERRORES. 
// SE OFRECE SOLAMENTE CON PROPÓSITO DIDÁCTICO Y DESACTIVADO PARA QUE NO EJECUTE 
// ÓRDENES EN EL MERCADO (TRANSMIT = FALSE).
// EN NINGÚN CASO SE RECOMIENDA LA OPERATIVA EN DERIVADOS
// VÍDEO YOUTUBE CON EJEMPLO DE USO: https://youtu.be/psjU21LYGL4
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

//PARÁMETROS DE IBCONTROLLER
AutoTrading = ParamToggle("AutoTrading","No|Yes", 0);
transmit = False; 

//INICIALIZAR
tib = Name();
BuyLMT = BuyMKT = SellLMT = SellMKT = Flat = StopLoss = Cancel = PrecioSL = 0;

//FUNCION REDONDEAR PARA OBTENER PRECIO CON 2 DECIMALES
function Redondea( precio )
{
return ( round( precio * 100 ) / 100 );
}  

//------------ BOTONES EN PANTALLA -------------

//TIPO DE LETRA
GuiSetFont( "Lucida", 8 );

//BOTONES DE 100 A 1000 CON EL NÚMERO DE ACCIONES
GuiButton( "100", 100, 10,600, 100, 50, notifyClicked );
GuiButton( "200", 200, 10,660, 100, 50, notifyClicked );
GuiButton( "300", 300, 10,720, 100, 50, notifyClicked );
GuiButton( "400", 400, 10,780, 100, 50, notifyClicked );
GuiButton( "500", 500, 10,840, 100, 50, notifyClicked );
GuiButton( "600", 600, 10,900, 100, 50, notifyClicked );
GuiButton( "700", 700, 10,960, 100, 50, notifyClicked );
GuiButton( "800", 800, 10,1020, 100, 50, notifyClicked );
GuiButton( "900", 900, 10,1080, 100, 50, notifyClicked );
GuiButton( "1000", 1000, 10,1140, 100, 50, notifyClicked );
GuiSetColors( 100, 1000, 2, colorBlack, colorSkyblue, colorblue );

id = GuiGetEvent( 0, 0 );
event = GuiGetEvent( 0, 1 );
for( i = 100; i <= 1000; i+=100 )
{
    if( id == i && event == 1 ) 
staticvarset("NumShares", i);
}

//NÚMERO DE TÍTULOS SELECCIONADO EN PANTALLA
NumShares = Nz( StaticVarGet("NumShares") );

//BOTONES DE COMPRA
ancho = 200; largo = 70;
fila1 = 50; fila2 =  130;
GuiButton( "Buy " + NumShares + " LMT", 1, 500, fila1, ancho, largo, notifyClicked );
GuiButton( "Buy " + NumShares, 2, 500, fila2, ancho, largo, notifyClicked );
GuiSetColors( 1, 2, 2, colorBlack, colorPaleGreen, colorGreen );

//BOTONES DE VENTA
GuiButton( "Sell " + NumShares + " LMT", 3, 800, fila1, ancho, largo, notifyClicked );
GuiButton( "Sell " + NumShares, 4, 800, fila2, ancho, largo, notifyClicked );
GuiSetColors( 3, 4, 2, colorBlack, colorPink, colorBlack);

//BOTONES DE FLAT, STOP LOSS Y CANCEL
GuiButton( "FLAT", 5, 1100, fila1, ancho, largo, notifyClicked );
GuiButton( "StopLoss", 6, 1100, fila2, ancho, largo, notifyClicked );
GuiButton( "CANCEL", 7, 1400, fila1, ancho, largo, notifyClicked );
GuiSetColors( 5, 7, 2, colorBlack, colorSkyblue, colorBlack);

//REGISTRAR LOS EVENTOS
id = GuiGetEvent( 0, 0 );
event = GuiGetEvent( 0, 1 );
for( i = 1; i <= 10; i++ )
{
    if( id == i && event == 1 ) VarSet("click" + i, 1);  
}

//------------------------------------------

//ACTIVACIONES
if ( VarGet("click1") ) BuyLMT  = 1; //BOTÓN 1 COMPRA LIMITE
if ( VarGet("click2") ) BuyMKT  = 1; //BOTÓN 2 COMPRA MKT
if ( VarGet("click3") ) SellLMT = 1; //BOTÓN 3 VENTA LIMITE
if ( VarGet("click4") ) SellMKT = 1; //BOTÓN 4 VENTA MERCADO
if ( VarGet("click5") ) Flat = 1; //BOTÓN 5 FLAT
if ( VarGet("click6") ) StopLoss = 1; //BOTÓN 6 STOP LOSS
if ( VarGet("click7") ) Cancel = 1; //BOTÓN 7 CANCEL

//------ -CHART -------------------

GraphXSpace = 7;
Plot(C,"Close",colorBlack,styleCandle);
Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", 
O, H, L, C, SelectedValue( ROC( C, 1 ) ) );
//--------------------------------


//BID y ASK PARA REAL TIME TRADING
Bid = GetRTData("Bid");
Ask = GetRTData("Ask");

//OPERA SI ESTÁ SELECCIONADO "AUTOTRADING"
if ( AutoTrading )
{
ibc = GetTradingInterface("IB"); 
RequestTimedRefresh( 1, False );  //refresca cada 1 seg
//DATOS QUE SE LEEN DEL IBCONTROLLER
Pos = ibc.GetPositionSize( tib ); //Num de acciones en mercado
StaticVarSet("Pos", Pos);
AvgPrice = Nz( ibc.GetPositionInfo( tib, "Avg. cost") ); //Precio de entrada promedio
StaticVarSet("AvgPrice", AvgPrice);
//STOP LOSS 20 CÉNTIMOS POR DEBAJO DEL PRECIO ACTUAL
PrecioStopLargos = GetRTData("Last") - 0.2;
//SI ESTÁ CONECTADO
if ( ibc.IsConnected() )
{
//BUY LIMIT
if( LastValue(BuyLMT) )
{
BuyLMT =  ibc.PlaceOrder( tib, "BUY", NumShares, "LMT", Bid , 0, "DAY", transmit, 100);
StaticVarSetText("BuyLMTlog", StaticVarGetText("BuyLMTlog") + tib + " BUY " + NumShares + " @ " + Bid + "\n" );
StaticVarSetText("BuyLMT", BuyLMT);
}
//BuyMKT
if( LastValue(BuyMKT) )
{
BuyMKT = ibc.PlaceOrder( tib, "BUY", NumShares, "LMT", Ask , 0, "DAY", transmit, 100);
StaticVarSetText("BuyMKTlog", StaticVarGetText("BuyMKTlog") + tib + " BUY " + NumShares + " @ " + Ask + "\n" );
StaticVarSetText("BuyMKT", BuyMKT);
}
//SellLMT
if( LastValue(SellLMT) )
{
SellLMT = ibc.PlaceOrder( tib, "SELL", NumShares, "LMT", Ask , 0, "DAY", transmit, 100);
StaticVarSetText("SellLMTlog", StaticVarGetText("SellLMTlog") + tib + " SELL " + NumShares + " @ " + Ask + "\n" );
StaticVarSetText("SellLMT", SellLMT);
}
//SellMKT
if( LastValue(SellMKT) )
{
SellMKT = ibc.PlaceOrder( tib, "SELL", NumShares, "LMT", Bid , 0, "DAY", transmit, 100);
StaticVarSetText("SellMKTlog", StaticVarGetText("SellMKTlog") + tib + " SELL " + NumShares + " @ " + Bid + "\n" );
StaticVarSetText("SellMKT", SellMKT);
}
//Flat
if( LastValue(Flat) )
{
Flat = ibc.PlaceOrder( tib, "SELL", StaticVarGet("Pos"), "LMT", Bid, 0, "DAY", transmit, 100);
StaticVarSetText("Flatlog", StaticVarGetText("Flatlog") + tib + " SELL " + StaticVarGet("Pos") + " @ " + Bid + "\n" );
StaticVarSetText("Flat", Flat);
}
//StopLoss
if( LastValue(StopLoss) )
{
PrecioSL = Redondea( StaticVarGet("AvgPrice") ) - 0.2;
StopLoss = ibc.PlaceOrder( tib, "SELL", StaticVarGet("Pos"), "STP", 0, PrecioSL, "DAY", transmit, 100);
StaticVarSetText("StopLosslog", StaticVarGetText("StopLosslog") + tib + " SELL " + StaticVarGet("Pos") + " @ " + PrecioSL + "\n" );
StaticVarSetText("StopLoss", StopLoss);
}
//Cancel
if( LastValue(Cancel) )
{
ibc.CancelAllPendingOrders( tib );
StaticVarSetText("BuyLMTlog","");
StaticVarSetText("BuyMKTlog","");
StaticVarSetText("SellLMTlog","");
StaticVarSetText("SellMKTlog","");
StaticVarSetText("Flatlog","");
StaticVarSetText("StopLosslog","");
}
//RESETEAR EL LOG SI SE VAN HACIENDO LAS ÓRDENES
if( ibc.GetStatus( StaticVarGetText("BuyLMT"), True ) == "Filled" ) StaticVarSetText("BuyLMTlog", "" );
if( ibc.GetStatus( StaticVarGetText("BuyMKT"), True ) == "Filled" ) StaticVarSetText("BuyMKTlog", "" );
if( ibc.GetStatus( StaticVarGetText("SellLMT"), True ) == "Filled" ) StaticVarSetText("SellLMTlog", "" );
if( ibc.GetStatus( StaticVarGetText("SellMKT"), True ) == "Filled" ) StaticVarSetText("SellMKTlog", "" );
if( ibc.GetStatus( StaticVarGetText("Flat"), True ) == "Filled" ) StaticVarSetText("Flatlog", "" );
if( ibc.GetStatus( StaticVarGetText("StopLoss"), True ) == "Filled" ) StaticVarSetText("StopLosslog", "" );
//EL LOG ES LA SUMA DE LOS MENSAJES INDIVIDUALES
StaticVarSetText("log", StaticVarGetText("BuyLMTlog") + StaticVarGetText("BuyMKTlog") + StaticVarGetText("SellLMTlog") 
+ StaticVarGetText("SellMKTlog") + StaticVarGetText("Flatlog") + StaticVarGetText("StopLosslog") );
//############ IDS EN EL CHART Y AVISO DE "CONECTADO" ###########
//COLOR VERDE PARA GANANCIAS Y ROJO PARA PÉRDIDAS
color = IIf( GetRTData("last") > AvgPrice, colorGreen, colorRed);
//EVOLUCIÓN DE LA OPERATIVA EN EL CHART
MktValue = Nz( ibc.GetPositionInfo( tib, "Mkt. Value") ); //INversión ($)
UnPNL = Nz( ibc.GetPositionInfo( tib,  "Unrealized PNL") );
RePNL = Nz( ibc.GetPositionInfo( tib,  "Realized PNL") );
Gan = GetRTData("Last") - AvgPrice; //Gananacia en puntos
//VEMOS LAS VARIABLES EN EL CHART
Title = Title + "\n" + "----------------------"
+ "\n" + "Bid/Ask: " + Bid + " x " + Ask
+ "\n" + "Shares = " + StaticVarGet("Pos")
+ "\n" + EncodeColor(color) + "Avg Price = " + AvgPrice + " (" + NumToStr(Gan,1.2) + ")"
+ "\n" + EncodeColor(colorBlack) + "PosSize = " + MktValue
+ "\n" + "\nUnrealized PNL = "  + UnPNL + "\nRealized PNL = " + RePNL
+ "\n" + EncodeColor(colorBlue) + StaticVarGetText("log");
//MOSTRAR QUE ESTÁ CONECTADO Y LISTO PARA OPERAR
if(transmit)
{
GfxSetBkColor( colorBrightGreen ); 
GfxSelectFont( "Tahoma", 14 );   
GfxSetBkMode( 2 ); 
GfxTextOut(" LIVE ", 0, 1300 );
}
} //connected
} //autotrading




No hay comentarios:

Publicar un comentario

ENTRADAS POPULARES