Trouble with getting flat while pyramiding orders

Questions about MultiCharts and user contributed studies.
User avatar
joebone
Posts: 175
Joined: 05 Sep 2018
Has thanked: 53 times
Been thanked: 4 times

Trouble with getting flat while pyramiding orders

Postby joebone » 11 Jan 2022

Hello I have a script that I am having trouble debugging.

The end use for this will be in portfolio trader but I am currently debugging in a chart.

Relevant information.....
I am using a weekly chart of SPY going back to 1996 as Data(1).
Data(2-10) are the SPDR Sectors XLE,XLU,XLK,XLB,XLP,XLY,XLI,XLV,XLF (I am using these cause they have the longest historical data, In future use I will use all of them)
Signal properties are "Allow up to 80 entry orders in the same direction.. Regardless of the entry that generated." Maximum shares are way above anything the script would hit.

The idea of the script is a simple sector rotation. The portfolio starts 100% in SPY. Some processing is done on the sector ETFs and then N of them are identified as potential buys. If the conditions are met the signal will sell 1/N portfolio equity in SPY and through a Global Variable tell another signal to buy the Sector ETF on another part of the portfolio. None of that is happening now as I am just testing the Pyramid orders for SPY on a chart at the moment.

The logic is pretty simple for your understanding below. If Position_count goes up 1 then that means I need to sell 1/N * Total_capital shares of SPY to make room for the new position in a lower priority part of the portfolio. Number_of_positions in an Input for max number of potential holdings

Code: Select all

Begin //Position sizing and base index holding orders Vars: Total_capital(0), Available_capital(0), Share_size(0); Total_capital = InitialCapital + netprofit + openpositionprofit; Share_size = ((Number_of_Positions - Position_count)/Number_of_Positions)* (Total_capital/C); i_setplotvalue(110,5); i_setplotvalue(111,5); i_setplotvalue(112,5); if Position_count <> Position_count[1] then Begin i_setplotvalue(110,0); if Share_size - currentcontracts > 0 then Begin i_setplotvalue(111,0); buy Share_size-currentcontracts shares this bar at close; End; if Share_size - currentcontracts < 0 then Begin i_setplotvalue(112,0); Sell absvalue(Share_size - currentcontracts) shares Total this bar at close; End; End; i_setplotvalue(100,position_count); i_setplotvalue(101,Share_size); i_setplotvalue(102,Total_capital/C); i_setplotvalue(103, absvalue(Share_size - currentcontracts)); End;

I am also using this Indicator to Debug to ensure orders are coming through when conditions are met.

Code: Select all

Vars: Position_count(0), Share_size(0), Full_Shares(0), Order_size(0), Position_change(0); Position_count = i_getplotvalue(100); Share_size = i_getplotvalue(101); Full_Shares = i_getplotvalue(102); Order_size = i_getplotvalue(103); //Position_change if i_getplotvalue(110) = 0 then plot6(Share_size); if i_getplotvalue(111) = 0 then plot7(Share_size); if i_getplotvalue(112) = 0 then plot8(Share_size); if i_getplotvalue(113) = 0 then plot9(Share_size); plot1(Position_count); plot2(Share_size); plot3(Full_Shares); plot4(i_CurrentContracts); plot5(Order_size);
plot6-9 are point plots to ensure the conditions are being met for an order to be fired.
I dont know what I have missed but this doesnt work at all. Its like the order for the sale wont go though after a period of time.... Getplotvalue(112) does go to 0 and plots and that would imply the order is being fired but sometimes the sell of shares never happens. It will do some sales for a while then it wont sell below a certain level..

Can you help me with this info or do I need to share the entire script?? Im at a loss????

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Trouble with getting flat while pyramiding orders

Postby TJ » 12 Jan 2022

This is quite a complex undertaking; I would engage a professional programmer. It is better to make the money to pay the programmer than to save money but getting stuck.

As a practice, Data1 should not be longer than 1 day.

User avatar
joebone
Posts: 175
Joined: 05 Sep 2018
Has thanked: 53 times
Been thanked: 4 times

Re: Trouble with getting flat while pyramiding orders

Postby joebone » 12 Jan 2022

This is quite a complex undertaking; I would engage a professional programmer. It is better to make the money to pay the programmer than to save money but getting stuck.

As a practice, Data1 should not be longer than 1 day.
Hey TJ,
Thanks for the response. A quick question. Why no longer than 1day for D1?

Unfortunately, I am the one. And I enjoy learning along the way. You never get better if you don't try. I will admit that there is a whole new "barrier to entry" when it comes to portfolio and pyramid trading. Way more coding just to get the piping to work.

I found the RejectedOrderAction yesterday afternoon and now my plan is to see if I can flag the order as rejected then figure out why.

If I put in this snippet of code

Code: Select all

if Position_count = Number_of_Positions then Begin i_setplotvalue(113,0); Sell this bar at close; End;
whenever the strategy would be flat it seems to reduce the problem which then leads me to believe I am having problems with the legacy orders not closing when I eclipse their value with a "sell shares total order"

Also, I dont have intentions to let this run wild with my portfolio. It is mostly just for backtesting. I would put these orders in manually.

User avatar
joebone
Posts: 175
Joined: 05 Sep 2018
Has thanked: 53 times
Been thanked: 4 times

Re: Trouble with getting flat while pyramiding orders

Postby joebone » 14 Jan 2022

Svetlana helped me out with my support request...

for anyone coming behind me to work on portfolio trading and pyramiding exits here is the answer..

Look at
SameExitFromOneEntryOnce = False;
https://www.multicharts.com/trading-sof ... eEntryOnce


Return to “MultiCharts”