Closing position

Section for discussions related to indicators, use of indicators, and building of trading stategies using indicators.

Moderator: admin

Closing position

Postby jacklrfx » Mon Oct 05, 2015 2:57 pm

Hi,
I have some problems with the closing of position.
What I'm trying to do is open a position (Market order, OM) with a stop.
Then when there is a signal (not close on opposite) exit with a (CM) and the stop must be cancelled with the close of the position. Allowed multiple positions in a direction, not presence of long and short trades at the same moment (but hedging is allowed on my account). Trades independent.
This is the code:

Code: Select all
if conditions
BUY(stop);
end

if conditions
SELL(stop)
end

if Exit then
if (Indicator.ST:colorI(period)==core.rgb(255,0,0) then
           if  haveTrades("B") then
                exitSpecific("B");
           Signal("Close Long");
      end
     end
         
     if (Indicator.ST:colorI(period)==core.rgb(0,0,255) then   
           if  haveTrades("S") then 
                exitSpecific("S");
           Signal("Close Short");
      end
    end
end
-------------------------------------------------------
------------------------------------------------------
--------------------------------------------------------
function BUY(Stop, Limit)
    if AllowTrade and AllowMultiple then
         if haveTrades("S") then
         exitSpecific("S");
         Signal ("Close Short");
         end 
         if  (not(haveTrades("B")) or (haveTrades("B"))) and (not(haveTrades("S"))) and AllowMultiple then
         enter("B", Stop);
         Signal ("Open Long");
         end 
    elseif ShowAlert then
        Signal ("Up Trend");
    end
end

function SELL(Stop, Limit)
    if AllowTrade and AllowMultiple then   
         if haveTrades("B") then
         exitSpecific("B");
         Signal ("Close Long");
         end   
         if (not(haveTrades("S")) or (haveTrades("S"))) and (not(haveTrades("B"))) and AllowMultiple then
         enter("S", Stop);
         Signal ("Open Short");
         end 
    elseif ShowAlert then
        Signal ("Down Trend");
    end
end

function haveTrades(BuySell)
    local enum, row;
    local found = false;
    enum = core.host:findTable("trades"):enumerator();
    row = enum:next();
    while (not found) and (row ~= nil) do
        if row.AccountID == Account and
           row.OfferID == Offer and
           (row.BS == BuySell or BuySell == nil) then
           found = true;
        end
        row = enum:next();
    end
    return found;
end

-- enter into the specified direction
function enter(BuySell, Stop, Limit)
    if ( not(AllowTrade) and not(AllowMultiple) ) then
        return true;
    end

    local valuemap, success, msg;
    valuemap = core.valuemap();

    valuemap.OrderType = "OM";
    valuemap.OfferID = Offer;
    valuemap.AcctID = Account;
    valuemap.Quantity = Amount * BaseSize;
    valuemap.BuySell = BuySell;
    valuemap.RateStop=Stop;

    if TrailingStop then
      valuemap.TrailStepStop = 1;
    end
if (not CanClose) then
        valuemap.EntryLimitStop = "Y";
    end

    success, msg = terminal:execute(200, valuemap);

    if not(success) then
        terminal:alertMessage(instance.bid:instrument(), instance.bid[instance.bid:size() - 1], "Open order failed" .. msg, instance.bid:date(instance.bid:size() - 1));
        return false;
    else
         
    end

    return true;
end

-- exit from the specified trade using the direction as a key
function exitSpecific(BuySell)
    -- we have to loop through to exit all trades in each direction instead
    -- of using the net qty flag because we may be running multiple strategies on the same account.
    local enum, row;
    local found = false;
    enum = core.host:findTable("trades"):enumerator();
    row = enum:next();
    while (not found) and (row ~= nil) do
        -- for every trade for this instance.
        if row.AccountID == Account and row.OfferID == Offer and (row.BS == BuySell or BuySell == nil) then
           exitTrade(row);
        end

        row = enum:next();
    end
end

-- exit from the specified direction
function exitTrade(tradeRow)
    if ( not(AllowTrade) and not(AllowMultiple) ) then
        return true;
    end
    local valuemap, success, msg;

    if tradesCount(BuySell) > 0 then
        valuemap = core.valuemap();

        -- switch the direction since the order must be in oppsite direction
        if BuySell == "B" then
            BuySell = "S";
        else
            BuySell = "B";
        end
        valuemap.OrderType = "CM";
        valuemap.OfferID = Offer;
        valuemap.AcctID = Account;
        if (CanClose) then
        -- Non-FIFO can close each trade independantly
        valuemap.TradeID = tradeRow.TradeID;
        valuemap.Quantity = tradeRow.Lot;
        else -- FIFO
        valuemap.NetQtyFlag = "Y";
        end
        valuemap.BuySell = BuySell;
        success, msg = terminal:execute(201, valuemap);

        if not(success) then
            terminal:alertMessage(instance.bid:instrument(), instance.bid[instance.bid:size() - 1], "Open order failed" .. msg, instance.bid:date(instance.bid:size() - 1));
            return false;
        end
        return true;
    end
    return false;
end

dofile(core.app_path() .. "\\strategies\\standard\\include\\helper.lua");



The strategy doesn't close the positions.

EX:

Open Short with a OM and a stop
Open another Short with a OM and a stop
Signal of Close Short Trades
So Close all shorts with a CM and cancelled the stops
Wait till another signal of open a trade
Another signal for open a trade is given

Signal of open a trade (long) is possible in the same period of close a short but not always because the conditions that follow the signals are different. (condition of opening and closing different).
jacklrfx
 
Posts: 63
Joined: Mon Apr 20, 2015 2:40 pm

Re: Closing position

Postby cnikitopoulos94 » Wed Oct 07, 2015 7:01 am

Hi Jack, I will try to assist you as much as possible. I had a similar problem.

Before I try to help though i just want to make sure if this is exactly what your asking for.

1. Your stops arnt working when it has an open trade?
2. Do you want it to switch your position even if it didnt hit your stop yet?
cnikitopoulos94
 
Posts: 47
Joined: Sat Sep 12, 2015 8:10 am

Re: Closing position

Postby jacklrfx » Sun Oct 11, 2015 2:40 pm

Hi cnikitopoulos94,

Thanks for your help:
1)The stops work but the strategy isn't able to close the trades.

It is based on a change of color (of SSNLAGMA for precision) that gives a signal. This signal is of exit, so close the position, but it is not working.
jacklrfx
 
Posts: 63
Joined: Mon Apr 20, 2015 2:40 pm

Re: Closing position

Postby Julia CJ » Mon Oct 12, 2015 11:53 pm

Hi Jacklrfx,
I would like to suggest you to check this strategy using Indicore debugger. The results will show positions that are actually deleted. There is probably a mistake in your algorithm. It may this piece:

- Switch the direction since the order must be in oppsite direction
if BuySell == "B" then
BuySell = "S";
else
BuySell = "B";
End

If I get it right, the strategy from this piece closes one position only.
Julia CJ
 

Re: Closing position

Postby jacklrfx » Tue Oct 13, 2015 10:09 am

Hi Julia CJ,

perfect I have correct it and now it works, I mean it closes the positions (all) but there is an error which I am not be able to eliminate it, I mean:

when optional exit is on:

Code: Select all
if Exit then
       
      if (Indicator.One:colorI(period)]==instance.parameters.SSNLAGMA_clrDN) then
            if   haveTrades("B") then
                 exitSpecific("B");
            Signal("Close Long");
       end   
      else (Indicator.One:colorI(period)]==instance.parameters.SSNLAGMA_clrUP) then   
            if   haveTrades("S") then 
                 exitSpecific("S");
            Signal("Close Short");
       end
      end
    end


what I mean is maybe the error is in the call to the Indicator.One, where One==stream of SSNLAGMA. I don't know why, seems the strategy don't know which is the last color of the stream, I think the problem is with the period of the stream. There is a way to access to the last color, some other methods to by pass the code written (Indicator.One:colorI(period))?
jacklrfx
 
Posts: 63
Joined: Mon Apr 20, 2015 2:40 pm

Re: Closing position

Postby Julia CJ » Wed Oct 14, 2015 5:30 am

Hi Jacklrfx,
Please write in the strategy (it will not work in the indicator), the following information: Indicator.One:colorI(NOW) instead of (Indicator.One:colorI(period)).
Julia CJ
 


Return to Discussions

Who is online

Users browsing this forum: No registered users and 2 guests