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);
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????