Algo Trading Buy Sell Short Cover help needed

hi,
New Joiner to this site, hence apologies if duplicate, I have a simple Super Trend AFL and I am adding the buy sell short cover and all I am looking to do is to send the orders to my broker via third party auto trading bridge.

  1. Buy and Short Signals are generating and orders are going to the broker but Sell and Cover are not going, because the Auto Trading code bridge has an include file that checks for repeat order and gives a delay of 26000 seconds between orders, however, lets say after first buy signal I want to sell the buy order after the signal changes and in the next candle I want to short. similarly after the trade reversed (shorted) when next buy signal comes, first it should cover and then next candle it should Buy.

  2. Right now only Buy and Short is working (due to repeat order seconds)

  3. I can make the repeat seconds to 0 but in that case it keeps sending buy or short signals continuously

Please help!

AFL Code****

#include <algotrader-util.afl>

QUANTITY_FILE = ParamStr(“Quantity File”, “C:\autotrader\quantity.csv”);

AT_SYMBOL = Name();

orderCountName = “ORDER_COUNT”;
previousSignalName = “LAST_SIGNAL”;

function getPlacedOrderCount() {
return Nz(atStaticVarGet(orderCountName));
}

function incrementPlacedOrderCount(lastOrderId) {
if(lastOrderId != “”) {
atStaticVarSet(orderCountName, getPlacedOrderCount() + 1);
}
}

function getPreviousSignal() {
return atStaticVarGetText(previousSignalName);
}

function setPreviousSignal(signalType) {
return atStaticVarSetText(previousSignalName, signalType);
}

/********************************************************************

  • SECTION 2 - BEGIN
  • This section contains your strategy afl code.
    *******************************************************************/

_SECTION_BEGIN(“ST”);
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, “Close”, ParamColor(“Color”, colorDefault ), styleNoTitle | ParamStyle(“Style”) | GetPriceStyle() );

m = Param( “ATR Multiple”, 3, 0.5, 10, 0.5 );
per = Param( “ATR Period”, 10, 3, 100, 1 );

CAvg = ( O + H + L + C ) / 4;
SL = m * ATR( per );

Trend[0] = 1;
TSL[0] = 0;
TSL[BarCount - 1] = Null;

for( i = 1; i < BarCount - 1; i++ )
{
if( Trend[i - 1] == 1 )
{
if( CAvg[i] < TSL[i - 1] )
{
Trend[i] = -1;
TSL[i] = CAvg[i] + SL[i];
}
else
{
Trend[i] = 1;

        if( ( CAvg[i] - SL[i] ) > TSL[i - 1] )
        {
            TSL[i] = CAvg[i] - SL[i];
        }
        else
        {
            TSL[i] = TSL[i - 1];
        }
    }
}

if( Trend[i - 1] == -1 )
{
    if( CAvg[i] > TSL[i - 1] )
    {
        Trend[i] = 1;
        TSL[i] = CAvg[i] - SL[i];
    }
    else
    {
        Trend[i] = -1;
        if( ( CAvg[i] + SL[i] ) < TSL[i - 1] )
        {
            TSL[i] = CAvg[i] + SL[i];
        }
        else
        {
            TSL[i] = TSL[i - 1];
        }
    }
}

}

Plot( TSL, “SuperTrend”, IIf( Trend == 1, colorGreen, colorRed ), 4 );

//NewDay=Day() != Ref(Day(), -1);
//ExitToday = (Ref(DateNum(),1)>DateNum());

starttime = 093000; // strat time or put ur own to start
closetime = 150000; // time to end or put ur own time to end

timeWindow = TimeNum() >= starttime AND TimeNum() <= closetime;
firstBarOfDay = TimeNum() >= starttime ;
firstBarOfDay = firstBarOfDay - Ref(firstBarOfDay,-1);
lastBarOfDay = TimeNum() >= closetime;
lastBarOfDay = lastBarOfDay - Ref(lastBarOfDay,-1);
ttt = firstBarOfDay OR lastBarOfDay;

Buy= trend==1 AND Ref(trend,-1)==-1 AND timeWindow;
Short= trend==-1 AND Ref(trend,-1)==1 AND timeWindow;

Buy= Ref(Buy,-1); Short=Ref(Short,-1);

Sell= Ref(Short,1) OR lastBarOfDay == 1;
Cover= Ref(Buy,1) OR lastBarOfDay == 1;

Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);

Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);

BuyPrice = LastValue©;
SellPrice = LastValue©;
CoverPrice = LastValue©;
ShortPrice = LastValue©;

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);

PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);

PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);

PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);

PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

PlotShapes(IIf(Sell, shapeHollowStar, shapeNone),colorRed, 0,L, Offset=-25);

PlotShapes(IIf(Cover, shapeHollowStar, shapeNone),colorGold, 0,L, Offset=-25);

