What is Algomojo – Smart Order Execution Module for Amibroker Based Trading Systems?

Smart Order Module brings a 100% end-to-end automated trading solution for Amibroker users to automate their trading ideas with a simple plug-and-play module. Traders have to connect their trading system along with smart order execution to make their system trading life easier.

Smart order execution looks into the position book before taking any trading decisions.

Signal and PositionInterpretation
Buy Signal (Long Entry) and No Open PositionBuy x1 times of Quantity
Sell Signal (Long Exit) and No Open Position Do Nothing
Sell Signal (Long Exit) and Positive Open Position (Prev Long Signal Continues)Sell x1 times of Quantity
Short Signal (Short Entry) and No Open PositionSell x1 times of Quantity
Cover Signal (Short Exit) and No Open PositionDo Nothing
Cover Signal (Short Exit) and Negative Open Position (Prev Short Signal Continues)Buy x1 times of Quantity
Buy and Cover Signal and No Open PositionBuy x1 times of Quantity
Buy and Cover Signal and Negative Open Position (Prev Short Signal Continues) Buy x2 times of Quantity
Short and Sell Signal and No Open Position Sell x1 times of Quantity
Short and Sell Signal and Positive Open Position (Prev Long Signal Continues) Sell x2 times of Quantity

AFL Code provided for the brokers : Angel Broking, Aliceblue, Tradejini, Zebu

How to use the smart order module?

1)Apply your trading system on a new blank chart

2)Save the Smart Order Module to any folder of your choice

3)drag and drop the Smart order Module on top of the trading system

4)Configure Smart order Module parameters

5)Enable the Algo Mode and Start 100% Smart Automation.

Smart Order Module for Angel Broking Users (Parameter Controls)

Smart Order Module – Amibroker AFL Code for AngelOne users


//////////////////////////////////////////////
//Multi Broker Amibroker Charting Module
//Coded by Rajandran R - Algomojo Co-Founder
//Date : 14/09/2021
//////////////////////////////////////////////


_SECTION_BEGIN("AngelOne - Algomojo Charting Module");

// Send orders even if Amibroker is minimized or Chart is not active
RequestTimedRefresh(1, False); 

//Initialization

clnt_id = ParamStr("Client ID","xxxxxxxxxxxxxxxxx"); 
api_key = ParamStr("user_apikey","xxxxxxxxxxxxxxxxx"); //Enter your API key here
api_secret = ParamStr("api_secret","xxxxxxxxxxxxxxxxx"); //Enter your API secret key here
stgy_name = ParamStr("Strategy Name", "Smart Order Module");
tradingsymbol = ParamStr("Trading Symbol","RELIANCE-EQ");
exchange = ParamList("Exchange","NSE|NFO|BSE|MCX",0); 
ordertype = ParamList("Order Type","MARKET|LIMIT|STOPLOSS_LIMIT|STOPLOSS_MARKET",0);
price = Param("Price",0,0,100000);
producttype = ParamList("Product Type","DELIVERY|CARRYFORWARD|MARGIN|INTRADAY|AMO_MARGIN|AMO_DELIVERY|AMO_CARRYFORWARD",3);
triggerprice = Param("Trigger Price",0,0,100000);
variety = ParamList("Variety","NORMAL|STOPLOSS|AMO",0); 
quantity = Param("Quatity",40);
squareoff = ParamStr("SqrOffvalue","0"); 
stoploss = ParamStr("SLvalue","0");
trailingStopLoss = ParamStr("trailingSL","0"); 
disclosedquantity = 0;
duration = "DAY";

broker = ParamStr("Broker","an"); //Broker Short Code - an- angel broking, ab - aliceblue, tj - tradejini, zb - zebu, en - enrich
ver = ParamStr("API Version","1.0");
tradedelay = Param("Execution Delay",0,0,1); // 0 - Execution with No Delay after a signal, 1- Execution at the end of the candle
EnableAlgo = ParamList("Algo Mode","Disable|Enable|LongOnly|ShortOnly",0); // Algo Mode

resp="";

//Configure Trade Execution Delay


AlgoBuy = lastvalue(Ref(Buy,-tradedelay));
AlgoSell = lastvalue(Ref(Sell,-tradedelay));
AlgoShort = lastvalue(Ref(Short,-tradedelay));
AlgoCover = lastvalue(Ref(Cover,-tradedelay));

//Static Varibales for Order Protection

static_name_ = Name()+GetChartID()+interval(2)+stgy_name;
static_name_algo = Name()+GetChartID()+interval(2)+stgy_name+"algostatus";
//StaticVarSet(static_name_algo, -1); 


//Algo Dashboard

GfxSelectFont( "BOOK ANTIQUA", 14, 100 );
GfxSetBkMode( 1 );
if(EnableAlgo == "Enable")
{
AlgoStatus = "Algo Enabled";
GfxSetTextColor( colorGreen ); 
GfxTextOut( "Algostatus : "+AlgoStatus , 20, 40); 
if(Nz(StaticVarGet(static_name_algo),0)!=1)
{
_TRACE("Algo Status : Enabled");
StaticVarSet(static_name_algo, 1);
}
}
if(EnableAlgo == "Disable")
{
AlgoStatus = "Algo Disabled";
GfxSetTextColor( colorRed ); 
GfxTextOut( "Algostatus : "+AlgoStatus , 20, 40); 
if(Nz(StaticVarGet(static_name_algo),0)!=0)
{
_TRACE("Algo Status : Disabled");
StaticVarSet(static_name_algo, 0);
}
}
if(EnableAlgo == "LongOnly")
{
AlgoStatus = "Long Only";
GfxSetTextColor( colorYellow ); 
GfxTextOut( "Algostatus : "+AlgoStatus , 20, 40); 
if(Nz(StaticVarGet(static_name_algo),0)!=2)
{
_TRACE("Algo Status : Long Only");
StaticVarSet(static_name_algo, 2);
}
}
if(EnableAlgo == "ShortOnly")
{
AlgoStatus = "Short Only";
GfxSetTextColor( colorYellow ); 
GfxTextOut( "Algostatus : "+AlgoStatus , 20, 40); 
if(Nz(StaticVarGet(static_name_algo),0)!=3)
{
_TRACE("Algo Status : Short Only");
StaticVarSet(static_name_algo, 3);
}
}

//Buy and Sell Order Functions

function FetchToken(tradingsymbol)
{
algomojo=CreateObject("AMAMIBRIDGE.Main");
api_data = "{\"s\":\""+tradingsymbol+"\"}";
resp=algomojo.AMDispatcher(api_key, api_secret,"fetchtoken",api_data,broker,ver);
_TRACE(resp);
sym = StrExtract( resp, 2,'{' );
sym = StrExtract( sym, 0,',' );
sym = StrExtract( sym, 1,':' );
sym = StrExtract( sym, 1,'"' );
_TRACE("Symbol Token :"+sym);
return sym;
}


function Buyorder(orderquantity)
{
	
    algomojo=CreateObject("AMAMIBRIDGE.Main");
    api_data ="{\"stgy_name\":\""+stgy_name+"\",\"variety\":\""+variety+"\",\"tradingsymbol\":\""+tradingsymbol+"\",\"symboltoken\":\""+FetchToken(tradingsymbol)+"\",\"transactiontype\":\""+"BUY"+"\",\"price\":\""+price+"\",\"exchange\":\""+exchange+"\",\"ordertype\":\""+ordertype+"\",\"producttype\":\""+producttype+"\",\"duration\":\""+duration+"\",\"price\":\""+price+"\",\"squareoff\":\""+squareoff+"\",\"stoploss\":\""+stoploss+"\",\"quantity\":\""+orderquantity+"\",\"triggerprice\":\""+triggerprice+"\",\"trailingStopLoss\":\""+trailingStopLoss+"\",\"disclosedquantity\":\""+disclosedquantity+"\"}";
    _TRACE("API Request"+api_data);
    resp=algomojo.AMDispatcher(api_key, api_secret,"PlaceOrder",api_data,broker,ver);
    _TRACE("API Response : "+resp);
    _TRACE("Strategy Name : "+stgy_name+"  AlgoStatus : "+ EnableAlgo);
    _TRACE("Chart Symbol : "+ Name() +"  Trading Symbol : "+  tradingsymbol +"  Quantity : "+ orderquantity +"  Signal : Buy Signal  TimeFrame : "+ Interval(2)+"  ChardId : "+ GetChartID() + " LTP : "+LastValue(C));
	Say( "Buy Order Placed" ); 
	return resp;
}



function sellorder(orderquantity)
{
    algomojo=CreateObject("AMAMIBRIDGE.Main");
    api_data ="{\"stgy_name\":\""+stgy_name+"\",\"variety\":\""+variety+"\",\"tradingsymbol\":\""+tradingsymbol+"\",\"symboltoken\":\""+FetchToken(tradingsymbol)+"\",\"transactiontype\":\""+"SELL"+"\",\"price\":\""+price+"\",\"exchange\":\""+exchange+"\",\"ordertype\":\""+ordertype+"\",\"producttype\":\""+producttype+"\",\"duration\":\""+duration+"\",\"price\":\""+price+"\",\"squareoff\":\""+squareoff+"\",\"stoploss\":\""+stoploss+"\",\"quantity\":\""+orderquantity+"\",\"triggerprice\":\""+triggerprice+"\",\"trailingStopLoss\":\""+trailingStopLoss+"\",\"disclosedquantity\":\""+disclosedquantity+"\"}";
    
    _TRACE("API Request"+api_data);
    resp=algomojo.AMDispatcher(api_key, api_secret,"PlaceOrder",api_data,broker,ver);
    _TRACE("API Response : "+resp);
    _TRACE("Strategy Name : "+stgy_name+"  AlgoStatus : "+ EnableAlgo);
    _TRACE("Chart Symbol : "+ Name() +"  Trading Symbol : "+  tradingsymbol +"  Quantity : "+ orderquantity +"  Signal : Sell Signal  TimeFrame : "+ Interval(2)+"  ChardId : "+ GetChartID() + " LTP : "+LastValue(C));
	Say( "Sell Order Placed" ); 
	return resp;
}



function NetOpenPositions(api_key,api_secret,clnt_id)
{
flag = 0;

algomojo=CreateObject("AMAMIBRIDGE.Main");
api_data ="{}";
resp=algomojo.AMDispatcher(api_key, api_secret,"Positions",api_data,broker,ver);
_TRACE("Position Book Response : " +resp);
posNetqty =0;

for( item = 1; ( sym = StrExtract( resp, item,'{' )) != ""; item++ )
{

_TRACE("Trading Symbol : " + tradingsymbol);
_TRACE("Product Type : " +producttype);
_TRACE("Sym : " + sym);

if(Strfind(sym,tradingsymbol) AND StrFind(sym,producttype)) //Matches the symbol and //Matches the Order Type
{

flag = 1; //turn on the flag

for( jitem = 0; ( posdetails = StrExtract( sym, jitem,',' )) != ""; jitem++ )
{

 if(Strfind(posdetails,"tradingsymbol"))
  {
   posdetails = StrExtract(posdetails,1,':');
   possym = StrTrim(posdetails,"\"");
    _TRACE("\nSymbol : "+possym);
  }

  if(Strfind(posdetails,"netqty"))
  {
   posdetails = StrExtract(posdetails,1,':');
   posNetqty = StrToNum(StrTrim(posdetails,"\""));
   _TRACE("\nNetQty : "+posNetqty);
  }
  

} //end of for loop
}

}//end of for loop


if(flag==0)
{
_TRACE("\nTrading Symbol Not Found");
 posNetqty =0;
_TRACE("\nNetQty : "+posNetqty);
}

return posNetqty;


}//end of Net Open Positions


