Skip to content

Instantly share code, notes, and snippets.

@marketcalls
Last active September 19, 2021 10:12
Show Gist options
  • Save marketcalls/f6e1417b858cf2728ab94a0f4d7ddac6 to your computer and use it in GitHub Desktop.
Save marketcalls/f6e1417b858cf2728ab94a0f4d7ddac6 to your computer and use it in GitHub Desktop.
Button Trading in Amibroker
/*
Marketcalls Financial Services Pvt Ltd.
Created By : Rajandran R(Founder - Marketcalls.
Created on : 01 Dec 2018.
Website : www.marketcalls.in
*/
_SECTION_BEGIN("Button Trading - Algoaction");
//Notes
//This afl code works only on Amibroker 6.22 and higher version
//Requires Algoaction.in + Tradejini Arrow API to place orders from button
//Replace the API Key and API Secrety key with yours
Version(6.22);
user_apikey = ParamStr("user_apikey","xxxxxxxxxxxxxxxxxxx");
user_apisecrete = ParamStr("user_apisecret","yyyyyyyyyyyyyyyyyyyyyy");
stcode = ParamList("statergy CODE","S1|S2|S3|S4|S5|S6",0);
s_prdt_ali = ParamList("s_prdt_ali","NRML:NRML|BO:BO|CNC:CNC|CO:CO|MIS:MIS",0);
Tsym = ParamStr("Symbol","BANKNIFTY18DECFUT");
exch = ParamList("Exchange","NFO|NSE|BSE|CDS|MCX|NCDEX|BFO|MCXSXFO|MCXSX",0);
qty = Param("Quatity",40,0,50000,1);
Ret = ParamList("Ret","DAY|IOC",0);
prctyp = ParamList("prctyp","MKT|L|SL|SL-M",0);
TokenNo =ParamStr("TokenNo","12345");
Pcode = ParamList("Pcode","NRML|BO|CNC|CO|MIS",0);
AMO = ParamList("AMO","NO|YES",0);
placeordertype = ParamList("Place Order On","Realtime|CandleCompletion",0);
function Buyorder()
{
algoaction=CreateStaticObject("Tradejini.Algo");
a = algoaction.PlaceOrder(user_apikey,user_apisecrete,stcode,s_prdt_ali,Tsym,exch,"B",Ret,prctyp,qty,0,TokenNo,"NA",0,0,Pcode,AMO);
Say( "Order Placed" );
}
function Sellorder()
{
algoaction=CreateStaticObject("Tradejini.Algo");
a = algoaction.PlaceOrder(user_apikey,user_apisecrete,stcode,s_prdt_ali,Tsym,exch,"S",Ret,prctyp,qty,0,TokenNo,"NA",0,0,Pcode,AMO);
Say( "Order Placed" );
}
function GuiButtonTrigger( ButtonName, x, y, width, Height ) {
/// @link http://forum.amibroker.com/t/guibuttons-for-everyone/1716/4
/// by beaver & fxshrat
/// version 1.1
global IDset;
local id, event, clickeven;
if( typeof( IDset ) == "undefined" ) IDset = 0;
//_TRACEF( "IDset before: %g", IDset );
GuiButton( ButtonName, ++IDset, x, y, width, height, 7 );
//_TRACEF( "IDset after: %g", IDset );
result = 0;
id = GuiGetEvent( 0, 0 );// receiving button id
event = GuiGetEvent( 0, 1 );// receiving notifyflag
clickevent = event == 1;
BuyClicked = id == 1 && clickevent;
SellClicked = id == 2 && clickevent;
if( BuyClicked AND StaticVarGet(Name()+GetChartID()+"buyAlgo")==0 )
{
BuyOrder();
result = 1;
StaticVarSet(Name()+GetChartID()+"buyAlgo",1);
}
else
{
StaticVarSet(Name()+GetChartID()+"buyAlgo",0);
}
if( SellClicked AND StaticVarGet(Name()+GetChartID()+"sellAlgo")==0 )
{
SellOrder();
result = -1;
StaticVarSet(Name()+GetChartID()+"sellAlgo",1);
}
else
{
StaticVarSet(Name()+GetChartID()+"sellAlgo",0);
}
return result;
}
BuyTrigger = GuiButtonTrigger( "Buy "+qty+" Shares", 0, 50, 200, 30 );
SellTrigger = GuiButtonTrigger( "Sell "+qty+" Shares", 200, 50, 200, 30 );
GuiSetColors( 1, 3, 2, colorRed, colorBlack, colorRed, colorWhite, colorBlue, colorYellow, colorRed, colorBlack, colorYellow );
Title = "Trigger: " + WriteIf(BuyTrigger==1,"Buy Triggered",WriteIf(BuyTrigger==-1,"Sell Triggered","0"));
SetChartOptions(0 , chartShowArrows | chartShowDates);
Plot(Close,"Candle", colorDefault, styleCandle);
_SECTION_END();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment