I've created an AFL script that sends Buy, Stoploss, and Target values directly to my telegram with the help of a telegram bot. I've made it call the telegram sender function when the buy is true. But the problem is that since buy will be true for a whole candle so when I switch to for example 5 minute candle it sends me that buy signal 5 times (since I'm backtesting with a 1minute bar replay)
I tried using additional variables to set the telegram parameters value to zero when buy is not true and then process telegram sender only when the set parameter has non zero value, but still since buy will be true for the whole candle so the parameter is getting non-zero value for each tick for if the tick belonging to the same candle. For your reference here is the complete code. (With Bot API's removed)
_SECTION_BEGIN("5min EMA Longing");
FirstTradeTime = 094500;
LastTradeTime = 151500;
function writecsv( pricetowrite, wrtitethis )
{
if (StrFind(pricetowrite,"0.000"))
{
}
else
{
fh = fopen( "C:\\Users\\<username>\\Desktop\\newfile.csv", "a");
if(fh)
{
fputs(wrtitethis+"\n", fh );
fclose(fh);
ih = InternetOpenURL("https://api.telegram.org/bot<ChatID>:<API>/sendMessage?chat_id=1487299238&text="+wrtitethis);
InternetClose(ih);
}
else
{
printf("Error opening file");
}
}
}
//Current candle data
CandleOpen = TimeFrameGetPrice("O",in5Minute); //as it is fixed
CandleHigh = TimeFrameGetPrice("H",in5Minute); //as it is fixed
CandleLow = TimeFrameGetPrice("L",in5Minute); //as it is fixed
CandleClose = TimeFrameGetPrice("C",in5Minute); //as it is fixed
//Prev candle's data
PrevCanOpen = TimeFrameGetPrice("H",in5Minute,-1);
PrevCanHigh = TimeFrameGetPrice("H",in5Minute,-1);
PrevCanLow = TimeFrameGetPrice("L",in5Minute,-1);
PrevCanClose = TimeFrameGetPrice("C",in5Minute,-1);
Ema5 = ref(EMA(Close,5),-1); //5 period EMA
HighNotLow = PrevCanHigh-PrevCanLow;
Ema20 = EMA(close,20);
Rsi15 = RSI(14);
EMA20_Slope = Ema20 - ref(EMA(Close,20),-6);
//EMA20_Slope = 0;
indicator1 = PrevCanOpen<Ema5 AND PrevCanHigh<Ema5 AND PrevCanLow<Ema5 AND PrevCanClose<Ema5 AND CandleHigh>=PrevCanHigh; //main 5EMA strategy
indicator2 = EMA20_Slope>=0; //20 EMA slope or RSI indicating buy zone
BUY = indicator1 AND indicator2 AND HighNotLow>0 AND (TimeNum() >=FirstTradeTime AND TimeNum()<= LastTradeTime);
BuyPrice = HighestSince(Buy,Ref(H,-1));
stopLevelLong = ValueWhen(Buy, Ref(Low,-1));
PriceBuy = ValueWhen(Buy, BuyPrice); //entry price
stopAmountLong = PriceBuy-stopLevelLong;
targetAmountLong = 2*stopAmountLong;
Stoploss = stopAmountLong;
Target = targetAmountLong;
Plot(PriceBuy ,"Bought Price",colorGreen); //drawing entry price
Plot(PriceBuy-Stoploss ,"Stoploss",colorAqua); //drawing entry price
Plot(PriceBuy+Target ,"Target",colorYellow); //drawing entry price
ApplyStop(Type=0,Mode=2,Amount=Stoploss);
ApplyStop(Type=1,Mode=2,Amount=Target);
Sell = (((HighestSince(Buy,H)>PriceBuy+Target) OR (LOWESTSince(Buy,L)<PriceBuy-Stoploss)) AND (TimeNum() >=FirstTradeTime AND TimeNum()<= LastTradeTime)) OR TimeNum()>= LastTradeTime;
BUY = ExRem(BUY,Sell);
timenow = ValueWhen(Buy, TimeNum());
priceto = IIf(Buy,PriceBuy,null);
timenow = NumToStr(timenow,1);
timenow = StrReplace(timenow,",",".");
//logging
wrtitethis =Name()+" "+timenow +", BUY: "+NumToStr(PriceBuy)+", SL: "+NumToStr(PriceBuy-Stoploss)+", TG: "+NumToStr(PriceBuy+Target);
writecsv( NumToStr(LastValue(priceto)),wrtitethis);
//logging
Sell = ExRem(Sell,buy);
PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorYellow,0,L,offset=-50);
PlotShapes(IIf(Sell AND (HighestSince(Buy,H)>PriceBuy+Target),shapedownArrow,shapeNone),colorGreen,0,H,offset=-50);
PlotShapes(IIf(Sell AND (LOWESTSince(Buy,L)<PriceBuy-Stoploss),shapedownArrow,shapeNone),colorRed,0,H,offset=-50);
PlotShapes(IIf(Sell AND TimeNum()>= LastTradeTime,shapedownArrow,shapeNone),colorAqua,0,H,offset=-50);
//Show bar index on selection
bi = BarIndex();
sar1 = SAR(0.01);
sarsigs = C < sar1 AND Ref(C,-1) > Ref(sar1,-1);
// Here I changed SelectedValue() to LastValue()
lastSarSiggBarNum = LastValue(ValueWhen(sarsigs, bi));
_TRACE("Last Bar Num for sarsigs: " + lastSarSiggBarNum);
PlotText(StrFormat("%1.0f", lastSarSiggBarNum), lastSarSiggBarNum, sar1[lastSarSiggBarNum], colorWhite, colorRed);
currentlySelectedBarNum = SelectedValue(bi);
_TRACE("Currently selected BarNum " + currentlySelectedBarNum);
// plot the current bar text at the close price level
PlotText(StrFormat("%1.0f", currentlySelectedBarNum), currentlySelectedBarNum, C[currentlySelectedBarNum], colorBlack, colorAqua, 10);
//Show bar index on selection
_SECTION_END();