Hello,
This question is regarding placing actual trades by calling InternetPostRequest.
In order to call this function from AFL, I need some logic in place to invoke it and pass correct values like is it a Buy trade or short, what price, which symbol, etc.
The problem is how can I have Entry and Exit signals shown in the Detailed Log of backtester in AFL?
I have PositionScore and Buy, Short, Sell Cover variables. Using them I tried to put some logic as shown in the below code, but that is definitely not equal to how entry and exit signals are generated. Because Amibroker is doing something smart. It's using PositionScore and Position Sizing along with Buy sell signals to generate these Entry-Exit signals. How can I incorporate those signals in my code so that at those events I can call InternetPostRequest ?
Here is my code:
SetBacktestMode( backtestRegular );
Buy = Sell = Short = Cover = 0;
fastPeriod = 7;
rank = 12;
maxPositions = 4;
fast = MA( Close, fastPeriod );
slow= MA( Close, 15);
SetOption("worstrankheld", rank);
SetOption("Maxopenpositions", maxPositions);
SetOption("allowpositionshrinking", True);
PositionSize = -(100/maxPositions);
PositionScore = fast - slow; // Use Ranking over 30 stocks
Plot(PositionScore,"PositionScore",colorGold,styleCandle);
Buy = Cross(fast,slow);
Short = Cross(slow, fast);
Cover = PositionScore > 0; // Or just use Cover = Buy ?
Sell = PositionScore < 0; // Or just use Sell = Short ?
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
Short = ExRem(Short,Cover);
Cover = ExRem(Cover,Short);
sym =Name();
view = "noview";
AlgoBuy = LastValue(Ref(Buy,0));
AlgoShort = LastValue(Ref(Short,0));
AlgoCover = LastValue(Ref(Cover,0));
AlgoSell = LastValue(Ref(Sell,0));
headers = "Content-Type: application/json\r\n" +
"Accept-Encoding: gzip, deflate\r\n";
InternetSetHeaders(headers);
if (AlgoBuy == True) {
view = "buy";
}
if (AlgoSell == True) {
view = "sell";
}
if (AlgoShort == True) {
view = "short";
}
if (AlgoCover == True) {
view = "cover";
}
api_data ="{\"symbol\":"+ "\""+sym+"\",\"view\":"+ "\""+view+"\",\"price\":"+ "\""+Close+"\",\"paperTrade\":"+ "\"true\"}";
//Call the Broker API endpoint to place Order
if (view != "noview") {
ih = InternetPostRequest("https://order-execution.com/stocks/orders/", api_data, 1);
if( ih )
{
while( ( str = InternetReadString( ih ) ) != "" )
{
_TRACEF( "%s", str );
}
InternetClose( ih );
}
}
AlertIF( True, "", "Actual Trade Initiated for " + sym , 0);
In the above code, I am trying to send data to the order execution application based on the Buy-Sell condition.
But something is not correct there as it doesn't consider PositionScore.
Here is attached snipped of detailed log:
The entry and exit signals do not match the logic of variables AlgoBuy, AlgoShort, AlgoCover, and AlgoSell. Because entries and exists also have some logic going on behind the scenes. How do I use that logic in my code?
Regards