GraphXSpace = 5;

dist = 1.5 * ATR( 5 );

for ( i = 0; i < BarCount; i++ )
{
if ( Buy[i] )
PlotText( “BUY\n@” + C [i ], i, L[ i ] - dist[i], colorWhite );

if ( Short[i] )
    PlotText( "Short\n@" + C[ i ], i, H[ i ] + dist[i], colorWhite );

if ( Sell[i] )
    PlotText( "Sell\n@" + C[ i ], i, H[ i ] + dist[i], colorWhite );

if ( Cover[i] )
    PlotText( "Cover\n@" + C[ i ], i, H[ i ] + dist[i], colorWhite );

}

/********************************************************************

  • SECTION 2 - END
    ********************************************************************/

/********************************************************************

  • SECTION 3 - BEGIN
  • This section contains your code for placing orders.
    ********************************************************************/

orderCount = getPlacedOrderCount();

if ( LastValue(Buy) == True AND getPreviousSignal() != “COVER”)
{
_TRACE(“Entering Long position”);

qty = readQuantityFromFile(QUANTITY_FILE, Name(), AT_QUANTITY);
lastOrderId = placeOrderUsingParams(AT_EXCHANGE, AT_SYMBOL, "BUY", AT_ORDER_TYPE, qty, buyPrice, defaultTriggerPrice(), 1);
incrementPlacedOrderCount(lastOrderId);
setPreviousSignal("BUY");

AlertIf( Buy, "", "Entering Long position @ " + C, 1 );		

}

if ( LastValue(Sell) == True AND orderCount > 1 AND getPreviousSignal() != “SHORT”)
{
_TRACE(“Exiting Long position”);
qty = readQuantityFromFile(QUANTITY_FILE, Name(), AT_QUANTITY);
lastOrderId = placeOrderUsingParams(AT_EXCHANGE, AT_SYMBOL, “SELL”, AT_ORDER_TYPE, qty, sellPrice, defaultTriggerPrice(), 1);
incrementPlacedOrderCount(lastOrderId);
setPreviousSignal(“SELL”);

AlertIf( Sell, "", "Exiting Long position @ " + C, 1 );

}

if ( LastValue(Short) == True )
{
_TRACE(“Entering Short position”);
qty = readQuantityFromFile(QUANTITY_FILE, Name(), AT_QUANTITY);
lastOrderId = placeOrderUsingParams(AT_EXCHANGE, AT_SYMBOL, “SHORT”, AT_ORDER_TYPE, qty, shortPrice, defaultTriggerPrice(), 1);
incrementPlacedOrderCount(lastOrderId);
setPreviousSignal(“SHORT”);
AlertIf( Short, “”, "Entering Short position @ " + C, 1 );
}

if ( LastValue(Cover) == True AND orderCount > 1)
{
_TRACE(“Exiting Short position”);
qty = readQuantityFromFile(QUANTITY_FILE, Name(), AT_QUANTITY);
lastOrderId = placeOrderUsingParams(AT_EXCHANGE, AT_SYMBOL, “COVER”, AT_ORDER_TYPE, qty, coverPrice, defaultTriggerPrice(), 1);
incrementPlacedOrderCount(lastOrderId);
setPreviousSignal(“COVER”);
AlertIf( Cover, “”, "Exiting Short position @ " + C, 1 );
}
/********************************************************************

  • SECTION 3 - END
    ********************************************************************/
    _SECTION_END();

which bridge you are using? it is not recommended auto-trade using third party bridges, it has the risk of sending continuous orders as you mentioned above.

Sell = ExRem(Sell,Buy);
Buy = ExRem(Buy,Sell);

This will regulate continues buy or sell signals as already added.

Hello I am using Autotrader from Stock Developers

my issue is that after a buy it should sell when the ST signal changes and immediately in the next candle it should short as well but this is not happening as there is a repeat order delay function that is in effect from the include file that will not allow the repeat order for 26000 seconds.

and if I make the seconds to 0 then lots of orders are going to broker.

I need help with :- It should send only 1 order per candle (5 minutes).

My solution is in this page but i am not an coder hence unable to code the logic (https://forum.amibroker.com/t/preventing-repetition-of-trade-in-liveautotrading/2173/4

Thanks

its not recommended to use third-party software, as those were not authorized.

like I mentioned earlier this is what the risk it carries

these plugins would be built on the top of amibroker application,

i don’t think it can be done with AFL, you can contact your application provider

Thank you all for your replies. I have changed the logic and its working fine now.

@AlgoGeek, so when you say third party? are you saying we should only use the bridges provided by our broker ? in my case i use wisdom capital and i dont think they have API for Algo.

yes, In my view trading accounts are as equivalent as a bank’s online account, we should only use authorised and reliable software products.