if(EnableAlgo != "Disable")
    {
        lasttime = StrFormat("%0.f",LastValue(BarIndex()));
        
        SetChartBkColor(colorDarkGrey);
        if(EnableAlgo == "Enable")
        {   
            if (AlgoBuy==True AND AlgoCover == True AND StaticVarGet(static_name_+"buyCoverAlgo")==0 AND StaticVarGetText(static_name_+"buyCoverAlgo_barvalue") != lasttime )
            {
            // reverse Long Entry 
				if(NetOpenPositions(api_key,api_secret,clnt_id) == 0)
				{
				Newquantity = quantity;
				_TRACE("\nNo Open Positions");
				}
				else if(NetOpenPositions(api_key,api_secret,clnt_id) == -quantity)
				{
				Newquantity = 2*quantity;
				}
				else
				{
				_TRACE("\nReversal signal will be taken with 2 times of Trading Quantity");
				_TRACE("\nWarning Mismatch in Trading Positions. Check the Open Positions and Trade Quantity and Take Necessary Actions Manually.");
				}
				resp=buyorder(Newquantity);
                
				StaticVarSetText(static_name_+"buyCoverAlgo_barvalue",lasttime);
                StaticVarSet(static_name_+"buyCoverAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  tradingsymbol +"  Quantity : "+ Newquantity +"  Signal : Buy and Cover Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
        
            }
            else if ((AlgoBuy != True OR AlgoCover != True))
            {   
                StaticVarSet(static_name_+"buyCoverAlgo",0);
                StaticVarSetText(static_name_+"buyCoverAlgo_barvalue","");
            }
            
            if (AlgoBuy==True AND AlgoCover != True AND StaticVarGet(static_name_+"buyAlgo")==0 AND StaticVarGetText(static_name_+"buyAlgo_barvalue") != lasttime )
            {
            // Long Entry 
                if(NetOpenPositions(api_key,api_secret,clnt_id) == 0)
				{
				Newquantity = quantity;
				}
				else
				{
				Newquantity = quantity;
				_TRACE("\nWarning: Reversal signal will be taken with 1 times of Trading Quantity. However Trading Positions Already Exists");
				_TRACE("\nWarning: Mismatch in Trading Positions. Check the Open Positions and Trade Quantity and Take Necessary Actions Manually if required.");
				}
                
                resp=buyorder(Newquantity);
                StaticVarSetText(static_name_+"buyAlgo_barvalue",lasttime);
                StaticVarSet(static_name_+"buyAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  tradingsymbol +"  Quantity : "+ Newquantity +"  Signal : Buy Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
            }
            else if (AlgoBuy != True)
            {   
                StaticVarSet(static_name_+"buyAlgo",0);
                StaticVarSetText(static_name_+"buyAlgo_barvalue","");                
            }
            if (AlgoSell==true AND AlgoShort != True AND StaticVarGet(static_name_+"sellAlgo")==0 AND StaticVarGetText(static_name_+"sellAlgo_barvalue") != lasttime )
            {     
            // Long Exit 
				if(NetOpenPositions(api_key,api_secret,clnt_id) == 0)
				{
				Newquantity = 0;
				_TRACE("\nWarning: No Trading Positions Already Exists. Short Exit Signal(Cover Signal) will be discarded");
				}
				else if(NetOpenPositions(api_key,api_secret,clnt_id) == quantity)
				{
				Newquantity = quantity;
				}
				else
				{
				Newquantity = quantity;
				_TRACE("\nWarning: Reversal signal will be taken with 1 times of Trading Quantity. However Trading Positions Already Exists");
				_TRACE("\nWarning: Mismatch in Trading Positions. Check the Open Positions and Trade Quantity and Take Necessary Actions Manually if required.");
				}
                
                resp=sellorder(Newquantity);
                
                StaticVarSetText(static_name_+"sellAlgo_barvalue",lasttime);
                StaticVarSet(static_name_+"sellAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  tradingsymbol +"  Quantity : "+ Newquantity +"  Signal : Sell Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
            }
            else if (AlgoSell != True )
            {   
                StaticVarSet(static_name_+"sellAlgo",0);
                StaticVarSetText(static_name_+"sellAlgo_barvalue","");
            }
            if (AlgoShort==True AND AlgoSell==True AND  StaticVarGet(static_name_+"ShortSellAlgo")==0 AND StaticVarGetText(static_name_+"ShortSellAlgo_barvalue") != lasttime )
            {
            // reverse Short Entry 
                if(NetOpenPositions(api_key,api_secret,clnt_id) == 0)
				{
				Newquantity = quantity;
				_TRACE("\nNo Open Positions");
				}
				else if(NetOpenPositions(api_key,api_secret,clnt_id) == quantity)
				{
				Newquantity = 2*quantity;
				}
				else
				{
				Newquantity = 2*quantity;
				_TRACE("\nReversal signal will be taken with 2 times of Trading Quantity");
				_TRACE("\nWarning Mismatch in Trading Positions. Check the Open Positions and Trade Quantity and Take Necessary Actions Manually.");
				}
                
                resp=sellorder(Newquantity);
                
                StaticVarSetText(static_name_+"ShortsellAlgo_barvalue",lasttime);
                StaticVarSet(static_name_+"ShortSellAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  tradingsymbol +"  Quantity : "+ quantity*2 +"  Signal : Short and Sell Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
                
            }
            else if ((AlgoShort != True OR AlgoSell != True))
            {   
                StaticVarSet(static_name_+"ShortSellAlgo",0);
                StaticVarSetText(static_name_+"ShortsellAlgo_barvalue","");
            }
                
            if (AlgoShort==True  AND  AlgoSell != True AND StaticVarGet(static_name_+"ShortAlgo")==0 AND  StaticVarGetText(static_name_+"ShortAlgo_barvalue") != lasttime  )
            {
            // Short Entry
               if(NetOpenPositions(api_key,api_secret,clnt_id) == 0)
				{
				Newquantity = quantity;
				}
				else
				{
				Newquantity = quantity;
				_TRACE("\nWarning: Reversal signal will be taken with 1 times of Trading Quantity. However Trading Positions Already Exists");
				_TRACE("\nWarning: Mismatch in Trading Positions. Check the Open Positions and Trade Quantity and Take Necessary Actions Manually if required.");
				}
                
                resp=sellorder(Newquantity);
               
                StaticVarSetText(static_name_+"ShortAlgo_barvalue",lasttime);
                StaticVarSet(static_name_+"ShortAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  tradingsymbol +"  Quantity : "+ Newquantity +"  Signal : Short Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
            }
            else if (AlgoShort != True )
            {   
                StaticVarSet(static_name_+"ShortAlgo",0);
                StaticVarSetText(static_name_+"ShortAlgo_barvalue","");
            }
            if (AlgoCover==true AND AlgoBuy != True AND StaticVarGet(static_name_+"CoverAlgo")==0 AND StaticVarGetText(static_name_+"CoverAlgo_barvalue") != lasttime )
            {
            // Short Exit
				if(NetOpenPositions(api_key,api_secret,clnt_id) == 0)
				{
				Newquantity = 0;
				_TRACE("\nWarning: No Trading Positions Already Exists. Short Exit Signal(Cover Signal) will be discarded");
				}
				else if(NetOpenPositions(api_key,api_secret,clnt_id) == -quantity)
				{
				Newquantity = quantity;
				}
				else
				{
				Newquantity = quantity;
				_TRACE("\nWarning: Reversal signal will be taken with 1 times of Trading Quantity. However Trading Positions Already Exists");
				_TRACE("\nWarning: Mismatch in Trading Positions. Check the Open Positions and Trade Quantity and Take Necessary Actions Manually if required.");
				}
                
                resp=buyorder(Newquantity);
                
                StaticVarSetText(static_name_+"CoverAlgo_barvalue",lasttime); 
                StaticVarSet(static_name_+"CoverAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  tradingsymbol +"  Quantity : "+ quantity +"  Signal : Cover Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
            }
            else if (AlgoCover != True )
            {   
                StaticVarSet(static_name_+"CoverAlgo",0);
                StaticVarSetText(static_name_+"CoverAlgo_barvalue","");
            }
        }
        
        
        
        else if(EnableAlgo == "LongOnly")
        {
            
            if (AlgoBuy==True AND StaticVarGet(static_name_+"buyAlgo")==0 AND StaticVarGetText(static_name_+"buyAlgo_barvalue") != lasttime)
            {  
            //  Long Entry
             if(NetOpenPositions(api_key,api_secret,clnt_id) == 0)
				{
				Newquantity = quantity;
				}
				else
				{
				Newquantity = quantity;
				_TRACE("\nWarning: Reversal signal will be taken with 1 times of Trading Quantity. However Trading Positions Already Exists");
				_TRACE("\nWarning: Mismatch in Trading Positions. Check the Open Positions and Trade Quantity and Take Necessary Actions Manually if required.");
				}
                
                resp=buyorder(Newquantity);
                
                StaticVarSetText(static_name_+"buyAlgo_barvalue",lasttime);
                StaticVarSet(static_name_+"buyAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  tradingsymbol +"  Quantity : "+ Newquantity +"  Signal : Buy Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
            }
            else if (AlgoBuy != True)
            {   
                StaticVarSet(static_name_+"buyAlgo",0);
                StaticVarSetText(static_name_+"buyAlgo_barvalue","");
            }
            if (AlgoSell==true AND StaticVarGet(static_name_+"sellAlgo")==0 AND StaticVarGetText(static_name_+"sellAlgo_barvalue") != lasttime)
            {  
            // Long Exit
				if(NetOpenPositions(api_key,api_secret,clnt_id) == 0)
				{
				Newquantity = 0;
				_TRACE("\nWarning: No Trading Positions Already Exists. Short Exit Signal(Cover Signal) will be discarded");
				}
				else if(NetOpenPositions(api_key,api_secret,clnt_id) == quantity)
				{
				Newquantity = quantity;
				}
				else
				{
				Newquantity = quantity;
				_TRACE("\nWarning: Reversal signal will be taken with 1 times of Trading Quantity. However Trading Positions Already Exists");
				_TRACE("\nWarning: Mismatch in Trading Positions. Check the Open Positions and Trade Quantity and Take Necessary Actions Manually if required.");
				}
                
                resp=sellorder(Newquantity);
                
                StaticVarSetText(static_name_+"sellAlgo_barvalue",lasttime);
                StaticVarSet(static_name_+"sellAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  tradingsymbol +"  Quantity : "+ Newquantity +"  Signal : Sell Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
            }
            else if (AlgoSell != True )
            {   
                StaticVarSet(static_name_+"sellAlgo",0);
                StaticVarSetText(static_name_+"sellAlgo_barvalue","");
            }
        }
        else if(EnableAlgo == "ShortOnly")
        {
            if (AlgoShort==True AND StaticVarGet(static_name_+"ShortAlgo")==0 AND StaticVarGetText(static_name_+"ShortAlgo_barvalue") != lasttime)
            {
            // Short Entry
                if(NetOpenPositions(api_key,api_secret,clnt_id) == 0)
				{
				Newquantity = quantity;
				}
				else
				{
				Newquantity = quantity;
				_TRACE("\nWarning: Reversal signal will be taken with 1 times of Trading Quantity. However Trading Positions Already Exists");
				_TRACE("\nWarning: Mismatch in Trading Positions. Check the Open Positions and Trade Quantity and Take Necessary Actions Manually if required.");
				}
                resp=sellorder(Newquantity);
                
                StaticVarSetText(static_name_+"ShortAlgo_barvalue",lasttime);
                StaticVarSet(static_name_+"ShortAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  tradingsymbol +"  Quantity : "+ Newquantity +"  Signal : Short Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
            }
            else if (AlgoShort != True )
            {   
                StaticVarSet(static_name_+"ShortAlgo",0);
                StaticVarSetText(static_name_+"ShortAlgo_barvalue","");
            }
            if (AlgoCover==true AND StaticVarGet(static_name_+"CoverAlgo")==0 AND StaticVarGetText(static_name_+"CoverAlgo_barvalue") != lasttime)
            {
            // Short Exit
				if(NetOpenPositions(api_key,api_secret,clnt_id) == 0)
				{
				Newquantity = 0;
				_TRACE("\nWarning: No Trading Positions Already Exists. Short Exit Signal(Cover Signal) will be discarded");
				}
				else if(NetOpenPositions(api_key,api_secret,clnt_id) == -quantity)
				{
				Newquantity = quantity;
				}
				else
				{
				Newquantity = quantity;
				_TRACE("\nWarning: Reversal signal will be taken with 1 times of Trading Quantity. However Trading Positions Already Exists");
				_TRACE("\nWarning: Mismatch in Trading Positions. Check the Open Positions and Trade Quantity and Take Necessary Actions Manually if required.");
				}
                
                resp=buyorder(Newquantity);
                
                StaticVarSetText(static_name_+"CoverAlgo_barvalue",lasttime);
                StaticVarSet(static_name_+"CoverAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ stgy_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  tradingsymbol +"  Quantity : "+ Newquantity +"  Signal : Cover Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
            }
            else if (AlgoCover != True)
            {   
                StaticVarSet(static_name_+"CoverAlgo",0);
                StaticVarSetText(static_name_+"CoverAlgo_barvalue","");
            }
        }
        
    }//end main if
    
_SECTION_END();

SmartOrder Module for Aliceblue, Tradejini, Zebu users

//Algomojo Chart Based Trading - Smart Trading Feature//



// Developer: Rajandran R (Founder - Marketcalls / Co-Founder - Algomojo)
// Date: 28-Oct-2020
// Website: algomojo.com / marketcalls.in

_SECTION_BEGIN("Algomojo Charting Module");

RequestTimedRefresh(1, False); // Send orders even if Amibroker is minimized or Chart is not active


user_apikey = ParamStr("user_apikey","xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); //Enter your API key here
api_secretkey = ParamStr("api_secretkey","xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); //Enter your API secret key here
clnt_id = ParamStr("Client ID","TS2499"); //Enter your trading client ID
s_prdt_ali = "BO:BO|CNC:CNC|CO:CO|MIS:MIS|NRML:NRML"; //Product Alias
Tsym = ParamStr("Trading Symbol","RELIANCE-EQ"); //Symbol Name
exch = ParamList("Exchange","NFO|NSE|BSE|CDS|MCX|NCDEX|BFO|MCXSXFO|MCXSX",1); //Exchange
Ret = "DAY"; //Retention
prctyp = ParamList("prctyp","MKT|L|SL|SL-M",0); // Pricetype
Pcode = ParamList("Product code","NRML|BO|CNC|CO|MIS",4); // Product Code
qty = Param("Quantity",40,0,100000,1); // Quantity
AMO = "NO"; //AMO Order
placeordertype = tradedelay = Param("Execution Delay",0,0,1); // 0 - Execution with No Delay after a signal, 1- Execution at the end of the candle
strg_name = ParamStr("Strategy Name","Test Strategy Chart"); // Strategy Name
EnableAlgo = ParamList("Algo Mode","Disable|Enable|LongOnly|ShortOnly",0); // Algo Mode
resp = "";
broker =ParamList("Broker","ab|tj|mt|en|zb",0); // Product Code
ver = ParamStr("API Version ","1.0");

static_name_ = Name()+GetChartID()+interval(2)+strg_name;
static_name_algo = Name()+GetChartID()+interval(2)+strg_name+"algostatus";

//Configure Trade Execution Delay


AlgoBuy = lastvalue(Ref(Buy,-tradedelay));
AlgoSell = lastvalue(Ref(Sell,-tradedelay));
AlgoShort = lastvalue(Ref(Short,-tradedelay));
AlgoCover = lastvalue(Ref(Cover,-tradedelay));



GfxSelectFont( "BOOK ANTIQUA", 14, 100 );
GfxSetBkMode( 1 );
if(EnableAlgo == "Enable")
{
AlgoStatus = "Algo Enabled";
GfxSetTextColor( colorGreen ); 
GfxTextOut( "Algostatus : "+AlgoStatus , 20, 40); 
if(Nz(StaticVarGet(static_name_algo),0)!=1)
{
_TRACE("Algo Status : Enabled");
StaticVarSet(static_name_algo, 1);
}
}
if(EnableAlgo == "Disable")
{
AlgoStatus = "Algo Disabled";
GfxSetTextColor( colorRed ); 
GfxTextOut( "Algostatus : "+AlgoStatus , 20, 40); 
if(Nz(StaticVarGet(static_name_algo),0)!=0)
{
_TRACE("Algo Status : Disabled");
StaticVarSet(static_name_algo, 0);
}
}
if(EnableAlgo == "LongOnly")
{
AlgoStatus = "Long Only";
GfxSetTextColor( colorYellow ); 
GfxTextOut( "Algostatus : "+AlgoStatus , 20, 40); 
if(Nz(StaticVarGet(static_name_algo),0)!=2)
{
_TRACE("Algo Status : Long Only");
StaticVarSet(static_name_algo, 2);
}
}
if(EnableAlgo == "ShortOnly")
{
AlgoStatus = "Short Only";
GfxSetTextColor( colorYellow ); 
GfxTextOut( "Algostatus : "+AlgoStatus , 20, 40); 
if(Nz(StaticVarGet(static_name_algo),0)!=3)
{
_TRACE("Algo Status : Short Only");
StaticVarSet(static_name_algo, 3);
}
}


function NetOpenPositions(user_apikey,api_secretkey,clnt_id)
{
flag = 0;

algomojo=CreateObject("AMAMIBRIDGE.Main");
api_data ="{\"uid\":\""+clnt_id+"\",\"actid\":\""+clnt_id+"\",\"type\":\""+"NET"+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\"}";
resp=algomojo.AMDispatcher(user_apikey, api_secretkey,"PositionBook",api_data,broker,ver);
_TRACE("Position Book Response : " +resp);
posNetqty =0;

for( item = 0; ( sym = StrExtract( resp, item,'{' )) != ""; item++ )
{


if(Strfind(sym,Tsym) AND StrFind(sym,Pcode)) //Matches the symbol and //Matches the Order Type
{

flag = 1; //turn on the flag

for( jitem = 0; ( posdetails = StrExtract( sym, jitem,',' )) != ""; jitem++ )
{

 if(Strfind(posdetails,"Tsym"))
  {
   posdetails = StrExtract(posdetails,1,':');
   possym = StrTrim(posdetails,"\"");
    _TRACE("\nSymbol : "+possym);
  }

  if(Strfind(posdetails,"Netqty"))
  {
   posdetails = StrExtract(posdetails,1,':');
   posNetqty = StrToNum(StrTrim(posdetails,"\""));
   _TRACE("\nNetQty : "+posNetqty);
  }
  if(Strfind(posdetails,"unrealisedprofitloss"))
  {
	posdetails = StrExtract(posdetails,1,':');
	posupnl = StrTrim(posdetails,"\"");
    _TRACE("\nUnRealized PNL : "+posupnl);
  }
  if(Strfind(posdetails,"realisedprofitloss"))
  {
    posdetails = StrExtract(posdetails,1,':');
	posrpnl = StrToNum(StrTrim(posdetails,"\""));
    _TRACE("\n Realized PNL : "+posrpnl);
  }
 
  if(Strfind(posdetails,"MtoM"))
  {
    posdetails = StrExtract(posdetails,1,':');
	posmtm = StrToNum(StrTrim(posdetails,"\""));
    _TRACE("\nMTM PNL : "+posmtm);
  }

} //end of for loop
}

}//end of for loop


if(flag==0)
{
_TRACE("\nTrading Symbol Not Found");
//posNetqty =0;
_TRACE("\nNetQty : "+posNetqty);
}

return posNetqty;


}//end of Net Open Positions


if(EnableAlgo != "Disable")
    {
        lasttime = StrFormat("%0.f",LastValue(BarIndex()));
        
        SetChartBkColor(colorDarkGrey);
        if(EnableAlgo == "Enable")
        {   
            if (AlgoBuy==True AND AlgoCover == True AND StaticVarGet(static_name_+"buyCoverAlgo")==0 AND StaticVarGetText(static_name_+"buyCoverAlgo_barvalue") != lasttime )
            {
            // reverse Long Entry 
				if(NetOpenPositions(user_apikey,api_secretkey,clnt_id) == 0)
				{
				NewQty = qty;
				_TRACE("\nNo Open Positions");
				}
				else if(NetOpenPositions(user_apikey,api_secretkey,clnt_id) == -qty)
				{
				NewQty = 2*qty;
				}
				else
				{
				NewQty = 2*qty;
				_TRACE("\nReversal signal will be taken with 2 times of Trading Quantity");
				_TRACE("\nWarning Mismatch in Trading Positions. Check the Open Positions and Trade Quantity and Take Necessary Actions Manually.");
				}
                algomojo=CreateObject("AMAMIBRIDGE.Main");
                api_data ="{\"strg_name\":\""+strg_name+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\",\"Tsym\":\""+Tsym+"\",\"exch\":\""+exch+"\",\"Ttranstype\":\""+"B"+"\",\"Ret\":\""+Ret+"\",\"prctyp\":\""+prctyp+"\",\"qty\":\""+NewQty+"\",\"discqty\":\""+"0"+"\",\"MktPro\":\""+"NA"+"\",\"Price\":\""+"0"+"\",\"TrigPrice\":\""+"0"+"\",\"Pcode\":\""+Pcode+"\",\"AMO\":\""+AMO+"\"}";
                resp=algomojo.AMDispatcher(user_apikey, api_secretkey,"PlaceOrder",api_data,broker,ver);
				StaticVarSetText(static_name_+"buyCoverAlgo_barvalue",lasttime);
                StaticVarSet(static_name_+"buyCoverAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ strg_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  Tsym +"  Quantity : "+ NewQty +"  Signal : Buy and Cover Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
        
            }
            else if ((AlgoBuy != True OR AlgoCover != True))
            {   
                StaticVarSet(static_name_+"buyCoverAlgo",0);
                StaticVarSetText(static_name_+"buyCoverAlgo_barvalue","");
            }
            
            if (AlgoBuy==True AND AlgoCover != True AND StaticVarGet(static_name_+"buyAlgo")==0 AND StaticVarGetText(static_name_+"buyAlgo_barvalue") != lasttime )
            {
            // Long Entry 
                if(NetOpenPositions(user_apikey,api_secretkey,clnt_id) == 0)
				{
				NewQty = qty;
				}
				else
				{
				NewQty = qty;
				_TRACE("\nWarning: Reversal signal will be taken with 1 times of Trading Quantity. However Trading Positions Already Exists");
				_TRACE("\nWarning: Mismatch in Trading Positions. Check the Open Positions and Trade Quantity and Take Necessary Actions Manually if required.");
				}
                
                algomojo=CreateObject("AMAMIBRIDGE.Main");
                api_data ="{\"strg_name\":\""+strg_name+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\",\"Tsym\":\""+Tsym+"\",\"exch\":\""+exch+"\",\"Ttranstype\":\""+"B"+"\",\"Ret\":\""+Ret+"\",\"prctyp\":\""+prctyp+"\",\"qty\":\""+NewQty+"\",\"discqty\":\""+"0"+"\",\"MktPro\":\""+"NA"+"\",\"Price\":\""+"0"+"\",\"TrigPrice\":\""+"0"+"\",\"Pcode\":\""+Pcode+"\",\"AMO\":\""+AMO+"\"}";
                resp=algomojo.AMDispatcher(user_apikey, api_secretkey,"PlaceOrder",api_data,broker,ver);
				StaticVarSetText(static_name_+"buyAlgo_barvalue",lasttime);
                StaticVarSet(static_name_+"buyAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ strg_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  Tsym +"  Quantity : "+ NewQty +"  Signal : Buy Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
            }
            else if (AlgoBuy != True)
            {   
                StaticVarSet(static_name_+"buyAlgo",0);
                StaticVarSetText(static_name_+"buyAlgo_barvalue","");                
            }
            if (AlgoSell==true AND AlgoShort != True AND StaticVarGet(static_name_+"sellAlgo")==0 AND StaticVarGetText(static_name_+"sellAlgo_barvalue") != lasttime )
            {     
            // Long Exit 
				if(NetOpenPositions(user_apikey,api_secretkey,clnt_id) == 0)
				{
				NewQty = 0;
				_TRACE("\nWarning: No Trading Positions Already Exists. Short Exit Signal(Cover Signal) will be discarded");
				}
				else if(NetOpenPositions(user_apikey,api_secretkey,clnt_id) == qty)
				{
				NewQty = qty;
				}
				else
				{
				NewQty = qty;
				_TRACE("\nWarning: Reversal signal will be taken with 1 times of Trading Quantity. However Trading Positions Already Exists");
				_TRACE("\nWarning: Mismatch in Trading Positions. Check the Open Positions and Trade Quantity and Take Necessary Actions Manually if required.");
				}
                algomojo=CreateObject("AMAMIBRIDGE.Main");
                api_data ="{\"strg_name\":\""+strg_name+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\",\"Tsym\":\""+Tsym+"\",\"exch\":\""+exch+"\",\"Ttranstype\":\""+"S"+"\",\"Ret\":\""+Ret+"\",\"prctyp\":\""+prctyp+"\",\"qty\":\""+NewQty+"\",\"discqty\":\""+"0"+"\",\"MktPro\":\""+"NA"+"\",\"Price\":\""+"0"+"\",\"TrigPrice\":\""+"0"+"\",\"Pcode\":\""+Pcode+"\",\"AMO\":\""+AMO+"\"}";
                resp=algomojo.AMDispatcher(user_apikey, api_secretkey,"PlaceOrder",api_data,broker,ver);
				StaticVarSetText(static_name_+"sellAlgo_barvalue",lasttime);
                StaticVarSet(static_name_+"sellAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ strg_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  Tsym +"  Quantity : "+ NewQty +"  Signal : Sell Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
            }
            else if (AlgoSell != True )
            {   
                StaticVarSet(static_name_+"sellAlgo",0);
                StaticVarSetText(static_name_+"sellAlgo_barvalue","");
            }
            if (AlgoShort==True AND AlgoSell==True AND  StaticVarGet(static_name_+"ShortSellAlgo")==0 AND StaticVarGetText(static_name_+"ShortSellAlgo_barvalue") != lasttime )
            {
            // reverse Short Entry 
                if(NetOpenPositions(user_apikey,api_secretkey,clnt_id) == 0)
				{
				NewQty = qty;
				_TRACE("\nNo Open Positions");
				}
				else if(NetOpenPositions(user_apikey,api_secretkey,clnt_id) == qty)
				{
				NewQty = 2*qty;
				}
				else
				{
				NewQty = 2*qty;
				_TRACE("\nReversal signal will be taken with 2 times of Trading Quantity");
				_TRACE("\nWarning Mismatch in Trading Positions. Check the Open Positions and Trade Quantity and Take Necessary Actions Manually.");
				}
                algomojo=CreateObject("AMAMIBRIDGE.Main");
                api_data ="{\"strg_name\":\""+strg_name+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\",\"Tsym\":\""+Tsym+"\",\"exch\":\""+exch+"\",\"Ttranstype\":\""+"S"+"\",\"Ret\":\""+Ret+"\",\"prctyp\":\""+prctyp+"\",\"qty\":\""+NewQty+"\",\"discqty\":\""+"0"+"\",\"MktPro\":\""+"NA"+"\",\"Price\":\""+"0"+"\",\"TrigPrice\":\""+"0"+"\",\"Pcode\":\""+Pcode+"\",\"AMO\":\""+AMO+"\"}";
                resp=algomojo.AMDispatcher(user_apikey, api_secretkey,"PlaceOrder",api_data,broker,ver);
				StaticVarSetText(static_name_+"ShortsellAlgo_barvalue",lasttime);
                StaticVarSet(static_name_+"ShortSellAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ strg_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  Tsym +"  Quantity : "+ NewQty +"  Signal : Short and Sell Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
            }
            else if ((AlgoShort != True OR AlgoSell != True))
            {   
                StaticVarSet(static_name_+"ShortSellAlgo",0);
                StaticVarSetText(static_name_+"ShortsellAlgo_barvalue","");
            }
                
            if (AlgoShort==True  AND  AlgoSell != True AND StaticVarGet(static_name_+"ShortAlgo")==0 AND  StaticVarGetText(static_name_+"ShortAlgo_barvalue") != lasttime  )
            {
            // Short Entry
               if(NetOpenPositions(user_apikey,api_secretkey,clnt_id) == 0)
				{
				NewQty = qty;
				}
				else
				{
				NewQty = qty;
				_TRACE("\nWarning: Reversal signal will be taken with 1 times of Trading Quantity. However Trading Positions Already Exists");
				_TRACE("\nWarning: Mismatch in Trading Positions. Check the Open Positions and Trade Quantity and Take Necessary Actions Manually if required.");
				}
               algomojo=CreateObject("AMAMIBRIDGE.Main");
                api_data ="{\"strg_name\":\""+strg_name+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\",\"Tsym\":\""+Tsym+"\",\"exch\":\""+exch+"\",\"Ttranstype\":\""+"S"+"\",\"Ret\":\""+Ret+"\",\"prctyp\":\""+prctyp+"\",\"qty\":\""+NewQty+"\",\"discqty\":\""+"0"+"\",\"MktPro\":\""+"NA"+"\",\"Price\":\""+"0"+"\",\"TrigPrice\":\""+"0"+"\",\"Pcode\":\""+Pcode+"\",\"AMO\":\""+AMO+"\"}";
                resp=algomojo.AMDispatcher(user_apikey, api_secretkey,"PlaceOrder",api_data,broker,ver);
				StaticVarSetText(static_name_+"ShortAlgo_barvalue",lasttime);
                StaticVarSet(static_name_+"ShortAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ strg_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  Tsym +"  Quantity : "+ NewQty +"  Signal : Short Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
            }
            else if (AlgoShort != True )
            {   
                StaticVarSet(static_name_+"ShortAlgo",0);
                StaticVarSetText(static_name_+"ShortAlgo_barvalue","");
            }
            if (AlgoCover==true AND AlgoBuy != True AND StaticVarGet(static_name_+"CoverAlgo")==0 AND StaticVarGetText(static_name_+"CoverAlgo_barvalue") != lasttime )
            {
            // Short Exit
				if(NetOpenPositions(user_apikey,api_secretkey,clnt_id) == 0)
				{
				NewQty = 0;
				_TRACE("\nWarning: No Trading Positions Already Exists. Short Exit Signal(Cover Signal) will be discarded");
				}
				else if(NetOpenPositions(user_apikey,api_secretkey,clnt_id) == -qty)
				{
				NewQty = qty;
				}
				else
				{
				NewQty = qty;
				_TRACE("\nWarning: Reversal signal will be taken with 1 times of Trading Quantity. However Trading Positions Already Exists");
				_TRACE("\nWarning: Mismatch in Trading Positions. Check the Open Positions and Trade Quantity and Take Necessary Actions Manually if required.");
				}
                algomojo=CreateObject("AMAMIBRIDGE.Main");
                api_data ="{\"strg_name\":\""+strg_name+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\",\"Tsym\":\""+Tsym+"\",\"exch\":\""+exch+"\",\"Ttranstype\":\""+"B"+"\",\"Ret\":\""+Ret+"\",\"prctyp\":\""+prctyp+"\",\"qty\":\""+NewQty+"\",\"discqty\":\""+"0"+"\",\"MktPro\":\""+"NA"+"\",\"Price\":\""+"0"+"\",\"TrigPrice\":\""+"0"+"\",\"Pcode\":\""+Pcode+"\",\"AMO\":\""+AMO+"\"}";
                resp=algomojo.AMDispatcher(user_apikey, api_secretkey,"PlaceOrder",api_data,broker,ver);
				StaticVarSetText(static_name_+"CoverAlgo_barvalue",lasttime); 
                StaticVarSet(static_name_+"CoverAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ strg_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  Tsym +"  Quantity : "+ NewQty +"  Signal : Cover Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
            }
            else if (AlgoCover != True )
            {   
                StaticVarSet(static_name_+"CoverAlgo",0);
                StaticVarSetText(static_name_+"CoverAlgo_barvalue","");
            }
        }
        
        
        
        else if(EnableAlgo == "LongOnly")
        {
            
            if (AlgoBuy==True AND StaticVarGet(static_name_+"buyAlgo")==0 AND StaticVarGetText(static_name_+"buyAlgo_barvalue") != lasttime)
            {  
            //  Long Entry
             if(NetOpenPositions(user_apikey,api_secretkey,clnt_id) == 0)
				{
				NewQty = qty;
				}
				else
				{
				NewQty = qty;
				_TRACE("\nWarning: Reversal signal will be taken with 1 times of Trading Quantity. However Trading Positions Already Exists");
				_TRACE("\nWarning: Mismatch in Trading Positions. Check the Open Positions and Trade Quantity and Take Necessary Actions Manually if required.");
				}
                algomojo=CreateObject("AMAMIBRIDGE.Main");
                api_data ="{\"strg_name\":\""+strg_name+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\",\"Tsym\":\""+Tsym+"\",\"exch\":\""+exch+"\",\"Ttranstype\":\""+"B"+"\",\"Ret\":\""+Ret+"\",\"prctyp\":\""+prctyp+"\",\"qty\":\""+NewQty+"\",\"discqty\":\""+"0"+"\",\"MktPro\":\""+"NA"+"\",\"Price\":\""+"0"+"\",\"TrigPrice\":\""+"0"+"\",\"Pcode\":\""+Pcode+"\",\"AMO\":\""+AMO+"\"}";
                resp=algomojo.AMDispatcher(user_apikey, api_secretkey,"PlaceOrder",api_data,broker,ver);
				StaticVarSetText(static_name_+"buyAlgo_barvalue",lasttime);
                StaticVarSet(static_name_+"buyAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ strg_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  Tsym +"  Quantity : "+ NewQty +"  Signal : Buy Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
            }
            else if (AlgoBuy != True)
            {   
                StaticVarSet(static_name_+"buyAlgo",0);
                StaticVarSetText(static_name_+"buyAlgo_barvalue","");
            }
            if (AlgoSell==true AND StaticVarGet(static_name_+"sellAlgo")==0 AND StaticVarGetText(static_name_+"sellAlgo_barvalue") != lasttime)
            {  
            // Long Exit
				if(NetOpenPositions(user_apikey,api_secretkey,clnt_id) == 0)
				{
				NewQty = 0;
				_TRACE("\nWarning: No Trading Positions Already Exists. Short Exit Signal(Cover Signal) will be discarded");
				}
				else if(NetOpenPositions(user_apikey,api_secretkey,clnt_id) == qty)
				{
				NewQty = qty;
				}
				else
				{
				NewQty = qty;
				_TRACE("\nWarning: Reversal signal will be taken with 1 times of Trading Quantity. However Trading Positions Already Exists");
				_TRACE("\nWarning: Mismatch in Trading Positions. Check the Open Positions and Trade Quantity and Take Necessary Actions Manually if required.");
				}
                algomojo=CreateObject("AMAMIBRIDGE.Main");
                api_data ="{\"strg_name\":\""+strg_name+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\",\"Tsym\":\""+Tsym+"\",\"exch\":\""+exch+"\",\"Ttranstype\":\""+"S"+"\",\"Ret\":\""+Ret+"\",\"prctyp\":\""+prctyp+"\",\"qty\":\""+NewQty+"\",\"discqty\":\""+"0"+"\",\"MktPro\":\""+"NA"+"\",\"Price\":\""+"0"+"\",\"TrigPrice\":\""+"0"+"\",\"Pcode\":\""+Pcode+"\",\"AMO\":\""+AMO+"\"}";
                resp=algomojo.AMDispatcher(user_apikey, api_secretkey,"PlaceOrder",api_data,broker,ver);
				StaticVarSetText(static_name_+"sellAlgo_barvalue",lasttime);
                StaticVarSet(static_name_+"sellAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ strg_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  Tsym +"  Quantity : "+ NewQty +"  Signal : Sell Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
            }
            else if (AlgoSell != True )
            {   
                StaticVarSet(static_name_+"sellAlgo",0);
                StaticVarSetText(static_name_+"sellAlgo_barvalue","");
            }
        }
        else if(EnableAlgo == "ShortOnly")
        {
            if (AlgoShort==True AND StaticVarGet(static_name_+"ShortAlgo")==0 AND StaticVarGetText(static_name_+"ShortAlgo_barvalue") != lasttime)
            {
            // Short Entry
                if(NetOpenPositions(user_apikey,api_secretkey,clnt_id) == 0)
				{
				NewQty = qty;
				}
				else
				{
				NewQty = qty;
				_TRACE("\nWarning: Reversal signal will be taken with 1 times of Trading Quantity. However Trading Positions Already Exists");
				_TRACE("\nWarning: Mismatch in Trading Positions. Check the Open Positions and Trade Quantity and Take Necessary Actions Manually if required.");
				}
                algomojo=CreateObject("AMAMIBRIDGE.Main");
                api_data ="{\"strg_name\":\""+strg_name+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\",\"Tsym\":\""+Tsym+"\",\"exch\":\""+exch+"\",\"Ttranstype\":\""+"S"+"\",\"Ret\":\""+Ret+"\",\"prctyp\":\""+prctyp+"\",\"qty\":\""+NewQty+"\",\"discqty\":\""+"0"+"\",\"MktPro\":\""+"NA"+"\",\"Price\":\""+"0"+"\",\"TrigPrice\":\""+"0"+"\",\"Pcode\":\""+Pcode+"\",\"AMO\":\""+AMO+"\"}";
                resp=algomojo.AMDispatcher(user_apikey, api_secretkey,"PlaceOrder",api_data,broker,ver);
				StaticVarSetText(static_name_+"ShortAlgo_barvalue",lasttime);
                StaticVarSet(static_name_+"ShortAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ strg_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  Tsym +"  Quantity : "+ NewQty +"  Signal : Short Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
            }
            else if (AlgoShort != True )
            {   
                StaticVarSet(static_name_+"ShortAlgo",0);
                StaticVarSetText(static_name_+"ShortAlgo_barvalue","");
            }
            if (AlgoCover==true AND StaticVarGet(static_name_+"CoverAlgo")==0 AND StaticVarGetText(static_name_+"CoverAlgo_barvalue") != lasttime)
            {
            // Short Exit
				if(NetOpenPositions(user_apikey,api_secretkey,clnt_id) == 0)
				{
				NewQty = 0;
				_TRACE("\nWarning: No Trading Positions Already Exists. Short Exit Signal(Cover Signal) will be discarded");
				}
				else if(NetOpenPositions(user_apikey,api_secretkey,clnt_id) == -qty)
				{
				NewQty = qty;
				}
				else
				{
				NewQty = qty;
				_TRACE("\nWarning: Reversal signal will be taken with 1 times of Trading Quantity. However Trading Positions Already Exists");
				_TRACE("\nWarning: Mismatch in Trading Positions. Check the Open Positions and Trade Quantity and Take Necessary Actions Manually if required.");
				}
                algomojo=CreateObject("AMAMIBRIDGE.Main");
                api_data ="{\"strg_name\":\""+strg_name+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\",\"Tsym\":\""+Tsym+"\",\"exch\":\""+exch+"\",\"Ttranstype\":\""+"B"+"\",\"Ret\":\""+Ret+"\",\"prctyp\":\""+prctyp+"\",\"qty\":\""+NewQty+"\",\"discqty\":\""+"0"+"\",\"MktPro\":\""+"NA"+"\",\"Price\":\""+"0"+"\",\"TrigPrice\":\""+"0"+"\",\"Pcode\":\""+Pcode+"\",\"AMO\":\""+AMO+"\"}";
                resp=algomojo.AMDispatcher(user_apikey, api_secretkey,"PlaceOrder",api_data,broker,ver);
				StaticVarSetText(static_name_+"CoverAlgo_barvalue",lasttime);
                StaticVarSet(static_name_+"CoverAlgo",1); //Algo Order was triggered, no more order on this bar
                _TRACE("Strategy : "+ strg_name +"AlgoStatus : "+ EnableAlgo +"Chart Symbol : "+ Name() +"  Trading Symbol : "+  Tsym +"  Quantity : "+ NewQty +"  Signal : Cover Signal  TimeFrame : "+ Interval(2)+"  Response : "+ resp +"  ChardId : "+ GetChartID() + " Latest Price : "+LastValue(C));
            }
            else if (AlgoCover != True)
            {   
                StaticVarSet(static_name_+"CoverAlgo",0);
                StaticVarSetText(static_name_+"CoverAlgo_barvalue","");
            }
        }
        
    }//end main if
    
_SECTION_END();

Leave a Reply

Your email address will not be published. Required fields are marked *